blob: 1f593a0bd9381def8d88adb26c9434f402126d87 [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
David Quigleyaa9c2662013-05-22 12:50:44 -0400108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
110 if (err == 0)
111 return label;
112
113 return NULL;
114}
115static inline void
116nfs4_label_release_security(struct nfs4_label *label)
117{
118 if (label)
119 security_release_secctx(label->label, label->len);
120}
121static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
122{
123 if (label)
124 return server->attr_bitmask;
125
126 return server->attr_bitmask_nl;
127}
128#else
129static inline struct nfs4_label *
130nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
132{ return NULL; }
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{ return; }
136static inline u32 *
137nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{ return server->attr_bitmask; }
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500142static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Trond Myklebust52567b02009-10-23 14:46:42 -0400144 if (err >= -1000)
145 return err;
146 switch (err) {
147 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400150 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000151 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400152 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000153 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
156 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400157 case -NFS4ERR_SHARE_DENIED:
158 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400161 case -NFS4ERR_ACCESS:
162 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400163 case -NFS4ERR_FILE_OPEN:
164 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700167 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
174 * This is our standard bitmap for GETATTR requests.
175 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400176const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 FATTR4_WORD0_TYPE
178 | FATTR4_WORD0_CHANGE
179 | FATTR4_WORD0_SIZE
180 | FATTR4_WORD0_FSID
181 | FATTR4_WORD0_FILEID,
182 FATTR4_WORD1_MODE
183 | FATTR4_WORD1_NUMLINKS
184 | FATTR4_WORD1_OWNER
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400190 | FATTR4_WORD1_TIME_MODIFY,
191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Trond Myklebust65de8722008-12-23 15:21:44 -0500317static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
318{
319 int res = 0;
320
321 might_sleep();
322
323 if (*timeout <= 0)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000327 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 if (fatal_signal_pending(current))
329 res = -ERESTARTSYS;
330 *timeout <<= 1;
331 return res;
332}
333
334/* This is the error handling routine for processes that are allowed
335 * to sleep.
336 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800337static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500338{
339 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500340 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500341 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 int ret = errorcode;
343
344 exception->retry = 0;
345 switch(errorcode) {
346 case 0:
347 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500348 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400350 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 exception->retry = 1;
352 return 0;
353 }
354 if (state == NULL)
355 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400356 ret = nfs4_schedule_stateid_recovery(server, state);
357 if (ret < 0)
358 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500359 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500360 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
366 break;
367 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500368 if (state == NULL)
369 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400370 ret = nfs4_schedule_stateid_recovery(server, state);
371 if (ret < 0)
372 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500373 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400374 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400375 if (state != NULL) {
376 ret = nfs4_schedule_stateid_recovery(server, state);
377 if (ret < 0)
378 break;
379 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500381 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400384 case -NFS4ERR_MOVED:
385 ret = nfs4_schedule_migration_recovery(server);
386 if (ret < 0)
387 break;
388 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500392#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
401 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400403 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500404#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
408 * fail
409 */
410 ret = -EBUSY;
411 break;
412 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_GRACE:
414 case -NFS4ERR_DELAY:
415 ret = nfs4_delay(server->client, &exception->timeout);
416 if (ret != 0)
417 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400418 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800421 break;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
430 "uid/gids. "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
433 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 }
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
440 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 if (ret == 0)
442 exception->retry = 1;
443 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500444}
445
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400446/*
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
449 */
450static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
451{
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
453
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
456 return true;
457
458 return false;
459}
Trond Myklebust65de8722008-12-23 15:21:44 -0500460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
467}
468
Trond Myklebust452e9352010-07-31 14:29:06 -0400469static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
470{
471 do_renew_lease(server->nfs_client, timestamp);
472}
473
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400474struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
478};
479
Chuck Levera9c92d62013-08-09 12:48:18 -0400480static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
482{
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
486
487 res->sr_slot = NULL;
488}
489
490static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
491{
492 args->sa_privileged = 1;
493}
494
Chuck Lever3bd23842013-08-09 12:49:19 -0400495static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
499{
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
502
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
505 goto out_start;
506
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
509 goto out_sleep;
510
511 slot = nfs4_alloc_slot(tbl);
512 if (IS_ERR(slot)) {
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
515 goto out_sleep;
516 }
517 spin_unlock(&tbl->slot_tbl_lock);
518
519 args->sa_slot = slot;
520 res->sr_slot = slot;
521
522out_start:
523 rpc_call_start(task);
524 return 0;
525
526out_sleep:
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
530 else
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 return -EAGAIN;
534}
535
536static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
538{
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
541
Trond Myklebustcab92c12014-01-29 12:12:15 -0500542 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400543 goto out;
544
545 tbl = slot->table;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
550
551 res->sr_slot = NULL;
552out:
553 return 1;
554}
555
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400556#if defined(CONFIG_NFS_V4_1)
557
Trond Myklebustd185a332010-06-16 09:52:25 -0400558static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400559{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500560 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400561 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500562 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500563 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400564
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500565 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500566 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500567
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500568 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500569 /* Be nice to the server: try to ensure that the last transmitted
570 * value for highest_user_slotid <= target_highest_slotid
571 */
572 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
573 send_new_highest_used_slotid = true;
574
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500575 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500576 send_new_highest_used_slotid = false;
577 goto out_unlock;
578 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500579 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500580
581 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
582 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500583out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500584 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400585 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500586 if (send_new_highest_used_slotid)
587 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400588}
589
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500590int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400591{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500592 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500593 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400594 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500595 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500596 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400597
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500598 if (slot == NULL)
599 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400600 /* don't increment the sequence number if the task wasn't sent */
601 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400602 goto out;
603
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500604 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500605
Trond Myklebustac20d162012-12-15 15:36:07 -0500606 if (slot->interrupted) {
607 slot->interrupted = 0;
608 interrupted = true;
609 }
610
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400611 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500612 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400613 switch (res->sr_status) {
614 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400615 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500616 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500617 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500618 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500619 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500620 if (res->sr_status_flags != 0)
621 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500622 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400623 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500624 case 1:
625 /*
626 * sr_status remains 1 if an RPC level error occurred.
627 * The server may or may not have processed the sequence
628 * operation..
629 * Mark the slot as having hosted an interrupted RPC call.
630 */
631 slot->interrupted = 1;
632 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400633 case -NFS4ERR_DELAY:
634 /* The server detected a resend of the RPC call and
635 * returned NFS4ERR_DELAY as per Section 2.10.6.2
636 * of RFC5661.
637 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500638 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400639 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500640 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500641 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400642 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500643 case -NFS4ERR_BADSLOT:
644 /*
645 * The slot id we used was probably retired. Try again
646 * using a different slot id.
647 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500648 goto retry_nowait;
649 case -NFS4ERR_SEQ_MISORDERED:
650 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500651 * Was the last operation on this sequence interrupted?
652 * If so, retry after bumping the sequence number.
653 */
654 if (interrupted) {
655 ++slot->seq_nr;
656 goto retry_nowait;
657 }
658 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500659 * Could this slot have been previously retired?
660 * If so, then the server may be expecting seq_nr = 1!
661 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500662 if (slot->seq_nr != 1) {
663 slot->seq_nr = 1;
664 goto retry_nowait;
665 }
666 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500667 case -NFS4ERR_SEQ_FALSE_RETRY:
668 ++slot->seq_nr;
669 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400670 default:
671 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500672 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400673 }
674out:
675 /* The session may be reset by one of the error handlers. */
676 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400677 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500678out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500679 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500680retry_nowait:
681 if (rpc_restart_call_prepare(task)) {
682 task->tk_status = 0;
683 ret = 0;
684 }
685 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400686out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400687 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 goto out;
689 rpc_delay(task, NFS4_POLL_RETRY_MAX);
690 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500692EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400693
Trond Myklebust14516c32010-07-31 14:29:06 -0400694static int nfs4_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
703
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000704int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 struct rpc_task *task)
708{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400709 struct nfs4_slot *slot;
710 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711
712 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400714 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400715 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400717 tbl = &session->fc_slot_table;
718
Trond Myklebust69d206b2012-11-22 13:21:02 -0500719 task->tk_timeout = 0;
720
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400721 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400722 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400723 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500724 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500725 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400726 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400727 }
728
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500729 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500730 if (IS_ERR(slot)) {
731 /* If out of memory, try again in 1/4 second */
732 if (slot == ERR_PTR(-ENOMEM))
733 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400734 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400735 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736 }
737 spin_unlock(&tbl->slot_tbl_lock);
738
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500739 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400740
Chuck Levere8d92382013-08-09 12:47:51 -0400741 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500742 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400743
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500745 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400746 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400747 /*
748 * sr_status is only set in decode_sequence, and so will remain
749 * set to 1 if an rpc level failure occurs.
750 */
751 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400752 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400753out_success:
754 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400756out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400757 /* Privileged tasks are queued with top priority */
758 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400759 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
760 NULL, RPC_PRIORITY_PRIVILEGED);
761 else
762 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400763 spin_unlock(&tbl->slot_tbl_lock);
764 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400765}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000766EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767
Chuck Lever5a580e02013-08-09 12:48:09 -0400768static int nfs4_setup_sequence(const struct nfs_server *server,
769 struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res,
771 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400772{
Trond Myklebust035168a2010-06-16 09:52:26 -0400773 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400774 int ret = 0;
775
Chuck Lever3bd23842013-08-09 12:49:19 -0400776 if (!session)
777 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400778
Chuck Levere8d92382013-08-09 12:47:51 -0400779 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400780 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400782
Trond Myklebust9d12b212012-01-17 22:04:25 -0500783 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400784
Andy Adamsonce5039c2009-04-01 09:22:13 -0400785 dprintk("<-- %s status=%d\n", __func__, ret);
786 return ret;
787}
788
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
790{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400791 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793
Trond Myklebust035168a2010-06-16 09:52:26 -0400794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
795
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797}
798
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
800{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400801 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804}
805
Trond Myklebust17280172012-03-11 13:11:00 -0400806static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809};
810
Chuck Lever3bd23842013-08-09 12:49:19 -0400811#else /* !CONFIG_NFS_V4_1 */
812
Chuck Lever5a580e02013-08-09 12:48:09 -0400813static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
817{
Chuck Lever3bd23842013-08-09 12:49:19 -0400818 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400819}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400820
Trond Myklebust14516c32010-07-31 14:29:06 -0400821static int nfs4_sequence_done(struct rpc_task *task,
822 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400823{
Chuck Lever3bd23842013-08-09 12:49:19 -0400824 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400825}
Chuck Lever3bd23842013-08-09 12:49:19 -0400826
827#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400828
Chuck Lever9915ea72013-08-09 12:48:27 -0400829static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
830{
831 struct nfs4_call_sync_data *data = calldata;
832 nfs4_setup_sequence(data->seq_server,
833 data->seq_args, data->seq_res, task);
834}
835
836static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
837{
838 struct nfs4_call_sync_data *data = calldata;
839 nfs4_sequence_done(task, data->seq_res);
840}
841
842static const struct rpc_call_ops nfs40_call_sync_ops = {
843 .rpc_call_prepare = nfs40_call_sync_prepare,
844 .rpc_call_done = nfs40_call_sync_done,
845};
846
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400847static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400848 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 struct nfs4_sequence_args *args,
851 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400852{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 int ret;
854 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400855 struct nfs_client *clp = server->nfs_client;
856 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 .seq_server = server,
858 .seq_args = args,
859 .seq_res = res,
860 };
861 struct rpc_task_setup task_setup = {
862 .rpc_client = clnt,
863 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400864 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 .callback_data = &data
866 };
867
868 task = rpc_run_task(&task_setup);
869 if (IS_ERR(task))
870 ret = PTR_ERR(task);
871 else {
872 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400873 rpc_put_task(task);
874 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 return ret;
876}
877
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400878static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000879int nfs4_call_sync(struct rpc_clnt *clnt,
880 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000881 struct rpc_message *msg,
882 struct nfs4_sequence_args *args,
883 struct nfs4_sequence_res *res,
884 int cache_reply)
885{
Chuck Levera9c92d62013-08-09 12:48:18 -0400886 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400887 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000888}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
891{
892 struct nfs_inode *nfsi = NFS_I(dir);
893
894 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400895 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700896 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700898 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000899 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 spin_unlock(&dir->i_lock);
901}
902
903struct nfs4_opendata {
904 struct kref kref;
905 struct nfs_openargs o_arg;
906 struct nfs_openres o_res;
907 struct nfs_open_confirmargs c_arg;
908 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500909 struct nfs4_string owner_name;
910 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400912 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400914 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nfs4_state_owner *owner;
916 struct nfs4_state *state;
917 struct iattr attrs;
918 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400919 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400920 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400921 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400922 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int cancelled;
924};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400925
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400926static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
927 int err, struct nfs4_exception *exception)
928{
929 if (err != -EINVAL)
930 return false;
931 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
932 return false;
933 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
934 exception->retry = 1;
935 return true;
936}
937
938static enum open_claim_type4
939nfs4_map_atomic_open_claim(struct nfs_server *server,
940 enum open_claim_type4 claim)
941{
942 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
943 return claim;
944 switch (claim) {
945 default:
946 return claim;
947 case NFS4_OPEN_CLAIM_FH:
948 return NFS4_OPEN_CLAIM_NULL;
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
951 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
953 }
954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957{
958 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400959 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 p->o_res.seqid = p->o_arg.seqid;
961 p->c_res.seqid = p->c_arg.seqid;
962 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400963 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500965 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966}
967
Al Viro82a2c1b2011-06-22 18:30:55 -0400968static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400970 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400971 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400972 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400973 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974{
Al Viro82a2c1b2011-06-22 18:30:55 -0400975 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 struct inode *dir = parent->d_inode;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs4_opendata *p;
979
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400980 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 if (p == NULL)
982 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400983
984 p->f_label = nfs4_label_alloc(server, gfp_mask);
985 if (IS_ERR(p->f_label))
986 goto err_free_p;
987
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400988 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400990 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400991 nfs_sb_active(dentry->d_sb);
992 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 p->dir = parent;
994 p->owner = sp;
995 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 p->o_arg.open_flags = flags;
997 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700998 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
999 * will return permission denied for all bits until close */
1000 if (!(flags & O_EXCL)) {
1001 /* ask server to check for all possible rights as results
1002 * are cached */
1003 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1004 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001005 }
David Howells7539bba2006-08-22 20:06:09 -04001006 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001007 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1008 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001009 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001011 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001012 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001013 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001014 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1015 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001016 case NFS4_OPEN_CLAIM_NULL:
1017 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1018 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1019 p->o_arg.fh = NFS_FH(dir);
1020 break;
1021 case NFS4_OPEN_CLAIM_PREVIOUS:
1022 case NFS4_OPEN_CLAIM_FH:
1023 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1024 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1025 p->o_arg.fh = NFS_FH(dentry->d_inode);
1026 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001027 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001028 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001029
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 p->o_arg.u.attrs = &p->attrs;
1031 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001032
1033 verf[0] = jiffies;
1034 verf[1] = current->pid;
1035 memcpy(p->o_arg.u.verifier.data, verf,
1036 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001038 p->c_arg.fh = &p->o_res.fh;
1039 p->c_arg.stateid = &p->o_res.stateid;
1040 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001041 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001042 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001044
1045err_free_label:
1046 nfs4_label_free(p->f_label);
1047err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 kfree(p);
1049err:
1050 dput(parent);
1051 return NULL;
1052}
1053
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001055{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001056 struct nfs4_opendata *p = container_of(kref,
1057 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001058 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001059
1060 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 if (p->state != NULL)
1062 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001063 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001064
1065 nfs4_label_free(p->f_label);
1066
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001067 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001068 dput(p->dentry);
1069 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001070 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001071 kfree(p);
1072}
1073
1074static void nfs4_opendata_put(struct nfs4_opendata *p)
1075{
1076 if (p != NULL)
1077 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001078}
1079
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1081{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001082 int ret;
1083
Trond Myklebust06f814a2006-01-03 09:55:07 +01001084 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001085 return ret;
1086}
1087
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089{
1090 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091
Trond Myklebust536e43d2012-01-17 22:04:26 -05001092 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093 goto out;
1094 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001096 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1097 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001098 break;
1099 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001100 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1101 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 break;
1103 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001104 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1105 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 return ret;
1109}
1110
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001113 if (delegation == NULL)
1114 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001117 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001119 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1120 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001121 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 return 1;
1123}
1124
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001126{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001128 case FMODE_WRITE:
1129 state->n_wronly++;
1130 break;
1131 case FMODE_READ:
1132 state->n_rdonly++;
1133 break;
1134 case FMODE_READ|FMODE_WRITE:
1135 state->n_rdwr++;
1136 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001138}
1139
Trond Myklebuste999e802014-02-10 18:20:47 -05001140static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1141 nfs4_stateid *stateid)
1142{
1143 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1144 return true;
1145 if (!nfs4_stateid_match_other(stateid, &state->open_stateid))
1146 return true;
1147 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1148 return true;
1149 return false;
1150}
1151
Trond Myklebust226056c2014-02-11 10:41:07 -05001152static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1153 nfs4_stateid *stateid, fmode_t fmode)
1154{
1155 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1156 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1157 case FMODE_WRITE:
1158 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1159 break;
1160 case FMODE_READ:
1161 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1162 break;
1163 case 0:
1164 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1165 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1166 clear_bit(NFS_OPEN_STATE, &state->flags);
1167 }
1168 if (stateid == NULL)
1169 return;
1170 if (!nfs_need_update_open_stateid(state, stateid))
1171 return;
1172 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1173 nfs4_stateid_copy(&state->stateid, stateid);
1174 nfs4_stateid_copy(&state->open_stateid, stateid);
1175}
1176
1177static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1178{
1179 write_seqlock(&state->seqlock);
1180 nfs_clear_open_stateid_locked(state, stateid, fmode);
1181 write_sequnlock(&state->seqlock);
1182}
1183
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001185{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001186 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001187 case FMODE_READ:
1188 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1189 break;
1190 case FMODE_WRITE:
1191 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1192 break;
1193 case FMODE_READ|FMODE_WRITE:
1194 set_bit(NFS_O_RDWR_STATE, &state->flags);
1195 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001196 if (!nfs_need_update_open_stateid(state, stateid))
1197 return;
1198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1199 nfs4_stateid_copy(&state->stateid, stateid);
1200 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001201}
1202
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203static 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 -07001204{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001205 /*
1206 * Protect the call to nfs4_state_set_mode_locked and
1207 * serialise the stateid update
1208 */
1209 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001210 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001211 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001212 set_bit(NFS_DELEGATED_STATE, &state->flags);
1213 }
1214 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001215 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001216 write_sequnlock(&state->seqlock);
1217 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001219 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222static 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 -05001223{
1224 struct nfs_inode *nfsi = NFS_I(state->inode);
1225 struct nfs_delegation *deleg_cur;
1226 int ret = 0;
1227
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001228 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001229
1230 rcu_read_lock();
1231 deleg_cur = rcu_dereference(nfsi->delegation);
1232 if (deleg_cur == NULL)
1233 goto no_delegation;
1234
1235 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001236 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001237 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001239 goto no_delegation_unlock;
1240
1241 if (delegation == NULL)
1242 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001243 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001244 goto no_delegation_unlock;
1245
Trond Myklebustb7391f42008-12-23 15:21:52 -05001246 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001248 ret = 1;
1249no_delegation_unlock:
1250 spin_unlock(&deleg_cur->lock);
1251no_delegation:
1252 rcu_read_unlock();
1253
1254 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001255 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001256 ret = 1;
1257 }
1258
1259 return ret;
1260}
1261
1262
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001264{
1265 struct nfs_delegation *delegation;
1266
1267 rcu_read_lock();
1268 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001270 rcu_read_unlock();
1271 return;
1272 }
1273 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001274 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275}
1276
Trond Myklebust6ee41262007-07-08 14:11:36 -04001277static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001278{
1279 struct nfs4_state *state = opendata->state;
1280 struct nfs_inode *nfsi = NFS_I(state->inode);
1281 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001282 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001283 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001284 nfs4_stateid stateid;
1285 int ret = -EAGAIN;
1286
Trond Myklebustaac00a82007-07-05 19:02:21 -04001287 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001288 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001289 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 if (can_open_cached(state, fmode, open_mode)) {
1291 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001292 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001293 goto out_return_state;
1294 }
1295 spin_unlock(&state->owner->so_lock);
1296 }
Trond Myklebust34310432008-12-23 15:21:38 -05001297 rcu_read_lock();
1298 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001299 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001300 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001301 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001302 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001304 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001305 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001306 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001307 if (!opendata->is_recover) {
1308 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1309 if (ret != 0)
1310 goto out;
1311 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001312 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001313
1314 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001315 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001316 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001317 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001318out:
1319 return ERR_PTR(ret);
1320out_return_state:
1321 atomic_inc(&state->count);
1322 return state;
1323}
1324
Andy Adamsone23008e2012-10-02 21:07:32 -04001325static void
1326nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1327{
1328 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1329 struct nfs_delegation *delegation;
1330 int delegation_flags = 0;
1331
1332 rcu_read_lock();
1333 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1334 if (delegation)
1335 delegation_flags = delegation->flags;
1336 rcu_read_unlock();
1337 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1338 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1339 "returning a delegation for "
1340 "OPEN(CLAIM_DELEGATE_CUR)\n",
1341 clp->cl_hostname);
1342 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1343 nfs_inode_set_delegation(state->inode,
1344 data->owner->so_cred,
1345 &data->o_res);
1346 else
1347 nfs_inode_reclaim_delegation(state->inode,
1348 data->owner->so_cred,
1349 &data->o_res);
1350}
1351
1352/*
1353 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1354 * and update the nfs4_state.
1355 */
1356static struct nfs4_state *
1357_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1358{
1359 struct inode *inode = data->state->inode;
1360 struct nfs4_state *state = data->state;
1361 int ret;
1362
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001363 if (!data->rpc_done) {
1364 if (data->rpc_status) {
1365 ret = data->rpc_status;
1366 goto err;
1367 }
1368 /* cached opens have already been processed */
1369 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001370 }
1371
Andy Adamsone23008e2012-10-02 21:07:32 -04001372 ret = nfs_refresh_inode(inode, &data->f_attr);
1373 if (ret)
1374 goto err;
1375
1376 if (data->o_res.delegation_type != 0)
1377 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001378update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001379 update_open_stateid(state, &data->o_res.stateid, NULL,
1380 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001381 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001382
1383 return state;
1384err:
1385 return ERR_PTR(ret);
1386
1387}
1388
1389static struct nfs4_state *
1390_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391{
1392 struct inode *inode;
1393 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001394 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395
Trond Myklebustaac00a82007-07-05 19:02:21 -04001396 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001397 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398 goto out;
1399 }
1400
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001401 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001403 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001404 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001405 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001406 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001407 goto err;
1408 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409 state = nfs4_get_open_state(inode, data->owner);
1410 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001411 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001412 if (data->o_res.delegation_type != 0)
1413 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001414 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001415 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001417out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001418 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001420err_put_inode:
1421 iput(inode);
1422err:
1423 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424}
1425
Andy Adamsone23008e2012-10-02 21:07:32 -04001426static struct nfs4_state *
1427nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1428{
1429 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1430 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1431 return _nfs4_opendata_to_nfs4_state(data);
1432}
1433
Trond Myklebust864472e2006-01-03 09:55:15 +01001434static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1435{
1436 struct nfs_inode *nfsi = NFS_I(state->inode);
1437 struct nfs_open_context *ctx;
1438
1439 spin_lock(&state->inode->i_lock);
1440 list_for_each_entry(ctx, &nfsi->open_files, list) {
1441 if (ctx->state != state)
1442 continue;
1443 get_nfs_open_context(ctx);
1444 spin_unlock(&state->inode->i_lock);
1445 return ctx;
1446 }
1447 spin_unlock(&state->inode->i_lock);
1448 return ERR_PTR(-ENOENT);
1449}
1450
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001451static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1452 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001453{
1454 struct nfs4_opendata *opendata;
1455
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001456 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001457 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001458 if (opendata == NULL)
1459 return ERR_PTR(-ENOMEM);
1460 opendata->state = state;
1461 atomic_inc(&state->count);
1462 return opendata;
1463}
1464
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001465static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001466{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001468 int ret;
1469
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001470 opendata->o_arg.open_flags = 0;
1471 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001472 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1473 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1474 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001475 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001476 if (ret != 0)
1477 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001478 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001479 if (IS_ERR(newstate))
1480 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001481 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001482 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001483 return 0;
1484}
1485
1486static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1487{
Trond Myklebust864472e2006-01-03 09:55:15 +01001488 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001489 int ret;
1490
1491 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001492 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001493 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001494 smp_rmb();
1495 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001496 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001497 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001498 if (ret != 0)
1499 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001500 if (newstate != state)
1501 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001502 }
1503 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001504 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001505 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001506 if (ret != 0)
1507 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001508 if (newstate != state)
1509 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 }
1511 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001512 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001513 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001514 if (ret != 0)
1515 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001516 if (newstate != state)
1517 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001518 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001519 /*
1520 * We may have performed cached opens for all three recoveries.
1521 * Check if we need to update the current stateid.
1522 */
1523 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001524 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001525 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001526 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001527 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001528 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001529 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001530 return 0;
1531}
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/*
1534 * OPEN_RECLAIM:
1535 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001537static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001539 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001540 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001541 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 int status;
1543
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001544 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1545 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001546 if (IS_ERR(opendata))
1547 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001548 rcu_read_lock();
1549 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001550 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001551 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001552 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001553 opendata->o_arg.u.delegation_type = delegation_type;
1554 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001555 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return status;
1557}
1558
Trond Myklebust539cd032007-06-05 11:46:42 -04001559static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct nfs_server *server = NFS_SERVER(state->inode);
1562 struct nfs4_exception exception = { };
1563 int err;
1564 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001565 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001566 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001567 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1568 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001569 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001570 break;
1571 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 } while (exception.retry);
1573 return err;
1574}
1575
Trond Myklebust864472e2006-01-03 09:55:15 +01001576static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1577{
1578 struct nfs_open_context *ctx;
1579 int ret;
1580
1581 ctx = nfs4_state_find_open_context(state);
1582 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001583 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001584 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001585 put_nfs_open_context(ctx);
1586 return ret;
1587}
1588
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001589static 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 -07001590{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001591 switch (err) {
1592 default:
1593 printk(KERN_ERR "NFS: %s: unhandled error "
1594 "%d.\n", __func__, err);
1595 case 0:
1596 case -ENOENT:
1597 case -ESTALE:
1598 break;
1599 case -NFS4ERR_BADSESSION:
1600 case -NFS4ERR_BADSLOT:
1601 case -NFS4ERR_BAD_HIGH_SLOT:
1602 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1603 case -NFS4ERR_DEADSESSION:
1604 set_bit(NFS_DELEGATED_STATE, &state->flags);
1605 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1606 return -EAGAIN;
1607 case -NFS4ERR_STALE_CLIENTID:
1608 case -NFS4ERR_STALE_STATEID:
1609 set_bit(NFS_DELEGATED_STATE, &state->flags);
1610 case -NFS4ERR_EXPIRED:
1611 /* Don't recall a delegation if it was lost */
1612 nfs4_schedule_lease_recovery(server->nfs_client);
1613 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001614 case -NFS4ERR_MOVED:
1615 nfs4_schedule_migration_recovery(server);
1616 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001617 case -NFS4ERR_LEASE_MOVED:
1618 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1619 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001620 case -NFS4ERR_DELEG_REVOKED:
1621 case -NFS4ERR_ADMIN_REVOKED:
1622 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001623 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001624 nfs_inode_find_state_and_recover(state->inode,
1625 stateid);
1626 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001627 return 0;
1628 case -NFS4ERR_DELAY:
1629 case -NFS4ERR_GRACE:
1630 set_bit(NFS_DELEGATED_STATE, &state->flags);
1631 ssleep(1);
1632 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001633 case -ENOMEM:
1634 case -NFS4ERR_DENIED:
1635 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1636 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return err;
1639}
1640
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001641int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1642{
1643 struct nfs_server *server = NFS_SERVER(state->inode);
1644 struct nfs4_opendata *opendata;
1645 int err;
1646
1647 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1648 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1649 if (IS_ERR(opendata))
1650 return PTR_ERR(opendata);
1651 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1652 err = nfs4_open_recover(opendata, state);
1653 nfs4_opendata_put(opendata);
1654 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1655}
1656
Chuck Leverbe05c862013-08-09 12:49:47 -04001657static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1658{
1659 struct nfs4_opendata *data = calldata;
1660
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001661 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1662 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001663}
1664
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001665static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1666{
1667 struct nfs4_opendata *data = calldata;
1668
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001669 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001670
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001671 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001672 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001673 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001674 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001675 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001676 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001677 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001678}
1679
1680static void nfs4_open_confirm_release(void *calldata)
1681{
1682 struct nfs4_opendata *data = calldata;
1683 struct nfs4_state *state = NULL;
1684
1685 /* If this request hasn't been cancelled, do nothing */
1686 if (data->cancelled == 0)
1687 goto out_free;
1688 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001689 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001690 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001691 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001692 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001693 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001694out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001695 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696}
1697
1698static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001699 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001700 .rpc_call_done = nfs4_open_confirm_done,
1701 .rpc_release = nfs4_open_confirm_release,
1702};
1703
1704/*
1705 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1706 */
1707static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1708{
1709 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1710 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001711 struct rpc_message msg = {
1712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1713 .rpc_argp = &data->c_arg,
1714 .rpc_resp = &data->c_res,
1715 .rpc_cred = data->owner->so_cred,
1716 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001717 struct rpc_task_setup task_setup_data = {
1718 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001719 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001720 .callback_ops = &nfs4_open_confirm_ops,
1721 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001722 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001723 .flags = RPC_TASK_ASYNC,
1724 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 int status;
1726
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001727 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001728 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001729 data->rpc_done = 0;
1730 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001731 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001732 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001733 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001734 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001735 status = nfs4_wait_for_completion_rpc_task(task);
1736 if (status != 0) {
1737 data->cancelled = 1;
1738 smp_wmb();
1739 } else
1740 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001741 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 return status;
1743}
1744
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001745static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747 struct nfs4_opendata *data = calldata;
1748 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001749 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001750
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001751 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001752 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001753 /*
1754 * Check if we still need to send an OPEN call, or if we can use
1755 * a delegation instead.
1756 */
1757 if (data->state != NULL) {
1758 struct nfs_delegation *delegation;
1759
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001760 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001761 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762 rcu_read_lock();
1763 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001764 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001765 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001766 can_open_delegated(delegation, data->o_arg.fmode))
1767 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001768 rcu_read_unlock();
1769 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001770 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001771 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001772 switch (data->o_arg.claim) {
1773 case NFS4_OPEN_CLAIM_PREVIOUS:
1774 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1775 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001776 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001777 case NFS4_OPEN_CLAIM_FH:
1778 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001779 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1780 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001781 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001782 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001783 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001784 &data->o_res.seq_res,
1785 task) != 0)
1786 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001787
1788 /* Set the create mode (note dependency on the session type) */
1789 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1790 if (data->o_arg.open_flags & O_EXCL) {
1791 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1792 if (nfs4_has_persistent_session(clp))
1793 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1794 else if (clp->cl_mvops->minor_version > 0)
1795 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1796 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001797 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001798unlock_no_action:
1799 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001800out_no_action:
1801 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001802out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001803 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001804}
1805
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001806static void nfs4_open_done(struct rpc_task *task, void *calldata)
1807{
1808 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001810 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001811
Trond Myklebust14516c32010-07-31 14:29:06 -04001812 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1813 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001814
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001815 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001816 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1817 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001818 case S_IFREG:
1819 break;
1820 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001821 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001822 break;
1823 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001824 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001825 break;
1826 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001827 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001828 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001829 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001830 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001831 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1832 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001833 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001834 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001835}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001836
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837static void nfs4_open_release(void *calldata)
1838{
1839 struct nfs4_opendata *data = calldata;
1840 struct nfs4_state *state = NULL;
1841
1842 /* If this request hasn't been cancelled, do nothing */
1843 if (data->cancelled == 0)
1844 goto out_free;
1845 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001846 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 goto out_free;
1848 /* In case we need an open_confirm, no cleanup! */
1849 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1850 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001851 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001852 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001853 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001854out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001855 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001856}
1857
1858static const struct rpc_call_ops nfs4_open_ops = {
1859 .rpc_call_prepare = nfs4_open_prepare,
1860 .rpc_call_done = nfs4_open_done,
1861 .rpc_release = nfs4_open_release,
1862};
1863
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001864static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001865{
1866 struct inode *dir = data->dir->d_inode;
1867 struct nfs_server *server = NFS_SERVER(dir);
1868 struct nfs_openargs *o_arg = &data->o_arg;
1869 struct nfs_openres *o_res = &data->o_res;
1870 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001871 struct rpc_message msg = {
1872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1873 .rpc_argp = o_arg,
1874 .rpc_resp = o_res,
1875 .rpc_cred = data->owner->so_cred,
1876 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001877 struct rpc_task_setup task_setup_data = {
1878 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001879 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001880 .callback_ops = &nfs4_open_ops,
1881 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001882 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001883 .flags = RPC_TASK_ASYNC,
1884 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001885 int status;
1886
Chuck Levera9c92d62013-08-09 12:48:18 -04001887 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001888 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001889 data->rpc_done = 0;
1890 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001891 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001892 data->is_recover = 0;
1893 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001894 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001895 data->is_recover = 1;
1896 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001897 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001898 if (IS_ERR(task))
1899 return PTR_ERR(task);
1900 status = nfs4_wait_for_completion_rpc_task(task);
1901 if (status != 0) {
1902 data->cancelled = 1;
1903 smp_wmb();
1904 } else
1905 status = data->rpc_status;
1906 rpc_put_task(task);
1907
1908 return status;
1909}
1910
1911static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1912{
1913 struct inode *dir = data->dir->d_inode;
1914 struct nfs_openres *o_res = &data->o_res;
1915 int status;
1916
1917 status = nfs4_run_open_task(data, 1);
1918 if (status != 0 || !data->rpc_done)
1919 return status;
1920
Trond Myklebust6926afd2012-01-07 13:22:46 -05001921 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1922
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001923 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1924 status = _nfs4_proc_open_confirm(data);
1925 if (status != 0)
1926 return status;
1927 }
1928
1929 return status;
1930}
1931
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001932static int nfs4_opendata_access(struct rpc_cred *cred,
1933 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001934 struct nfs4_state *state, fmode_t fmode,
1935 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001936{
1937 struct nfs_access_entry cache;
1938 u32 mask;
1939
1940 /* access call failed or for some reason the server doesn't
1941 * support any access modes -- defer access call until later */
1942 if (opendata->o_res.access_supported == 0)
1943 return 0;
1944
1945 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001946 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001947 * write mode bits, but POSIX allows the creating process to write.
1948 * use openflags to check for exec, because fmode won't
1949 * always have FMODE_EXEC set when file open for exec. */
1950 if (openflags & __FMODE_EXEC) {
1951 /* ONLY check for exec rights */
1952 mask = MAY_EXEC;
1953 } else if (fmode & FMODE_READ)
1954 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001955
1956 cache.cred = cred;
1957 cache.jiffies = jiffies;
1958 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1959 nfs_access_add_cache(state->inode, &cache);
1960
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001961 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001962 return 0;
1963
1964 /* even though OPEN succeeded, access is denied. Close the file */
1965 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001966 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001967}
1968
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001969/*
1970 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1971 */
1972static int _nfs4_proc_open(struct nfs4_opendata *data)
1973{
1974 struct inode *dir = data->dir->d_inode;
1975 struct nfs_server *server = NFS_SERVER(dir);
1976 struct nfs_openargs *o_arg = &data->o_arg;
1977 struct nfs_openres *o_res = &data->o_res;
1978 int status;
1979
1980 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001981 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001982 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001983 if (status != 0) {
1984 if (status == -NFS4ERR_BADNAME &&
1985 !(o_arg->open_flags & O_CREAT))
1986 return -ENOENT;
1987 return status;
1988 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001989
Trond Myklebust6926afd2012-01-07 13:22:46 -05001990 nfs_fattr_map_and_free_names(server, &data->f_attr);
1991
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001992 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001993 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001994 if (o_arg->open_flags & O_EXCL)
1995 data->file_created = 1;
1996 else if (o_res->cinfo.before != o_res->cinfo.after)
1997 data->file_created = 1;
1998 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001999 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2000 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002002 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04002007 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Andy Adamsond83217c2011-03-01 01:34:17 +00002011static int nfs4_recover_expired_lease(struct nfs_server *server)
2012{
2013 return nfs4_client_recover_expired_lease(server->nfs_client);
2014}
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016/*
2017 * OPEN_EXPIRED:
2018 * reclaim state on the server after a network partition.
2019 * Assumes caller holds the appropriate lock
2020 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002021static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002023 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002024 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002026 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002027 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002028 if (IS_ERR(opendata))
2029 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002030 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002031 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002032 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002033 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002034 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002037static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002038{
Trond Myklebust539cd032007-06-05 11:46:42 -04002039 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002040 struct nfs4_exception exception = { };
2041 int err;
2042
2043 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002044 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002045 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002046 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2047 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002048 switch (err) {
2049 default:
2050 goto out;
2051 case -NFS4ERR_GRACE:
2052 case -NFS4ERR_DELAY:
2053 nfs4_handle_exception(server, err, &exception);
2054 err = 0;
2055 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002056 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002057out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002058 return err;
2059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002064 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Trond Myklebust864472e2006-01-03 09:55:15 +01002066 ctx = nfs4_state_find_open_context(state);
2067 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002068 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002069 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002070 put_nfs_open_context(ctx);
2071 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002074#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002075static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002076{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002077 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002078 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002079 struct nfs_delegation *delegation;
2080 struct rpc_cred *cred = NULL;
2081 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002082
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002083 /* If a state reset has been done, test_stateid is unneeded */
2084 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2085 return;
2086
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002087 /* Get the delegation credential for use by test/free_stateid */
2088 rcu_read_lock();
2089 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2090 if (delegation != NULL &&
2091 nfs4_stateid_match(&delegation->stateid, stateid)) {
2092 cred = get_rpccred(delegation->cred);
2093 rcu_read_unlock();
2094 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002095 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002096 } else
2097 rcu_read_unlock();
2098
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002099 if (status != NFS_OK) {
2100 /* Free the stateid unless the server explicitly
2101 * informs us the stateid is unrecognized. */
2102 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002103 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002104 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002105
Bryan Schumaker69388672012-09-26 15:25:52 -04002106 write_seqlock(&state->seqlock);
2107 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2108 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002109 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2110 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002111
2112 if (cred != NULL)
2113 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002114}
2115
2116/**
2117 * nfs41_check_open_stateid - possibly free an open stateid
2118 *
2119 * @state: NFSv4 state for an inode
2120 *
2121 * Returns NFS_OK if recovery for this stateid is now finished.
2122 * Otherwise a negative NFS4ERR value is returned.
2123 */
2124static int nfs41_check_open_stateid(struct nfs4_state *state)
2125{
2126 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002127 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002128 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002129 int status;
2130
2131 /* If a state reset has been done, test_stateid is unneeded */
2132 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2133 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2134 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2135 return -NFS4ERR_BAD_STATEID;
2136
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002137 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002138 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002139 if (status != NFS_OK) {
2140 /* Free the stateid unless the server explicitly
2141 * informs us the stateid is unrecognized. */
2142 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002143 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002144
2145 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2146 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2147 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002148 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002149 }
2150 return status;
2151}
2152
2153static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2154{
Chuck Levereb64cf92012-07-11 16:30:05 -04002155 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002156
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002157 nfs41_clear_delegation_stateid(state);
2158 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002159 if (status != NFS_OK)
2160 status = nfs4_open_expired(sp, state);
2161 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002162}
2163#endif
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002166 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2167 * fields corresponding to attributes that were used to store the verifier.
2168 * Make sure we clobber those fields in the later setattr call
2169 */
2170static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2171{
2172 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2173 !(sattr->ia_valid & ATTR_ATIME_SET))
2174 sattr->ia_valid |= ATTR_ATIME;
2175
2176 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2177 !(sattr->ia_valid & ATTR_MTIME_SET))
2178 sattr->ia_valid |= ATTR_MTIME;
2179}
2180
Trond Myklebustc21443c2013-02-07 14:26:21 -05002181static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2182 fmode_t fmode,
2183 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002184 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002185{
2186 struct nfs4_state_owner *sp = opendata->owner;
2187 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002188 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002189 struct nfs4_state *state;
2190 unsigned int seq;
2191 int ret;
2192
2193 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2194
2195 ret = _nfs4_proc_open(opendata);
2196 if (ret != 0)
2197 goto out;
2198
2199 state = nfs4_opendata_to_nfs4_state(opendata);
2200 ret = PTR_ERR(state);
2201 if (IS_ERR(state))
2202 goto out;
2203 if (server->caps & NFS_CAP_POSIX_LOCK)
2204 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2205
Trond Myklebust275bb302013-05-29 13:11:28 -04002206 dentry = opendata->dentry;
2207 if (dentry->d_inode == NULL) {
2208 /* FIXME: Is this d_drop() ever needed? */
2209 d_drop(dentry);
2210 dentry = d_add_unique(dentry, igrab(state->inode));
2211 if (dentry == NULL) {
2212 dentry = opendata->dentry;
2213 } else if (dentry != ctx->dentry) {
2214 dput(ctx->dentry);
2215 ctx->dentry = dget(dentry);
2216 }
2217 nfs_set_verifier(dentry,
2218 nfs_save_change_attribute(opendata->dir->d_inode));
2219 }
2220
Trond Myklebustc21443c2013-02-07 14:26:21 -05002221 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2222 if (ret != 0)
2223 goto out;
2224
Trond Myklebust3efb9722013-05-29 13:17:04 -04002225 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002226 if (dentry->d_inode == state->inode) {
2227 nfs_inode_attach_open_context(ctx);
2228 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2229 nfs4_schedule_stateid_recovery(server, state);
2230 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002231out:
2232 return ret;
2233}
2234
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002235/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002236 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 */
Andy Adamson82be4172012-05-23 05:02:35 -04002238static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002239 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002240 int flags,
2241 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002242 struct nfs4_label *label,
2243 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
2245 struct nfs4_state_owner *sp;
2246 struct nfs4_state *state = NULL;
2247 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002248 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002249 struct dentry *dentry = ctx->dentry;
2250 struct rpc_cred *cred = ctx->cred;
2251 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2252 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002253 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002254 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002255 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002259 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2260 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2262 goto out_err;
2263 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002264 status = nfs4_recover_expired_lease(server);
2265 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002266 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002267 if (dentry->d_inode != NULL)
2268 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002269 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002270 if (dentry->d_inode)
2271 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002272 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002273 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002274 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002275 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
David Quigley14c43f72013-05-22 12:50:43 -04002277 if (label) {
2278 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2279 if (IS_ERR(olabel)) {
2280 status = PTR_ERR(olabel);
2281 goto err_opendata_put;
2282 }
2283 }
2284
Andy Adamson82be4172012-05-23 05:02:35 -04002285 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2286 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2287 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002288 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002289 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002290 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002291 if (dentry->d_inode != NULL)
2292 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002293
Trond Myklebust3efb9722013-05-29 13:17:04 -04002294 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002295 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002296 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002297 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002298
Trond Myklebust549b19c2013-04-16 18:42:34 -04002299 if ((opendata->o_arg.open_flags & O_EXCL) &&
2300 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002301 nfs4_exclusive_attrset(opendata, sattr);
2302
2303 nfs_fattr_init(opendata->o_res.f_attr);
2304 status = nfs4_do_setattr(state->inode, cred,
2305 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002306 state, label, olabel);
2307 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002308 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002309 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002310 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002311 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002312 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002313 if (opendata->file_created)
2314 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002315
2316 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2317 *ctx_th = opendata->f_attr.mdsthreshold;
2318 else
2319 kfree(opendata->f_attr.mdsthreshold);
2320 opendata->f_attr.mdsthreshold = NULL;
2321
David Quigley14c43f72013-05-22 12:50:43 -04002322 nfs4_label_free(olabel);
2323
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002324 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002327err_free_label:
2328 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002329err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002330 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002331 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002332err_put_state_owner:
2333 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return status;
2336}
2337
2338
Andy Adamson82be4172012-05-23 05:02:35 -04002339static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002340 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002341 int flags,
2342 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002343 struct nfs4_label *label,
2344 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002346 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct nfs4_exception exception = { };
2348 struct nfs4_state *res;
2349 int status;
2350
2351 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002352 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002353 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002354 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (status == 0)
2356 break;
2357 /* NOTE: BAD_SEQID means the server and client disagree about the
2358 * book-keeping w.r.t. state-changing operations
2359 * (OPEN/CLOSE/LOCK/LOCKU...)
2360 * It is actually a sign of a bug on the client or on the server.
2361 *
2362 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002363 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 * have unhashed the old state_owner for us, and that we can
2365 * therefore safely retry using a new one. We should still warn
2366 * the user though...
2367 */
2368 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002369 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002370 " returned a bad sequence-id error!\n",
2371 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 exception.retry = 1;
2373 continue;
2374 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002375 /*
2376 * BAD_STATEID on OPEN means that the server cancelled our
2377 * state before it received the OPEN_CONFIRM.
2378 * Recover by retrying the request as per the discussion
2379 * on Page 181 of RFC3530.
2380 */
2381 if (status == -NFS4ERR_BAD_STATEID) {
2382 exception.retry = 1;
2383 continue;
2384 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002385 if (status == -EAGAIN) {
2386 /* We must have found a delegation */
2387 exception.retry = 1;
2388 continue;
2389 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002390 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2391 continue;
2392 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 status, &exception));
2394 } while (exception.retry);
2395 return res;
2396}
2397
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002398static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2399 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002400 struct nfs4_state *state, struct nfs4_label *ilabel,
2401 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002403 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002405 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 .iap = sattr,
2407 .server = server,
2408 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002409 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 };
2411 struct nfs_setattrres res = {
2412 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002413 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 .server = server,
2415 };
2416 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2418 .rpc_argp = &arg,
2419 .rpc_resp = &res,
2420 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002422 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002423 fmode_t fmode;
2424 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002425 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
David Quigleyaa9c2662013-05-22 12:50:44 -04002427 arg.bitmask = nfs4_bitmask(server, ilabel);
2428 if (ilabel)
2429 arg.bitmask = nfs4_bitmask(server, olabel);
2430
Trond Myklebust0e574af2005-10-27 22:12:38 -04002431 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Trond Myklebustee3ae842013-04-29 10:35:36 -04002433 /* Servers should only apply open mode checks for file size changes */
2434 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2435 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2436
2437 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2438 /* Use that stateid */
2439 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002440 struct nfs_lockowner lockowner = {
2441 .l_owner = current->files,
2442 .l_pid = current->tgid,
2443 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002444 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002445 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002446 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002447 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Bryan Schumaker7c513052011-03-24 17:12:24 +00002449 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002450 if (status == 0 && state != NULL)
2451 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002452 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002455static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2456 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002457 struct nfs4_state *state, struct nfs4_label *ilabel,
2458 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002460 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002461 struct nfs4_exception exception = {
2462 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002463 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002464 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 int err;
2466 do {
David Quigley1775fd32013-05-22 12:50:42 -04002467 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002468 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002469 switch (err) {
2470 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002471 if (!(sattr->ia_valid & ATTR_SIZE)) {
2472 pr_warn_once("NFSv4: server %s is incorrectly "
2473 "applying open mode checks to "
2474 "a SETATTR that is not "
2475 "changing file size.\n",
2476 server->nfs_client->cl_hostname);
2477 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002478 if (state && !(state->state & FMODE_WRITE)) {
2479 err = -EBADF;
2480 if (sattr->ia_valid & ATTR_OPEN)
2481 err = -EACCES;
2482 goto out;
2483 }
2484 }
2485 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002487out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 return err;
2489}
2490
2491struct nfs4_closedata {
2492 struct inode *inode;
2493 struct nfs4_state *state;
2494 struct nfs_closeargs arg;
2495 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002496 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002497 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002498 bool roc;
2499 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500};
2501
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002502static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002503{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002504 struct nfs4_closedata *calldata = data;
2505 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002506 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002507
Fred Isamanf7e89172011-01-06 11:36:32 +00002508 if (calldata->roc)
2509 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002510 nfs4_put_open_state(calldata->state);
2511 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002512 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002513 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002514 kfree(calldata);
2515}
2516
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002517static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002519 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 struct nfs_server *server = NFS_SERVER(calldata->inode);
2522
Chuck Levera3ca5652012-03-01 17:00:40 -05002523 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002524 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2525 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002526 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 /* hmm. we are done with the inode, and in the process of freeing
2528 * the state_owner. we keep this around to process errors
2529 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 switch (task->tk_status) {
2531 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002532 if (calldata->roc)
2533 pnfs_roc_set_barrier(state->inode,
2534 calldata->roc_barrier);
Trond Myklebust226056c2014-02-11 10:41:07 -05002535 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002536 renew_lease(server, calldata->timestamp);
Trond Myklebust226056c2014-02-11 10:41:07 -05002537 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002538 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002540 case -NFS4ERR_OLD_STATEID:
2541 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002543 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002544 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 default:
Trond Myklebust69794ad2013-11-20 12:57:19 -05002546 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002547 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002548 goto out_release;
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Trond Myklebust226056c2014-02-11 10:41:07 -05002551 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002552out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002553 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002554 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002555 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556}
2557
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002558static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002560 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002561 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002562 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002563 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002564
Chuck Levera3ca5652012-03-01 17:00:40 -05002565 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002566 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002567 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002568
Trond Myklebust88069f72009-12-08 08:33:16 -05002569 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2570 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002571 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002572 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002573 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002574 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002575 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2576 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2577 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002578 }
2579 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002580 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2581 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2582 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002583 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002584 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002585 if (!nfs4_valid_open_stateid(state))
2586 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002587 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002588
2589 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002590 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002591 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002592 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002593
Fred Isamanf7e89172011-01-06 11:36:32 +00002594 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002595 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002596 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002597 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2598 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002599 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002600 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002601 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002602
Trond Myklebust516a6af2005-10-27 22:12:41 -04002603 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002604 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002605 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002606 &calldata->arg.seq_args,
2607 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002608 task) != 0)
2609 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002610 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002611 return;
2612out_no_action:
2613 task->tk_action = NULL;
2614out_wait:
2615 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002618static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002619 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002620 .rpc_call_done = nfs4_close_done,
2621 .rpc_release = nfs4_free_closedata,
2622};
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624/*
2625 * It is possible for data to be read/written from a mem-mapped file
2626 * after the sys_close call (which hits the vfs layer as a flush).
2627 * This means that we can't safely call nfsv4 close on a file until
2628 * the inode is cleared. This in turn means that we are not good
2629 * NFSv4 citizens - we do not indicate to the server to update the file's
2630 * share state even when we are done with one of the three share
2631 * stateid's in the inode.
2632 *
2633 * NOTE: Caller must be holding the sp->so_owner semaphore!
2634 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002635int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002637 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002639 struct nfs4_state_owner *sp = state->owner;
2640 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002641 struct rpc_message msg = {
2642 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2643 .rpc_cred = state->owner->so_cred,
2644 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002645 struct rpc_task_setup task_setup_data = {
2646 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002647 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002648 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002649 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002650 .flags = RPC_TASK_ASYNC,
2651 };
Trond Myklebust95121352005-10-18 14:20:12 -07002652 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002654 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2655 &task_setup_data.rpc_client, &msg);
2656
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002657 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002659 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002660 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002661 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002663 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002664 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002666 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002667 if (calldata->arg.seqid == NULL)
2668 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002669 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002670 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002671 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002672 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002673 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002674 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002675 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002676
Trond Myklebust1174dd12010-12-21 10:52:24 -05002677 msg.rpc_argp = &calldata->arg;
2678 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002679 task_setup_data.callback_data = calldata;
2680 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002681 if (IS_ERR(task))
2682 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002683 status = 0;
2684 if (wait)
2685 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002686 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002687 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002688out_free_calldata:
2689 kfree(calldata);
2690out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002691 nfs4_put_open_state(state);
2692 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002693 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Trond Myklebust2b484292010-09-17 10:56:51 -04002696static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002697nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2698 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002701 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2702
2703 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebust565277f2007-10-15 18:17:53 -04002705 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002706 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002707
2708 nfs4_label_release_security(label);
2709
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002710 if (IS_ERR(state))
2711 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002712 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Trond Myklebust1185a552009-12-03 15:54:02 -05002715static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002716{
2717 if (ctx->state == NULL)
2718 return;
2719 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002720 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002721 else
Al Viro643168c2011-06-22 18:20:23 -04002722 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002723}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Trond Myklebustb944dba2013-11-04 15:20:20 -05002725#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2726#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2727#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2730{
Benny Halevy43652ad2009-04-01 09:21:54 -04002731 struct nfs4_server_caps_arg args = {
2732 .fhandle = fhandle,
2733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct nfs4_server_caps_res res = {};
2735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002737 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 .rpc_resp = &res,
2739 };
2740 int status;
2741
Bryan Schumaker7c513052011-03-24 17:12:24 +00002742 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002744 /* Sanity check the server answers */
2745 switch (server->nfs_client->cl_minorversion) {
2746 case 0:
2747 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2748 res.attr_bitmask[2] = 0;
2749 break;
2750 case 1:
2751 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2752 break;
2753 case 2:
2754 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002757 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2758 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2759 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2760 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002761 NFS_CAP_CTIME|NFS_CAP_MTIME|
2762 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002763 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2764 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 server->caps |= NFS_CAP_ACLS;
2766 if (res.has_links != 0)
2767 server->caps |= NFS_CAP_HARDLINKS;
2768 if (res.has_symlinks != 0)
2769 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002770 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2771 server->caps |= NFS_CAP_FILEID;
2772 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2773 server->caps |= NFS_CAP_MODE;
2774 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2775 server->caps |= NFS_CAP_NLINK;
2776 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2777 server->caps |= NFS_CAP_OWNER;
2778 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2779 server->caps |= NFS_CAP_OWNER_GROUP;
2780 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2781 server->caps |= NFS_CAP_ATIME;
2782 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2783 server->caps |= NFS_CAP_CTIME;
2784 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2785 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002786#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2787 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2788 server->caps |= NFS_CAP_SECURITY_LABEL;
2789#endif
2790 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2791 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002792 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002793
Trond Myklebusta65318b2009-03-11 14:10:28 -04002794 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2795 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2796 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002797 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002799 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return status;
2803}
2804
Trond Myklebust55a97592006-06-09 09:34:19 -04002805int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 struct nfs4_exception exception = { };
2808 int err;
2809 do {
2810 err = nfs4_handle_exception(server,
2811 _nfs4_server_capabilities(server, fhandle),
2812 &exception);
2813 } while (exception.retry);
2814 return err;
2815}
2816
2817static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2818 struct nfs_fsinfo *info)
2819{
David Quigleyaa9c2662013-05-22 12:50:44 -04002820 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002822 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 };
2824 struct nfs4_lookup_res res = {
2825 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002826 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 .fh = fhandle,
2828 };
2829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2831 .rpc_argp = &args,
2832 .rpc_resp = &res,
2833 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002834
David Quigleyaa9c2662013-05-22 12:50:44 -04002835 bitmask[0] = nfs4_fattr_bitmap[0];
2836 bitmask[1] = nfs4_fattr_bitmap[1];
2837 /*
2838 * Process the label in the upcoming getfattr
2839 */
2840 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2841
Trond Myklebust0e574af2005-10-27 22:12:38 -04002842 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002843 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
2846static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2847 struct nfs_fsinfo *info)
2848{
2849 struct nfs4_exception exception = { };
2850 int err;
2851 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002852 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002853 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002854 switch (err) {
2855 case 0:
2856 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002857 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002858 default:
2859 err = nfs4_handle_exception(server, err, &exception);
2860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return err;
2864}
2865
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002866static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2867 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2868{
Trond Myklebustc2190662013-08-26 19:23:04 -04002869 struct rpc_auth_create_args auth_args = {
2870 .pseudoflavor = flavor,
2871 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002872 struct rpc_auth *auth;
2873 int ret;
2874
Trond Myklebustc2190662013-08-26 19:23:04 -04002875 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002876 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002877 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002878 goto out;
2879 }
2880 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002881out:
2882 return ret;
2883}
2884
Chuck Lever9a744ba2013-03-16 15:56:02 -04002885/*
2886 * Retry pseudoroot lookup with various security flavors. We do this when:
2887 *
2888 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2889 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2890 *
2891 * Returns zero on success, or a negative NFS4ERR value, or a
2892 * negative errno value.
2893 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002894static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002895 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002897 /* Per 3530bis 15.33.5 */
2898 static const rpc_authflavor_t flav_array[] = {
2899 RPC_AUTH_GSS_KRB5P,
2900 RPC_AUTH_GSS_KRB5I,
2901 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002902 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002903 RPC_AUTH_NULL,
2904 };
2905 int status = -EPERM;
2906 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04002908 if (server->auth_info.flavor_len > 0) {
2909 /* try each flavor specified by user */
2910 for (i = 0; i < server->auth_info.flavor_len; i++) {
2911 status = nfs4_lookup_root_sec(server, fhandle, info,
2912 server->auth_info.flavors[i]);
2913 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2914 continue;
2915 break;
2916 }
2917 } else {
2918 /* no flavors specified by user, try default list */
2919 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2920 status = nfs4_lookup_root_sec(server, fhandle, info,
2921 flav_array[i]);
2922 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2923 continue;
2924 break;
2925 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002926 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002927
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002928 /*
2929 * -EACCESS could mean that the user doesn't have correct permissions
2930 * to access the mount. It could also mean that we tried to mount
2931 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2932 * existing mount programs don't handle -EACCES very well so it should
2933 * be mapped to -EPERM instead.
2934 */
2935 if (status == -EACCES)
2936 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002937 return status;
2938}
2939
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002940static int nfs4_do_find_root_sec(struct nfs_server *server,
2941 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2942{
2943 int mv = server->nfs_client->cl_minorversion;
2944 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2945}
2946
2947/**
2948 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2949 * @server: initialized nfs_server handle
2950 * @fhandle: we fill in the pseudo-fs root file handle
2951 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002952 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002953 *
2954 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002955 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002956int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002957 struct nfs_fsinfo *info,
2958 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002959{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002960 int status;
2961
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002962 switch (auth_probe) {
2963 case false:
2964 status = nfs4_lookup_root(server, fhandle, info);
2965 if (status != -NFS4ERR_WRONGSEC)
2966 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002967 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002968 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002969 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (status == 0)
2972 status = nfs4_server_capabilities(server, fhandle);
2973 if (status == 0)
2974 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002975
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002976 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977}
2978
Bryan Schumakerbae36242012-05-10 15:07:31 -04002979static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2980 struct nfs_fsinfo *info)
2981{
2982 int error;
2983 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002984 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002985
2986 error = nfs4_server_capabilities(server, mntfh);
2987 if (error < 0) {
2988 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2989 return error;
2990 }
2991
David Quigley14c43f72013-05-22 12:50:43 -04002992 label = nfs4_label_alloc(server, GFP_KERNEL);
2993 if (IS_ERR(label))
2994 return PTR_ERR(label);
2995
David Quigley1775fd32013-05-22 12:50:42 -04002996 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002997 if (error < 0) {
2998 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002999 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003000 }
3001
3002 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3003 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3004 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3005
David Quigley14c43f72013-05-22 12:50:43 -04003006err_free_label:
3007 nfs4_label_free(label);
3008
Bryan Schumakerbae36242012-05-10 15:07:31 -04003009 return error;
3010}
3011
Manoj Naik6b97fd32006-06-09 09:34:29 -04003012/*
3013 * Get locations and (maybe) other attributes of a referral.
3014 * Note that we'll actually follow the referral later when
3015 * we detect fsid mismatch in inode revalidation
3016 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003017static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3018 const struct qstr *name, struct nfs_fattr *fattr,
3019 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003020{
3021 int status = -ENOMEM;
3022 struct page *page = NULL;
3023 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003024
3025 page = alloc_page(GFP_KERNEL);
3026 if (page == NULL)
3027 goto out;
3028 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3029 if (locations == NULL)
3030 goto out;
3031
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003032 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003033 if (status != 0)
3034 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003035
3036 /*
3037 * If the fsid didn't change, this is a migration event, not a
3038 * referral. Cause us to drop into the exception handler, which
3039 * will kick off migration recovery.
3040 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003041 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003042 dprintk("%s: server did not return a different fsid for"
3043 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003044 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003045 goto out;
3046 }
Andy Adamson533eb462011-06-13 18:25:56 -04003047 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3048 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003049
Andy Adamson533eb462011-06-13 18:25:56 -04003050 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003051 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003052 memset(fhandle, 0, sizeof(struct nfs_fh));
3053out:
3054 if (page)
3055 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003056 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003057 return status;
3058}
3059
David Quigley1775fd32013-05-22 12:50:42 -04003060static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3061 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
3063 struct nfs4_getattr_arg args = {
3064 .fh = fhandle,
3065 .bitmask = server->attr_bitmask,
3066 };
3067 struct nfs4_getattr_res res = {
3068 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003069 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 .server = server,
3071 };
3072 struct rpc_message msg = {
3073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3074 .rpc_argp = &args,
3075 .rpc_resp = &res,
3076 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003077
3078 args.bitmask = nfs4_bitmask(server, label);
3079
Trond Myklebust0e574af2005-10-27 22:12:38 -04003080 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003081 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
David Quigley1775fd32013-05-22 12:50:42 -04003084static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3085 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086{
3087 struct nfs4_exception exception = { };
3088 int err;
3089 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003090 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3091 trace_nfs4_getattr(server, fhandle, fattr, err);
3092 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 &exception);
3094 } while (exception.retry);
3095 return err;
3096}
3097
3098/*
3099 * The file is not closed if it is opened due to the a request to change
3100 * the size of the file. The open call will not be needed once the
3101 * VFS layer lookup-intents are implemented.
3102 *
3103 * Close is called when the inode is destroyed.
3104 * If we haven't opened the file for O_WRONLY, we
3105 * need to in the size_change case to obtain a stateid.
3106 *
3107 * Got race?
3108 * Because OPEN is always done by name in nfsv4, it is
3109 * possible that we opened a different file by the same
3110 * name. We can recognize this race condition, but we
3111 * can't do anything about it besides returning an error.
3112 *
3113 * This will be fixed with VFS changes (lookup-intent).
3114 */
3115static int
3116nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3117 struct iattr *sattr)
3118{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003119 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003120 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003121 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003122 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 int status;
3124
Benny Halevy8a1636c2010-07-14 15:43:57 -04003125 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003126 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003127
Trond Myklebust0e574af2005-10-27 22:12:38 -04003128 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Andy Adamson26699402012-05-30 16:12:24 -04003130 /* Deal with open(O_TRUNC) */
3131 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003132 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003133
3134 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003135 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003136 return 0;
3137
Trond Myklebustd5308382005-11-04 15:33:38 -05003138 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003139 if (sattr->ia_valid & ATTR_FILE) {
3140 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003141
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003142 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003143 if (ctx) {
3144 cred = ctx->cred;
3145 state = ctx->state;
3146 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003147 }
3148
David Quigley14c43f72013-05-22 12:50:43 -04003149 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3150 if (IS_ERR(label))
3151 return PTR_ERR(label);
3152
3153 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003154 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003155 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003156 nfs_setsecurity(inode, fattr, label);
3157 }
David Quigley14c43f72013-05-22 12:50:43 -04003158 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return status;
3160}
3161
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003162static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3163 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003164 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003165{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003166 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003167 int status;
3168 struct nfs4_lookup_arg args = {
3169 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003170 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003171 .name = name,
3172 };
3173 struct nfs4_lookup_res res = {
3174 .server = server,
3175 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003176 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003177 .fh = fhandle,
3178 };
3179 struct rpc_message msg = {
3180 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3181 .rpc_argp = &args,
3182 .rpc_resp = &res,
3183 };
3184
David Quigleyaa9c2662013-05-22 12:50:44 -04003185 args.bitmask = nfs4_bitmask(server, label);
3186
David Howells2b3de442006-08-22 20:06:09 -04003187 nfs_fattr_init(fattr);
3188
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003190 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 dprintk("NFS reply lookup: %d\n", status);
3192 return status;
3193}
3194
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003195static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003196{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003197 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003198 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003199 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3200 fattr->nlink = 2;
3201}
3202
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003203static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3204 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003205 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003206{
3207 struct nfs4_exception exception = { };
3208 struct rpc_clnt *client = *clnt;
3209 int err;
3210 do {
David Quigley1775fd32013-05-22 12:50:42 -04003211 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003212 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003213 switch (err) {
3214 case -NFS4ERR_BADNAME:
3215 err = -ENOENT;
3216 goto out;
3217 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003218 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003219 goto out;
3220 case -NFS4ERR_WRONGSEC:
3221 err = -EPERM;
3222 if (client != *clnt)
3223 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003224 client = nfs4_create_sec_client(client, dir, name);
3225 if (IS_ERR(client))
3226 return PTR_ERR(client);
3227
3228 exception.retry = 1;
3229 break;
3230 default:
3231 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3232 }
3233 } while (exception.retry);
3234
3235out:
3236 if (err == 0)
3237 *clnt = client;
3238 else if (client != *clnt)
3239 rpc_shutdown_client(client);
3240
3241 return err;
3242}
3243
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003244static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003245 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3246 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003248 int status;
3249 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003250
David Quigley1775fd32013-05-22 12:50:42 -04003251 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003252 if (client != NFS_CLIENT(dir)) {
3253 rpc_shutdown_client(client);
3254 nfs_fixup_secinfo_attributes(fattr);
3255 }
3256 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003259struct rpc_clnt *
3260nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3261 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3262{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003263 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003264 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003265
David Quigley1775fd32013-05-22 12:50:42 -04003266 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003267 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003268 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003269 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003270}
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3273{
Trond Myklebust76b32992007-08-10 17:45:11 -04003274 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 struct nfs4_accessargs args = {
3276 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003277 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003279 struct nfs4_accessres res = {
3280 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003281 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 struct rpc_message msg = {
3283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3284 .rpc_argp = &args,
3285 .rpc_resp = &res,
3286 .rpc_cred = entry->cred,
3287 };
3288 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003289 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 /*
3292 * Determine which access bits we want to ask for...
3293 */
3294 if (mode & MAY_READ)
3295 args.access |= NFS4_ACCESS_READ;
3296 if (S_ISDIR(inode->i_mode)) {
3297 if (mode & MAY_WRITE)
3298 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3299 if (mode & MAY_EXEC)
3300 args.access |= NFS4_ACCESS_LOOKUP;
3301 } else {
3302 if (mode & MAY_WRITE)
3303 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3304 if (mode & MAY_EXEC)
3305 args.access |= NFS4_ACCESS_EXECUTE;
3306 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003307
3308 res.fattr = nfs_alloc_fattr();
3309 if (res.fattr == NULL)
3310 return -ENOMEM;
3311
Bryan Schumaker7c513052011-03-24 17:12:24 +00003312 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003314 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003315 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003317 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 return status;
3319}
3320
3321static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3322{
3323 struct nfs4_exception exception = { };
3324 int err;
3325 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003326 err = _nfs4_proc_access(inode, entry);
3327 trace_nfs4_access(inode, err);
3328 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 &exception);
3330 } while (exception.retry);
3331 return err;
3332}
3333
3334/*
3335 * TODO: For the time being, we don't try to get any attributes
3336 * along with any of the zero-copy operations READ, READDIR,
3337 * READLINK, WRITE.
3338 *
3339 * In the case of the first three, we want to put the GETATTR
3340 * after the read-type operation -- this is because it is hard
3341 * to predict the length of a GETATTR response in v4, and thus
3342 * align the READ data correctly. This means that the GETATTR
3343 * may end up partially falling into the page cache, and we should
3344 * shift it into the 'tail' of the xdr_buf before processing.
3345 * To do this efficiently, we need to know the total length
3346 * of data received, which doesn't seem to be available outside
3347 * of the RPC layer.
3348 *
3349 * In the case of WRITE, we also want to put the GETATTR after
3350 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003351 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 *
3353 * Both of these changes to the XDR layer would in fact be quite
3354 * minor, but I decided to leave them for a subsequent patch.
3355 */
3356static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3357 unsigned int pgbase, unsigned int pglen)
3358{
3359 struct nfs4_readlink args = {
3360 .fh = NFS_FH(inode),
3361 .pgbase = pgbase,
3362 .pglen = pglen,
3363 .pages = &page,
3364 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003365 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3368 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003369 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 };
3371
Bryan Schumaker7c513052011-03-24 17:12:24 +00003372 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 -07003373}
3374
3375static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3376 unsigned int pgbase, unsigned int pglen)
3377{
3378 struct nfs4_exception exception = { };
3379 int err;
3380 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003381 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3382 trace_nfs4_readlink(inode, err);
3383 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 &exception);
3385 } while (exception.retry);
3386 return err;
3387}
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003390 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392static int
3393nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003394 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395{
David Quigleyaa9c2662013-05-22 12:50:44 -04003396 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003397 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003399 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 int status = 0;
3401
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003402 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3403 if (IS_ERR(ctx))
3404 return PTR_ERR(ctx);
3405
David Quigleyaa9c2662013-05-22 12:50:44 -04003406 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3407
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003408 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003409 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (IS_ERR(state)) {
3411 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003415 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003416 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return status;
3418}
3419
3420static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3421{
Trond Myklebust16e42952005-10-27 22:12:44 -04003422 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003423 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003425 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003427 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003428 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003429 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003431 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3432 .rpc_argp = &args,
3433 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003435 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003436
Bryan Schumaker7c513052011-03-24 17:12:24 +00003437 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003438 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003439 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 return status;
3441}
3442
3443static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3444{
3445 struct nfs4_exception exception = { };
3446 int err;
3447 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003448 err = _nfs4_proc_remove(dir, name);
3449 trace_nfs4_remove(dir, name, err);
3450 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 &exception);
3452 } while (exception.retry);
3453 return err;
3454}
3455
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003456static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003458 struct nfs_server *server = NFS_SERVER(dir);
3459 struct nfs_removeargs *args = msg->rpc_argp;
3460 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003462 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003464 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003465
3466 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003469static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3470{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003471 nfs4_setup_sequence(NFS_SERVER(data->dir),
3472 &data->args.seq_args,
3473 &data->res.seq_res,
3474 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475}
3476
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003477static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003479 struct nfs_unlinkdata *data = task->tk_calldata;
3480 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003481
Trond Myklebust14516c32010-07-31 14:29:06 -04003482 if (!nfs4_sequence_done(task, &res->seq_res))
3483 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003484 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003485 return 0;
3486 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003487 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488}
3489
Jeff Laytond3d41522010-09-17 17:31:57 -04003490static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3491{
3492 struct nfs_server *server = NFS_SERVER(dir);
3493 struct nfs_renameargs *arg = msg->rpc_argp;
3494 struct nfs_renameres *res = msg->rpc_resp;
3495
3496 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003497 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003498 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003499}
3500
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003501static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3502{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003503 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3504 &data->args.seq_args,
3505 &data->res.seq_res,
3506 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003507}
3508
3509static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3510 struct inode *new_dir)
3511{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003512 struct nfs_renamedata *data = task->tk_calldata;
3513 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003514
3515 if (!nfs4_sequence_done(task, &res->seq_res))
3516 return 0;
3517 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3518 return 0;
3519
3520 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003521 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003522 return 1;
3523}
3524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3526 struct inode *new_dir, struct qstr *new_name)
3527{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003528 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003529 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 .old_dir = NFS_FH(old_dir),
3531 .new_dir = NFS_FH(new_dir),
3532 .old_name = old_name,
3533 .new_name = new_name,
3534 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003535 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003536 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003537 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3540 .rpc_argp = &arg,
3541 .rpc_resp = &res,
3542 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003543 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003544
Bryan Schumaker7c513052011-03-24 17:12:24 +00003545 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if (!status) {
3547 update_changeattr(old_dir, &res.old_cinfo);
3548 update_changeattr(new_dir, &res.new_cinfo);
3549 }
3550 return status;
3551}
3552
3553static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3554 struct inode *new_dir, struct qstr *new_name)
3555{
3556 struct nfs4_exception exception = { };
3557 int err;
3558 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003559 err = _nfs4_proc_rename(old_dir, old_name,
3560 new_dir, new_name);
3561 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3562 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 &exception);
3564 } while (exception.retry);
3565 return err;
3566}
3567
3568static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3569{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003570 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 struct nfs4_link_arg arg = {
3572 .fh = NFS_FH(inode),
3573 .dir_fh = NFS_FH(dir),
3574 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003575 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003577 struct nfs4_link_res res = {
3578 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003579 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003580 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 struct rpc_message msg = {
3582 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3583 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003584 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003586 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
Trond Myklebust136f2622010-04-16 16:22:49 -04003588 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003589 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003590 goto out;
3591
David Quigley14c43f72013-05-22 12:50:43 -04003592 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3593 if (IS_ERR(res.label)) {
3594 status = PTR_ERR(res.label);
3595 goto out;
3596 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003598
Bryan Schumaker7c513052011-03-24 17:12:24 +00003599 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003600 if (!status) {
3601 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003602 status = nfs_post_op_update_inode(inode, res.fattr);
3603 if (!status)
3604 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003605 }
David Quigley14c43f72013-05-22 12:50:43 -04003606
3607
3608 nfs4_label_free(res.label);
3609
Trond Myklebust136f2622010-04-16 16:22:49 -04003610out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003611 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 return status;
3613}
3614
3615static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3616{
3617 struct nfs4_exception exception = { };
3618 int err;
3619 do {
3620 err = nfs4_handle_exception(NFS_SERVER(inode),
3621 _nfs4_proc_link(inode, dir, name),
3622 &exception);
3623 } while (exception.retry);
3624 return err;
3625}
3626
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003627struct nfs4_createdata {
3628 struct rpc_message msg;
3629 struct nfs4_create_arg arg;
3630 struct nfs4_create_res res;
3631 struct nfs_fh fh;
3632 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003633 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003634};
3635
3636static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3637 struct qstr *name, struct iattr *sattr, u32 ftype)
3638{
3639 struct nfs4_createdata *data;
3640
3641 data = kzalloc(sizeof(*data), GFP_KERNEL);
3642 if (data != NULL) {
3643 struct nfs_server *server = NFS_SERVER(dir);
3644
David Quigley14c43f72013-05-22 12:50:43 -04003645 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3646 if (IS_ERR(data->label))
3647 goto out_free;
3648
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003649 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3650 data->msg.rpc_argp = &data->arg;
3651 data->msg.rpc_resp = &data->res;
3652 data->arg.dir_fh = NFS_FH(dir);
3653 data->arg.server = server;
3654 data->arg.name = name;
3655 data->arg.attrs = sattr;
3656 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003657 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003658 data->res.server = server;
3659 data->res.fh = &data->fh;
3660 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003661 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003662 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663 }
3664 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003665out_free:
3666 kfree(data);
3667 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003668}
3669
3670static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3671{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003672 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003673 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003674 if (status == 0) {
3675 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003676 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 }
3678 return status;
3679}
3680
3681static void nfs4_free_createdata(struct nfs4_createdata *data)
3682{
David Quigley14c43f72013-05-22 12:50:43 -04003683 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003684 kfree(data);
3685}
3686
Chuck Lever4f390c12006-08-22 20:06:22 -04003687static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003688 struct page *page, unsigned int len, struct iattr *sattr,
3689 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003691 struct nfs4_createdata *data;
3692 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
Chuck Lever94a6d752006-08-22 20:06:23 -04003694 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003695 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003696
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003697 status = -ENOMEM;
3698 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3699 if (data == NULL)
3700 goto out;
3701
3702 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3703 data->arg.u.symlink.pages = &page;
3704 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003705 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003707 status = nfs4_do_create(dir, dentry, data);
3708
3709 nfs4_free_createdata(data);
3710out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return status;
3712}
3713
Chuck Lever4f390c12006-08-22 20:06:22 -04003714static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003715 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716{
3717 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003718 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003720
3721 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003724 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3725 trace_nfs4_symlink(dir, &dentry->d_name, err);
3726 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 &exception);
3728 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003729
3730 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return err;
3732}
3733
3734static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003735 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003737 struct nfs4_createdata *data;
3738 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003740 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3741 if (data == NULL)
3742 goto out;
3743
David Quigley1775fd32013-05-22 12:50:42 -04003744 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003745 status = nfs4_do_create(dir, dentry, data);
3746
3747 nfs4_free_createdata(data);
3748out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return status;
3750}
3751
3752static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3753 struct iattr *sattr)
3754{
3755 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003756 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003758
David Quigleyaa9c2662013-05-22 12:50:44 -04003759 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3760
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003761 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003763 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3764 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3765 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 &exception);
3767 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003768 nfs4_label_release_security(label);
3769
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return err;
3771}
3772
3773static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003774 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
3776 struct inode *dir = dentry->d_inode;
3777 struct nfs4_readdir_arg args = {
3778 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003779 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 .pgbase = 0,
3781 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003782 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003783 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 };
3785 struct nfs4_readdir_res res;
3786 struct rpc_message msg = {
3787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3788 .rpc_argp = &args,
3789 .rpc_resp = &res,
3790 .rpc_cred = cred,
3791 };
3792 int status;
3793
Al Viro6de14722013-09-16 10:53:17 -04003794 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3795 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003796 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003797 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003799 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 -05003800 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003801 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003802 status += args.pgbase;
3803 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003804
3805 nfs_invalidate_atime(dir);
3806
Harvey Harrison3110ff82008-05-02 13:42:44 -07003807 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 return status;
3809}
3810
3811static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003812 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813{
3814 struct nfs4_exception exception = { };
3815 int err;
3816 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003817 err = _nfs4_proc_readdir(dentry, cred, cookie,
3818 pages, count, plus);
3819 trace_nfs4_readdir(dentry->d_inode, err);
3820 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 &exception);
3822 } while (exception.retry);
3823 return err;
3824}
3825
3826static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003827 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003829 struct nfs4_createdata *data;
3830 int mode = sattr->ia_mode;
3831 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003833 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3834 if (data == NULL)
3835 goto out;
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003840 data->arg.ftype = NF4BLK;
3841 data->arg.u.device.specdata1 = MAJOR(rdev);
3842 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
3844 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003845 data->arg.ftype = NF4CHR;
3846 data->arg.u.device.specdata1 = MAJOR(rdev);
3847 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003848 } else if (!S_ISSOCK(mode)) {
3849 status = -EINVAL;
3850 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
David Quigley1775fd32013-05-22 12:50:42 -04003852
David Quigleyaa9c2662013-05-22 12:50:44 -04003853 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003854 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003855out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003856 nfs4_free_createdata(data);
3857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 return status;
3859}
3860
3861static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3862 struct iattr *sattr, dev_t rdev)
3863{
3864 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003865 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003867
David Quigleyaa9c2662013-05-22 12:50:44 -04003868 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3869
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003870 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003872 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3873 trace_nfs4_mknod(dir, &dentry->d_name, err);
3874 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 &exception);
3876 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003877
3878 nfs4_label_release_security(label);
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return err;
3881}
3882
3883static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3884 struct nfs_fsstat *fsstat)
3885{
3886 struct nfs4_statfs_arg args = {
3887 .fh = fhandle,
3888 .bitmask = server->attr_bitmask,
3889 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003890 struct nfs4_statfs_res res = {
3891 .fsstat = fsstat,
3892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3895 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 };
3898
Trond Myklebust0e574af2005-10-27 22:12:38 -04003899 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003900 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901}
3902
3903static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3904{
3905 struct nfs4_exception exception = { };
3906 int err;
3907 do {
3908 err = nfs4_handle_exception(server,
3909 _nfs4_proc_statfs(server, fhandle, fsstat),
3910 &exception);
3911 } while (exception.retry);
3912 return err;
3913}
3914
3915static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3916 struct nfs_fsinfo *fsinfo)
3917{
3918 struct nfs4_fsinfo_arg args = {
3919 .fh = fhandle,
3920 .bitmask = server->attr_bitmask,
3921 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003922 struct nfs4_fsinfo_res res = {
3923 .fsinfo = fsinfo,
3924 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 struct rpc_message msg = {
3926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3927 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003928 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 };
3930
Bryan Schumaker7c513052011-03-24 17:12:24 +00003931 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
3934static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3935{
3936 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003937 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 int err;
3939
3940 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003941 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003942 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003943 if (err == 0) {
3944 struct nfs_client *clp = server->nfs_client;
3945
3946 spin_lock(&clp->cl_lock);
3947 clp->cl_lease_time = fsinfo->lease_time * HZ;
3948 clp->cl_last_renewal = now;
3949 spin_unlock(&clp->cl_lock);
3950 break;
3951 }
3952 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 } while (exception.retry);
3954 return err;
3955}
3956
3957static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3958{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003959 int error;
3960
Trond Myklebust0e574af2005-10-27 22:12:38 -04003961 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003962 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003963 if (error == 0) {
3964 /* block layout checks this! */
3965 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003966 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003967 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003968
3969 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
3972static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3973 struct nfs_pathconf *pathconf)
3974{
3975 struct nfs4_pathconf_arg args = {
3976 .fh = fhandle,
3977 .bitmask = server->attr_bitmask,
3978 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003979 struct nfs4_pathconf_res res = {
3980 .pathconf = pathconf,
3981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct rpc_message msg = {
3983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3984 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003985 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
3987
3988 /* None of the pathconf attributes are mandatory to implement */
3989 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3990 memset(pathconf, 0, sizeof(*pathconf));
3991 return 0;
3992 }
3993
Trond Myklebust0e574af2005-10-27 22:12:38 -04003994 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003995 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
3998static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3999 struct nfs_pathconf *pathconf)
4000{
4001 struct nfs4_exception exception = { };
4002 int err;
4003
4004 do {
4005 err = nfs4_handle_exception(server,
4006 _nfs4_proc_pathconf(server, fhandle, pathconf),
4007 &exception);
4008 } while (exception.retry);
4009 return err;
4010}
4011
Trond Myklebust5521abf2013-03-16 20:54:34 -04004012int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004013 const struct nfs_open_context *ctx,
4014 const struct nfs_lock_context *l_ctx,
4015 fmode_t fmode)
4016{
4017 const struct nfs_lockowner *lockowner = NULL;
4018
4019 if (l_ctx != NULL)
4020 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004021 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004022}
4023EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4024
Trond Myklebust5521abf2013-03-16 20:54:34 -04004025static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4026 const struct nfs_open_context *ctx,
4027 const struct nfs_lock_context *l_ctx,
4028 fmode_t fmode)
4029{
4030 nfs4_stateid current_stateid;
4031
4032 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
4033 return false;
4034 return nfs4_stateid_match(stateid, &current_stateid);
4035}
4036
4037static bool nfs4_error_stateid_expired(int err)
4038{
4039 switch (err) {
4040 case -NFS4ERR_DELEG_REVOKED:
4041 case -NFS4ERR_ADMIN_REVOKED:
4042 case -NFS4ERR_BAD_STATEID:
4043 case -NFS4ERR_STALE_STATEID:
4044 case -NFS4ERR_OLD_STATEID:
4045 case -NFS4ERR_OPENMODE:
4046 case -NFS4ERR_EXPIRED:
4047 return true;
4048 }
4049 return false;
4050}
4051
Benny Halevyd20581a2011-05-22 19:52:03 +03004052void __nfs4_read_done_cb(struct nfs_read_data *data)
4053{
Fred Isamancd841602012-04-20 14:47:44 -04004054 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004055}
4056
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004057static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
Fred Isamancd841602012-04-20 14:47:44 -04004059 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004061 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004062 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004063 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004064 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004066
Benny Halevyd20581a2011-05-22 19:52:03 +03004067 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004069 renew_lease(server, data->timestamp);
4070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071}
4072
Trond Myklebust5521abf2013-03-16 20:54:34 -04004073static bool nfs4_read_stateid_changed(struct rpc_task *task,
4074 struct nfs_readargs *args)
4075{
4076
4077 if (!nfs4_error_stateid_expired(task->tk_status) ||
4078 nfs4_stateid_is_current(&args->stateid,
4079 args->context,
4080 args->lock_context,
4081 FMODE_READ))
4082 return false;
4083 rpc_restart_call_prepare(task);
4084 return true;
4085}
4086
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004087static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4088{
4089
4090 dprintk("--> %s\n", __func__);
4091
4092 if (!nfs4_sequence_done(task, &data->res.seq_res))
4093 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004094 if (nfs4_read_stateid_changed(task, &data->args))
4095 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004096 return data->read_done_cb ? data->read_done_cb(task, data) :
4097 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004098}
4099
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004100static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004103 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004104 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004105 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
NeilBrownef1820f2013-09-04 17:04:49 +10004108static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004109{
Trond Myklebust9b206142013-03-17 15:52:00 -04004110 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004111 &data->args.seq_args,
4112 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004113 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004114 return 0;
4115 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4116 data->args.lock_context, FMODE_READ) == -EIO)
4117 return -EIO;
4118 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4119 return -EIO;
4120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121}
4122
Fred Isamanb029bc92011-03-03 15:13:42 +00004123static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Fred Isamancd841602012-04-20 14:47:44 -04004125 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004127 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004128 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004129 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004130 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004132 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004134 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004135 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Trond Myklebust5521abf2013-03-16 20:54:34 -04004139static bool nfs4_write_stateid_changed(struct rpc_task *task,
4140 struct nfs_writeargs *args)
4141{
4142
4143 if (!nfs4_error_stateid_expired(task->tk_status) ||
4144 nfs4_stateid_is_current(&args->stateid,
4145 args->context,
4146 args->lock_context,
4147 FMODE_WRITE))
4148 return false;
4149 rpc_restart_call_prepare(task);
4150 return true;
4151}
4152
Fred Isamanb029bc92011-03-03 15:13:42 +00004153static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4154{
4155 if (!nfs4_sequence_done(task, &data->res.seq_res))
4156 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004157 if (nfs4_write_stateid_changed(task, &data->args))
4158 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004159 return data->write_done_cb ? data->write_done_cb(task, data) :
4160 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004161}
4162
Trond Myklebust5a37f852012-04-28 14:55:16 -04004163static
4164bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004165{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004166 const struct nfs_pgio_header *hdr = data->header;
4167
4168 /* Don't request attributes for pNFS or O_DIRECT writes */
4169 if (data->ds_clp != NULL || hdr->dreq != NULL)
4170 return false;
4171 /* Otherwise, request attributes if and only if we don't hold
4172 * a delegation
4173 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004174 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004175}
Fred Isamana69aef12011-03-03 15:13:47 +00004176
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004177static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178{
Fred Isamancd841602012-04-20 14:47:44 -04004179 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004180
Trond Myklebust5a37f852012-04-28 14:55:16 -04004181 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004182 data->args.bitmask = NULL;
4183 data->res.fattr = NULL;
4184 } else
4185 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004186
Fred Isamanb029bc92011-03-03 15:13:42 +00004187 if (!data->write_done_cb)
4188 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004189 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 data->timestamp = jiffies;
4191
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004192 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004193 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
NeilBrownef1820f2013-09-04 17:04:49 +10004196static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004197{
Trond Myklebust9b206142013-03-17 15:52:00 -04004198 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004199 &data->args.seq_args,
4200 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004201 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004202 return 0;
4203 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4204 data->args.lock_context, FMODE_WRITE) == -EIO)
4205 return -EIO;
4206 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4207 return -EIO;
4208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Fred Isaman0b7c0152012-04-20 14:47:39 -04004211static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4212{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004213 nfs4_setup_sequence(NFS_SERVER(data->inode),
4214 &data->args.seq_args,
4215 &data->res.seq_res,
4216 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Fred Isaman0b7c0152012-04-20 14:47:39 -04004219static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004222
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004223 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004224 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004225 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004226 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229}
4230
Fred Isaman0b7c0152012-04-20 14:47:39 -04004231static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004232{
4233 if (!nfs4_sequence_done(task, &data->res.seq_res))
4234 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004235 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004236}
4237
Fred Isaman0b7c0152012-04-20 14:47:39 -04004238static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004240 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004241
Fred Isaman0b7c0152012-04-20 14:47:39 -04004242 if (data->commit_done_cb == NULL)
4243 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004244 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004245 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004246 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004249struct nfs4_renewdata {
4250 struct nfs_client *client;
4251 unsigned long timestamp;
4252};
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254/*
4255 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4256 * standalone procedure for queueing an asynchronous RENEW.
4257 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004258static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004259{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004260 struct nfs4_renewdata *data = calldata;
4261 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004262
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004263 if (atomic_read(&clp->cl_count) > 1)
4264 nfs4_schedule_state_renewal(clp);
4265 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004266 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004267}
4268
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004269static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004271 struct nfs4_renewdata *data = calldata;
4272 struct nfs_client *clp = data->client;
4273 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004275 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004276 switch (task->tk_status) {
4277 case 0:
4278 break;
4279 case -NFS4ERR_LEASE_MOVED:
4280 nfs4_schedule_lease_moved_recovery(clp);
4281 break;
4282 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004283 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004284 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4285 return;
4286 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004287 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004288 return;
4289 }
4290 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004292 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004295static const struct rpc_call_ops nfs4_renew_ops = {
4296 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004297 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004298};
4299
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004300static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
4302 struct rpc_message msg = {
4303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4304 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004305 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004307 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004309 if (renew_flags == 0)
4310 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004311 if (!atomic_inc_not_zero(&clp->cl_count))
4312 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004313 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004314 if (data == NULL)
4315 return -ENOMEM;
4316 data->client = clp;
4317 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004318 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004319 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320}
4321
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004322static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
4324 struct rpc_message msg = {
4325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4326 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004327 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
4329 unsigned long now = jiffies;
4330 int status;
4331
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004332 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (status < 0)
4334 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004335 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 return 0;
4337}
4338
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004339static inline int nfs4_server_supports_acls(struct nfs_server *server)
4340{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004341 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004342}
4343
Trond Myklebust21f498c2012-08-24 10:59:25 -04004344/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4345 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004346 * the stack.
4347 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004348#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004349
Neil Hormane9e3d722011-03-04 19:26:03 -05004350static int buf_to_pages_noslab(const void *buf, size_t buflen,
4351 struct page **pages, unsigned int *pgbase)
4352{
4353 struct page *newpage, **spages;
4354 int rc = 0;
4355 size_t len;
4356 spages = pages;
4357
4358 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004359 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004360 newpage = alloc_page(GFP_KERNEL);
4361
4362 if (newpage == NULL)
4363 goto unwind;
4364 memcpy(page_address(newpage), buf, len);
4365 buf += len;
4366 buflen -= len;
4367 *pages++ = newpage;
4368 rc++;
4369 } while (buflen != 0);
4370
4371 return rc;
4372
4373unwind:
4374 for(; rc > 0; rc--)
4375 __free_page(spages[rc-1]);
4376 return -ENOMEM;
4377}
4378
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004379struct nfs4_cached_acl {
4380 int cached;
4381 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004382 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004383};
4384
4385static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004386{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004387 struct nfs_inode *nfsi = NFS_I(inode);
4388
4389 spin_lock(&inode->i_lock);
4390 kfree(nfsi->nfs4_acl);
4391 nfsi->nfs4_acl = acl;
4392 spin_unlock(&inode->i_lock);
4393}
4394
4395static void nfs4_zap_acl_attr(struct inode *inode)
4396{
4397 nfs4_set_cached_acl(inode, NULL);
4398}
4399
4400static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4401{
4402 struct nfs_inode *nfsi = NFS_I(inode);
4403 struct nfs4_cached_acl *acl;
4404 int ret = -ENOENT;
4405
4406 spin_lock(&inode->i_lock);
4407 acl = nfsi->nfs4_acl;
4408 if (acl == NULL)
4409 goto out;
4410 if (buf == NULL) /* user is just asking for length */
4411 goto out_len;
4412 if (acl->cached == 0)
4413 goto out;
4414 ret = -ERANGE; /* see getxattr(2) man page */
4415 if (acl->len > buflen)
4416 goto out;
4417 memcpy(buf, acl->data, acl->len);
4418out_len:
4419 ret = acl->len;
4420out:
4421 spin_unlock(&inode->i_lock);
4422 return ret;
4423}
4424
Sachin Prabhu5794d212012-04-17 14:36:40 +01004425static 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 +00004426{
4427 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004428 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004429
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004430 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004431 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004432 if (acl == NULL)
4433 goto out;
4434 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004435 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004436 } else {
4437 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4438 if (acl == NULL)
4439 goto out;
4440 acl->cached = 0;
4441 }
4442 acl->len = acl_len;
4443out:
4444 nfs4_set_cached_acl(inode, acl);
4445}
4446
Andy Adamsonbf118a32011-12-07 11:55:27 -05004447/*
4448 * The getxattr API returns the required buffer length when called with a
4449 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4450 * the required buf. On a NULL buf, we send a page of data to the server
4451 * guessing that the ACL request can be serviced by a page. If so, we cache
4452 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4453 * the cache. If not so, we throw away the page, and cache the required
4454 * length. The next getxattr call will then produce another round trip to
4455 * the server, this time with the input buf of the required size.
4456 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004457static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004458{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004459 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004460 struct nfs_getaclargs args = {
4461 .fh = NFS_FH(inode),
4462 .acl_pages = pages,
4463 .acl_len = buflen,
4464 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004465 struct nfs_getaclres res = {
4466 .acl_len = buflen,
4467 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004468 struct rpc_message msg = {
4469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4470 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004471 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004472 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004473 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4474 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004475
Andy Adamsonbf118a32011-12-07 11:55:27 -05004476 /* As long as we're doing a round trip to the server anyway,
4477 * let's be prepared for a page of acl data. */
4478 if (npages == 0)
4479 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004480 if (npages > ARRAY_SIZE(pages))
4481 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004482
Andy Adamsonbf118a32011-12-07 11:55:27 -05004483 for (i = 0; i < npages; i++) {
4484 pages[i] = alloc_page(GFP_KERNEL);
4485 if (!pages[i])
4486 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004487 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004488
4489 /* for decoding across pages */
4490 res.acl_scratch = alloc_page(GFP_KERNEL);
4491 if (!res.acl_scratch)
4492 goto out_free;
4493
Andy Adamsonbf118a32011-12-07 11:55:27 -05004494 args.acl_len = npages * PAGE_SIZE;
4495 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004496
Peng Taode040be2012-01-10 22:42:47 +08004497 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004498 __func__, buf, buflen, npages, args.acl_len);
4499 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4500 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004501 if (ret)
4502 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004503
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004504 /* Handle the case where the passed-in buffer is too short */
4505 if (res.acl_flags & NFS4_ACL_TRUNC) {
4506 /* Did the user only issue a request for the acl length? */
4507 if (buf == NULL)
4508 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004509 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004510 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004511 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004512 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004513 if (buf) {
4514 if (res.acl_len > buflen) {
4515 ret = -ERANGE;
4516 goto out_free;
4517 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004518 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004519 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004520out_ok:
4521 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004522out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004523 for (i = 0; i < npages; i++)
4524 if (pages[i])
4525 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004526 if (res.acl_scratch)
4527 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004528 return ret;
4529}
4530
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004531static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4532{
4533 struct nfs4_exception exception = { };
4534 ssize_t ret;
4535 do {
4536 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004537 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004538 if (ret >= 0)
4539 break;
4540 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4541 } while (exception.retry);
4542 return ret;
4543}
4544
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004545static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4546{
4547 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004548 int ret;
4549
4550 if (!nfs4_server_supports_acls(server))
4551 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004552 ret = nfs_revalidate_inode(server, inode);
4553 if (ret < 0)
4554 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004555 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4556 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004557 ret = nfs4_read_cached_acl(inode, buf, buflen);
4558 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004559 /* -ENOENT is returned if there is no ACL or if there is an ACL
4560 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004561 return ret;
4562 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004563}
4564
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004565static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004566{
4567 struct nfs_server *server = NFS_SERVER(inode);
4568 struct page *pages[NFS4ACL_MAXPAGES];
4569 struct nfs_setaclargs arg = {
4570 .fh = NFS_FH(inode),
4571 .acl_pages = pages,
4572 .acl_len = buflen,
4573 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004574 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004575 struct rpc_message msg = {
4576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4577 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004578 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004579 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004580 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004581 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004582
4583 if (!nfs4_server_supports_acls(server))
4584 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004585 if (npages > ARRAY_SIZE(pages))
4586 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004587 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4588 if (i < 0)
4589 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004590 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004591 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004592
4593 /*
4594 * Free each page after tx, so the only ref left is
4595 * held by the network stack
4596 */
4597 for (; i > 0; i--)
4598 put_page(pages[i-1]);
4599
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004600 /*
4601 * Acl update can result in inode attribute update.
4602 * so mark the attribute cache invalid.
4603 */
4604 spin_lock(&inode->i_lock);
4605 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4606 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004607 nfs_access_zap_cache(inode);
4608 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004609 return ret;
4610}
4611
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004612static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4613{
4614 struct nfs4_exception exception = { };
4615 int err;
4616 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004617 err = __nfs4_proc_set_acl(inode, buf, buflen);
4618 trace_nfs4_set_acl(inode, err);
4619 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004620 &exception);
4621 } while (exception.retry);
4622 return err;
4623}
4624
David Quigleyaa9c2662013-05-22 12:50:44 -04004625#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4626static int _nfs4_get_security_label(struct inode *inode, void *buf,
4627 size_t buflen)
4628{
4629 struct nfs_server *server = NFS_SERVER(inode);
4630 struct nfs_fattr fattr;
4631 struct nfs4_label label = {0, 0, buflen, buf};
4632
4633 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004634 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004635 .fh = NFS_FH(inode),
4636 .bitmask = bitmask,
4637 };
4638 struct nfs4_getattr_res res = {
4639 .fattr = &fattr,
4640 .label = &label,
4641 .server = server,
4642 };
4643 struct rpc_message msg = {
4644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004645 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004646 .rpc_resp = &res,
4647 };
4648 int ret;
4649
4650 nfs_fattr_init(&fattr);
4651
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004652 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004653 if (ret)
4654 return ret;
4655 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4656 return -ENOENT;
4657 if (buflen < label.len)
4658 return -ERANGE;
4659 return 0;
4660}
4661
4662static int nfs4_get_security_label(struct inode *inode, void *buf,
4663 size_t buflen)
4664{
4665 struct nfs4_exception exception = { };
4666 int err;
4667
4668 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4669 return -EOPNOTSUPP;
4670
4671 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004672 err = _nfs4_get_security_label(inode, buf, buflen);
4673 trace_nfs4_get_security_label(inode, err);
4674 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004675 &exception);
4676 } while (exception.retry);
4677 return err;
4678}
4679
4680static int _nfs4_do_set_security_label(struct inode *inode,
4681 struct nfs4_label *ilabel,
4682 struct nfs_fattr *fattr,
4683 struct nfs4_label *olabel)
4684{
4685
4686 struct iattr sattr = {0};
4687 struct nfs_server *server = NFS_SERVER(inode);
4688 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004689 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004690 .fh = NFS_FH(inode),
4691 .iap = &sattr,
4692 .server = server,
4693 .bitmask = bitmask,
4694 .label = ilabel,
4695 };
4696 struct nfs_setattrres res = {
4697 .fattr = fattr,
4698 .label = olabel,
4699 .server = server,
4700 };
4701 struct rpc_message msg = {
4702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004703 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004704 .rpc_resp = &res,
4705 };
4706 int status;
4707
Jeff Layton12207f62013-11-01 10:49:32 -04004708 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004709
Jeff Layton12207f62013-11-01 10:49:32 -04004710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004711 if (status)
4712 dprintk("%s failed: %d\n", __func__, status);
4713
4714 return status;
4715}
4716
4717static int nfs4_do_set_security_label(struct inode *inode,
4718 struct nfs4_label *ilabel,
4719 struct nfs_fattr *fattr,
4720 struct nfs4_label *olabel)
4721{
4722 struct nfs4_exception exception = { };
4723 int err;
4724
4725 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004726 err = _nfs4_do_set_security_label(inode, ilabel,
4727 fattr, olabel);
4728 trace_nfs4_set_security_label(inode, err);
4729 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004730 &exception);
4731 } while (exception.retry);
4732 return err;
4733}
4734
4735static int
4736nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4737{
4738 struct nfs4_label ilabel, *olabel = NULL;
4739 struct nfs_fattr fattr;
4740 struct rpc_cred *cred;
4741 struct inode *inode = dentry->d_inode;
4742 int status;
4743
4744 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4745 return -EOPNOTSUPP;
4746
4747 nfs_fattr_init(&fattr);
4748
4749 ilabel.pi = 0;
4750 ilabel.lfs = 0;
4751 ilabel.label = (char *)buf;
4752 ilabel.len = buflen;
4753
4754 cred = rpc_lookup_cred();
4755 if (IS_ERR(cred))
4756 return PTR_ERR(cred);
4757
4758 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4759 if (IS_ERR(olabel)) {
4760 status = -PTR_ERR(olabel);
4761 goto out;
4762 }
4763
4764 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4765 if (status == 0)
4766 nfs_setsecurity(inode, &fattr, olabel);
4767
4768 nfs4_label_free(olabel);
4769out:
4770 put_rpccred(cred);
4771 return status;
4772}
4773#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4774
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004777nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004779 struct nfs_client *clp = server->nfs_client;
4780
4781 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 return 0;
4783 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004784 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004785 case -NFS4ERR_ADMIN_REVOKED:
4786 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004787 if (state == NULL)
4788 break;
4789 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004790 case -NFS4ERR_OPENMODE:
4791 if (state == NULL)
4792 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004793 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004794 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004795 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004796 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004797 if (state != NULL) {
4798 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004799 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004802 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004803 nfs4_schedule_lease_recovery(clp);
4804 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004805 case -NFS4ERR_MOVED:
4806 if (nfs4_schedule_migration_recovery(server) < 0)
4807 goto recovery_failed;
4808 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004809 case -NFS4ERR_LEASE_MOVED:
4810 nfs4_schedule_lease_moved_recovery(clp);
4811 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004812#if defined(CONFIG_NFS_V4_1)
4813 case -NFS4ERR_BADSESSION:
4814 case -NFS4ERR_BADSLOT:
4815 case -NFS4ERR_BAD_HIGH_SLOT:
4816 case -NFS4ERR_DEADSESSION:
4817 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4818 case -NFS4ERR_SEQ_FALSE_RETRY:
4819 case -NFS4ERR_SEQ_MISORDERED:
4820 dprintk("%s ERROR %d, Reset session\n", __func__,
4821 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004822 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004823 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004824#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004826 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004827 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004829 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004831 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 }
4833 task->tk_status = nfs4_map_errors(task->tk_status);
4834 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004835recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004836 task->tk_status = -EIO;
4837 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004838wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004839 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004840 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4841 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004842 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4843 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004844restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004845 task->tk_status = 0;
4846 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847}
4848
Chuck Leverf0920752012-05-21 22:45:41 -04004849static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4850 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004851{
4852 __be32 verf[2];
4853
Chuck Lever2c820d92012-05-21 22:45:33 -04004854 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4855 /* An impossible timestamp guarantees this value
4856 * will never match a generated boot time. */
4857 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004858 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004859 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004860 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004861 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4862 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004863 }
Chuck Levercd937102012-03-02 17:14:31 -05004864 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4865}
4866
Chuck Levere984a552012-09-14 17:24:21 -04004867static unsigned int
4868nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4869 char *buf, size_t len)
4870{
4871 unsigned int result;
4872
4873 rcu_read_lock();
4874 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4875 clp->cl_ipaddr,
4876 rpc_peeraddr2str(clp->cl_rpcclient,
4877 RPC_DISPLAY_ADDR),
4878 rpc_peeraddr2str(clp->cl_rpcclient,
4879 RPC_DISPLAY_PROTO));
4880 rcu_read_unlock();
4881 return result;
4882}
4883
4884static unsigned int
4885nfs4_init_uniform_client_string(const struct nfs_client *clp,
4886 char *buf, size_t len)
4887{
Trond Myklebust55b59292013-07-24 16:36:35 -04004888 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004889
4890 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004891 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4892 clp->rpc_ops->version,
4893 clp->cl_minorversion,
4894 nfs4_client_id_uniquifier,
4895 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004896 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4897 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004898 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004899}
4900
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004901/**
4902 * nfs4_proc_setclientid - Negotiate client ID
4903 * @clp: state data structure
4904 * @program: RPC program for NFSv4 callback service
4905 * @port: IP port number for NFS4 callback service
4906 * @cred: RPC credential to use for this call
4907 * @res: where to place the result
4908 *
4909 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4910 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004911int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4912 unsigned short port, struct rpc_cred *cred,
4913 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
4915 nfs4_verifier sc_verifier;
4916 struct nfs4_setclientid setclientid = {
4917 .sc_verifier = &sc_verifier,
4918 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004919 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 };
4921 struct rpc_message msg = {
4922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4923 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004924 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004925 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004927 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Chuck Leverde734832012-07-11 16:30:50 -04004929 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004930 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004931 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4932 setclientid.sc_name_len =
4933 nfs4_init_uniform_client_string(clp,
4934 setclientid.sc_name,
4935 sizeof(setclientid.sc_name));
4936 else
4937 setclientid.sc_name_len =
4938 nfs4_init_nonuniform_client_string(clp,
4939 setclientid.sc_name,
4940 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004941 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004942 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004943 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004944 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004945 rpc_peeraddr2str(clp->cl_rpcclient,
4946 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004947 rcu_read_unlock();
4948 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004949 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 clp->cl_ipaddr, port >> 8, port & 255);
4951
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004952 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4953 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4954 setclientid.sc_name_len, setclientid.sc_name);
4955 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004956 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004957 dprintk("NFS reply setclientid: %d\n", status);
4958 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959}
4960
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004961/**
4962 * nfs4_proc_setclientid_confirm - Confirm client ID
4963 * @clp: state data structure
4964 * @res: result of a previous SETCLIENTID
4965 * @cred: RPC credential to use for this call
4966 *
4967 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4968 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004969int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004970 struct nfs4_setclientid_res *arg,
4971 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004975 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004976 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 int status;
4979
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004980 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4981 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4982 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004983 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004984 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004985 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 return status;
4987}
4988
Trond Myklebustfe650402006-01-03 09:55:18 +01004989struct nfs4_delegreturndata {
4990 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004991 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004992 struct nfs_fh fh;
4993 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004994 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004995 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004996 int rpc_status;
4997};
4998
Trond Myklebustfe650402006-01-03 09:55:18 +01004999static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5000{
5001 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005002
Trond Myklebust14516c32010-07-31 14:29:06 -04005003 if (!nfs4_sequence_done(task, &data->res.seq_res))
5004 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005005
Trond Myklebustca8acf82013-08-13 10:36:56 -04005006 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005007 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005008 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005009 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005010 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005011 case -NFS4ERR_ADMIN_REVOKED:
5012 case -NFS4ERR_DELEG_REVOKED:
5013 case -NFS4ERR_BAD_STATEID:
5014 case -NFS4ERR_OLD_STATEID:
5015 case -NFS4ERR_STALE_STATEID:
5016 case -NFS4ERR_EXPIRED:
5017 task->tk_status = 0;
5018 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005019 default:
5020 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5021 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005022 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005023 return;
5024 }
5025 }
5026 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005027}
5028
5029static void nfs4_delegreturn_release(void *calldata)
5030{
Trond Myklebustfe650402006-01-03 09:55:18 +01005031 kfree(calldata);
5032}
5033
Andy Adamson938e1012009-04-01 09:22:28 -04005034static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5035{
5036 struct nfs4_delegreturndata *d_data;
5037
5038 d_data = (struct nfs4_delegreturndata *)data;
5039
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005040 nfs4_setup_sequence(d_data->res.server,
5041 &d_data->args.seq_args,
5042 &d_data->res.seq_res,
5043 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005044}
Andy Adamson938e1012009-04-01 09:22:28 -04005045
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005046static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005047 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005048 .rpc_call_done = nfs4_delegreturn_done,
5049 .rpc_release = nfs4_delegreturn_release,
5050};
5051
Trond Myklebuste6f81072008-01-24 18:14:34 -05005052static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005053{
5054 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005055 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005056 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005057 struct rpc_message msg = {
5058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5059 .rpc_cred = cred,
5060 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005061 struct rpc_task_setup task_setup_data = {
5062 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005063 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005064 .callback_ops = &nfs4_delegreturn_ops,
5065 .flags = RPC_TASK_ASYNC,
5066 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005067 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005068
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005069 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005070 if (data == NULL)
5071 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005072 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005073 data->args.fhandle = &data->fh;
5074 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005075 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005076 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005077 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005078 data->res.fattr = &data->fattr;
5079 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005080 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005081 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005082 data->rpc_status = 0;
5083
Trond Myklebustc970aa82007-07-14 15:39:59 -04005084 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005085 msg.rpc_argp = &data->args;
5086 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005087 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005088 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005089 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005090 if (!issync)
5091 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005092 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005093 if (status != 0)
5094 goto out;
5095 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005096 if (status == 0)
5097 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5098 else
5099 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005100out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005101 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005102 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
Trond Myklebuste6f81072008-01-24 18:14:34 -05005105int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
5107 struct nfs_server *server = NFS_SERVER(inode);
5108 struct nfs4_exception exception = { };
5109 int err;
5110 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005111 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005112 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 switch (err) {
5114 case -NFS4ERR_STALE_STATEID:
5115 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 case 0:
5117 return 0;
5118 }
5119 err = nfs4_handle_exception(server, err, &exception);
5120 } while (exception.retry);
5121 return err;
5122}
5123
5124#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5125#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5126
5127/*
5128 * sleep, with exponential backoff, and retry the LOCK operation.
5129 */
5130static unsigned long
5131nfs4_set_lock_task_retry(unsigned long timeout)
5132{
Colin Cross416ad3c2013-05-06 23:50:06 +00005133 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 timeout <<= 1;
5135 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5136 return NFS4_LOCK_MAXTIMEOUT;
5137 return timeout;
5138}
5139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5141{
5142 struct inode *inode = state->inode;
5143 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005144 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005145 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005147 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005149 struct nfs_lockt_res res = {
5150 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 };
5152 struct rpc_message msg = {
5153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5154 .rpc_argp = &arg,
5155 .rpc_resp = &res,
5156 .rpc_cred = state->owner->so_cred,
5157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 struct nfs4_lock_state *lsp;
5159 int status;
5160
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005161 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005162 status = nfs4_set_lock_state(state, request);
5163 if (status != 0)
5164 goto out;
5165 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005166 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005167 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005168 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005169 switch (status) {
5170 case 0:
5171 request->fl_type = F_UNLCK;
5172 break;
5173 case -NFS4ERR_DENIED:
5174 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005176 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005177 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005178out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 return status;
5180}
5181
5182static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5183{
5184 struct nfs4_exception exception = { };
5185 int err;
5186
5187 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005188 err = _nfs4_proc_getlk(state, cmd, request);
5189 trace_nfs4_get_lock(request, state, cmd, err);
5190 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 &exception);
5192 } while (exception.retry);
5193 return err;
5194}
5195
5196static int do_vfs_lock(struct file *file, struct file_lock *fl)
5197{
5198 int res = 0;
5199 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5200 case FL_POSIX:
5201 res = posix_lock_file_wait(file, fl);
5202 break;
5203 case FL_FLOCK:
5204 res = flock_lock_file_wait(file, fl);
5205 break;
5206 default:
5207 BUG();
5208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 return res;
5210}
5211
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005212struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005213 struct nfs_locku_args arg;
5214 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005215 struct nfs4_lock_state *lsp;
5216 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005217 struct file_lock fl;
5218 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005219 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005220};
5221
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005222static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5223 struct nfs_open_context *ctx,
5224 struct nfs4_lock_state *lsp,
5225 struct nfs_seqid *seqid)
5226{
5227 struct nfs4_unlockdata *p;
5228 struct inode *inode = lsp->ls_state->inode;
5229
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005230 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005231 if (p == NULL)
5232 return NULL;
5233 p->arg.fh = NFS_FH(inode);
5234 p->arg.fl = &p->fl;
5235 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005236 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005237 p->arg.stateid = &lsp->ls_stateid;
5238 p->lsp = lsp;
5239 atomic_inc(&lsp->ls_count);
5240 /* Ensure we don't close file until we're done freeing locks! */
5241 p->ctx = get_nfs_open_context(ctx);
5242 memcpy(&p->fl, fl, sizeof(p->fl));
5243 p->server = NFS_SERVER(inode);
5244 return p;
5245}
5246
Trond Myklebust06f814a2006-01-03 09:55:07 +01005247static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005248{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005249 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005250 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005251 nfs4_put_lock_state(calldata->lsp);
5252 put_nfs_open_context(calldata->ctx);
5253 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005254}
5255
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005256static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005257{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005258 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005259
Trond Myklebust14516c32010-07-31 14:29:06 -04005260 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5261 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005262 switch (task->tk_status) {
5263 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005264 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5265 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005266 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005267 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005268 case -NFS4ERR_BAD_STATEID:
5269 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005270 case -NFS4ERR_STALE_STATEID:
5271 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005272 break;
5273 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005274 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005275 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005276 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005277 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005278}
5279
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005280static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005281{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005282 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005284 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005285 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005286 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005287 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005288 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005289 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005290 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005291 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005292 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005293 &calldata->res.seq_res,
5294 task) != 0)
5295 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005296 return;
5297out_no_action:
5298 task->tk_action = NULL;
5299out_wait:
5300 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301}
5302
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005303static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005304 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005305 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005306 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005307};
5308
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5310 struct nfs_open_context *ctx,
5311 struct nfs4_lock_state *lsp,
5312 struct nfs_seqid *seqid)
5313{
5314 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005315 struct rpc_message msg = {
5316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5317 .rpc_cred = ctx->cred,
5318 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005319 struct rpc_task_setup task_setup_data = {
5320 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005321 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005322 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005323 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005324 .flags = RPC_TASK_ASYNC,
5325 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005326
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005327 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5328 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5329
Frank Filz137d6ac2007-07-09 15:32:29 -07005330 /* Ensure this is an unlock - when canceling a lock, the
5331 * canceled lock is passed in, and it won't be an unlock.
5332 */
5333 fl->fl_type = F_UNLCK;
5334
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005335 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5336 if (data == NULL) {
5337 nfs_free_seqid(seqid);
5338 return ERR_PTR(-ENOMEM);
5339 }
5340
Chuck Levera9c92d62013-08-09 12:48:18 -04005341 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005342 msg.rpc_argp = &data->arg;
5343 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005344 task_setup_data.callback_data = data;
5345 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005346}
5347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5349{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005350 struct inode *inode = state->inode;
5351 struct nfs4_state_owner *sp = state->owner;
5352 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005353 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005354 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005355 struct rpc_task *task;
5356 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005357 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Trond Myklebust9b073572006-06-29 16:38:34 -04005359 status = nfs4_set_lock_state(state, request);
5360 /* Unlock _before_ we do the RPC call */
5361 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005362 /* Exclude nfs_delegation_claim_locks() */
5363 mutex_lock(&sp->so_delegreturn_mutex);
5364 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005365 down_read(&nfsi->rwsem);
5366 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5367 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005368 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005369 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005370 }
5371 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005372 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005373 if (status != 0)
5374 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005375 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005376 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005377 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5378 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005379 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005380 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005381 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005382 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005383 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 status = PTR_ERR(task);
5385 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005386 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005387 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005388 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005389out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005390 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005391 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005392 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393}
5394
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005395struct nfs4_lockdata {
5396 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005397 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398 struct nfs4_lock_state *lsp;
5399 struct nfs_open_context *ctx;
5400 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005401 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005402 int rpc_status;
5403 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005404 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405};
5406
5407static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005408 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5409 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005410{
5411 struct nfs4_lockdata *p;
5412 struct inode *inode = lsp->ls_state->inode;
5413 struct nfs_server *server = NFS_SERVER(inode);
5414
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005415 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416 if (p == NULL)
5417 return NULL;
5418
5419 p->arg.fh = NFS_FH(inode);
5420 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005421 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005422 if (p->arg.open_seqid == NULL)
5423 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005424 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005425 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005426 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005428 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005429 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005430 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005431 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005432 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005433 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005434 atomic_inc(&lsp->ls_count);
5435 p->ctx = get_nfs_open_context(ctx);
5436 memcpy(&p->fl, fl, sizeof(p->fl));
5437 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005438out_free_seqid:
5439 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005440out_free:
5441 kfree(p);
5442 return NULL;
5443}
5444
5445static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5446{
5447 struct nfs4_lockdata *data = calldata;
5448 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Harvey Harrison3110ff82008-05-02 13:42:44 -07005450 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005451 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005452 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005453 /* Do we need to do an open_to_lock_owner? */
5454 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005455 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005456 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005457 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005458 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005459 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005460 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005461 } else
5462 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005463 if (!nfs4_valid_open_stateid(state)) {
5464 data->rpc_status = -EBADF;
5465 task->tk_action = NULL;
5466 goto out_release_open_seqid;
5467 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005468 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005469 if (nfs4_setup_sequence(data->server,
5470 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005471 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005472 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005473 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005474out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005475 nfs_release_seqid(data->arg.open_seqid);
5476out_release_lock_seqid:
5477 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005478out_wait:
5479 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005480 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005481}
5482
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005483static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5484{
5485 struct nfs4_lockdata *data = calldata;
5486
Harvey Harrison3110ff82008-05-02 13:42:44 -07005487 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005488
Trond Myklebust14516c32010-07-31 14:29:06 -04005489 if (!nfs4_sequence_done(task, &data->res.seq_res))
5490 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005491
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005492 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005493 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005494 if (data->rpc_status == 0)
5495 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5496 else
5497 goto out;
5498 }
5499 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005500 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005501 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005502 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005503 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005504out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005505 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005506}
5507
5508static void nfs4_lock_release(void *calldata)
5509{
5510 struct nfs4_lockdata *data = calldata;
5511
Harvey Harrison3110ff82008-05-02 13:42:44 -07005512 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005513 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005514 if (data->cancelled != 0) {
5515 struct rpc_task *task;
5516 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5517 data->arg.lock_seqid);
5518 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005519 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005520 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005521 } else
5522 nfs_free_seqid(data->arg.lock_seqid);
5523 nfs4_put_lock_state(data->lsp);
5524 put_nfs_open_context(data->ctx);
5525 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005526 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005527}
5528
5529static const struct rpc_call_ops nfs4_lock_ops = {
5530 .rpc_call_prepare = nfs4_lock_prepare,
5531 .rpc_call_done = nfs4_lock_done,
5532 .rpc_release = nfs4_lock_release,
5533};
5534
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005535static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5536{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005537 switch (error) {
5538 case -NFS4ERR_ADMIN_REVOKED:
5539 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005540 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005541 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005542 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005543 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005544 break;
5545 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005546 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005547 case -NFS4ERR_EXPIRED:
5548 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005549 };
5550}
5551
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005552static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005553{
5554 struct nfs4_lockdata *data;
5555 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005556 struct rpc_message msg = {
5557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5558 .rpc_cred = state->owner->so_cred,
5559 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005560 struct rpc_task_setup task_setup_data = {
5561 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005562 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005563 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005564 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005565 .flags = RPC_TASK_ASYNC,
5566 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005567 int ret;
5568
Harvey Harrison3110ff82008-05-02 13:42:44 -07005569 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005570 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005571 fl->fl_u.nfs4_fl.owner,
5572 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005573 if (data == NULL)
5574 return -ENOMEM;
5575 if (IS_SETLKW(cmd))
5576 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005577 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005578 msg.rpc_argp = &data->arg;
5579 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005580 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005581 if (recovery_type > NFS_LOCK_NEW) {
5582 if (recovery_type == NFS_LOCK_RECLAIM)
5583 data->arg.reclaim = NFS_LOCK_RECLAIM;
5584 nfs4_set_sequence_privileged(&data->arg.seq_args);
5585 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005586 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005587 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005588 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005589 ret = nfs4_wait_for_completion_rpc_task(task);
5590 if (ret == 0) {
5591 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005592 if (ret)
5593 nfs4_handle_setlk_error(data->server, data->lsp,
5594 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005595 } else
5596 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005597 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005598 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600}
5601
5602static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5603{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005604 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005605 struct nfs4_exception exception = {
5606 .inode = state->inode,
5607 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005608 int err;
5609
5610 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005611 /* Cache the lock if possible... */
5612 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5613 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005614 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005615 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005616 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005617 break;
5618 nfs4_handle_exception(server, err, &exception);
5619 } while (exception.retry);
5620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
5623static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5624{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005625 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005626 struct nfs4_exception exception = {
5627 .inode = state->inode,
5628 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005629 int err;
5630
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005631 err = nfs4_set_lock_state(state, request);
5632 if (err != 0)
5633 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005634 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005635 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5636 return 0;
5637 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005638 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005639 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5640 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005641 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005642 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005643 switch (err) {
5644 default:
5645 goto out;
5646 case -NFS4ERR_GRACE:
5647 case -NFS4ERR_DELAY:
5648 nfs4_handle_exception(server, err, &exception);
5649 err = 0;
5650 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005651 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005652out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005653 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654}
5655
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005656#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005657/**
5658 * nfs41_check_expired_locks - possibly free a lock stateid
5659 *
5660 * @state: NFSv4 state for an inode
5661 *
5662 * Returns NFS_OK if recovery for this stateid is now finished.
5663 * Otherwise a negative NFS4ERR value is returned.
5664 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005665static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005666{
Chuck Levereb64cf92012-07-11 16:30:05 -04005667 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005668 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005669 struct nfs_server *server = NFS_SERVER(state->inode);
5670
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005671 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005672 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005673 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5674
5675 status = nfs41_test_stateid(server,
5676 &lsp->ls_stateid,
5677 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005678 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005679 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005680 /* Free the stateid unless the server
5681 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005682 if (status != -NFS4ERR_BAD_STATEID)
5683 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005684 &lsp->ls_stateid,
5685 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005686 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005687 ret = status;
5688 }
5689 }
5690 };
5691
5692 return ret;
5693}
5694
5695static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5696{
5697 int status = NFS_OK;
5698
5699 if (test_bit(LK_STATE_IN_USE, &state->flags))
5700 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005701 if (status != NFS_OK)
5702 status = nfs4_lock_expired(state, request);
5703 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005704}
5705#endif
5706
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5708{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005709 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005710 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005711 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005712 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005713 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005715 if ((fl_flags & FL_POSIX) &&
5716 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5717 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005718 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005719 status = nfs4_set_lock_state(state, request);
5720 if (status != 0)
5721 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005722 request->fl_flags |= FL_ACCESS;
5723 status = do_vfs_lock(request->fl_file, request);
5724 if (status < 0)
5725 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005726 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005727 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005728 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005729 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005730 request->fl_flags = fl_flags & ~FL_SLEEP;
5731 status = do_vfs_lock(request->fl_file, request);
5732 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005733 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005734 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5735 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005736 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005737 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005738 goto out;
5739 down_read(&nfsi->rwsem);
5740 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5741 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005742 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005743 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005744 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005745 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005746 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005747 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5748 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005749out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005750 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005751out:
5752 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 return status;
5754}
5755
5756static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5757{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005758 struct nfs4_exception exception = {
5759 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005760 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005761 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 int err;
5763
5764 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005765 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005766 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005767 if (err == -NFS4ERR_DENIED)
5768 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005770 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 } while (exception.retry);
5772 return err;
5773}
5774
5775static int
5776nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5777{
5778 struct nfs_open_context *ctx;
5779 struct nfs4_state *state;
5780 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5781 int status;
5782
5783 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005784 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 state = ctx->state;
5786
5787 if (request->fl_start < 0 || request->fl_end < 0)
5788 return -EINVAL;
5789
Trond Myklebustd9531262009-07-21 19:22:38 -04005790 if (IS_GETLK(cmd)) {
5791 if (state != NULL)
5792 return nfs4_proc_getlk(state, F_GETLK, request);
5793 return 0;
5794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
5796 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5797 return -EINVAL;
5798
Trond Myklebustd9531262009-07-21 19:22:38 -04005799 if (request->fl_type == F_UNLCK) {
5800 if (state != NULL)
5801 return nfs4_proc_unlck(state, cmd, request);
5802 return 0;
5803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804
Trond Myklebustd9531262009-07-21 19:22:38 -04005805 if (state == NULL)
5806 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005807 /*
5808 * Don't rely on the VFS having checked the file open mode,
5809 * since it won't do this for flock() locks.
5810 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005811 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005812 case F_RDLCK:
5813 if (!(filp->f_mode & FMODE_READ))
5814 return -EBADF;
5815 break;
5816 case F_WRLCK:
5817 if (!(filp->f_mode & FMODE_WRITE))
5818 return -EBADF;
5819 }
5820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 do {
5822 status = nfs4_proc_setlk(state, cmd, request);
5823 if ((status != -EAGAIN) || IS_SETLK(cmd))
5824 break;
5825 timeout = nfs4_set_lock_task_retry(timeout);
5826 status = -ERESTARTSYS;
5827 if (signalled())
5828 break;
5829 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 return status;
5831}
5832
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005833int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005834{
5835 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005836 int err;
5837
5838 err = nfs4_set_lock_state(state, fl);
5839 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005840 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005841 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005842 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005843}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005844
Trond Myklebustcf470c32012-03-07 13:49:12 -05005845struct nfs_release_lockowner_data {
5846 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005847 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005848 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005849 struct nfs4_sequence_args seq_args;
5850 struct nfs4_sequence_res seq_res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005851 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005852};
5853
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005854static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5855{
5856 struct nfs_release_lockowner_data *data = calldata;
5857 nfs40_setup_sequence(data->server,
5858 &data->seq_args, &data->seq_res, task);
Chuck Lever60ea6812013-10-17 14:13:47 -04005859 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005860}
5861
5862static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5863{
5864 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005865 struct nfs_server *server = data->server;
5866
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005867 nfs40_sequence_done(task, &data->seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005868
5869 switch (task->tk_status) {
5870 case 0:
5871 renew_lease(server, data->timestamp);
5872 break;
5873 case -NFS4ERR_STALE_CLIENTID:
5874 case -NFS4ERR_EXPIRED:
5875 case -NFS4ERR_LEASE_MOVED:
5876 case -NFS4ERR_DELAY:
5877 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5878 rpc_restart_call_prepare(task);
5879 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005880}
5881
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005882static void nfs4_release_lockowner_release(void *calldata)
5883{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005884 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005885 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005886 kfree(calldata);
5887}
5888
Trond Myklebust17280172012-03-11 13:11:00 -04005889static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005890 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5891 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005892 .rpc_release = nfs4_release_lockowner_release,
5893};
5894
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005895static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005896{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005897 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005898 struct rpc_message msg = {
5899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5900 };
5901
5902 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005903 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005904
Trond Myklebustcf470c32012-03-07 13:49:12 -05005905 data = kmalloc(sizeof(*data), GFP_NOFS);
5906 if (!data)
5907 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005908 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005909 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005910 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005911 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5912 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5913 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005914
Trond Myklebustcf470c32012-03-07 13:49:12 -05005915 msg.rpc_argp = &data->args;
5916 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5917 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005918}
5919
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005920#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5921
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005922static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5923 const void *buf, size_t buflen,
5924 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005925{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005926 if (strcmp(key, "") != 0)
5927 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005928
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005929 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005930}
5931
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005932static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5933 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005934{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005935 if (strcmp(key, "") != 0)
5936 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005937
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005938 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005939}
5940
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005941static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5942 size_t list_len, const char *name,
5943 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005944{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005945 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005946
J. Bruce Fields096455a2006-03-20 23:23:42 -05005947 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5948 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005949
5950 if (list && len <= list_len)
5951 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005952 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005953}
5954
David Quigleyc9bccef2013-05-22 12:50:45 -04005955#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5956static inline int nfs4_server_supports_labels(struct nfs_server *server)
5957{
5958 return server->caps & NFS_CAP_SECURITY_LABEL;
5959}
5960
5961static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5962 const void *buf, size_t buflen,
5963 int flags, int type)
5964{
5965 if (security_ismaclabel(key))
5966 return nfs4_set_security_label(dentry, buf, buflen);
5967
5968 return -EOPNOTSUPP;
5969}
5970
5971static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5972 void *buf, size_t buflen, int type)
5973{
5974 if (security_ismaclabel(key))
5975 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5976 return -EOPNOTSUPP;
5977}
5978
5979static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5980 size_t list_len, const char *name,
5981 size_t name_len, int type)
5982{
5983 size_t len = 0;
5984
5985 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5986 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5987 if (list && len <= list_len)
5988 security_inode_listsecurity(dentry->d_inode, list, len);
5989 }
5990 return len;
5991}
5992
5993static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5994 .prefix = XATTR_SECURITY_PREFIX,
5995 .list = nfs4_xattr_list_nfs4_label,
5996 .get = nfs4_xattr_get_nfs4_label,
5997 .set = nfs4_xattr_set_nfs4_label,
5998};
5999#endif
6000
6001
Andy Adamson533eb462011-06-13 18:25:56 -04006002/*
6003 * nfs_fhget will use either the mounted_on_fileid or the fileid
6004 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006005static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6006{
Andy Adamson533eb462011-06-13 18:25:56 -04006007 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6008 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6009 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006010 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006011 return;
6012
6013 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006014 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006015 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6016 fattr->nlink = 2;
6017}
6018
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006019static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6020 const struct qstr *name,
6021 struct nfs4_fs_locations *fs_locations,
6022 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006023{
6024 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006025 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006026 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006027 };
6028 struct nfs4_fs_locations_arg args = {
6029 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006030 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006031 .page = page,
6032 .bitmask = bitmask,
6033 };
Benny Halevy22958462009-04-01 09:22:02 -04006034 struct nfs4_fs_locations_res res = {
6035 .fs_locations = fs_locations,
6036 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006037 struct rpc_message msg = {
6038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6039 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006040 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006041 };
6042 int status;
6043
Harvey Harrison3110ff82008-05-02 13:42:44 -07006044 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006045
6046 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6047 * is not supported */
6048 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6049 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6050 else
6051 bitmask[0] |= FATTR4_WORD0_FILEID;
6052
Trond Myklebustc228fd32007-01-13 02:28:11 -05006053 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006054 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006055 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006056 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006057 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006058 return status;
6059}
6060
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006061int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6062 const struct qstr *name,
6063 struct nfs4_fs_locations *fs_locations,
6064 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006065{
6066 struct nfs4_exception exception = { };
6067 int err;
6068 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006069 err = _nfs4_proc_fs_locations(client, dir, name,
6070 fs_locations, page);
6071 trace_nfs4_get_fs_locations(dir, name, err);
6072 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006073 &exception);
6074 } while (exception.retry);
6075 return err;
6076}
6077
Chuck Leverb03d7352013-10-17 14:12:50 -04006078/*
6079 * This operation also signals the server that this client is
6080 * performing migration recovery. The server can stop returning
6081 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6082 * appended to this compound to identify the client ID which is
6083 * performing recovery.
6084 */
6085static int _nfs40_proc_get_locations(struct inode *inode,
6086 struct nfs4_fs_locations *locations,
6087 struct page *page, struct rpc_cred *cred)
6088{
6089 struct nfs_server *server = NFS_SERVER(inode);
6090 struct rpc_clnt *clnt = server->client;
6091 u32 bitmask[2] = {
6092 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6093 };
6094 struct nfs4_fs_locations_arg args = {
6095 .clientid = server->nfs_client->cl_clientid,
6096 .fh = NFS_FH(inode),
6097 .page = page,
6098 .bitmask = bitmask,
6099 .migration = 1, /* skip LOOKUP */
6100 .renew = 1, /* append RENEW */
6101 };
6102 struct nfs4_fs_locations_res res = {
6103 .fs_locations = locations,
6104 .migration = 1,
6105 .renew = 1,
6106 };
6107 struct rpc_message msg = {
6108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6109 .rpc_argp = &args,
6110 .rpc_resp = &res,
6111 .rpc_cred = cred,
6112 };
6113 unsigned long now = jiffies;
6114 int status;
6115
6116 nfs_fattr_init(&locations->fattr);
6117 locations->server = server;
6118 locations->nlocations = 0;
6119
6120 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6121 nfs4_set_sequence_privileged(&args.seq_args);
6122 status = nfs4_call_sync_sequence(clnt, server, &msg,
6123 &args.seq_args, &res.seq_res);
6124 if (status)
6125 return status;
6126
6127 renew_lease(server, now);
6128 return 0;
6129}
6130
6131#ifdef CONFIG_NFS_V4_1
6132
6133/*
6134 * This operation also signals the server that this client is
6135 * performing migration recovery. The server can stop asserting
6136 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6137 * performing this operation is identified in the SEQUENCE
6138 * operation in this compound.
6139 *
6140 * When the client supports GETATTR(fs_locations_info), it can
6141 * be plumbed in here.
6142 */
6143static int _nfs41_proc_get_locations(struct inode *inode,
6144 struct nfs4_fs_locations *locations,
6145 struct page *page, struct rpc_cred *cred)
6146{
6147 struct nfs_server *server = NFS_SERVER(inode);
6148 struct rpc_clnt *clnt = server->client;
6149 u32 bitmask[2] = {
6150 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6151 };
6152 struct nfs4_fs_locations_arg args = {
6153 .fh = NFS_FH(inode),
6154 .page = page,
6155 .bitmask = bitmask,
6156 .migration = 1, /* skip LOOKUP */
6157 };
6158 struct nfs4_fs_locations_res res = {
6159 .fs_locations = locations,
6160 .migration = 1,
6161 };
6162 struct rpc_message msg = {
6163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6164 .rpc_argp = &args,
6165 .rpc_resp = &res,
6166 .rpc_cred = cred,
6167 };
6168 int status;
6169
6170 nfs_fattr_init(&locations->fattr);
6171 locations->server = server;
6172 locations->nlocations = 0;
6173
6174 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6175 nfs4_set_sequence_privileged(&args.seq_args);
6176 status = nfs4_call_sync_sequence(clnt, server, &msg,
6177 &args.seq_args, &res.seq_res);
6178 if (status == NFS4_OK &&
6179 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6180 status = -NFS4ERR_LEASE_MOVED;
6181 return status;
6182}
6183
6184#endif /* CONFIG_NFS_V4_1 */
6185
6186/**
6187 * nfs4_proc_get_locations - discover locations for a migrated FSID
6188 * @inode: inode on FSID that is migrating
6189 * @locations: result of query
6190 * @page: buffer
6191 * @cred: credential to use for this operation
6192 *
6193 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6194 * operation failed, or a negative errno if a local error occurred.
6195 *
6196 * On success, "locations" is filled in, but if the server has
6197 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6198 * asserted.
6199 *
6200 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6201 * from this client that require migration recovery.
6202 */
6203int nfs4_proc_get_locations(struct inode *inode,
6204 struct nfs4_fs_locations *locations,
6205 struct page *page, struct rpc_cred *cred)
6206{
6207 struct nfs_server *server = NFS_SERVER(inode);
6208 struct nfs_client *clp = server->nfs_client;
6209 const struct nfs4_mig_recovery_ops *ops =
6210 clp->cl_mvops->mig_recovery_ops;
6211 struct nfs4_exception exception = { };
6212 int status;
6213
6214 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6215 (unsigned long long)server->fsid.major,
6216 (unsigned long long)server->fsid.minor,
6217 clp->cl_hostname);
6218 nfs_display_fhandle(NFS_FH(inode), __func__);
6219
6220 do {
6221 status = ops->get_locations(inode, locations, page, cred);
6222 if (status != -NFS4ERR_DELAY)
6223 break;
6224 nfs4_handle_exception(server, status, &exception);
6225 } while (exception.retry);
6226 return status;
6227}
6228
Chuck Lever44c99932013-10-17 14:13:30 -04006229/*
6230 * This operation also signals the server that this client is
6231 * performing "lease moved" recovery. The server can stop
6232 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6233 * is appended to this compound to identify the client ID which is
6234 * performing recovery.
6235 */
6236static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6237{
6238 struct nfs_server *server = NFS_SERVER(inode);
6239 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6240 struct rpc_clnt *clnt = server->client;
6241 struct nfs4_fsid_present_arg args = {
6242 .fh = NFS_FH(inode),
6243 .clientid = clp->cl_clientid,
6244 .renew = 1, /* append RENEW */
6245 };
6246 struct nfs4_fsid_present_res res = {
6247 .renew = 1,
6248 };
6249 struct rpc_message msg = {
6250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6251 .rpc_argp = &args,
6252 .rpc_resp = &res,
6253 .rpc_cred = cred,
6254 };
6255 unsigned long now = jiffies;
6256 int status;
6257
6258 res.fh = nfs_alloc_fhandle();
6259 if (res.fh == NULL)
6260 return -ENOMEM;
6261
6262 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6263 nfs4_set_sequence_privileged(&args.seq_args);
6264 status = nfs4_call_sync_sequence(clnt, server, &msg,
6265 &args.seq_args, &res.seq_res);
6266 nfs_free_fhandle(res.fh);
6267 if (status)
6268 return status;
6269
6270 do_renew_lease(clp, now);
6271 return 0;
6272}
6273
6274#ifdef CONFIG_NFS_V4_1
6275
6276/*
6277 * This operation also signals the server that this client is
6278 * performing "lease moved" recovery. The server can stop asserting
6279 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6280 * this operation is identified in the SEQUENCE operation in this
6281 * compound.
6282 */
6283static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6284{
6285 struct nfs_server *server = NFS_SERVER(inode);
6286 struct rpc_clnt *clnt = server->client;
6287 struct nfs4_fsid_present_arg args = {
6288 .fh = NFS_FH(inode),
6289 };
6290 struct nfs4_fsid_present_res res = {
6291 };
6292 struct rpc_message msg = {
6293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6294 .rpc_argp = &args,
6295 .rpc_resp = &res,
6296 .rpc_cred = cred,
6297 };
6298 int status;
6299
6300 res.fh = nfs_alloc_fhandle();
6301 if (res.fh == NULL)
6302 return -ENOMEM;
6303
6304 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6305 nfs4_set_sequence_privileged(&args.seq_args);
6306 status = nfs4_call_sync_sequence(clnt, server, &msg,
6307 &args.seq_args, &res.seq_res);
6308 nfs_free_fhandle(res.fh);
6309 if (status == NFS4_OK &&
6310 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6311 status = -NFS4ERR_LEASE_MOVED;
6312 return status;
6313}
6314
6315#endif /* CONFIG_NFS_V4_1 */
6316
6317/**
6318 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6319 * @inode: inode on FSID to check
6320 * @cred: credential to use for this operation
6321 *
6322 * Server indicates whether the FSID is present, moved, or not
6323 * recognized. This operation is necessary to clear a LEASE_MOVED
6324 * condition for this client ID.
6325 *
6326 * Returns NFS4_OK if the FSID is present on this server,
6327 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6328 * NFS4ERR code if some error occurred on the server, or a
6329 * negative errno if a local failure occurred.
6330 */
6331int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6332{
6333 struct nfs_server *server = NFS_SERVER(inode);
6334 struct nfs_client *clp = server->nfs_client;
6335 const struct nfs4_mig_recovery_ops *ops =
6336 clp->cl_mvops->mig_recovery_ops;
6337 struct nfs4_exception exception = { };
6338 int status;
6339
6340 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6341 (unsigned long long)server->fsid.major,
6342 (unsigned long long)server->fsid.minor,
6343 clp->cl_hostname);
6344 nfs_display_fhandle(NFS_FH(inode), __func__);
6345
6346 do {
6347 status = ops->fsid_present(inode, cred);
6348 if (status != -NFS4ERR_DELAY)
6349 break;
6350 nfs4_handle_exception(server, status, &exception);
6351 } while (exception.retry);
6352 return status;
6353}
6354
Andy Adamson5ec16a82013-08-08 10:57:55 -04006355/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006356 * If 'use_integrity' is true and the state managment nfs_client
6357 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6358 * and the machine credential as per RFC3530bis and RFC5661 Security
6359 * Considerations sections. Otherwise, just use the user cred with the
6360 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006361 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006362static 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 +00006363{
6364 int status;
6365 struct nfs4_secinfo_arg args = {
6366 .dir_fh = NFS_FH(dir),
6367 .name = name,
6368 };
6369 struct nfs4_secinfo_res res = {
6370 .flavors = flavors,
6371 };
6372 struct rpc_message msg = {
6373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6374 .rpc_argp = &args,
6375 .rpc_resp = &res,
6376 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006377 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006378 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006379
6380 if (use_integrity) {
6381 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006382 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6383 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006384 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006385
6386 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006387
6388 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6389 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6390
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006391 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6392 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006393 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006394
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006395 if (cred)
6396 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006397
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006398 return status;
6399}
6400
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006401int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6402 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006403{
6404 struct nfs4_exception exception = { };
6405 int err;
6406 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006407 err = -NFS4ERR_WRONGSEC;
6408
6409 /* try to use integrity protection with machine cred */
6410 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6411 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6412
6413 /*
6414 * if unable to use integrity protection, or SECINFO with
6415 * integrity protection returns NFS4ERR_WRONGSEC (which is
6416 * disallowed by spec, but exists in deployed servers) use
6417 * the current filesystem's rpc_client and the user cred.
6418 */
6419 if (err == -NFS4ERR_WRONGSEC)
6420 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6421
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006422 trace_nfs4_secinfo(dir, name, err);
6423 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006424 &exception);
6425 } while (exception.retry);
6426 return err;
6427}
6428
Andy Adamson557134a2009-04-01 09:21:53 -04006429#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006430/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006431 * Check the exchange flags returned by the server for invalid flags, having
6432 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6433 * DS flags set.
6434 */
6435static int nfs4_check_cl_exchange_flags(u32 flags)
6436{
6437 if (flags & ~EXCHGID4_FLAG_MASK_R)
6438 goto out_inval;
6439 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6440 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6441 goto out_inval;
6442 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6443 goto out_inval;
6444 return NFS_OK;
6445out_inval:
6446 return -NFS4ERR_INVAL;
6447}
6448
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006449static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006450nfs41_same_server_scope(struct nfs41_server_scope *a,
6451 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006452{
6453 if (a->server_scope_sz == b->server_scope_sz &&
6454 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6455 return true;
6456
6457 return false;
6458}
6459
Andy Adamson357f54d2010-12-14 10:11:57 -05006460/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006461 * nfs4_proc_bind_conn_to_session()
6462 *
6463 * The 4.1 client currently uses the same TCP connection for the
6464 * fore and backchannel.
6465 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006466int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006467{
6468 int status;
6469 struct nfs41_bind_conn_to_session_res res;
6470 struct rpc_message msg = {
6471 .rpc_proc =
6472 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6473 .rpc_argp = clp,
6474 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006475 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006476 };
6477
6478 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006479
6480 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6481 if (unlikely(res.session == NULL)) {
6482 status = -ENOMEM;
6483 goto out;
6484 }
6485
6486 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006487 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006488 if (status == 0) {
6489 if (memcmp(res.session->sess_id.data,
6490 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6491 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6492 status = -EIO;
6493 goto out_session;
6494 }
6495 if (res.dir != NFS4_CDFS4_BOTH) {
6496 dprintk("NFS: %s: Unexpected direction from server\n",
6497 __func__);
6498 status = -EIO;
6499 goto out_session;
6500 }
6501 if (res.use_conn_in_rdma_mode) {
6502 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6503 __func__);
6504 status = -EIO;
6505 goto out_session;
6506 }
6507 }
6508out_session:
6509 kfree(res.session);
6510out:
6511 dprintk("<-- %s status= %d\n", __func__, status);
6512 return status;
6513}
6514
6515/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006516 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6517 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006518 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006519static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6520 .how = SP4_MACH_CRED,
6521 .enforce.u.words = {
6522 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6523 1 << (OP_EXCHANGE_ID - 32) |
6524 1 << (OP_CREATE_SESSION - 32) |
6525 1 << (OP_DESTROY_SESSION - 32) |
6526 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006527 },
6528 .allow.u.words = {
6529 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006530 1 << (OP_LOCKU) |
6531 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006532 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006533 1 << (OP_SECINFO_NO_NAME - 32) |
6534 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006535 1 << (OP_FREE_STATEID - 32) |
6536 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006537 }
6538};
6539
6540/*
6541 * Select the state protection mode for client `clp' given the server results
6542 * from exchange_id in `sp'.
6543 *
6544 * Returns 0 on success, negative errno otherwise.
6545 */
6546static int nfs4_sp4_select_mode(struct nfs_client *clp,
6547 struct nfs41_state_protection *sp)
6548{
6549 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6550 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6551 1 << (OP_EXCHANGE_ID - 32) |
6552 1 << (OP_CREATE_SESSION - 32) |
6553 1 << (OP_DESTROY_SESSION - 32) |
6554 1 << (OP_DESTROY_CLIENTID - 32)
6555 };
6556 unsigned int i;
6557
6558 if (sp->how == SP4_MACH_CRED) {
6559 /* Print state protect result */
6560 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6561 for (i = 0; i <= LAST_NFS4_OP; i++) {
6562 if (test_bit(i, sp->enforce.u.longs))
6563 dfprintk(MOUNT, " enforce op %d\n", i);
6564 if (test_bit(i, sp->allow.u.longs))
6565 dfprintk(MOUNT, " allow op %d\n", i);
6566 }
6567
6568 /* make sure nothing is on enforce list that isn't supported */
6569 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6570 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6571 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6572 return -EINVAL;
6573 }
6574 }
6575
6576 /*
6577 * Minimal mode - state operations are allowed to use machine
6578 * credential. Note this already happens by default, so the
6579 * client doesn't have to do anything more than the negotiation.
6580 *
6581 * NOTE: we don't care if EXCHANGE_ID is in the list -
6582 * we're already using the machine cred for exchange_id
6583 * and will never use a different cred.
6584 */
6585 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6586 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6587 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6588 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6589 dfprintk(MOUNT, "sp4_mach_cred:\n");
6590 dfprintk(MOUNT, " minimal mode enabled\n");
6591 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6592 } else {
6593 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6594 return -EINVAL;
6595 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006596
6597 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6598 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6599 dfprintk(MOUNT, " cleanup mode enabled\n");
6600 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6601 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006602
6603 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6604 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6605 dfprintk(MOUNT, " secinfo mode enabled\n");
6606 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6607 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006608
6609 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6610 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " stateid mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6613 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006614
6615 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " write mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6618 }
6619
6620 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6621 dfprintk(MOUNT, " commit mode enabled\n");
6622 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6623 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006624 }
6625
6626 return 0;
6627}
6628
6629/*
6630 * _nfs4_proc_exchange_id()
6631 *
6632 * Wrapper for EXCHANGE_ID operation.
6633 */
6634static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6635 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006636{
6637 nfs4_verifier verifier;
6638 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006639 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006640 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006641#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006642 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006643 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6644 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6645#else
6646 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6647 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6648#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006649 };
6650 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006651 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006652 };
6653 int status;
6654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6656 .rpc_argp = &args,
6657 .rpc_resp = &res,
6658 .rpc_cred = cred,
6659 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006660
Chuck Leverf0920752012-05-21 22:45:41 -04006661 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006662 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6663 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006664 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6665 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6666 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006667
Chuck Leveracdeb692012-05-21 22:46:16 -04006668 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006669 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006670 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006671 status = -ENOMEM;
6672 goto out;
6673 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006674
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006675 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006676 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006677 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006678 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006679 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006680 }
6681
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006682 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006683 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006684 status = -ENOMEM;
6685 goto out_server_scope;
6686 }
6687
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006688 switch (sp4_how) {
6689 case SP4_NONE:
6690 args.state_protect.how = SP4_NONE;
6691 break;
6692
6693 case SP4_MACH_CRED:
6694 args.state_protect = nfs4_sp4_mach_cred_request;
6695 break;
6696
6697 default:
6698 /* unsupported! */
6699 WARN_ON_ONCE(1);
6700 status = -EINVAL;
6701 goto out_server_scope;
6702 }
6703
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006704 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006705 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006706 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006707 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006708
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006709 if (status == 0)
6710 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6711
Chuck Lever177313f2012-05-21 22:44:58 -04006712 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006713 clp->cl_clientid = res.clientid;
6714 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6715 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6716 clp->cl_seqid = res.seqid;
6717
Chuck Leveracdeb692012-05-21 22:46:16 -04006718 kfree(clp->cl_serverowner);
6719 clp->cl_serverowner = res.server_owner;
6720 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006721
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006722 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006723 kfree(clp->cl_implid);
6724 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006725
Chuck Lever177313f2012-05-21 22:44:58 -04006726 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006727 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006728 res.server_scope)) {
6729 dprintk("%s: server_scope mismatch detected\n",
6730 __func__);
6731 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006732 kfree(clp->cl_serverscope);
6733 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006734 }
6735
Chuck Lever177313f2012-05-21 22:44:58 -04006736 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006737 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006738 goto out;
6739 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006740 } else
6741 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006742
Chuck Leveracdeb692012-05-21 22:46:16 -04006743out_server_owner:
6744 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006745out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006746 kfree(res.server_scope);
6747out:
Chuck Lever177313f2012-05-21 22:44:58 -04006748 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006749 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006750 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006751 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006752 clp->cl_implid->date.seconds,
6753 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006754 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006755 return status;
6756}
6757
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006758/*
6759 * nfs4_proc_exchange_id()
6760 *
6761 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6762 *
6763 * Since the clientid has expired, all compounds using sessions
6764 * associated with the stale clientid will be returning
6765 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6766 * be in some phase of session reset.
6767 *
6768 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6769 */
6770int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6771{
6772 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6773 int status;
6774
6775 /* try SP4_MACH_CRED if krb5i/p */
6776 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6777 authflavor == RPC_AUTH_GSS_KRB5P) {
6778 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6779 if (!status)
6780 return 0;
6781 }
6782
6783 /* try SP4_NONE */
6784 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6785}
6786
Trond Myklebust66245532012-05-25 17:18:09 -04006787static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6788 struct rpc_cred *cred)
6789{
6790 struct rpc_message msg = {
6791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6792 .rpc_argp = clp,
6793 .rpc_cred = cred,
6794 };
6795 int status;
6796
6797 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006798 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006799 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006800 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006801 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6802 return status;
6803}
6804
6805static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6806 struct rpc_cred *cred)
6807{
6808 unsigned int loop;
6809 int ret;
6810
6811 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6812 ret = _nfs4_proc_destroy_clientid(clp, cred);
6813 switch (ret) {
6814 case -NFS4ERR_DELAY:
6815 case -NFS4ERR_CLIENTID_BUSY:
6816 ssleep(1);
6817 break;
6818 default:
6819 return ret;
6820 }
6821 }
6822 return 0;
6823}
6824
6825int nfs4_destroy_clientid(struct nfs_client *clp)
6826{
6827 struct rpc_cred *cred;
6828 int ret = 0;
6829
6830 if (clp->cl_mvops->minor_version < 1)
6831 goto out;
6832 if (clp->cl_exchange_flags == 0)
6833 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006834 if (clp->cl_preserve_clid)
6835 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006836 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006837 ret = nfs4_proc_destroy_clientid(clp, cred);
6838 if (cred)
6839 put_rpccred(cred);
6840 switch (ret) {
6841 case 0:
6842 case -NFS4ERR_STALE_CLIENTID:
6843 clp->cl_exchange_flags = 0;
6844 }
6845out:
6846 return ret;
6847}
6848
Andy Adamson2050f0c2009-04-01 09:22:30 -04006849struct nfs4_get_lease_time_data {
6850 struct nfs4_get_lease_time_args *args;
6851 struct nfs4_get_lease_time_res *res;
6852 struct nfs_client *clp;
6853};
6854
6855static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6856 void *calldata)
6857{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006858 struct nfs4_get_lease_time_data *data =
6859 (struct nfs4_get_lease_time_data *)calldata;
6860
6861 dprintk("--> %s\n", __func__);
6862 /* just setup sequence, do not trigger session recovery
6863 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006864 nfs41_setup_sequence(data->clp->cl_session,
6865 &data->args->la_seq_args,
6866 &data->res->lr_seq_res,
6867 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006868 dprintk("<-- %s\n", __func__);
6869}
6870
6871/*
6872 * Called from nfs4_state_manager thread for session setup, so don't recover
6873 * from sequence operation or clientid errors.
6874 */
6875static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6876{
6877 struct nfs4_get_lease_time_data *data =
6878 (struct nfs4_get_lease_time_data *)calldata;
6879
6880 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006881 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6882 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006883 switch (task->tk_status) {
6884 case -NFS4ERR_DELAY:
6885 case -NFS4ERR_GRACE:
6886 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6887 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6888 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006889 /* fall through */
6890 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006891 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006892 return;
6893 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006894 dprintk("<-- %s\n", __func__);
6895}
6896
Trond Myklebust17280172012-03-11 13:11:00 -04006897static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006898 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6899 .rpc_call_done = nfs4_get_lease_time_done,
6900};
6901
6902int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6903{
6904 struct rpc_task *task;
6905 struct nfs4_get_lease_time_args args;
6906 struct nfs4_get_lease_time_res res = {
6907 .lr_fsinfo = fsinfo,
6908 };
6909 struct nfs4_get_lease_time_data data = {
6910 .args = &args,
6911 .res = &res,
6912 .clp = clp,
6913 };
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6916 .rpc_argp = &args,
6917 .rpc_resp = &res,
6918 };
6919 struct rpc_task_setup task_setup = {
6920 .rpc_client = clp->cl_rpcclient,
6921 .rpc_message = &msg,
6922 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006923 .callback_data = &data,
6924 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006925 };
6926 int status;
6927
Chuck Levera9c92d62013-08-09 12:48:18 -04006928 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006929 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006930 dprintk("--> %s\n", __func__);
6931 task = rpc_run_task(&task_setup);
6932
6933 if (IS_ERR(task))
6934 status = PTR_ERR(task);
6935 else {
6936 status = task->tk_status;
6937 rpc_put_task(task);
6938 }
6939 dprintk("<-- %s return %d\n", __func__, status);
6940
6941 return status;
6942}
6943
Andy Adamsonfc931582009-04-01 09:22:31 -04006944/*
6945 * Initialize the values to be used by the client in CREATE_SESSION
6946 * If nfs4_init_session set the fore channel request and response sizes,
6947 * use them.
6948 *
6949 * Set the back channel max_resp_sz_cached to zero to force the client to
6950 * always set csa_cachethis to FALSE because the current implementation
6951 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6952 */
6953static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6954{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006955 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006956
Andy Adamson18aad3d2013-06-26 12:21:49 -04006957 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6958 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6959
Andy Adamsonfc931582009-04-01 09:22:31 -04006960 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006961 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6962 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006963 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006964 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006965
6966 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006967 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006968 __func__,
6969 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006970 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006971
6972 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006973 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6974 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6975 args->bc_attrs.max_resp_sz_cached = 0;
6976 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6977 args->bc_attrs.max_reqs = 1;
6978
6979 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6980 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6981 __func__,
6982 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6983 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6984 args->bc_attrs.max_reqs);
6985}
6986
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006987static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006988{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006989 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6990 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6991
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006992 if (rcvd->max_resp_sz > sent->max_resp_sz)
6993 return -EINVAL;
6994 /*
6995 * Our requested max_ops is the minimum we need; we're not
6996 * prepared to break up compounds into smaller pieces than that.
6997 * So, no point even trying to continue if the server won't
6998 * cooperate:
6999 */
7000 if (rcvd->max_ops < sent->max_ops)
7001 return -EINVAL;
7002 if (rcvd->max_reqs == 0)
7003 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007004 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7005 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007006 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007007}
7008
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007009static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7010{
7011 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7012 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007013
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007014 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7015 return -EINVAL;
7016 if (rcvd->max_resp_sz < sent->max_resp_sz)
7017 return -EINVAL;
7018 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7019 return -EINVAL;
7020 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007021 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007022 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007023 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007024 return -EINVAL;
7025 return 0;
7026}
Andy Adamson8d353012009-04-01 09:22:32 -04007027
Andy Adamson8d353012009-04-01 09:22:32 -04007028static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7029 struct nfs4_session *session)
7030{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007031 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007032
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007033 ret = nfs4_verify_fore_channel_attrs(args, session);
7034 if (ret)
7035 return ret;
7036 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007037}
7038
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007039static int _nfs4_proc_create_session(struct nfs_client *clp,
7040 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007041{
7042 struct nfs4_session *session = clp->cl_session;
7043 struct nfs41_create_session_args args = {
7044 .client = clp,
7045 .cb_program = NFS4_CALLBACK,
7046 };
7047 struct nfs41_create_session_res res = {
7048 .client = clp,
7049 };
7050 struct rpc_message msg = {
7051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7052 .rpc_argp = &args,
7053 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007054 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007055 };
7056 int status;
7057
7058 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007059 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007060
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007061 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007062 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007063
Trond Myklebust43095d32012-11-20 11:13:12 -05007064 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007065 /* Verify the session's negotiated channel_attrs values */
7066 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007067 /* Increment the clientid slot sequence id */
7068 clp->cl_seqid++;
7069 }
7070
7071 return status;
7072}
7073
7074/*
7075 * Issues a CREATE_SESSION operation to the server.
7076 * It is the responsibility of the caller to verify the session is
7077 * expired before calling this routine.
7078 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007079int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007080{
7081 int status;
7082 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007083 struct nfs4_session *session = clp->cl_session;
7084
7085 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7086
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007087 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007088 if (status)
7089 goto out;
7090
Andy Adamsonaacd5532011-11-09 13:58:21 -05007091 /* Init or reset the session slot tables */
7092 status = nfs4_setup_session_slot_tables(session);
7093 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007094 if (status)
7095 goto out;
7096
7097 ptr = (unsigned *)&session->sess_id.data[0];
7098 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7099 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007100out:
7101 dprintk("<-- %s\n", __func__);
7102 return status;
7103}
7104
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007105/*
7106 * Issue the over-the-wire RPC DESTROY_SESSION.
7107 * The caller must serialize access to this routine.
7108 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007109int nfs4_proc_destroy_session(struct nfs4_session *session,
7110 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007111{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007112 struct rpc_message msg = {
7113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7114 .rpc_argp = session,
7115 .rpc_cred = cred,
7116 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007117 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007118
7119 dprintk("--> nfs4_proc_destroy_session\n");
7120
7121 /* session is still being setup */
7122 if (session->clp->cl_cons_state != NFS_CS_READY)
7123 return status;
7124
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007125 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007126 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007127
7128 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007129 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007130 "Session has been destroyed regardless...\n", status);
7131
7132 dprintk("<-- nfs4_proc_destroy_session\n");
7133 return status;
7134}
7135
Trond Myklebust7b38c362012-05-23 13:23:31 -04007136/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007137 * Renew the cl_session lease.
7138 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007139struct nfs4_sequence_data {
7140 struct nfs_client *clp;
7141 struct nfs4_sequence_args args;
7142 struct nfs4_sequence_res res;
7143};
7144
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007145static void nfs41_sequence_release(void *data)
7146{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007147 struct nfs4_sequence_data *calldata = data;
7148 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007149
Alexandros Batsakis71358402010-02-05 03:45:05 -08007150 if (atomic_read(&clp->cl_count) > 1)
7151 nfs4_schedule_state_renewal(clp);
7152 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007153 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007154}
7155
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007156static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7157{
7158 switch(task->tk_status) {
7159 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007160 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7161 return -EAGAIN;
7162 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007163 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007164 }
7165 return 0;
7166}
7167
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007168static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007169{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007170 struct nfs4_sequence_data *calldata = data;
7171 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007172
Trond Myklebust14516c32010-07-31 14:29:06 -04007173 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7174 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007175
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007176 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007177 if (task->tk_status < 0) {
7178 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007179 if (atomic_read(&clp->cl_count) == 1)
7180 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007181
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007182 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7183 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007184 return;
7185 }
7186 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007187 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007188out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007189 dprintk("<-- %s\n", __func__);
7190}
7191
7192static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7193{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007194 struct nfs4_sequence_data *calldata = data;
7195 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007196 struct nfs4_sequence_args *args;
7197 struct nfs4_sequence_res *res;
7198
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007199 args = task->tk_msg.rpc_argp;
7200 res = task->tk_msg.rpc_resp;
7201
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007202 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007203}
7204
7205static const struct rpc_call_ops nfs41_sequence_ops = {
7206 .rpc_call_done = nfs41_sequence_call_done,
7207 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007208 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007209};
7210
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007211static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7212 struct rpc_cred *cred,
7213 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007214{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007215 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007216 struct rpc_message msg = {
7217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7218 .rpc_cred = cred,
7219 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007220 struct rpc_task_setup task_setup_data = {
7221 .rpc_client = clp->cl_rpcclient,
7222 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007223 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007224 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007225 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007226
Alexandros Batsakis71358402010-02-05 03:45:05 -08007227 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007228 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007229 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007230 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007231 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007232 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007233 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007234 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007235 if (is_privileged)
7236 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007237 msg.rpc_argp = &calldata->args;
7238 msg.rpc_resp = &calldata->res;
7239 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007240 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007241
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007242 return rpc_run_task(&task_setup_data);
7243}
7244
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007245static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007246{
7247 struct rpc_task *task;
7248 int ret = 0;
7249
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007250 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7251 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007252 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007253 if (IS_ERR(task))
7254 ret = PTR_ERR(task);
7255 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007256 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007257 dprintk("<-- %s status=%d\n", __func__, ret);
7258 return ret;
7259}
7260
7261static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7262{
7263 struct rpc_task *task;
7264 int ret;
7265
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007266 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007267 if (IS_ERR(task)) {
7268 ret = PTR_ERR(task);
7269 goto out;
7270 }
7271 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007272 if (!ret) {
7273 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7274
7275 if (task->tk_status == 0)
7276 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007277 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007278 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007279 rpc_put_task(task);
7280out:
7281 dprintk("<-- %s status=%d\n", __func__, ret);
7282 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007283}
7284
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007285struct nfs4_reclaim_complete_data {
7286 struct nfs_client *clp;
7287 struct nfs41_reclaim_complete_args arg;
7288 struct nfs41_reclaim_complete_res res;
7289};
7290
7291static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7292{
7293 struct nfs4_reclaim_complete_data *calldata = data;
7294
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007295 nfs41_setup_sequence(calldata->clp->cl_session,
7296 &calldata->arg.seq_args,
7297 &calldata->res.seq_res,
7298 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007299}
7300
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007301static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7302{
7303 switch(task->tk_status) {
7304 case 0:
7305 case -NFS4ERR_COMPLETE_ALREADY:
7306 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7307 break;
7308 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007309 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007310 /* fall through */
7311 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007312 return -EAGAIN;
7313 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007314 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007315 }
7316 return 0;
7317}
7318
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007319static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7320{
7321 struct nfs4_reclaim_complete_data *calldata = data;
7322 struct nfs_client *clp = calldata->clp;
7323 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7324
7325 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007326 if (!nfs41_sequence_done(task, res))
7327 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007328
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007329 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007330 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7331 rpc_restart_call_prepare(task);
7332 return;
7333 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007334 dprintk("<-- %s\n", __func__);
7335}
7336
7337static void nfs4_free_reclaim_complete_data(void *data)
7338{
7339 struct nfs4_reclaim_complete_data *calldata = data;
7340
7341 kfree(calldata);
7342}
7343
7344static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7345 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7346 .rpc_call_done = nfs4_reclaim_complete_done,
7347 .rpc_release = nfs4_free_reclaim_complete_data,
7348};
7349
7350/*
7351 * Issue a global reclaim complete.
7352 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007353static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7354 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007355{
7356 struct nfs4_reclaim_complete_data *calldata;
7357 struct rpc_task *task;
7358 struct rpc_message msg = {
7359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007360 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007361 };
7362 struct rpc_task_setup task_setup_data = {
7363 .rpc_client = clp->cl_rpcclient,
7364 .rpc_message = &msg,
7365 .callback_ops = &nfs4_reclaim_complete_call_ops,
7366 .flags = RPC_TASK_ASYNC,
7367 };
7368 int status = -ENOMEM;
7369
7370 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007371 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007372 if (calldata == NULL)
7373 goto out;
7374 calldata->clp = clp;
7375 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007376
Chuck Levera9c92d62013-08-09 12:48:18 -04007377 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007378 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007379 msg.rpc_argp = &calldata->arg;
7380 msg.rpc_resp = &calldata->res;
7381 task_setup_data.callback_data = calldata;
7382 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007383 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007384 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007385 goto out;
7386 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007387 status = nfs4_wait_for_completion_rpc_task(task);
7388 if (status == 0)
7389 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007390 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007391 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007392out:
7393 dprintk("<-- %s status=%d\n", __func__, status);
7394 return status;
7395}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007396
7397static void
7398nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7399{
7400 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007401 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007402 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007403
7404 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007405 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7406 * right now covering the LAYOUTGET we are about to send.
7407 * However, that is not so catastrophic, and there seems
7408 * to be no way to prevent it completely.
7409 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007410 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007411 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007412 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007413 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7414 NFS_I(lgp->args.inode)->layout,
7415 lgp->args.ctx->state)) {
7416 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007417 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007418}
7419
7420static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7421{
7422 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007423 struct inode *inode = lgp->args.inode;
7424 struct nfs_server *server = NFS_SERVER(inode);
7425 struct pnfs_layout_hdr *lo;
7426 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007427 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007428
Boaz Harroshed7e5422014-01-22 20:34:54 +02007429 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007430
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007431 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007432 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007433
7434 switch (task->tk_status) {
7435 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007436 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007437 /*
7438 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7439 * (or clients) writing to the same RAID stripe
7440 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007441 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007442 /*
7443 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7444 * existing layout before getting a new one).
7445 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007446 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007447 timeo = rpc_get_timeout(task->tk_client);
7448 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007449 now = jiffies;
7450 if (time_after(giveup, now)) {
7451 unsigned long delay;
7452
7453 /* Delay for:
7454 * - Not less then NFS4_POLL_RETRY_MIN.
7455 * - One last time a jiffie before we give up
7456 * - exponential backoff (time_now minus start_attempt)
7457 */
7458 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7459 min((giveup - now - 1),
7460 now - lgp->args.timestamp));
7461
7462 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7463 __func__, delay);
7464 rpc_delay(task, delay);
7465 task->tk_status = 0;
7466 rpc_restart_call_prepare(task);
7467 goto out; /* Do not call nfs4_async_handle_error() */
7468 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007469 break;
7470 case -NFS4ERR_EXPIRED:
7471 case -NFS4ERR_BAD_STATEID:
7472 spin_lock(&inode->i_lock);
7473 lo = NFS_I(inode)->layout;
7474 if (!lo || list_empty(&lo->plh_segs)) {
7475 spin_unlock(&inode->i_lock);
7476 /* If the open stateid was bad, then recover it. */
7477 state = lgp->args.ctx->state;
7478 } else {
7479 LIST_HEAD(head);
7480
7481 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7482 spin_unlock(&inode->i_lock);
7483 /* Mark the bad layout state as invalid, then
7484 * retry using the open stateid. */
7485 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007486 }
7487 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007488 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7489 rpc_restart_call_prepare(task);
7490out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007491 dprintk("<-- %s\n", __func__);
7492}
7493
Idan Kedar85541162012-08-02 11:47:10 +03007494static size_t max_response_pages(struct nfs_server *server)
7495{
7496 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7497 return nfs_page_array_len(0, max_resp_sz);
7498}
7499
7500static void nfs4_free_pages(struct page **pages, size_t size)
7501{
7502 int i;
7503
7504 if (!pages)
7505 return;
7506
7507 for (i = 0; i < size; i++) {
7508 if (!pages[i])
7509 break;
7510 __free_page(pages[i]);
7511 }
7512 kfree(pages);
7513}
7514
7515static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7516{
7517 struct page **pages;
7518 int i;
7519
7520 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7521 if (!pages) {
7522 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7523 return NULL;
7524 }
7525
7526 for (i = 0; i < size; i++) {
7527 pages[i] = alloc_page(gfp_flags);
7528 if (!pages[i]) {
7529 dprintk("%s: failed to allocate page\n", __func__);
7530 nfs4_free_pages(pages, size);
7531 return NULL;
7532 }
7533 }
7534
7535 return pages;
7536}
7537
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007538static void nfs4_layoutget_release(void *calldata)
7539{
7540 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007541 struct inode *inode = lgp->args.inode;
7542 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007543 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007544
7545 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007546 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007547 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007548 put_nfs_open_context(lgp->args.ctx);
7549 kfree(calldata);
7550 dprintk("<-- %s\n", __func__);
7551}
7552
7553static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7554 .rpc_call_prepare = nfs4_layoutget_prepare,
7555 .rpc_call_done = nfs4_layoutget_done,
7556 .rpc_release = nfs4_layoutget_release,
7557};
7558
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007559struct pnfs_layout_segment *
7560nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007561{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007562 struct inode *inode = lgp->args.inode;
7563 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007564 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007565 struct rpc_task *task;
7566 struct rpc_message msg = {
7567 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7568 .rpc_argp = &lgp->args,
7569 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007570 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007571 };
7572 struct rpc_task_setup task_setup_data = {
7573 .rpc_client = server->client,
7574 .rpc_message = &msg,
7575 .callback_ops = &nfs4_layoutget_call_ops,
7576 .callback_data = lgp,
7577 .flags = RPC_TASK_ASYNC,
7578 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007579 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007580 int status = 0;
7581
7582 dprintk("--> %s\n", __func__);
7583
Idan Kedar85541162012-08-02 11:47:10 +03007584 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7585 if (!lgp->args.layout.pages) {
7586 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007587 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007588 }
7589 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007590 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007591
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007592 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007593 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007594 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007595
7596 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7597 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7598
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007599 task = rpc_run_task(&task_setup_data);
7600 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007601 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007602 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007603 if (status == 0)
7604 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007605 trace_nfs4_layoutget(lgp->args.ctx,
7606 &lgp->args.range,
7607 &lgp->res.range,
7608 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007609 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7610 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007611 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007612 rpc_put_task(task);
7613 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007614 if (status)
7615 return ERR_PTR(status);
7616 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007617}
7618
Benny Halevycbe82602011-05-22 19:52:37 +03007619static void
7620nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7621{
7622 struct nfs4_layoutreturn *lrp = calldata;
7623
7624 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007625 nfs41_setup_sequence(lrp->clp->cl_session,
7626 &lrp->args.seq_args,
7627 &lrp->res.seq_res,
7628 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007629}
7630
7631static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7632{
7633 struct nfs4_layoutreturn *lrp = calldata;
7634 struct nfs_server *server;
7635
7636 dprintk("--> %s\n", __func__);
7637
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007638 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007639 return;
7640
7641 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007642 switch (task->tk_status) {
7643 default:
7644 task->tk_status = 0;
7645 case 0:
7646 break;
7647 case -NFS4ERR_DELAY:
7648 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7649 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007650 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007651 return;
7652 }
Benny Halevycbe82602011-05-22 19:52:37 +03007653 dprintk("<-- %s\n", __func__);
7654}
7655
7656static void nfs4_layoutreturn_release(void *calldata)
7657{
7658 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007659 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007660
7661 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007662 spin_lock(&lo->plh_inode->i_lock);
7663 if (lrp->res.lrs_present)
7664 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7665 lo->plh_block_lgets--;
7666 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007667 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007668 kfree(calldata);
7669 dprintk("<-- %s\n", __func__);
7670}
7671
7672static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7673 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7674 .rpc_call_done = nfs4_layoutreturn_done,
7675 .rpc_release = nfs4_layoutreturn_release,
7676};
7677
7678int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7679{
7680 struct rpc_task *task;
7681 struct rpc_message msg = {
7682 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7683 .rpc_argp = &lrp->args,
7684 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007685 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007686 };
7687 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007688 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007689 .rpc_message = &msg,
7690 .callback_ops = &nfs4_layoutreturn_call_ops,
7691 .callback_data = lrp,
7692 };
7693 int status;
7694
7695 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007696 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007697 task = rpc_run_task(&task_setup_data);
7698 if (IS_ERR(task))
7699 return PTR_ERR(task);
7700 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007701 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007702 dprintk("<-- %s status=%d\n", __func__, status);
7703 rpc_put_task(task);
7704 return status;
7705}
7706
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007707/*
7708 * Retrieve the list of Data Server devices from the MDS.
7709 */
7710static int _nfs4_getdevicelist(struct nfs_server *server,
7711 const struct nfs_fh *fh,
7712 struct pnfs_devicelist *devlist)
7713{
7714 struct nfs4_getdevicelist_args args = {
7715 .fh = fh,
7716 .layoutclass = server->pnfs_curr_ld->id,
7717 };
7718 struct nfs4_getdevicelist_res res = {
7719 .devlist = devlist,
7720 };
7721 struct rpc_message msg = {
7722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7723 .rpc_argp = &args,
7724 .rpc_resp = &res,
7725 };
7726 int status;
7727
7728 dprintk("--> %s\n", __func__);
7729 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7730 &res.seq_res, 0);
7731 dprintk("<-- %s status=%d\n", __func__, status);
7732 return status;
7733}
7734
7735int nfs4_proc_getdevicelist(struct nfs_server *server,
7736 const struct nfs_fh *fh,
7737 struct pnfs_devicelist *devlist)
7738{
7739 struct nfs4_exception exception = { };
7740 int err;
7741
7742 do {
7743 err = nfs4_handle_exception(server,
7744 _nfs4_getdevicelist(server, fh, devlist),
7745 &exception);
7746 } while (exception.retry);
7747
7748 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7749 err, devlist->num_devs);
7750
7751 return err;
7752}
7753EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7754
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007755static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007756_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7757 struct pnfs_device *pdev,
7758 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007759{
7760 struct nfs4_getdeviceinfo_args args = {
7761 .pdev = pdev,
7762 };
7763 struct nfs4_getdeviceinfo_res res = {
7764 .pdev = pdev,
7765 };
7766 struct rpc_message msg = {
7767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7768 .rpc_argp = &args,
7769 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007770 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007771 };
7772 int status;
7773
7774 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007775 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007776 dprintk("<-- %s status=%d\n", __func__, status);
7777
7778 return status;
7779}
7780
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007781int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7782 struct pnfs_device *pdev,
7783 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007784{
7785 struct nfs4_exception exception = { };
7786 int err;
7787
7788 do {
7789 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007790 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007791 &exception);
7792 } while (exception.retry);
7793 return err;
7794}
7795EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7796
Andy Adamson863a3c62011-03-23 13:27:54 +00007797static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7798{
7799 struct nfs4_layoutcommit_data *data = calldata;
7800 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007801 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007802
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007803 nfs41_setup_sequence(session,
7804 &data->args.seq_args,
7805 &data->res.seq_res,
7806 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007807}
7808
7809static void
7810nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7811{
7812 struct nfs4_layoutcommit_data *data = calldata;
7813 struct nfs_server *server = NFS_SERVER(data->args.inode);
7814
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007815 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007816 return;
7817
7818 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007819 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7820 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7821 case -NFS4ERR_BADLAYOUT: /* no layout */
7822 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007823 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007824 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007825 break;
7826 default:
7827 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7828 rpc_restart_call_prepare(task);
7829 return;
7830 }
7831 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007832}
7833
7834static void nfs4_layoutcommit_release(void *calldata)
7835{
7836 struct nfs4_layoutcommit_data *data = calldata;
7837
Andy Adamsondb29c082011-07-30 20:52:38 -04007838 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007839 nfs_post_op_update_inode_force_wcc(data->args.inode,
7840 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007841 put_rpccred(data->cred);
7842 kfree(data);
7843}
7844
7845static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7846 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7847 .rpc_call_done = nfs4_layoutcommit_done,
7848 .rpc_release = nfs4_layoutcommit_release,
7849};
7850
7851int
Andy Adamsonef311532011-03-12 02:58:10 -05007852nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007853{
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7856 .rpc_argp = &data->args,
7857 .rpc_resp = &data->res,
7858 .rpc_cred = data->cred,
7859 };
7860 struct rpc_task_setup task_setup_data = {
7861 .task = &data->task,
7862 .rpc_client = NFS_CLIENT(data->args.inode),
7863 .rpc_message = &msg,
7864 .callback_ops = &nfs4_layoutcommit_ops,
7865 .callback_data = data,
7866 .flags = RPC_TASK_ASYNC,
7867 };
7868 struct rpc_task *task;
7869 int status = 0;
7870
7871 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7872 "lbw: %llu inode %lu\n",
7873 data->task.tk_pid, sync,
7874 data->args.lastbytewritten,
7875 data->args.inode->i_ino);
7876
Chuck Levera9c92d62013-08-09 12:48:18 -04007877 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007878 task = rpc_run_task(&task_setup_data);
7879 if (IS_ERR(task))
7880 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007881 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007882 goto out;
7883 status = nfs4_wait_for_completion_rpc_task(task);
7884 if (status != 0)
7885 goto out;
7886 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007887 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007888out:
7889 dprintk("%s: status %d\n", __func__, status);
7890 rpc_put_task(task);
7891 return status;
7892}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007893
Andy Adamson97431202013-08-08 10:57:56 -04007894/**
7895 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7896 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7897 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007898static int
7899_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007900 struct nfs_fsinfo *info,
7901 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007902{
7903 struct nfs41_secinfo_no_name_args args = {
7904 .style = SECINFO_STYLE_CURRENT_FH,
7905 };
7906 struct nfs4_secinfo_res res = {
7907 .flavors = flavors,
7908 };
7909 struct rpc_message msg = {
7910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7911 .rpc_argp = &args,
7912 .rpc_resp = &res,
7913 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007914 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007915 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007916 int status;
7917
7918 if (use_integrity) {
7919 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007920 cred = nfs4_get_clid_cred(server->nfs_client);
7921 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007922 }
7923
7924 dprintk("--> %s\n", __func__);
7925 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7926 &res.seq_res, 0);
7927 dprintk("<-- %s status=%d\n", __func__, status);
7928
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007929 if (cred)
7930 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007931
7932 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007933}
7934
7935static int
7936nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7937 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7938{
7939 struct nfs4_exception exception = { };
7940 int err;
7941 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007942 /* first try using integrity protection */
7943 err = -NFS4ERR_WRONGSEC;
7944
7945 /* try to use integrity protection with machine cred */
7946 if (_nfs4_is_integrity_protected(server->nfs_client))
7947 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7948 flavors, true);
7949
7950 /*
7951 * if unable to use integrity protection, or SECINFO with
7952 * integrity protection returns NFS4ERR_WRONGSEC (which is
7953 * disallowed by spec, but exists in deployed servers) use
7954 * the current filesystem's rpc_client and the user cred.
7955 */
7956 if (err == -NFS4ERR_WRONGSEC)
7957 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7958 flavors, false);
7959
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007960 switch (err) {
7961 case 0:
7962 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007963 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007964 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007965 default:
7966 err = nfs4_handle_exception(server, err, &exception);
7967 }
7968 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007969out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007970 return err;
7971}
7972
7973static int
7974nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7975 struct nfs_fsinfo *info)
7976{
7977 int err;
7978 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007979 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007980 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007981 struct nfs4_secinfo4 *secinfo;
7982 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007983
7984 page = alloc_page(GFP_KERNEL);
7985 if (!page) {
7986 err = -ENOMEM;
7987 goto out;
7988 }
7989
7990 flavors = page_address(page);
7991 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7992
7993 /*
7994 * Fall back on "guess and check" method if
7995 * the server doesn't support SECINFO_NO_NAME
7996 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007997 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007998 err = nfs4_find_root_sec(server, fhandle, info);
7999 goto out_freepage;
8000 }
8001 if (err)
8002 goto out_freepage;
8003
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008004 for (i = 0; i < flavors->num_flavors; i++) {
8005 secinfo = &flavors->flavors[i];
8006
8007 switch (secinfo->flavor) {
8008 case RPC_AUTH_NULL:
8009 case RPC_AUTH_UNIX:
8010 case RPC_AUTH_GSS:
8011 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8012 &secinfo->flavor_info);
8013 break;
8014 default:
8015 flavor = RPC_AUTH_MAXFLAVOR;
8016 break;
8017 }
8018
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008019 if (!nfs_auth_info_match(&server->auth_info, flavor))
8020 flavor = RPC_AUTH_MAXFLAVOR;
8021
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008022 if (flavor != RPC_AUTH_MAXFLAVOR) {
8023 err = nfs4_lookup_root_sec(server, fhandle,
8024 info, flavor);
8025 if (!err)
8026 break;
8027 }
8028 }
8029
8030 if (flavor == RPC_AUTH_MAXFLAVOR)
8031 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008032
8033out_freepage:
8034 put_page(page);
8035 if (err == -EACCES)
8036 return -EPERM;
8037out:
8038 return err;
8039}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008040
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008041static int _nfs41_test_stateid(struct nfs_server *server,
8042 nfs4_stateid *stateid,
8043 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008044{
8045 int status;
8046 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008047 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008048 };
8049 struct nfs41_test_stateid_res res;
8050 struct rpc_message msg = {
8051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8052 .rpc_argp = &args,
8053 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008054 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008055 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008056 struct rpc_clnt *rpc_client = server->client;
8057
8058 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8059 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008060
Chuck Lever38527b12012-07-11 16:30:23 -04008061 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008062 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008063 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008064 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008065 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008066 if (status != NFS_OK) {
8067 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008068 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008069 }
8070 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008071 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008072}
8073
Chuck Lever38527b12012-07-11 16:30:23 -04008074/**
8075 * nfs41_test_stateid - perform a TEST_STATEID operation
8076 *
8077 * @server: server / transport on which to perform the operation
8078 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008079 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008080 *
8081 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8082 * Otherwise a negative NFS4ERR value is returned if the operation
8083 * failed or the state ID is not currently valid.
8084 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008085static int nfs41_test_stateid(struct nfs_server *server,
8086 nfs4_stateid *stateid,
8087 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008088{
8089 struct nfs4_exception exception = { };
8090 int err;
8091 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008092 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008093 if (err != -NFS4ERR_DELAY)
8094 break;
8095 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008096 } while (exception.retry);
8097 return err;
8098}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008099
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008100struct nfs_free_stateid_data {
8101 struct nfs_server *server;
8102 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008103 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008104};
8105
8106static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8107{
8108 struct nfs_free_stateid_data *data = calldata;
8109 nfs41_setup_sequence(nfs4_get_session(data->server),
8110 &data->args.seq_args,
8111 &data->res.seq_res,
8112 task);
8113}
8114
8115static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8116{
8117 struct nfs_free_stateid_data *data = calldata;
8118
8119 nfs41_sequence_done(task, &data->res.seq_res);
8120
8121 switch (task->tk_status) {
8122 case -NFS4ERR_DELAY:
8123 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8124 rpc_restart_call_prepare(task);
8125 }
8126}
8127
8128static void nfs41_free_stateid_release(void *calldata)
8129{
8130 kfree(calldata);
8131}
8132
Trond Myklebust17f26b12013-08-21 15:48:42 -04008133static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008134 .rpc_call_prepare = nfs41_free_stateid_prepare,
8135 .rpc_call_done = nfs41_free_stateid_done,
8136 .rpc_release = nfs41_free_stateid_release,
8137};
8138
8139static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8140 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008141 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008142 bool privileged)
8143{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008144 struct rpc_message msg = {
8145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008146 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008147 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008148 struct rpc_task_setup task_setup = {
8149 .rpc_client = server->client,
8150 .rpc_message = &msg,
8151 .callback_ops = &nfs41_free_stateid_ops,
8152 .flags = RPC_TASK_ASYNC,
8153 };
8154 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008155
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008156 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8157 &task_setup.rpc_client, &msg);
8158
Chuck Lever38527b12012-07-11 16:30:23 -04008159 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008160 data = kmalloc(sizeof(*data), GFP_NOFS);
8161 if (!data)
8162 return ERR_PTR(-ENOMEM);
8163 data->server = server;
8164 nfs4_stateid_copy(&data->args.stateid, stateid);
8165
8166 task_setup.callback_data = data;
8167
8168 msg.rpc_argp = &data->args;
8169 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008170 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008171 if (privileged)
8172 nfs4_set_sequence_privileged(&data->args.seq_args);
8173
8174 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008175}
8176
Chuck Lever38527b12012-07-11 16:30:23 -04008177/**
8178 * nfs41_free_stateid - perform a FREE_STATEID operation
8179 *
8180 * @server: server / transport on which to perform the operation
8181 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008182 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008183 *
8184 * Returns NFS_OK if the server freed "stateid". Otherwise a
8185 * negative NFS4ERR value is returned.
8186 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008187static int nfs41_free_stateid(struct nfs_server *server,
8188 nfs4_stateid *stateid,
8189 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008190{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008191 struct rpc_task *task;
8192 int ret;
8193
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008194 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008195 if (IS_ERR(task))
8196 return PTR_ERR(task);
8197 ret = rpc_wait_for_completion_task(task);
8198 if (!ret)
8199 ret = task->tk_status;
8200 rpc_put_task(task);
8201 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008202}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008203
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008204static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8205{
8206 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008207 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008208
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008209 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008210 nfs4_free_lock_state(server, lsp);
8211 if (IS_ERR(task))
8212 return PTR_ERR(task);
8213 rpc_put_task(task);
8214 return 0;
8215}
8216
Trond Myklebust36281ca2012-03-04 18:13:56 -05008217static bool nfs41_match_stateid(const nfs4_stateid *s1,
8218 const nfs4_stateid *s2)
8219{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008220 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008221 return false;
8222
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008223 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008224 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008225 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008226 return true;
8227
8228 return false;
8229}
8230
Andy Adamson557134a2009-04-01 09:21:53 -04008231#endif /* CONFIG_NFS_V4_1 */
8232
Trond Myklebust36281ca2012-03-04 18:13:56 -05008233static bool nfs4_match_stateid(const nfs4_stateid *s1,
8234 const nfs4_stateid *s2)
8235{
Trond Myklebustf597c532012-03-04 18:13:56 -05008236 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008237}
8238
8239
Trond Myklebust17280172012-03-11 13:11:00 -04008240static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008241 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008242 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243 .recover_open = nfs4_open_reclaim,
8244 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008245 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008246 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247};
8248
Andy Adamson591d71c2009-04-01 09:22:47 -04008249#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008250static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008251 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8252 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8253 .recover_open = nfs4_open_reclaim,
8254 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008255 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008256 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008257 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008258};
8259#endif /* CONFIG_NFS_V4_1 */
8260
Trond Myklebust17280172012-03-11 13:11:00 -04008261static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008262 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008263 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008264 .recover_open = nfs4_open_expired,
8265 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008266 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008267};
8268
Andy Adamson591d71c2009-04-01 09:22:47 -04008269#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008270static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008271 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8272 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008273 .recover_open = nfs41_open_expired,
8274 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008275 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008276};
8277#endif /* CONFIG_NFS_V4_1 */
8278
Trond Myklebust17280172012-03-11 13:11:00 -04008279static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008280 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008281 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008282 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008283};
8284
8285#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008286static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008287 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008288 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008289 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008290};
8291#endif
8292
Chuck Leverec011fe2013-10-17 14:12:39 -04008293static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008294 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008295 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008296};
8297
8298#if defined(CONFIG_NFS_V4_1)
8299static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008300 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008301 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008302};
8303#endif /* CONFIG_NFS_V4_1 */
8304
Trond Myklebust97dc1352010-06-16 09:52:26 -04008305static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8306 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008307 .init_caps = NFS_CAP_READDIRPLUS
8308 | NFS_CAP_ATOMIC_OPEN
8309 | NFS_CAP_CHANGE_ATTR
8310 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008311 .init_client = nfs40_init_client,
8312 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008313 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008314 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008315 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008316 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008317 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8318 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8319 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008320 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008321};
8322
8323#if defined(CONFIG_NFS_V4_1)
8324static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8325 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008326 .init_caps = NFS_CAP_READDIRPLUS
8327 | NFS_CAP_ATOMIC_OPEN
8328 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008329 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008330 | NFS_CAP_STATEID_NFSV41
8331 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008332 .init_client = nfs41_init_client,
8333 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008334 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008335 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008336 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008337 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008338 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8339 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8340 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008341 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008342};
8343#endif
8344
Steve Dickson42c2c422013-05-22 12:50:38 -04008345#if defined(CONFIG_NFS_V4_2)
8346static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8347 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008348 .init_caps = NFS_CAP_READDIRPLUS
8349 | NFS_CAP_ATOMIC_OPEN
8350 | NFS_CAP_CHANGE_ATTR
8351 | NFS_CAP_POSIX_LOCK
8352 | NFS_CAP_STATEID_NFSV41
8353 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008354 .init_client = nfs41_init_client,
8355 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008356 .match_stateid = nfs41_match_stateid,
8357 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008358 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008359 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008360 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8361 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8362 .state_renewal_ops = &nfs41_state_renewal_ops,
8363};
8364#endif
8365
Trond Myklebust97dc1352010-06-16 09:52:26 -04008366const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8367 [0] = &nfs_v4_0_minor_ops,
8368#if defined(CONFIG_NFS_V4_1)
8369 [1] = &nfs_v4_1_minor_ops,
8370#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008371#if defined(CONFIG_NFS_V4_2)
8372 [2] = &nfs_v4_2_minor_ops,
8373#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008374};
8375
Trond Myklebust17f26b12013-08-21 15:48:42 -04008376static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008377 .create = nfs_create,
8378 .lookup = nfs_lookup,
8379 .atomic_open = nfs_atomic_open,
8380 .link = nfs_link,
8381 .unlink = nfs_unlink,
8382 .symlink = nfs_symlink,
8383 .mkdir = nfs_mkdir,
8384 .rmdir = nfs_rmdir,
8385 .mknod = nfs_mknod,
8386 .rename = nfs_rename,
8387 .permission = nfs_permission,
8388 .getattr = nfs_getattr,
8389 .setattr = nfs_setattr,
8390 .getxattr = generic_getxattr,
8391 .setxattr = generic_setxattr,
8392 .listxattr = generic_listxattr,
8393 .removexattr = generic_removexattr,
8394};
8395
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008396static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008397 .permission = nfs_permission,
8398 .getattr = nfs_getattr,
8399 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008400 .getxattr = generic_getxattr,
8401 .setxattr = generic_setxattr,
8402 .listxattr = generic_listxattr,
8403 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008404};
8405
David Howells509de812006-08-22 20:06:11 -04008406const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 .version = 4, /* protocol version */
8408 .dentry_ops = &nfs4_dentry_operations,
8409 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008410 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008411 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008412 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008413 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008414 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 .getattr = nfs4_proc_getattr,
8416 .setattr = nfs4_proc_setattr,
8417 .lookup = nfs4_proc_lookup,
8418 .access = nfs4_proc_access,
8419 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008420 .create = nfs4_proc_create,
8421 .remove = nfs4_proc_remove,
8422 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008423 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 .unlink_done = nfs4_proc_unlink_done,
8425 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008426 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008427 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008428 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 .link = nfs4_proc_link,
8430 .symlink = nfs4_proc_symlink,
8431 .mkdir = nfs4_proc_mkdir,
8432 .rmdir = nfs4_proc_remove,
8433 .readdir = nfs4_proc_readdir,
8434 .mknod = nfs4_proc_mknod,
8435 .statfs = nfs4_proc_statfs,
8436 .fsinfo = nfs4_proc_fsinfo,
8437 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008438 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 .decode_dirent = nfs4_decode_dirent,
8440 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008441 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008442 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008443 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008445 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008446 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008447 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008448 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008449 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008450 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008452 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008453 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008454 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008455 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008456 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008457 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008458 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008459 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008460 .create_server = nfs4_create_server,
8461 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008462};
8463
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008464static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8465 .prefix = XATTR_NAME_NFSV4_ACL,
8466 .list = nfs4_xattr_list_nfs4_acl,
8467 .get = nfs4_xattr_get_nfs4_acl,
8468 .set = nfs4_xattr_set_nfs4_acl,
8469};
8470
8471const struct xattr_handler *nfs4_xattr_handlers[] = {
8472 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008473#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8474 &nfs4_xattr_nfs4_label_handler,
8475#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008476 NULL
8477};
8478
Linus Torvalds1da177e2005-04-16 15:20:36 -07008479/*
8480 * Local variables:
8481 * c-basic-offset: 8
8482 * End:
8483 */