blob: 96e0bd42f38c0e588a4e0443fb9dfdf218e55127 [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 Myklebustdc0b0272008-12-23 15:21:56 -05001152static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001153{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001155 case FMODE_READ:
1156 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1157 break;
1158 case FMODE_WRITE:
1159 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1160 break;
1161 case FMODE_READ|FMODE_WRITE:
1162 set_bit(NFS_O_RDWR_STATE, &state->flags);
1163 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001164 if (!nfs_need_update_open_stateid(state, stateid))
1165 return;
1166 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1167 nfs4_stateid_copy(&state->stateid, stateid);
1168 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001169}
1170
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001171static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001172{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001173 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001174 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001175 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001176}
1177
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001178static 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 -07001179{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001180 /*
1181 * Protect the call to nfs4_state_set_mode_locked and
1182 * serialise the stateid update
1183 */
1184 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001185 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001186 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001187 set_bit(NFS_DELEGATED_STATE, &state->flags);
1188 }
1189 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001190 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001191 write_sequnlock(&state->seqlock);
1192 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001193 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001194 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001197static 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 -05001198{
1199 struct nfs_inode *nfsi = NFS_I(state->inode);
1200 struct nfs_delegation *deleg_cur;
1201 int ret = 0;
1202
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001204
1205 rcu_read_lock();
1206 deleg_cur = rcu_dereference(nfsi->delegation);
1207 if (deleg_cur == NULL)
1208 goto no_delegation;
1209
1210 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001211 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001212 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001213 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001214 goto no_delegation_unlock;
1215
1216 if (delegation == NULL)
1217 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001218 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001219 goto no_delegation_unlock;
1220
Trond Myklebustb7391f42008-12-23 15:21:52 -05001221 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001223 ret = 1;
1224no_delegation_unlock:
1225 spin_unlock(&deleg_cur->lock);
1226no_delegation:
1227 rcu_read_unlock();
1228
1229 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001230 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001231 ret = 1;
1232 }
1233
1234 return ret;
1235}
1236
1237
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001239{
1240 struct nfs_delegation *delegation;
1241
1242 rcu_read_lock();
1243 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001245 rcu_read_unlock();
1246 return;
1247 }
1248 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001249 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001250}
1251
Trond Myklebust6ee41262007-07-08 14:11:36 -04001252static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001253{
1254 struct nfs4_state *state = opendata->state;
1255 struct nfs_inode *nfsi = NFS_I(state->inode);
1256 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001257 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001258 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001259 nfs4_stateid stateid;
1260 int ret = -EAGAIN;
1261
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001264 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 if (can_open_cached(state, fmode, open_mode)) {
1266 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001267 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001268 goto out_return_state;
1269 }
1270 spin_unlock(&state->owner->so_lock);
1271 }
Trond Myklebust34310432008-12-23 15:21:38 -05001272 rcu_read_lock();
1273 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001274 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001275 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001276 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001277 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001278 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001279 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001280 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001281 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001282 if (!opendata->is_recover) {
1283 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1284 if (ret != 0)
1285 goto out;
1286 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001287 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001288
1289 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001291 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001292 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001293out:
1294 return ERR_PTR(ret);
1295out_return_state:
1296 atomic_inc(&state->count);
1297 return state;
1298}
1299
Andy Adamsone23008e2012-10-02 21:07:32 -04001300static void
1301nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1302{
1303 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1304 struct nfs_delegation *delegation;
1305 int delegation_flags = 0;
1306
1307 rcu_read_lock();
1308 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1309 if (delegation)
1310 delegation_flags = delegation->flags;
1311 rcu_read_unlock();
1312 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1313 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1314 "returning a delegation for "
1315 "OPEN(CLAIM_DELEGATE_CUR)\n",
1316 clp->cl_hostname);
1317 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1318 nfs_inode_set_delegation(state->inode,
1319 data->owner->so_cred,
1320 &data->o_res);
1321 else
1322 nfs_inode_reclaim_delegation(state->inode,
1323 data->owner->so_cred,
1324 &data->o_res);
1325}
1326
1327/*
1328 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1329 * and update the nfs4_state.
1330 */
1331static struct nfs4_state *
1332_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1333{
1334 struct inode *inode = data->state->inode;
1335 struct nfs4_state *state = data->state;
1336 int ret;
1337
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001338 if (!data->rpc_done) {
1339 if (data->rpc_status) {
1340 ret = data->rpc_status;
1341 goto err;
1342 }
1343 /* cached opens have already been processed */
1344 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001345 }
1346
Andy Adamsone23008e2012-10-02 21:07:32 -04001347 ret = nfs_refresh_inode(inode, &data->f_attr);
1348 if (ret)
1349 goto err;
1350
1351 if (data->o_res.delegation_type != 0)
1352 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001353update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001354 update_open_stateid(state, &data->o_res.stateid, NULL,
1355 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001356 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001357
1358 return state;
1359err:
1360 return ERR_PTR(ret);
1361
1362}
1363
1364static struct nfs4_state *
1365_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001366{
1367 struct inode *inode;
1368 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001369 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373 goto out;
1374 }
1375
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001376 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001378 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001379 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001380 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001381 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001382 goto err;
1383 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001384 state = nfs4_get_open_state(inode, data->owner);
1385 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001386 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001387 if (data->o_res.delegation_type != 0)
1388 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001389 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001390 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001392out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001393 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001395err_put_inode:
1396 iput(inode);
1397err:
1398 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001399}
1400
Andy Adamsone23008e2012-10-02 21:07:32 -04001401static struct nfs4_state *
1402nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1403{
1404 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1405 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1406 return _nfs4_opendata_to_nfs4_state(data);
1407}
1408
Trond Myklebust864472e2006-01-03 09:55:15 +01001409static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1410{
1411 struct nfs_inode *nfsi = NFS_I(state->inode);
1412 struct nfs_open_context *ctx;
1413
1414 spin_lock(&state->inode->i_lock);
1415 list_for_each_entry(ctx, &nfsi->open_files, list) {
1416 if (ctx->state != state)
1417 continue;
1418 get_nfs_open_context(ctx);
1419 spin_unlock(&state->inode->i_lock);
1420 return ctx;
1421 }
1422 spin_unlock(&state->inode->i_lock);
1423 return ERR_PTR(-ENOENT);
1424}
1425
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001426static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1427 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001428{
1429 struct nfs4_opendata *opendata;
1430
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001431 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001432 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001433 if (opendata == NULL)
1434 return ERR_PTR(-ENOMEM);
1435 opendata->state = state;
1436 atomic_inc(&state->count);
1437 return opendata;
1438}
1439
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001441{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001442 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001443 int ret;
1444
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 opendata->o_arg.open_flags = 0;
1446 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001447 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1448 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1449 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001450 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 if (ret != 0)
1452 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001453 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001454 if (IS_ERR(newstate))
1455 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001456 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001457 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001458 return 0;
1459}
1460
1461static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1462{
Trond Myklebust864472e2006-01-03 09:55:15 +01001463 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001464 int ret;
1465
1466 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001468 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001469 smp_rmb();
1470 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001471 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001472 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001473 if (ret != 0)
1474 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001475 if (newstate != state)
1476 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001477 }
1478 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001479 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001480 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001481 if (ret != 0)
1482 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001483 if (newstate != state)
1484 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001485 }
1486 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001487 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001488 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001489 if (ret != 0)
1490 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001491 if (newstate != state)
1492 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001494 /*
1495 * We may have performed cached opens for all three recoveries.
1496 * Check if we need to update the current stateid.
1497 */
1498 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001499 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001500 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001501 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001502 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001503 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001504 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001505 return 0;
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/*
1509 * OPEN_RECLAIM:
1510 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001512static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001514 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001515 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001516 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 int status;
1518
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001519 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1520 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001521 if (IS_ERR(opendata))
1522 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001523 rcu_read_lock();
1524 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001525 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001526 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001527 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001528 opendata->o_arg.u.delegation_type = delegation_type;
1529 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001530 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 return status;
1532}
1533
Trond Myklebust539cd032007-06-05 11:46:42 -04001534static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
1536 struct nfs_server *server = NFS_SERVER(state->inode);
1537 struct nfs4_exception exception = { };
1538 int err;
1539 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001540 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001541 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001542 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1543 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001544 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001545 break;
1546 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 } while (exception.retry);
1548 return err;
1549}
1550
Trond Myklebust864472e2006-01-03 09:55:15 +01001551static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1552{
1553 struct nfs_open_context *ctx;
1554 int ret;
1555
1556 ctx = nfs4_state_find_open_context(state);
1557 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001558 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001559 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001560 put_nfs_open_context(ctx);
1561 return ret;
1562}
1563
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001564static 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 -07001565{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001566 switch (err) {
1567 default:
1568 printk(KERN_ERR "NFS: %s: unhandled error "
1569 "%d.\n", __func__, err);
1570 case 0:
1571 case -ENOENT:
1572 case -ESTALE:
1573 break;
1574 case -NFS4ERR_BADSESSION:
1575 case -NFS4ERR_BADSLOT:
1576 case -NFS4ERR_BAD_HIGH_SLOT:
1577 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1578 case -NFS4ERR_DEADSESSION:
1579 set_bit(NFS_DELEGATED_STATE, &state->flags);
1580 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1581 return -EAGAIN;
1582 case -NFS4ERR_STALE_CLIENTID:
1583 case -NFS4ERR_STALE_STATEID:
1584 set_bit(NFS_DELEGATED_STATE, &state->flags);
1585 case -NFS4ERR_EXPIRED:
1586 /* Don't recall a delegation if it was lost */
1587 nfs4_schedule_lease_recovery(server->nfs_client);
1588 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001589 case -NFS4ERR_MOVED:
1590 nfs4_schedule_migration_recovery(server);
1591 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001592 case -NFS4ERR_LEASE_MOVED:
1593 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1594 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001595 case -NFS4ERR_DELEG_REVOKED:
1596 case -NFS4ERR_ADMIN_REVOKED:
1597 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001598 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001599 nfs_inode_find_state_and_recover(state->inode,
1600 stateid);
1601 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001602 return 0;
1603 case -NFS4ERR_DELAY:
1604 case -NFS4ERR_GRACE:
1605 set_bit(NFS_DELEGATED_STATE, &state->flags);
1606 ssleep(1);
1607 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001608 case -ENOMEM:
1609 case -NFS4ERR_DENIED:
1610 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1611 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return err;
1614}
1615
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001616int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1617{
1618 struct nfs_server *server = NFS_SERVER(state->inode);
1619 struct nfs4_opendata *opendata;
1620 int err;
1621
1622 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1623 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1624 if (IS_ERR(opendata))
1625 return PTR_ERR(opendata);
1626 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1627 err = nfs4_open_recover(opendata, state);
1628 nfs4_opendata_put(opendata);
1629 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1630}
1631
Chuck Leverbe05c862013-08-09 12:49:47 -04001632static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1633{
1634 struct nfs4_opendata *data = calldata;
1635
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001636 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1637 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001638}
1639
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001640static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1641{
1642 struct nfs4_opendata *data = calldata;
1643
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001644 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001645
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001646 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001647 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001648 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001649 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001650 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001651 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001652 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001653}
1654
1655static void nfs4_open_confirm_release(void *calldata)
1656{
1657 struct nfs4_opendata *data = calldata;
1658 struct nfs4_state *state = NULL;
1659
1660 /* If this request hasn't been cancelled, do nothing */
1661 if (data->cancelled == 0)
1662 goto out_free;
1663 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001664 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001665 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001666 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001667 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001668 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001669out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001670 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001671}
1672
1673static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001674 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001675 .rpc_call_done = nfs4_open_confirm_done,
1676 .rpc_release = nfs4_open_confirm_release,
1677};
1678
1679/*
1680 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1681 */
1682static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1683{
1684 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1685 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001686 struct rpc_message msg = {
1687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1688 .rpc_argp = &data->c_arg,
1689 .rpc_resp = &data->c_res,
1690 .rpc_cred = data->owner->so_cred,
1691 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001692 struct rpc_task_setup task_setup_data = {
1693 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001694 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001695 .callback_ops = &nfs4_open_confirm_ops,
1696 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001697 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001698 .flags = RPC_TASK_ASYNC,
1699 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int status;
1701
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001702 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001704 data->rpc_done = 0;
1705 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001706 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001707 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001708 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001709 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001710 status = nfs4_wait_for_completion_rpc_task(task);
1711 if (status != 0) {
1712 data->cancelled = 1;
1713 smp_wmb();
1714 } else
1715 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001716 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return status;
1718}
1719
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001720static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001722 struct nfs4_opendata *data = calldata;
1723 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001724 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001725
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001727 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001728 /*
1729 * Check if we still need to send an OPEN call, or if we can use
1730 * a delegation instead.
1731 */
1732 if (data->state != NULL) {
1733 struct nfs_delegation *delegation;
1734
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001735 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001736 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001737 rcu_read_lock();
1738 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001739 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001740 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001741 can_open_delegated(delegation, data->o_arg.fmode))
1742 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743 rcu_read_unlock();
1744 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001745 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001746 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001747 switch (data->o_arg.claim) {
1748 case NFS4_OPEN_CLAIM_PREVIOUS:
1749 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1750 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001751 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001752 case NFS4_OPEN_CLAIM_FH:
1753 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001754 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1755 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001756 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001757 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001758 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001759 &data->o_res.seq_res,
1760 task) != 0)
1761 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001762
1763 /* Set the create mode (note dependency on the session type) */
1764 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1765 if (data->o_arg.open_flags & O_EXCL) {
1766 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1767 if (nfs4_has_persistent_session(clp))
1768 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1769 else if (clp->cl_mvops->minor_version > 0)
1770 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1771 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001772 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001773unlock_no_action:
1774 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001775out_no_action:
1776 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001777out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001778 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001779}
1780
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001781static void nfs4_open_done(struct rpc_task *task, void *calldata)
1782{
1783 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001785 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001786
Trond Myklebust14516c32010-07-31 14:29:06 -04001787 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1788 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001789
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001791 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1792 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001793 case S_IFREG:
1794 break;
1795 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001796 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001797 break;
1798 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001799 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001800 break;
1801 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001802 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001803 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001804 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001805 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001806 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1807 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001808 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001809 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001810}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001811
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812static void nfs4_open_release(void *calldata)
1813{
1814 struct nfs4_opendata *data = calldata;
1815 struct nfs4_state *state = NULL;
1816
1817 /* If this request hasn't been cancelled, do nothing */
1818 if (data->cancelled == 0)
1819 goto out_free;
1820 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001821 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001822 goto out_free;
1823 /* In case we need an open_confirm, no cleanup! */
1824 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1825 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001826 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001827 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001828 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001829out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001830 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001831}
1832
1833static const struct rpc_call_ops nfs4_open_ops = {
1834 .rpc_call_prepare = nfs4_open_prepare,
1835 .rpc_call_done = nfs4_open_done,
1836 .rpc_release = nfs4_open_release,
1837};
1838
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001839static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840{
1841 struct inode *dir = data->dir->d_inode;
1842 struct nfs_server *server = NFS_SERVER(dir);
1843 struct nfs_openargs *o_arg = &data->o_arg;
1844 struct nfs_openres *o_res = &data->o_res;
1845 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001846 struct rpc_message msg = {
1847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1848 .rpc_argp = o_arg,
1849 .rpc_resp = o_res,
1850 .rpc_cred = data->owner->so_cred,
1851 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001852 struct rpc_task_setup task_setup_data = {
1853 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001854 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001855 .callback_ops = &nfs4_open_ops,
1856 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001857 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001858 .flags = RPC_TASK_ASYNC,
1859 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860 int status;
1861
Chuck Levera9c92d62013-08-09 12:48:18 -04001862 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001863 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001864 data->rpc_done = 0;
1865 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001866 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001867 data->is_recover = 0;
1868 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001869 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001870 data->is_recover = 1;
1871 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001872 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001873 if (IS_ERR(task))
1874 return PTR_ERR(task);
1875 status = nfs4_wait_for_completion_rpc_task(task);
1876 if (status != 0) {
1877 data->cancelled = 1;
1878 smp_wmb();
1879 } else
1880 status = data->rpc_status;
1881 rpc_put_task(task);
1882
1883 return status;
1884}
1885
1886static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1887{
1888 struct inode *dir = data->dir->d_inode;
1889 struct nfs_openres *o_res = &data->o_res;
1890 int status;
1891
1892 status = nfs4_run_open_task(data, 1);
1893 if (status != 0 || !data->rpc_done)
1894 return status;
1895
Trond Myklebust6926afd2012-01-07 13:22:46 -05001896 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1897
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001898 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1899 status = _nfs4_proc_open_confirm(data);
1900 if (status != 0)
1901 return status;
1902 }
1903
1904 return status;
1905}
1906
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001907static int nfs4_opendata_access(struct rpc_cred *cred,
1908 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001909 struct nfs4_state *state, fmode_t fmode,
1910 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001911{
1912 struct nfs_access_entry cache;
1913 u32 mask;
1914
1915 /* access call failed or for some reason the server doesn't
1916 * support any access modes -- defer access call until later */
1917 if (opendata->o_res.access_supported == 0)
1918 return 0;
1919
1920 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001921 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001922 * write mode bits, but POSIX allows the creating process to write.
1923 * use openflags to check for exec, because fmode won't
1924 * always have FMODE_EXEC set when file open for exec. */
1925 if (openflags & __FMODE_EXEC) {
1926 /* ONLY check for exec rights */
1927 mask = MAY_EXEC;
1928 } else if (fmode & FMODE_READ)
1929 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001930
1931 cache.cred = cred;
1932 cache.jiffies = jiffies;
1933 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1934 nfs_access_add_cache(state->inode, &cache);
1935
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001936 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001937 return 0;
1938
1939 /* even though OPEN succeeded, access is denied. Close the file */
1940 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001941 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001942}
1943
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001944/*
1945 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1946 */
1947static int _nfs4_proc_open(struct nfs4_opendata *data)
1948{
1949 struct inode *dir = data->dir->d_inode;
1950 struct nfs_server *server = NFS_SERVER(dir);
1951 struct nfs_openargs *o_arg = &data->o_arg;
1952 struct nfs_openres *o_res = &data->o_res;
1953 int status;
1954
1955 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001956 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001957 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001958 if (status != 0) {
1959 if (status == -NFS4ERR_BADNAME &&
1960 !(o_arg->open_flags & O_CREAT))
1961 return -ENOENT;
1962 return status;
1963 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001964
Trond Myklebust6926afd2012-01-07 13:22:46 -05001965 nfs_fattr_map_and_free_names(server, &data->f_attr);
1966
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001967 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001968 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001969 if (o_arg->open_flags & O_EXCL)
1970 data->file_created = 1;
1971 else if (o_res->cinfo.before != o_res->cinfo.after)
1972 data->file_created = 1;
1973 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001974 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1975 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001977 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001979 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001982 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001983 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985
Andy Adamsond83217c2011-03-01 01:34:17 +00001986static int nfs4_recover_expired_lease(struct nfs_server *server)
1987{
1988 return nfs4_client_recover_expired_lease(server->nfs_client);
1989}
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991/*
1992 * OPEN_EXPIRED:
1993 * reclaim state on the server after a network partition.
1994 * Assumes caller holds the appropriate lock
1995 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001996static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001998 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001999 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002001 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002003 if (IS_ERR(opendata))
2004 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002005 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002006 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002007 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002008 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002009 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002012static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002013{
Trond Myklebust539cd032007-06-05 11:46:42 -04002014 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002015 struct nfs4_exception exception = { };
2016 int err;
2017
2018 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002019 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002020 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002021 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2022 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002023 switch (err) {
2024 default:
2025 goto out;
2026 case -NFS4ERR_GRACE:
2027 case -NFS4ERR_DELAY:
2028 nfs4_handle_exception(server, err, &exception);
2029 err = 0;
2030 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002031 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002032out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002033 return err;
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2037{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002039 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Trond Myklebust864472e2006-01-03 09:55:15 +01002041 ctx = nfs4_state_find_open_context(state);
2042 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002043 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002044 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002045 put_nfs_open_context(ctx);
2046 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002049#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002050static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002051{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002052 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002053 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002054 struct nfs_delegation *delegation;
2055 struct rpc_cred *cred = NULL;
2056 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002057
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002058 /* If a state reset has been done, test_stateid is unneeded */
2059 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2060 return;
2061
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002062 /* Get the delegation credential for use by test/free_stateid */
2063 rcu_read_lock();
2064 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2065 if (delegation != NULL &&
2066 nfs4_stateid_match(&delegation->stateid, stateid)) {
2067 cred = get_rpccred(delegation->cred);
2068 rcu_read_unlock();
2069 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002070 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002071 } else
2072 rcu_read_unlock();
2073
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002074 if (status != NFS_OK) {
2075 /* Free the stateid unless the server explicitly
2076 * informs us the stateid is unrecognized. */
2077 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002078 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002079 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002080
Bryan Schumaker69388672012-09-26 15:25:52 -04002081 write_seqlock(&state->seqlock);
2082 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2083 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002084 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2085 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002086
2087 if (cred != NULL)
2088 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002089}
2090
2091/**
2092 * nfs41_check_open_stateid - possibly free an open stateid
2093 *
2094 * @state: NFSv4 state for an inode
2095 *
2096 * Returns NFS_OK if recovery for this stateid is now finished.
2097 * Otherwise a negative NFS4ERR value is returned.
2098 */
2099static int nfs41_check_open_stateid(struct nfs4_state *state)
2100{
2101 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002102 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002103 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002104 int status;
2105
2106 /* If a state reset has been done, test_stateid is unneeded */
2107 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2108 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2109 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2110 return -NFS4ERR_BAD_STATEID;
2111
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002112 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002113 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002114 if (status != NFS_OK) {
2115 /* Free the stateid unless the server explicitly
2116 * informs us the stateid is unrecognized. */
2117 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002118 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002119
2120 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2121 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2122 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002123 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002124 }
2125 return status;
2126}
2127
2128static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2129{
Chuck Levereb64cf92012-07-11 16:30:05 -04002130 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002131
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002132 nfs41_clear_delegation_stateid(state);
2133 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002134 if (status != NFS_OK)
2135 status = nfs4_open_expired(sp, state);
2136 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002137}
2138#endif
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002141 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2142 * fields corresponding to attributes that were used to store the verifier.
2143 * Make sure we clobber those fields in the later setattr call
2144 */
2145static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2146{
2147 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2148 !(sattr->ia_valid & ATTR_ATIME_SET))
2149 sattr->ia_valid |= ATTR_ATIME;
2150
2151 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2152 !(sattr->ia_valid & ATTR_MTIME_SET))
2153 sattr->ia_valid |= ATTR_MTIME;
2154}
2155
Trond Myklebustc21443c2013-02-07 14:26:21 -05002156static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2157 fmode_t fmode,
2158 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002159 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002160{
2161 struct nfs4_state_owner *sp = opendata->owner;
2162 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002163 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002164 struct nfs4_state *state;
2165 unsigned int seq;
2166 int ret;
2167
2168 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2169
2170 ret = _nfs4_proc_open(opendata);
2171 if (ret != 0)
2172 goto out;
2173
2174 state = nfs4_opendata_to_nfs4_state(opendata);
2175 ret = PTR_ERR(state);
2176 if (IS_ERR(state))
2177 goto out;
2178 if (server->caps & NFS_CAP_POSIX_LOCK)
2179 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2180
Trond Myklebust275bb302013-05-29 13:11:28 -04002181 dentry = opendata->dentry;
2182 if (dentry->d_inode == NULL) {
2183 /* FIXME: Is this d_drop() ever needed? */
2184 d_drop(dentry);
2185 dentry = d_add_unique(dentry, igrab(state->inode));
2186 if (dentry == NULL) {
2187 dentry = opendata->dentry;
2188 } else if (dentry != ctx->dentry) {
2189 dput(ctx->dentry);
2190 ctx->dentry = dget(dentry);
2191 }
2192 nfs_set_verifier(dentry,
2193 nfs_save_change_attribute(opendata->dir->d_inode));
2194 }
2195
Trond Myklebustc21443c2013-02-07 14:26:21 -05002196 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2197 if (ret != 0)
2198 goto out;
2199
Trond Myklebust3efb9722013-05-29 13:17:04 -04002200 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002201 if (dentry->d_inode == state->inode) {
2202 nfs_inode_attach_open_context(ctx);
2203 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2204 nfs4_schedule_stateid_recovery(server, state);
2205 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002206out:
2207 return ret;
2208}
2209
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002210/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002211 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 */
Andy Adamson82be4172012-05-23 05:02:35 -04002213static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002214 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002215 int flags,
2216 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002217 struct nfs4_label *label,
2218 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct nfs4_state_owner *sp;
2221 struct nfs4_state *state = NULL;
2222 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002223 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002224 struct dentry *dentry = ctx->dentry;
2225 struct rpc_cred *cred = ctx->cred;
2226 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2227 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002228 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002229 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002230 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002234 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2235 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2237 goto out_err;
2238 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002239 status = nfs4_recover_expired_lease(server);
2240 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002241 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002242 if (dentry->d_inode != NULL)
2243 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002244 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002245 if (dentry->d_inode)
2246 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002247 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002248 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002249 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002250 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
David Quigley14c43f72013-05-22 12:50:43 -04002252 if (label) {
2253 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2254 if (IS_ERR(olabel)) {
2255 status = PTR_ERR(olabel);
2256 goto err_opendata_put;
2257 }
2258 }
2259
Andy Adamson82be4172012-05-23 05:02:35 -04002260 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2261 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2262 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002263 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002264 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002265 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002266 if (dentry->d_inode != NULL)
2267 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002268
Trond Myklebust3efb9722013-05-29 13:17:04 -04002269 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002270 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002271 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002272 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002273
Trond Myklebust549b19c2013-04-16 18:42:34 -04002274 if ((opendata->o_arg.open_flags & O_EXCL) &&
2275 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002276 nfs4_exclusive_attrset(opendata, sattr);
2277
2278 nfs_fattr_init(opendata->o_res.f_attr);
2279 status = nfs4_do_setattr(state->inode, cred,
2280 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002281 state, label, olabel);
2282 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002283 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002284 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002285 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002286 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002287 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 if (opendata->file_created)
2289 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002290
2291 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2292 *ctx_th = opendata->f_attr.mdsthreshold;
2293 else
2294 kfree(opendata->f_attr.mdsthreshold);
2295 opendata->f_attr.mdsthreshold = NULL;
2296
David Quigley14c43f72013-05-22 12:50:43 -04002297 nfs4_label_free(olabel);
2298
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002299 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002302err_free_label:
2303 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002304err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002305 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002306 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002307err_put_state_owner:
2308 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return status;
2311}
2312
2313
Andy Adamson82be4172012-05-23 05:02:35 -04002314static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002315 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002316 int flags,
2317 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002318 struct nfs4_label *label,
2319 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002321 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 struct nfs4_exception exception = { };
2323 struct nfs4_state *res;
2324 int status;
2325
2326 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002327 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002328 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002329 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (status == 0)
2331 break;
2332 /* NOTE: BAD_SEQID means the server and client disagree about the
2333 * book-keeping w.r.t. state-changing operations
2334 * (OPEN/CLOSE/LOCK/LOCKU...)
2335 * It is actually a sign of a bug on the client or on the server.
2336 *
2337 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002338 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 * have unhashed the old state_owner for us, and that we can
2340 * therefore safely retry using a new one. We should still warn
2341 * the user though...
2342 */
2343 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002344 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002345 " returned a bad sequence-id error!\n",
2346 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 exception.retry = 1;
2348 continue;
2349 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002350 /*
2351 * BAD_STATEID on OPEN means that the server cancelled our
2352 * state before it received the OPEN_CONFIRM.
2353 * Recover by retrying the request as per the discussion
2354 * on Page 181 of RFC3530.
2355 */
2356 if (status == -NFS4ERR_BAD_STATEID) {
2357 exception.retry = 1;
2358 continue;
2359 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002360 if (status == -EAGAIN) {
2361 /* We must have found a delegation */
2362 exception.retry = 1;
2363 continue;
2364 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002365 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2366 continue;
2367 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 status, &exception));
2369 } while (exception.retry);
2370 return res;
2371}
2372
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002373static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2374 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002375 struct nfs4_state *state, struct nfs4_label *ilabel,
2376 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002378 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002380 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 .iap = sattr,
2382 .server = server,
2383 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002384 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 };
2386 struct nfs_setattrres res = {
2387 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002388 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 .server = server,
2390 };
2391 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2393 .rpc_argp = &arg,
2394 .rpc_resp = &res,
2395 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002397 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002398 fmode_t fmode;
2399 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002400 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
David Quigleyaa9c2662013-05-22 12:50:44 -04002402 arg.bitmask = nfs4_bitmask(server, ilabel);
2403 if (ilabel)
2404 arg.bitmask = nfs4_bitmask(server, olabel);
2405
Trond Myklebust0e574af2005-10-27 22:12:38 -04002406 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Trond Myklebustee3ae842013-04-29 10:35:36 -04002408 /* Servers should only apply open mode checks for file size changes */
2409 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2410 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2411
2412 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2413 /* Use that stateid */
2414 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002415 struct nfs_lockowner lockowner = {
2416 .l_owner = current->files,
2417 .l_pid = current->tgid,
2418 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002419 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002420 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002421 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002422 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Bryan Schumaker7c513052011-03-24 17:12:24 +00002424 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002425 if (status == 0 && state != NULL)
2426 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002427 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002430static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2431 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002432 struct nfs4_state *state, struct nfs4_label *ilabel,
2433 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002435 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002436 struct nfs4_exception exception = {
2437 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002438 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002439 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 int err;
2441 do {
David Quigley1775fd32013-05-22 12:50:42 -04002442 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002443 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002444 switch (err) {
2445 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002446 if (!(sattr->ia_valid & ATTR_SIZE)) {
2447 pr_warn_once("NFSv4: server %s is incorrectly "
2448 "applying open mode checks to "
2449 "a SETATTR that is not "
2450 "changing file size.\n",
2451 server->nfs_client->cl_hostname);
2452 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002453 if (state && !(state->state & FMODE_WRITE)) {
2454 err = -EBADF;
2455 if (sattr->ia_valid & ATTR_OPEN)
2456 err = -EACCES;
2457 goto out;
2458 }
2459 }
2460 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002462out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 return err;
2464}
2465
2466struct nfs4_closedata {
2467 struct inode *inode;
2468 struct nfs4_state *state;
2469 struct nfs_closeargs arg;
2470 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002471 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002472 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002473 bool roc;
2474 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475};
2476
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002477static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002478{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002479 struct nfs4_closedata *calldata = data;
2480 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002481 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002482
Fred Isamanf7e89172011-01-06 11:36:32 +00002483 if (calldata->roc)
2484 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002485 nfs4_put_open_state(calldata->state);
2486 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002487 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002488 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002489 kfree(calldata);
2490}
2491
Trond Myklebust88069f72009-12-08 08:33:16 -05002492static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2493 fmode_t fmode)
2494{
2495 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002496 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002497 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2498 case FMODE_WRITE:
2499 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2500 break;
2501 case FMODE_READ:
2502 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2503 break;
2504 case 0:
2505 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2506 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2507 clear_bit(NFS_OPEN_STATE, &state->flags);
2508 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002509 spin_unlock(&state->owner->so_lock);
2510}
2511
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002512static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002514 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct nfs_server *server = NFS_SERVER(calldata->inode);
2517
Chuck Levera3ca5652012-03-01 17:00:40 -05002518 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002519 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2520 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002521 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /* hmm. we are done with the inode, and in the process of freeing
2523 * the state_owner. we keep this around to process errors
2524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 switch (task->tk_status) {
2526 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002527 if (calldata->roc)
2528 pnfs_roc_set_barrier(state->inode,
2529 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002530 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002531 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002533 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002535 case -NFS4ERR_OLD_STATEID:
2536 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002538 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 default:
Trond Myklebust69794ad2013-11-20 12:57:19 -05002541 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002542 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002543 goto out_release;
2544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Trond Myklebust69794ad2013-11-20 12:57:19 -05002546 nfs4_close_clear_stateid_flags(state, calldata->arg.fmode);
2547out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002548 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002549 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002550 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002553static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002555 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002556 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002557 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002558 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002559
Chuck Levera3ca5652012-03-01 17:00:40 -05002560 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002561 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002562 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002563
Trond Myklebust88069f72009-12-08 08:33:16 -05002564 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2565 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002566 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002567 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002568 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002569 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002570 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2571 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2572 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002573 }
2574 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002575 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2576 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2577 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002578 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002579 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002580 if (!nfs4_valid_open_stateid(state))
2581 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002582 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002583
2584 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002585 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002586 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002587 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002588
Fred Isamanf7e89172011-01-06 11:36:32 +00002589 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002590 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002591 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002592 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2593 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002594 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002595 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002596 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002597
Trond Myklebust516a6af2005-10-27 22:12:41 -04002598 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002599 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002600 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002601 &calldata->arg.seq_args,
2602 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002603 task) != 0)
2604 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002605 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002606 return;
2607out_no_action:
2608 task->tk_action = NULL;
2609out_wait:
2610 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002613static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002614 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002615 .rpc_call_done = nfs4_close_done,
2616 .rpc_release = nfs4_free_closedata,
2617};
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*
2620 * It is possible for data to be read/written from a mem-mapped file
2621 * after the sys_close call (which hits the vfs layer as a flush).
2622 * This means that we can't safely call nfsv4 close on a file until
2623 * the inode is cleared. This in turn means that we are not good
2624 * NFSv4 citizens - we do not indicate to the server to update the file's
2625 * share state even when we are done with one of the three share
2626 * stateid's in the inode.
2627 *
2628 * NOTE: Caller must be holding the sp->so_owner semaphore!
2629 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002630int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002632 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002634 struct nfs4_state_owner *sp = state->owner;
2635 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002636 struct rpc_message msg = {
2637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2638 .rpc_cred = state->owner->so_cred,
2639 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002640 struct rpc_task_setup task_setup_data = {
2641 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002642 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002643 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002644 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002645 .flags = RPC_TASK_ASYNC,
2646 };
Trond Myklebust95121352005-10-18 14:20:12 -07002647 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002649 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2650 &task_setup_data.rpc_client, &msg);
2651
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002652 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002654 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002655 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002656 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002658 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002659 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002661 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002662 if (calldata->arg.seqid == NULL)
2663 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002664 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002665 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002666 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002667 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002668 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002669 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002670 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002671
Trond Myklebust1174dd12010-12-21 10:52:24 -05002672 msg.rpc_argp = &calldata->arg;
2673 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002674 task_setup_data.callback_data = calldata;
2675 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002676 if (IS_ERR(task))
2677 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002678 status = 0;
2679 if (wait)
2680 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002681 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002682 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002683out_free_calldata:
2684 kfree(calldata);
2685out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002686 nfs4_put_open_state(state);
2687 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Trond Myklebust2b484292010-09-17 10:56:51 -04002691static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002692nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2693 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002696 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2697
2698 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Trond Myklebust565277f2007-10-15 18:17:53 -04002700 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002701 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002702
2703 nfs4_label_release_security(label);
2704
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002705 if (IS_ERR(state))
2706 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002707 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Trond Myklebust1185a552009-12-03 15:54:02 -05002710static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002711{
2712 if (ctx->state == NULL)
2713 return;
2714 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002715 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002716 else
Al Viro643168c2011-06-22 18:20:23 -04002717 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Trond Myklebustb944dba2013-11-04 15:20:20 -05002720#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2721#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2722#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2725{
Benny Halevy43652ad2009-04-01 09:21:54 -04002726 struct nfs4_server_caps_arg args = {
2727 .fhandle = fhandle,
2728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct nfs4_server_caps_res res = {};
2730 struct rpc_message msg = {
2731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002732 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 .rpc_resp = &res,
2734 };
2735 int status;
2736
Bryan Schumaker7c513052011-03-24 17:12:24 +00002737 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002739 /* Sanity check the server answers */
2740 switch (server->nfs_client->cl_minorversion) {
2741 case 0:
2742 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2743 res.attr_bitmask[2] = 0;
2744 break;
2745 case 1:
2746 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2747 break;
2748 case 2:
2749 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002752 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2753 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2754 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2755 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002756 NFS_CAP_CTIME|NFS_CAP_MTIME|
2757 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002758 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2759 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 server->caps |= NFS_CAP_ACLS;
2761 if (res.has_links != 0)
2762 server->caps |= NFS_CAP_HARDLINKS;
2763 if (res.has_symlinks != 0)
2764 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002765 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2766 server->caps |= NFS_CAP_FILEID;
2767 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2768 server->caps |= NFS_CAP_MODE;
2769 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2770 server->caps |= NFS_CAP_NLINK;
2771 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2772 server->caps |= NFS_CAP_OWNER;
2773 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2774 server->caps |= NFS_CAP_OWNER_GROUP;
2775 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2776 server->caps |= NFS_CAP_ATIME;
2777 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2778 server->caps |= NFS_CAP_CTIME;
2779 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2780 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002781#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2782 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2783 server->caps |= NFS_CAP_SECURITY_LABEL;
2784#endif
2785 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2786 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002787 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002788
Trond Myklebusta65318b2009-03-11 14:10:28 -04002789 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2790 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2791 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002792 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002794 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return status;
2798}
2799
Trond Myklebust55a97592006-06-09 09:34:19 -04002800int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801{
2802 struct nfs4_exception exception = { };
2803 int err;
2804 do {
2805 err = nfs4_handle_exception(server,
2806 _nfs4_server_capabilities(server, fhandle),
2807 &exception);
2808 } while (exception.retry);
2809 return err;
2810}
2811
2812static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2813 struct nfs_fsinfo *info)
2814{
David Quigleyaa9c2662013-05-22 12:50:44 -04002815 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002817 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 };
2819 struct nfs4_lookup_res res = {
2820 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002821 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .fh = fhandle,
2823 };
2824 struct rpc_message msg = {
2825 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2826 .rpc_argp = &args,
2827 .rpc_resp = &res,
2828 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002829
David Quigleyaa9c2662013-05-22 12:50:44 -04002830 bitmask[0] = nfs4_fattr_bitmap[0];
2831 bitmask[1] = nfs4_fattr_bitmap[1];
2832 /*
2833 * Process the label in the upcoming getfattr
2834 */
2835 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2836
Trond Myklebust0e574af2005-10-27 22:12:38 -04002837 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002838 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
2841static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2842 struct nfs_fsinfo *info)
2843{
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002847 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002848 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002849 switch (err) {
2850 case 0:
2851 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002852 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002853 default:
2854 err = nfs4_handle_exception(server, err, &exception);
2855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return err;
2859}
2860
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002861static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2862 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2863{
Trond Myklebustc2190662013-08-26 19:23:04 -04002864 struct rpc_auth_create_args auth_args = {
2865 .pseudoflavor = flavor,
2866 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002867 struct rpc_auth *auth;
2868 int ret;
2869
Trond Myklebustc2190662013-08-26 19:23:04 -04002870 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002871 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002872 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002873 goto out;
2874 }
2875 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002876out:
2877 return ret;
2878}
2879
Chuck Lever9a744ba2013-03-16 15:56:02 -04002880/*
2881 * Retry pseudoroot lookup with various security flavors. We do this when:
2882 *
2883 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2884 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2885 *
2886 * Returns zero on success, or a negative NFS4ERR value, or a
2887 * negative errno value.
2888 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002889static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002890 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002892 /* Per 3530bis 15.33.5 */
2893 static const rpc_authflavor_t flav_array[] = {
2894 RPC_AUTH_GSS_KRB5P,
2895 RPC_AUTH_GSS_KRB5I,
2896 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002897 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002898 RPC_AUTH_NULL,
2899 };
2900 int status = -EPERM;
2901 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04002903 if (server->auth_info.flavor_len > 0) {
2904 /* try each flavor specified by user */
2905 for (i = 0; i < server->auth_info.flavor_len; i++) {
2906 status = nfs4_lookup_root_sec(server, fhandle, info,
2907 server->auth_info.flavors[i]);
2908 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2909 continue;
2910 break;
2911 }
2912 } else {
2913 /* no flavors specified by user, try default list */
2914 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2915 status = nfs4_lookup_root_sec(server, fhandle, info,
2916 flav_array[i]);
2917 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2918 continue;
2919 break;
2920 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002921 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002922
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002923 /*
2924 * -EACCESS could mean that the user doesn't have correct permissions
2925 * to access the mount. It could also mean that we tried to mount
2926 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2927 * existing mount programs don't handle -EACCES very well so it should
2928 * be mapped to -EPERM instead.
2929 */
2930 if (status == -EACCES)
2931 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002932 return status;
2933}
2934
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002935static int nfs4_do_find_root_sec(struct nfs_server *server,
2936 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2937{
2938 int mv = server->nfs_client->cl_minorversion;
2939 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2940}
2941
2942/**
2943 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2944 * @server: initialized nfs_server handle
2945 * @fhandle: we fill in the pseudo-fs root file handle
2946 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002947 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002948 *
2949 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002950 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002951int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002952 struct nfs_fsinfo *info,
2953 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002954{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002955 int status;
2956
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002957 switch (auth_probe) {
2958 case false:
2959 status = nfs4_lookup_root(server, fhandle, info);
2960 if (status != -NFS4ERR_WRONGSEC)
2961 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002962 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002963 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002964 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (status == 0)
2967 status = nfs4_server_capabilities(server, fhandle);
2968 if (status == 0)
2969 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002970
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002971 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Bryan Schumakerbae36242012-05-10 15:07:31 -04002974static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2975 struct nfs_fsinfo *info)
2976{
2977 int error;
2978 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002979 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002980
2981 error = nfs4_server_capabilities(server, mntfh);
2982 if (error < 0) {
2983 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2984 return error;
2985 }
2986
David Quigley14c43f72013-05-22 12:50:43 -04002987 label = nfs4_label_alloc(server, GFP_KERNEL);
2988 if (IS_ERR(label))
2989 return PTR_ERR(label);
2990
David Quigley1775fd32013-05-22 12:50:42 -04002991 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002992 if (error < 0) {
2993 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002994 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002995 }
2996
2997 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2998 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2999 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3000
David Quigley14c43f72013-05-22 12:50:43 -04003001err_free_label:
3002 nfs4_label_free(label);
3003
Bryan Schumakerbae36242012-05-10 15:07:31 -04003004 return error;
3005}
3006
Manoj Naik6b97fd32006-06-09 09:34:29 -04003007/*
3008 * Get locations and (maybe) other attributes of a referral.
3009 * Note that we'll actually follow the referral later when
3010 * we detect fsid mismatch in inode revalidation
3011 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003012static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3013 const struct qstr *name, struct nfs_fattr *fattr,
3014 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003015{
3016 int status = -ENOMEM;
3017 struct page *page = NULL;
3018 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003019
3020 page = alloc_page(GFP_KERNEL);
3021 if (page == NULL)
3022 goto out;
3023 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3024 if (locations == NULL)
3025 goto out;
3026
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003027 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003028 if (status != 0)
3029 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003030
3031 /*
3032 * If the fsid didn't change, this is a migration event, not a
3033 * referral. Cause us to drop into the exception handler, which
3034 * will kick off migration recovery.
3035 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003036 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003037 dprintk("%s: server did not return a different fsid for"
3038 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003039 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003040 goto out;
3041 }
Andy Adamson533eb462011-06-13 18:25:56 -04003042 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3043 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003044
Andy Adamson533eb462011-06-13 18:25:56 -04003045 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003046 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003047 memset(fhandle, 0, sizeof(struct nfs_fh));
3048out:
3049 if (page)
3050 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003051 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003052 return status;
3053}
3054
David Quigley1775fd32013-05-22 12:50:42 -04003055static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3056 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
3058 struct nfs4_getattr_arg args = {
3059 .fh = fhandle,
3060 .bitmask = server->attr_bitmask,
3061 };
3062 struct nfs4_getattr_res res = {
3063 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003064 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .server = server,
3066 };
3067 struct rpc_message msg = {
3068 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3069 .rpc_argp = &args,
3070 .rpc_resp = &res,
3071 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003072
3073 args.bitmask = nfs4_bitmask(server, label);
3074
Trond Myklebust0e574af2005-10-27 22:12:38 -04003075 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003076 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
David Quigley1775fd32013-05-22 12:50:42 -04003079static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3080 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct nfs4_exception exception = { };
3083 int err;
3084 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003085 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3086 trace_nfs4_getattr(server, fhandle, fattr, err);
3087 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 &exception);
3089 } while (exception.retry);
3090 return err;
3091}
3092
3093/*
3094 * The file is not closed if it is opened due to the a request to change
3095 * the size of the file. The open call will not be needed once the
3096 * VFS layer lookup-intents are implemented.
3097 *
3098 * Close is called when the inode is destroyed.
3099 * If we haven't opened the file for O_WRONLY, we
3100 * need to in the size_change case to obtain a stateid.
3101 *
3102 * Got race?
3103 * Because OPEN is always done by name in nfsv4, it is
3104 * possible that we opened a different file by the same
3105 * name. We can recognize this race condition, but we
3106 * can't do anything about it besides returning an error.
3107 *
3108 * This will be fixed with VFS changes (lookup-intent).
3109 */
3110static int
3111nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3112 struct iattr *sattr)
3113{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003114 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003115 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003116 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003117 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 int status;
3119
Benny Halevy8a1636c2010-07-14 15:43:57 -04003120 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003121 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003122
Trond Myklebust0e574af2005-10-27 22:12:38 -04003123 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
Andy Adamson26699402012-05-30 16:12:24 -04003125 /* Deal with open(O_TRUNC) */
3126 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003127 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003128
3129 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003130 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003131 return 0;
3132
Trond Myklebustd5308382005-11-04 15:33:38 -05003133 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003134 if (sattr->ia_valid & ATTR_FILE) {
3135 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003136
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003137 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003138 if (ctx) {
3139 cred = ctx->cred;
3140 state = ctx->state;
3141 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003142 }
3143
David Quigley14c43f72013-05-22 12:50:43 -04003144 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3145 if (IS_ERR(label))
3146 return PTR_ERR(label);
3147
3148 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003149 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003150 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003151 nfs_setsecurity(inode, fattr, label);
3152 }
David Quigley14c43f72013-05-22 12:50:43 -04003153 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 return status;
3155}
3156
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003157static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3158 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003159 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003160{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003161 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003162 int status;
3163 struct nfs4_lookup_arg args = {
3164 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003165 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003166 .name = name,
3167 };
3168 struct nfs4_lookup_res res = {
3169 .server = server,
3170 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003171 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003172 .fh = fhandle,
3173 };
3174 struct rpc_message msg = {
3175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3176 .rpc_argp = &args,
3177 .rpc_resp = &res,
3178 };
3179
David Quigleyaa9c2662013-05-22 12:50:44 -04003180 args.bitmask = nfs4_bitmask(server, label);
3181
David Howells2b3de442006-08-22 20:06:09 -04003182 nfs_fattr_init(fattr);
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003185 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 dprintk("NFS reply lookup: %d\n", status);
3187 return status;
3188}
3189
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003190static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003191{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003192 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003193 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003194 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3195 fattr->nlink = 2;
3196}
3197
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003198static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3199 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003200 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003201{
3202 struct nfs4_exception exception = { };
3203 struct rpc_clnt *client = *clnt;
3204 int err;
3205 do {
David Quigley1775fd32013-05-22 12:50:42 -04003206 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003207 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003208 switch (err) {
3209 case -NFS4ERR_BADNAME:
3210 err = -ENOENT;
3211 goto out;
3212 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003213 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003214 goto out;
3215 case -NFS4ERR_WRONGSEC:
3216 err = -EPERM;
3217 if (client != *clnt)
3218 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003219 client = nfs4_create_sec_client(client, dir, name);
3220 if (IS_ERR(client))
3221 return PTR_ERR(client);
3222
3223 exception.retry = 1;
3224 break;
3225 default:
3226 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3227 }
3228 } while (exception.retry);
3229
3230out:
3231 if (err == 0)
3232 *clnt = client;
3233 else if (client != *clnt)
3234 rpc_shutdown_client(client);
3235
3236 return err;
3237}
3238
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003239static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003240 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3241 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003243 int status;
3244 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003245
David Quigley1775fd32013-05-22 12:50:42 -04003246 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003247 if (client != NFS_CLIENT(dir)) {
3248 rpc_shutdown_client(client);
3249 nfs_fixup_secinfo_attributes(fattr);
3250 }
3251 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003254struct rpc_clnt *
3255nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3256 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3257{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003258 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003259 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003260
David Quigley1775fd32013-05-22 12:50:42 -04003261 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003262 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003263 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003264 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003265}
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3268{
Trond Myklebust76b32992007-08-10 17:45:11 -04003269 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct nfs4_accessargs args = {
3271 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003272 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003274 struct nfs4_accessres res = {
3275 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003276 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 struct rpc_message msg = {
3278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3279 .rpc_argp = &args,
3280 .rpc_resp = &res,
3281 .rpc_cred = entry->cred,
3282 };
3283 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003284 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 /*
3287 * Determine which access bits we want to ask for...
3288 */
3289 if (mode & MAY_READ)
3290 args.access |= NFS4_ACCESS_READ;
3291 if (S_ISDIR(inode->i_mode)) {
3292 if (mode & MAY_WRITE)
3293 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3294 if (mode & MAY_EXEC)
3295 args.access |= NFS4_ACCESS_LOOKUP;
3296 } else {
3297 if (mode & MAY_WRITE)
3298 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3299 if (mode & MAY_EXEC)
3300 args.access |= NFS4_ACCESS_EXECUTE;
3301 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003302
3303 res.fattr = nfs_alloc_fattr();
3304 if (res.fattr == NULL)
3305 return -ENOMEM;
3306
Bryan Schumaker7c513052011-03-24 17:12:24 +00003307 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003309 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003310 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003312 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return status;
3314}
3315
3316static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3317{
3318 struct nfs4_exception exception = { };
3319 int err;
3320 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003321 err = _nfs4_proc_access(inode, entry);
3322 trace_nfs4_access(inode, err);
3323 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 &exception);
3325 } while (exception.retry);
3326 return err;
3327}
3328
3329/*
3330 * TODO: For the time being, we don't try to get any attributes
3331 * along with any of the zero-copy operations READ, READDIR,
3332 * READLINK, WRITE.
3333 *
3334 * In the case of the first three, we want to put the GETATTR
3335 * after the read-type operation -- this is because it is hard
3336 * to predict the length of a GETATTR response in v4, and thus
3337 * align the READ data correctly. This means that the GETATTR
3338 * may end up partially falling into the page cache, and we should
3339 * shift it into the 'tail' of the xdr_buf before processing.
3340 * To do this efficiently, we need to know the total length
3341 * of data received, which doesn't seem to be available outside
3342 * of the RPC layer.
3343 *
3344 * In the case of WRITE, we also want to put the GETATTR after
3345 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003346 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 *
3348 * Both of these changes to the XDR layer would in fact be quite
3349 * minor, but I decided to leave them for a subsequent patch.
3350 */
3351static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3352 unsigned int pgbase, unsigned int pglen)
3353{
3354 struct nfs4_readlink args = {
3355 .fh = NFS_FH(inode),
3356 .pgbase = pgbase,
3357 .pglen = pglen,
3358 .pages = &page,
3359 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003360 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 struct rpc_message msg = {
3362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3363 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003364 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 };
3366
Bryan Schumaker7c513052011-03-24 17:12:24 +00003367 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 -07003368}
3369
3370static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3371 unsigned int pgbase, unsigned int pglen)
3372{
3373 struct nfs4_exception exception = { };
3374 int err;
3375 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003376 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3377 trace_nfs4_readlink(inode, err);
3378 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 &exception);
3380 } while (exception.retry);
3381 return err;
3382}
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003385 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387static int
3388nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003389 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
David Quigleyaa9c2662013-05-22 12:50:44 -04003391 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003392 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003394 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 int status = 0;
3396
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003397 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3398 if (IS_ERR(ctx))
3399 return PTR_ERR(ctx);
3400
David Quigleyaa9c2662013-05-22 12:50:44 -04003401 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3402
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003403 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003404 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 if (IS_ERR(state)) {
3406 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003407 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003410 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003411 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return status;
3413}
3414
3415static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3416{
Trond Myklebust16e42952005-10-27 22:12:44 -04003417 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003418 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003420 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003422 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003423 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003424 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3427 .rpc_argp = &args,
3428 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003430 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003431
Bryan Schumaker7c513052011-03-24 17:12:24 +00003432 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003433 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003434 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return status;
3436}
3437
3438static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3439{
3440 struct nfs4_exception exception = { };
3441 int err;
3442 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003443 err = _nfs4_proc_remove(dir, name);
3444 trace_nfs4_remove(dir, name, err);
3445 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 &exception);
3447 } while (exception.retry);
3448 return err;
3449}
3450
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003451static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003453 struct nfs_server *server = NFS_SERVER(dir);
3454 struct nfs_removeargs *args = msg->rpc_argp;
3455 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003457 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003459 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003460
3461 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003464static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3465{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003466 nfs4_setup_sequence(NFS_SERVER(data->dir),
3467 &data->args.seq_args,
3468 &data->res.seq_res,
3469 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003472static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003474 struct nfs_unlinkdata *data = task->tk_calldata;
3475 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003476
Trond Myklebust14516c32010-07-31 14:29:06 -04003477 if (!nfs4_sequence_done(task, &res->seq_res))
3478 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003479 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003480 return 0;
3481 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003482 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Jeff Laytond3d41522010-09-17 17:31:57 -04003485static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3486{
3487 struct nfs_server *server = NFS_SERVER(dir);
3488 struct nfs_renameargs *arg = msg->rpc_argp;
3489 struct nfs_renameres *res = msg->rpc_resp;
3490
3491 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003492 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003493 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003494}
3495
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003496static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3497{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003498 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3499 &data->args.seq_args,
3500 &data->res.seq_res,
3501 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003502}
3503
3504static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3505 struct inode *new_dir)
3506{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003507 struct nfs_renamedata *data = task->tk_calldata;
3508 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003509
3510 if (!nfs4_sequence_done(task, &res->seq_res))
3511 return 0;
3512 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3513 return 0;
3514
3515 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003516 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003517 return 1;
3518}
3519
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3521 struct inode *new_dir, struct qstr *new_name)
3522{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003523 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003524 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 .old_dir = NFS_FH(old_dir),
3526 .new_dir = NFS_FH(new_dir),
3527 .old_name = old_name,
3528 .new_name = new_name,
3529 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003530 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003531 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003532 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 struct rpc_message msg = {
3534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3535 .rpc_argp = &arg,
3536 .rpc_resp = &res,
3537 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003538 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003539
Bryan Schumaker7c513052011-03-24 17:12:24 +00003540 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 if (!status) {
3542 update_changeattr(old_dir, &res.old_cinfo);
3543 update_changeattr(new_dir, &res.new_cinfo);
3544 }
3545 return status;
3546}
3547
3548static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3549 struct inode *new_dir, struct qstr *new_name)
3550{
3551 struct nfs4_exception exception = { };
3552 int err;
3553 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003554 err = _nfs4_proc_rename(old_dir, old_name,
3555 new_dir, new_name);
3556 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3557 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 &exception);
3559 } while (exception.retry);
3560 return err;
3561}
3562
3563static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3564{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003565 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 struct nfs4_link_arg arg = {
3567 .fh = NFS_FH(inode),
3568 .dir_fh = NFS_FH(dir),
3569 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003570 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003572 struct nfs4_link_res res = {
3573 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003574 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003575 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 struct rpc_message msg = {
3577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3578 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003579 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003581 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
Trond Myklebust136f2622010-04-16 16:22:49 -04003583 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003584 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003585 goto out;
3586
David Quigley14c43f72013-05-22 12:50:43 -04003587 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3588 if (IS_ERR(res.label)) {
3589 status = PTR_ERR(res.label);
3590 goto out;
3591 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003592 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003593
Bryan Schumaker7c513052011-03-24 17:12:24 +00003594 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003595 if (!status) {
3596 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 status = nfs_post_op_update_inode(inode, res.fattr);
3598 if (!status)
3599 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003600 }
David Quigley14c43f72013-05-22 12:50:43 -04003601
3602
3603 nfs4_label_free(res.label);
3604
Trond Myklebust136f2622010-04-16 16:22:49 -04003605out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003606 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return status;
3608}
3609
3610static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3611{
3612 struct nfs4_exception exception = { };
3613 int err;
3614 do {
3615 err = nfs4_handle_exception(NFS_SERVER(inode),
3616 _nfs4_proc_link(inode, dir, name),
3617 &exception);
3618 } while (exception.retry);
3619 return err;
3620}
3621
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003622struct nfs4_createdata {
3623 struct rpc_message msg;
3624 struct nfs4_create_arg arg;
3625 struct nfs4_create_res res;
3626 struct nfs_fh fh;
3627 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003628 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003629};
3630
3631static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3632 struct qstr *name, struct iattr *sattr, u32 ftype)
3633{
3634 struct nfs4_createdata *data;
3635
3636 data = kzalloc(sizeof(*data), GFP_KERNEL);
3637 if (data != NULL) {
3638 struct nfs_server *server = NFS_SERVER(dir);
3639
David Quigley14c43f72013-05-22 12:50:43 -04003640 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3641 if (IS_ERR(data->label))
3642 goto out_free;
3643
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003644 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3645 data->msg.rpc_argp = &data->arg;
3646 data->msg.rpc_resp = &data->res;
3647 data->arg.dir_fh = NFS_FH(dir);
3648 data->arg.server = server;
3649 data->arg.name = name;
3650 data->arg.attrs = sattr;
3651 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003652 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003653 data->res.server = server;
3654 data->res.fh = &data->fh;
3655 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003656 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003657 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003658 }
3659 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003660out_free:
3661 kfree(data);
3662 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663}
3664
3665static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3666{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003667 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003668 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003669 if (status == 0) {
3670 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003671 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003672 }
3673 return status;
3674}
3675
3676static void nfs4_free_createdata(struct nfs4_createdata *data)
3677{
David Quigley14c43f72013-05-22 12:50:43 -04003678 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 kfree(data);
3680}
3681
Chuck Lever4f390c12006-08-22 20:06:22 -04003682static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003683 struct page *page, unsigned int len, struct iattr *sattr,
3684 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686 struct nfs4_createdata *data;
3687 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Chuck Lever94a6d752006-08-22 20:06:23 -04003689 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003690 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003691
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003692 status = -ENOMEM;
3693 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3694 if (data == NULL)
3695 goto out;
3696
3697 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3698 data->arg.u.symlink.pages = &page;
3699 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003700 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003702 status = nfs4_do_create(dir, dentry, data);
3703
3704 nfs4_free_createdata(data);
3705out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return status;
3707}
3708
Chuck Lever4f390c12006-08-22 20:06:22 -04003709static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003710 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003713 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003715
3716 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003719 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3720 trace_nfs4_symlink(dir, &dentry->d_name, err);
3721 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 &exception);
3723 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003724
3725 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return err;
3727}
3728
3729static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003730 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003732 struct nfs4_createdata *data;
3733 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003735 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3736 if (data == NULL)
3737 goto out;
3738
David Quigley1775fd32013-05-22 12:50:42 -04003739 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003740 status = nfs4_do_create(dir, dentry, data);
3741
3742 nfs4_free_createdata(data);
3743out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return status;
3745}
3746
3747static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3748 struct iattr *sattr)
3749{
3750 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003751 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003753
David Quigleyaa9c2662013-05-22 12:50:44 -04003754 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3755
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003756 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003758 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3759 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3760 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 &exception);
3762 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003763 nfs4_label_release_security(label);
3764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return err;
3766}
3767
3768static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003769 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
3771 struct inode *dir = dentry->d_inode;
3772 struct nfs4_readdir_arg args = {
3773 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003774 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 .pgbase = 0,
3776 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003777 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003778 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 };
3780 struct nfs4_readdir_res res;
3781 struct rpc_message msg = {
3782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3783 .rpc_argp = &args,
3784 .rpc_resp = &res,
3785 .rpc_cred = cred,
3786 };
3787 int status;
3788
Al Viro6de14722013-09-16 10:53:17 -04003789 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3790 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003791 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003792 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003794 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 -05003795 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003796 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003797 status += args.pgbase;
3798 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003799
3800 nfs_invalidate_atime(dir);
3801
Harvey Harrison3110ff82008-05-02 13:42:44 -07003802 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return status;
3804}
3805
3806static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003807 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
3809 struct nfs4_exception exception = { };
3810 int err;
3811 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003812 err = _nfs4_proc_readdir(dentry, cred, cookie,
3813 pages, count, plus);
3814 trace_nfs4_readdir(dentry->d_inode, err);
3815 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 &exception);
3817 } while (exception.retry);
3818 return err;
3819}
3820
3821static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003822 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003824 struct nfs4_createdata *data;
3825 int mode = sattr->ia_mode;
3826 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003828 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3829 if (data == NULL)
3830 goto out;
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003833 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003835 data->arg.ftype = NF4BLK;
3836 data->arg.u.device.specdata1 = MAJOR(rdev);
3837 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 }
3839 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003840 data->arg.ftype = NF4CHR;
3841 data->arg.u.device.specdata1 = MAJOR(rdev);
3842 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003843 } else if (!S_ISSOCK(mode)) {
3844 status = -EINVAL;
3845 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
David Quigley1775fd32013-05-22 12:50:42 -04003847
David Quigleyaa9c2662013-05-22 12:50:44 -04003848 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003849 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003850out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003851 nfs4_free_createdata(data);
3852out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 return status;
3854}
3855
3856static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3857 struct iattr *sattr, dev_t rdev)
3858{
3859 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003860 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003862
David Quigleyaa9c2662013-05-22 12:50:44 -04003863 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3864
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003865 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003867 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3868 trace_nfs4_mknod(dir, &dentry->d_name, err);
3869 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 &exception);
3871 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003872
3873 nfs4_label_release_security(label);
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return err;
3876}
3877
3878static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3879 struct nfs_fsstat *fsstat)
3880{
3881 struct nfs4_statfs_arg args = {
3882 .fh = fhandle,
3883 .bitmask = server->attr_bitmask,
3884 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003885 struct nfs4_statfs_res res = {
3886 .fsstat = fsstat,
3887 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 struct rpc_message msg = {
3889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3890 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003891 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 };
3893
Trond Myklebust0e574af2005-10-27 22:12:38 -04003894 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003895 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896}
3897
3898static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3899{
3900 struct nfs4_exception exception = { };
3901 int err;
3902 do {
3903 err = nfs4_handle_exception(server,
3904 _nfs4_proc_statfs(server, fhandle, fsstat),
3905 &exception);
3906 } while (exception.retry);
3907 return err;
3908}
3909
3910static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3911 struct nfs_fsinfo *fsinfo)
3912{
3913 struct nfs4_fsinfo_arg args = {
3914 .fh = fhandle,
3915 .bitmask = server->attr_bitmask,
3916 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003917 struct nfs4_fsinfo_res res = {
3918 .fsinfo = fsinfo,
3919 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 struct rpc_message msg = {
3921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3922 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003923 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 };
3925
Bryan Schumaker7c513052011-03-24 17:12:24 +00003926 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927}
3928
3929static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3930{
3931 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003932 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 int err;
3934
3935 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003936 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003937 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003938 if (err == 0) {
3939 struct nfs_client *clp = server->nfs_client;
3940
3941 spin_lock(&clp->cl_lock);
3942 clp->cl_lease_time = fsinfo->lease_time * HZ;
3943 clp->cl_last_renewal = now;
3944 spin_unlock(&clp->cl_lock);
3945 break;
3946 }
3947 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 } while (exception.retry);
3949 return err;
3950}
3951
3952static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3953{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003954 int error;
3955
Trond Myklebust0e574af2005-10-27 22:12:38 -04003956 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003957 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003958 if (error == 0) {
3959 /* block layout checks this! */
3960 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003961 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003962 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003963
3964 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
3967static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3968 struct nfs_pathconf *pathconf)
3969{
3970 struct nfs4_pathconf_arg args = {
3971 .fh = fhandle,
3972 .bitmask = server->attr_bitmask,
3973 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003974 struct nfs4_pathconf_res res = {
3975 .pathconf = pathconf,
3976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 struct rpc_message msg = {
3978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3979 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003980 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 };
3982
3983 /* None of the pathconf attributes are mandatory to implement */
3984 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3985 memset(pathconf, 0, sizeof(*pathconf));
3986 return 0;
3987 }
3988
Trond Myklebust0e574af2005-10-27 22:12:38 -04003989 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
3993static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3994 struct nfs_pathconf *pathconf)
3995{
3996 struct nfs4_exception exception = { };
3997 int err;
3998
3999 do {
4000 err = nfs4_handle_exception(server,
4001 _nfs4_proc_pathconf(server, fhandle, pathconf),
4002 &exception);
4003 } while (exception.retry);
4004 return err;
4005}
4006
Trond Myklebust5521abf2013-03-16 20:54:34 -04004007int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004008 const struct nfs_open_context *ctx,
4009 const struct nfs_lock_context *l_ctx,
4010 fmode_t fmode)
4011{
4012 const struct nfs_lockowner *lockowner = NULL;
4013
4014 if (l_ctx != NULL)
4015 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004016 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004017}
4018EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4019
Trond Myklebust5521abf2013-03-16 20:54:34 -04004020static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4021 const struct nfs_open_context *ctx,
4022 const struct nfs_lock_context *l_ctx,
4023 fmode_t fmode)
4024{
4025 nfs4_stateid current_stateid;
4026
4027 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
4028 return false;
4029 return nfs4_stateid_match(stateid, &current_stateid);
4030}
4031
4032static bool nfs4_error_stateid_expired(int err)
4033{
4034 switch (err) {
4035 case -NFS4ERR_DELEG_REVOKED:
4036 case -NFS4ERR_ADMIN_REVOKED:
4037 case -NFS4ERR_BAD_STATEID:
4038 case -NFS4ERR_STALE_STATEID:
4039 case -NFS4ERR_OLD_STATEID:
4040 case -NFS4ERR_OPENMODE:
4041 case -NFS4ERR_EXPIRED:
4042 return true;
4043 }
4044 return false;
4045}
4046
Benny Halevyd20581a2011-05-22 19:52:03 +03004047void __nfs4_read_done_cb(struct nfs_read_data *data)
4048{
Fred Isamancd841602012-04-20 14:47:44 -04004049 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004050}
4051
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004052static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
Fred Isamancd841602012-04-20 14:47:44 -04004054 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004056 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004057 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004058 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004059 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004061
Benny Halevyd20581a2011-05-22 19:52:03 +03004062 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004064 renew_lease(server, data->timestamp);
4065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
Trond Myklebust5521abf2013-03-16 20:54:34 -04004068static bool nfs4_read_stateid_changed(struct rpc_task *task,
4069 struct nfs_readargs *args)
4070{
4071
4072 if (!nfs4_error_stateid_expired(task->tk_status) ||
4073 nfs4_stateid_is_current(&args->stateid,
4074 args->context,
4075 args->lock_context,
4076 FMODE_READ))
4077 return false;
4078 rpc_restart_call_prepare(task);
4079 return true;
4080}
4081
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004082static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4083{
4084
4085 dprintk("--> %s\n", __func__);
4086
4087 if (!nfs4_sequence_done(task, &data->res.seq_res))
4088 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004089 if (nfs4_read_stateid_changed(task, &data->args))
4090 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004091 return data->read_done_cb ? data->read_done_cb(task, data) :
4092 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004093}
4094
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004095static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004098 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004100 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101}
4102
NeilBrownef1820f2013-09-04 17:04:49 +10004103static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004104{
Trond Myklebust9b206142013-03-17 15:52:00 -04004105 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004106 &data->args.seq_args,
4107 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004108 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004109 return 0;
4110 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4111 data->args.lock_context, FMODE_READ) == -EIO)
4112 return -EIO;
4113 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4114 return -EIO;
4115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Fred Isamanb029bc92011-03-03 15:13:42 +00004118static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Fred Isamancd841602012-04-20 14:47:44 -04004120 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004122 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004123 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004124 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004125 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004127 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004129 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004130 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132}
4133
Trond Myklebust5521abf2013-03-16 20:54:34 -04004134static bool nfs4_write_stateid_changed(struct rpc_task *task,
4135 struct nfs_writeargs *args)
4136{
4137
4138 if (!nfs4_error_stateid_expired(task->tk_status) ||
4139 nfs4_stateid_is_current(&args->stateid,
4140 args->context,
4141 args->lock_context,
4142 FMODE_WRITE))
4143 return false;
4144 rpc_restart_call_prepare(task);
4145 return true;
4146}
4147
Fred Isamanb029bc92011-03-03 15:13:42 +00004148static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4149{
4150 if (!nfs4_sequence_done(task, &data->res.seq_res))
4151 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004152 if (nfs4_write_stateid_changed(task, &data->args))
4153 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004154 return data->write_done_cb ? data->write_done_cb(task, data) :
4155 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004156}
4157
Trond Myklebust5a37f852012-04-28 14:55:16 -04004158static
4159bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004160{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004161 const struct nfs_pgio_header *hdr = data->header;
4162
4163 /* Don't request attributes for pNFS or O_DIRECT writes */
4164 if (data->ds_clp != NULL || hdr->dreq != NULL)
4165 return false;
4166 /* Otherwise, request attributes if and only if we don't hold
4167 * a delegation
4168 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004169 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004170}
Fred Isamana69aef12011-03-03 15:13:47 +00004171
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004172static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173{
Fred Isamancd841602012-04-20 14:47:44 -04004174 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004175
Trond Myklebust5a37f852012-04-28 14:55:16 -04004176 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004177 data->args.bitmask = NULL;
4178 data->res.fattr = NULL;
4179 } else
4180 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004181
Fred Isamanb029bc92011-03-03 15:13:42 +00004182 if (!data->write_done_cb)
4183 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004184 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 data->timestamp = jiffies;
4186
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004187 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004188 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
NeilBrownef1820f2013-09-04 17:04:49 +10004191static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004192{
Trond Myklebust9b206142013-03-17 15:52:00 -04004193 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004194 &data->args.seq_args,
4195 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004196 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004197 return 0;
4198 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4199 data->args.lock_context, FMODE_WRITE) == -EIO)
4200 return -EIO;
4201 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4202 return -EIO;
4203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204}
4205
Fred Isaman0b7c0152012-04-20 14:47:39 -04004206static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4207{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004208 nfs4_setup_sequence(NFS_SERVER(data->inode),
4209 &data->args.seq_args,
4210 &data->res.seq_res,
4211 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Fred Isaman0b7c0152012-04-20 14:47:39 -04004214static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004217
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004218 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004219 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004220 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004221 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224}
4225
Fred Isaman0b7c0152012-04-20 14:47:39 -04004226static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004227{
4228 if (!nfs4_sequence_done(task, &data->res.seq_res))
4229 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004230 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004231}
4232
Fred Isaman0b7c0152012-04-20 14:47:39 -04004233static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004235 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004236
Fred Isaman0b7c0152012-04-20 14:47:39 -04004237 if (data->commit_done_cb == NULL)
4238 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004239 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004240 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004241 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004244struct nfs4_renewdata {
4245 struct nfs_client *client;
4246 unsigned long timestamp;
4247};
4248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249/*
4250 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4251 * standalone procedure for queueing an asynchronous RENEW.
4252 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004253static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004254{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004255 struct nfs4_renewdata *data = calldata;
4256 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004257
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004258 if (atomic_read(&clp->cl_count) > 1)
4259 nfs4_schedule_state_renewal(clp);
4260 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004261 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004262}
4263
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004264static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004266 struct nfs4_renewdata *data = calldata;
4267 struct nfs_client *clp = data->client;
4268 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004270 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004271 switch (task->tk_status) {
4272 case 0:
4273 break;
4274 case -NFS4ERR_LEASE_MOVED:
4275 nfs4_schedule_lease_moved_recovery(clp);
4276 break;
4277 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004278 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004279 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4280 return;
4281 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004282 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004283 return;
4284 }
4285 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004287 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004290static const struct rpc_call_ops nfs4_renew_ops = {
4291 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004292 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004293};
4294
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004295static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
4297 struct rpc_message msg = {
4298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4299 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004300 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004302 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004304 if (renew_flags == 0)
4305 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004306 if (!atomic_inc_not_zero(&clp->cl_count))
4307 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004308 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004309 if (data == NULL)
4310 return -ENOMEM;
4311 data->client = clp;
4312 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004313 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004314 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004317static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318{
4319 struct rpc_message msg = {
4320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4321 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004322 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 };
4324 unsigned long now = jiffies;
4325 int status;
4326
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004327 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (status < 0)
4329 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004330 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 return 0;
4332}
4333
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004334static inline int nfs4_server_supports_acls(struct nfs_server *server)
4335{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004336 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004337}
4338
Trond Myklebust21f498c2012-08-24 10:59:25 -04004339/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4340 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004341 * the stack.
4342 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004343#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004344
Neil Hormane9e3d722011-03-04 19:26:03 -05004345static int buf_to_pages_noslab(const void *buf, size_t buflen,
4346 struct page **pages, unsigned int *pgbase)
4347{
4348 struct page *newpage, **spages;
4349 int rc = 0;
4350 size_t len;
4351 spages = pages;
4352
4353 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004354 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004355 newpage = alloc_page(GFP_KERNEL);
4356
4357 if (newpage == NULL)
4358 goto unwind;
4359 memcpy(page_address(newpage), buf, len);
4360 buf += len;
4361 buflen -= len;
4362 *pages++ = newpage;
4363 rc++;
4364 } while (buflen != 0);
4365
4366 return rc;
4367
4368unwind:
4369 for(; rc > 0; rc--)
4370 __free_page(spages[rc-1]);
4371 return -ENOMEM;
4372}
4373
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004374struct nfs4_cached_acl {
4375 int cached;
4376 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004377 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004378};
4379
4380static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004381{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004382 struct nfs_inode *nfsi = NFS_I(inode);
4383
4384 spin_lock(&inode->i_lock);
4385 kfree(nfsi->nfs4_acl);
4386 nfsi->nfs4_acl = acl;
4387 spin_unlock(&inode->i_lock);
4388}
4389
4390static void nfs4_zap_acl_attr(struct inode *inode)
4391{
4392 nfs4_set_cached_acl(inode, NULL);
4393}
4394
4395static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4396{
4397 struct nfs_inode *nfsi = NFS_I(inode);
4398 struct nfs4_cached_acl *acl;
4399 int ret = -ENOENT;
4400
4401 spin_lock(&inode->i_lock);
4402 acl = nfsi->nfs4_acl;
4403 if (acl == NULL)
4404 goto out;
4405 if (buf == NULL) /* user is just asking for length */
4406 goto out_len;
4407 if (acl->cached == 0)
4408 goto out;
4409 ret = -ERANGE; /* see getxattr(2) man page */
4410 if (acl->len > buflen)
4411 goto out;
4412 memcpy(buf, acl->data, acl->len);
4413out_len:
4414 ret = acl->len;
4415out:
4416 spin_unlock(&inode->i_lock);
4417 return ret;
4418}
4419
Sachin Prabhu5794d212012-04-17 14:36:40 +01004420static 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 +00004421{
4422 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004423 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004424
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004425 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004426 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004427 if (acl == NULL)
4428 goto out;
4429 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004430 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004431 } else {
4432 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4433 if (acl == NULL)
4434 goto out;
4435 acl->cached = 0;
4436 }
4437 acl->len = acl_len;
4438out:
4439 nfs4_set_cached_acl(inode, acl);
4440}
4441
Andy Adamsonbf118a32011-12-07 11:55:27 -05004442/*
4443 * The getxattr API returns the required buffer length when called with a
4444 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4445 * the required buf. On a NULL buf, we send a page of data to the server
4446 * guessing that the ACL request can be serviced by a page. If so, we cache
4447 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4448 * the cache. If not so, we throw away the page, and cache the required
4449 * length. The next getxattr call will then produce another round trip to
4450 * the server, this time with the input buf of the required size.
4451 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004452static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004453{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004454 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004455 struct nfs_getaclargs args = {
4456 .fh = NFS_FH(inode),
4457 .acl_pages = pages,
4458 .acl_len = buflen,
4459 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004460 struct nfs_getaclres res = {
4461 .acl_len = buflen,
4462 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004463 struct rpc_message msg = {
4464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4465 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004466 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004467 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004468 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4469 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004470
Andy Adamsonbf118a32011-12-07 11:55:27 -05004471 /* As long as we're doing a round trip to the server anyway,
4472 * let's be prepared for a page of acl data. */
4473 if (npages == 0)
4474 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004475 if (npages > ARRAY_SIZE(pages))
4476 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004477
Andy Adamsonbf118a32011-12-07 11:55:27 -05004478 for (i = 0; i < npages; i++) {
4479 pages[i] = alloc_page(GFP_KERNEL);
4480 if (!pages[i])
4481 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004482 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004483
4484 /* for decoding across pages */
4485 res.acl_scratch = alloc_page(GFP_KERNEL);
4486 if (!res.acl_scratch)
4487 goto out_free;
4488
Andy Adamsonbf118a32011-12-07 11:55:27 -05004489 args.acl_len = npages * PAGE_SIZE;
4490 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004491
Peng Taode040be2012-01-10 22:42:47 +08004492 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004493 __func__, buf, buflen, npages, args.acl_len);
4494 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4495 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004496 if (ret)
4497 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004498
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004499 /* Handle the case where the passed-in buffer is too short */
4500 if (res.acl_flags & NFS4_ACL_TRUNC) {
4501 /* Did the user only issue a request for the acl length? */
4502 if (buf == NULL)
4503 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004504 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004505 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004506 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004507 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004508 if (buf) {
4509 if (res.acl_len > buflen) {
4510 ret = -ERANGE;
4511 goto out_free;
4512 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004513 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004514 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004515out_ok:
4516 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004517out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004518 for (i = 0; i < npages; i++)
4519 if (pages[i])
4520 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004521 if (res.acl_scratch)
4522 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004523 return ret;
4524}
4525
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004526static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4527{
4528 struct nfs4_exception exception = { };
4529 ssize_t ret;
4530 do {
4531 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004532 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004533 if (ret >= 0)
4534 break;
4535 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4536 } while (exception.retry);
4537 return ret;
4538}
4539
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004540static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4541{
4542 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004543 int ret;
4544
4545 if (!nfs4_server_supports_acls(server))
4546 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004547 ret = nfs_revalidate_inode(server, inode);
4548 if (ret < 0)
4549 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004550 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4551 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004552 ret = nfs4_read_cached_acl(inode, buf, buflen);
4553 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004554 /* -ENOENT is returned if there is no ACL or if there is an ACL
4555 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004556 return ret;
4557 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004558}
4559
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004560static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004561{
4562 struct nfs_server *server = NFS_SERVER(inode);
4563 struct page *pages[NFS4ACL_MAXPAGES];
4564 struct nfs_setaclargs arg = {
4565 .fh = NFS_FH(inode),
4566 .acl_pages = pages,
4567 .acl_len = buflen,
4568 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004569 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004570 struct rpc_message msg = {
4571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4572 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004573 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004574 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004575 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004576 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004577
4578 if (!nfs4_server_supports_acls(server))
4579 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004580 if (npages > ARRAY_SIZE(pages))
4581 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004582 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4583 if (i < 0)
4584 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004585 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004586 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004587
4588 /*
4589 * Free each page after tx, so the only ref left is
4590 * held by the network stack
4591 */
4592 for (; i > 0; i--)
4593 put_page(pages[i-1]);
4594
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004595 /*
4596 * Acl update can result in inode attribute update.
4597 * so mark the attribute cache invalid.
4598 */
4599 spin_lock(&inode->i_lock);
4600 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4601 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004602 nfs_access_zap_cache(inode);
4603 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004604 return ret;
4605}
4606
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004607static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4608{
4609 struct nfs4_exception exception = { };
4610 int err;
4611 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004612 err = __nfs4_proc_set_acl(inode, buf, buflen);
4613 trace_nfs4_set_acl(inode, err);
4614 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004615 &exception);
4616 } while (exception.retry);
4617 return err;
4618}
4619
David Quigleyaa9c2662013-05-22 12:50:44 -04004620#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4621static int _nfs4_get_security_label(struct inode *inode, void *buf,
4622 size_t buflen)
4623{
4624 struct nfs_server *server = NFS_SERVER(inode);
4625 struct nfs_fattr fattr;
4626 struct nfs4_label label = {0, 0, buflen, buf};
4627
4628 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004629 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004630 .fh = NFS_FH(inode),
4631 .bitmask = bitmask,
4632 };
4633 struct nfs4_getattr_res res = {
4634 .fattr = &fattr,
4635 .label = &label,
4636 .server = server,
4637 };
4638 struct rpc_message msg = {
4639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004640 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004641 .rpc_resp = &res,
4642 };
4643 int ret;
4644
4645 nfs_fattr_init(&fattr);
4646
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004647 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004648 if (ret)
4649 return ret;
4650 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4651 return -ENOENT;
4652 if (buflen < label.len)
4653 return -ERANGE;
4654 return 0;
4655}
4656
4657static int nfs4_get_security_label(struct inode *inode, void *buf,
4658 size_t buflen)
4659{
4660 struct nfs4_exception exception = { };
4661 int err;
4662
4663 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4664 return -EOPNOTSUPP;
4665
4666 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004667 err = _nfs4_get_security_label(inode, buf, buflen);
4668 trace_nfs4_get_security_label(inode, err);
4669 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004670 &exception);
4671 } while (exception.retry);
4672 return err;
4673}
4674
4675static int _nfs4_do_set_security_label(struct inode *inode,
4676 struct nfs4_label *ilabel,
4677 struct nfs_fattr *fattr,
4678 struct nfs4_label *olabel)
4679{
4680
4681 struct iattr sattr = {0};
4682 struct nfs_server *server = NFS_SERVER(inode);
4683 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004684 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004685 .fh = NFS_FH(inode),
4686 .iap = &sattr,
4687 .server = server,
4688 .bitmask = bitmask,
4689 .label = ilabel,
4690 };
4691 struct nfs_setattrres res = {
4692 .fattr = fattr,
4693 .label = olabel,
4694 .server = server,
4695 };
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004698 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004699 .rpc_resp = &res,
4700 };
4701 int status;
4702
Jeff Layton12207f62013-11-01 10:49:32 -04004703 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004704
Jeff Layton12207f62013-11-01 10:49:32 -04004705 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004706 if (status)
4707 dprintk("%s failed: %d\n", __func__, status);
4708
4709 return status;
4710}
4711
4712static int nfs4_do_set_security_label(struct inode *inode,
4713 struct nfs4_label *ilabel,
4714 struct nfs_fattr *fattr,
4715 struct nfs4_label *olabel)
4716{
4717 struct nfs4_exception exception = { };
4718 int err;
4719
4720 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004721 err = _nfs4_do_set_security_label(inode, ilabel,
4722 fattr, olabel);
4723 trace_nfs4_set_security_label(inode, err);
4724 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004725 &exception);
4726 } while (exception.retry);
4727 return err;
4728}
4729
4730static int
4731nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4732{
4733 struct nfs4_label ilabel, *olabel = NULL;
4734 struct nfs_fattr fattr;
4735 struct rpc_cred *cred;
4736 struct inode *inode = dentry->d_inode;
4737 int status;
4738
4739 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4740 return -EOPNOTSUPP;
4741
4742 nfs_fattr_init(&fattr);
4743
4744 ilabel.pi = 0;
4745 ilabel.lfs = 0;
4746 ilabel.label = (char *)buf;
4747 ilabel.len = buflen;
4748
4749 cred = rpc_lookup_cred();
4750 if (IS_ERR(cred))
4751 return PTR_ERR(cred);
4752
4753 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4754 if (IS_ERR(olabel)) {
4755 status = -PTR_ERR(olabel);
4756 goto out;
4757 }
4758
4759 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4760 if (status == 0)
4761 nfs_setsecurity(inode, &fattr, olabel);
4762
4763 nfs4_label_free(olabel);
4764out:
4765 put_rpccred(cred);
4766 return status;
4767}
4768#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4769
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004772nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004774 struct nfs_client *clp = server->nfs_client;
4775
4776 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 return 0;
4778 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004779 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004780 case -NFS4ERR_ADMIN_REVOKED:
4781 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004782 if (state == NULL)
4783 break;
4784 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004785 case -NFS4ERR_OPENMODE:
4786 if (state == NULL)
4787 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004788 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004789 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004790 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004791 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004792 if (state != NULL) {
4793 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004794 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004797 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004798 nfs4_schedule_lease_recovery(clp);
4799 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004800 case -NFS4ERR_MOVED:
4801 if (nfs4_schedule_migration_recovery(server) < 0)
4802 goto recovery_failed;
4803 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004804 case -NFS4ERR_LEASE_MOVED:
4805 nfs4_schedule_lease_moved_recovery(clp);
4806 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004807#if defined(CONFIG_NFS_V4_1)
4808 case -NFS4ERR_BADSESSION:
4809 case -NFS4ERR_BADSLOT:
4810 case -NFS4ERR_BAD_HIGH_SLOT:
4811 case -NFS4ERR_DEADSESSION:
4812 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4813 case -NFS4ERR_SEQ_FALSE_RETRY:
4814 case -NFS4ERR_SEQ_MISORDERED:
4815 dprintk("%s ERROR %d, Reset session\n", __func__,
4816 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004817 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004818 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004819#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004821 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004822 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004824 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004826 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 }
4828 task->tk_status = nfs4_map_errors(task->tk_status);
4829 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004830recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004831 task->tk_status = -EIO;
4832 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004833wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004834 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004835 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4836 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004837 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4838 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004839restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004840 task->tk_status = 0;
4841 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842}
4843
Chuck Leverf0920752012-05-21 22:45:41 -04004844static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4845 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004846{
4847 __be32 verf[2];
4848
Chuck Lever2c820d92012-05-21 22:45:33 -04004849 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4850 /* An impossible timestamp guarantees this value
4851 * will never match a generated boot time. */
4852 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004853 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004854 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004855 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004856 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4857 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004858 }
Chuck Levercd937102012-03-02 17:14:31 -05004859 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4860}
4861
Chuck Levere984a552012-09-14 17:24:21 -04004862static unsigned int
4863nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4864 char *buf, size_t len)
4865{
4866 unsigned int result;
4867
4868 rcu_read_lock();
4869 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4870 clp->cl_ipaddr,
4871 rpc_peeraddr2str(clp->cl_rpcclient,
4872 RPC_DISPLAY_ADDR),
4873 rpc_peeraddr2str(clp->cl_rpcclient,
4874 RPC_DISPLAY_PROTO));
4875 rcu_read_unlock();
4876 return result;
4877}
4878
4879static unsigned int
4880nfs4_init_uniform_client_string(const struct nfs_client *clp,
4881 char *buf, size_t len)
4882{
Trond Myklebust55b59292013-07-24 16:36:35 -04004883 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004884
4885 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004886 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4887 clp->rpc_ops->version,
4888 clp->cl_minorversion,
4889 nfs4_client_id_uniquifier,
4890 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004891 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4892 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004893 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004894}
4895
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004896/**
4897 * nfs4_proc_setclientid - Negotiate client ID
4898 * @clp: state data structure
4899 * @program: RPC program for NFSv4 callback service
4900 * @port: IP port number for NFS4 callback service
4901 * @cred: RPC credential to use for this call
4902 * @res: where to place the result
4903 *
4904 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4905 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004906int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4907 unsigned short port, struct rpc_cred *cred,
4908 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909{
4910 nfs4_verifier sc_verifier;
4911 struct nfs4_setclientid setclientid = {
4912 .sc_verifier = &sc_verifier,
4913 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004914 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 };
4916 struct rpc_message msg = {
4917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4918 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004919 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004920 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004922 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
Chuck Leverde734832012-07-11 16:30:50 -04004924 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004925 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004926 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4927 setclientid.sc_name_len =
4928 nfs4_init_uniform_client_string(clp,
4929 setclientid.sc_name,
4930 sizeof(setclientid.sc_name));
4931 else
4932 setclientid.sc_name_len =
4933 nfs4_init_nonuniform_client_string(clp,
4934 setclientid.sc_name,
4935 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004936 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004937 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004938 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004939 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004940 rpc_peeraddr2str(clp->cl_rpcclient,
4941 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004942 rcu_read_unlock();
4943 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004944 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 clp->cl_ipaddr, port >> 8, port & 255);
4946
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004947 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4948 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4949 setclientid.sc_name_len, setclientid.sc_name);
4950 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004951 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004952 dprintk("NFS reply setclientid: %d\n", status);
4953 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004956/**
4957 * nfs4_proc_setclientid_confirm - Confirm client ID
4958 * @clp: state data structure
4959 * @res: result of a previous SETCLIENTID
4960 * @cred: RPC credential to use for this call
4961 *
4962 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4963 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004964int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004965 struct nfs4_setclientid_res *arg,
4966 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004970 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004971 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 int status;
4974
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004975 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4976 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4977 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004978 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004979 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004980 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return status;
4982}
4983
Trond Myklebustfe650402006-01-03 09:55:18 +01004984struct nfs4_delegreturndata {
4985 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004986 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004987 struct nfs_fh fh;
4988 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004989 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004990 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004991 int rpc_status;
4992};
4993
Trond Myklebustfe650402006-01-03 09:55:18 +01004994static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4995{
4996 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004997
Trond Myklebust14516c32010-07-31 14:29:06 -04004998 if (!nfs4_sequence_done(task, &data->res.seq_res))
4999 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005000
Trond Myklebustca8acf82013-08-13 10:36:56 -04005001 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005002 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005003 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005004 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005005 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005006 case -NFS4ERR_ADMIN_REVOKED:
5007 case -NFS4ERR_DELEG_REVOKED:
5008 case -NFS4ERR_BAD_STATEID:
5009 case -NFS4ERR_OLD_STATEID:
5010 case -NFS4ERR_STALE_STATEID:
5011 case -NFS4ERR_EXPIRED:
5012 task->tk_status = 0;
5013 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005014 default:
5015 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5016 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005017 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005018 return;
5019 }
5020 }
5021 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005022}
5023
5024static void nfs4_delegreturn_release(void *calldata)
5025{
Trond Myklebustfe650402006-01-03 09:55:18 +01005026 kfree(calldata);
5027}
5028
Andy Adamson938e1012009-04-01 09:22:28 -04005029static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5030{
5031 struct nfs4_delegreturndata *d_data;
5032
5033 d_data = (struct nfs4_delegreturndata *)data;
5034
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005035 nfs4_setup_sequence(d_data->res.server,
5036 &d_data->args.seq_args,
5037 &d_data->res.seq_res,
5038 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005039}
Andy Adamson938e1012009-04-01 09:22:28 -04005040
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005041static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005042 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005043 .rpc_call_done = nfs4_delegreturn_done,
5044 .rpc_release = nfs4_delegreturn_release,
5045};
5046
Trond Myklebuste6f81072008-01-24 18:14:34 -05005047static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005048{
5049 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005050 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005051 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5054 .rpc_cred = cred,
5055 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005056 struct rpc_task_setup task_setup_data = {
5057 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005058 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005059 .callback_ops = &nfs4_delegreturn_ops,
5060 .flags = RPC_TASK_ASYNC,
5061 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005062 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005063
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005064 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005065 if (data == NULL)
5066 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005068 data->args.fhandle = &data->fh;
5069 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005070 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005071 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005072 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005073 data->res.fattr = &data->fattr;
5074 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005075 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005076 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005077 data->rpc_status = 0;
5078
Trond Myklebustc970aa82007-07-14 15:39:59 -04005079 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005080 msg.rpc_argp = &data->args;
5081 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005082 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005083 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005084 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005085 if (!issync)
5086 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005087 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005088 if (status != 0)
5089 goto out;
5090 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005091 if (status == 0)
5092 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5093 else
5094 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005095out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005096 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005097 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098}
5099
Trond Myklebuste6f81072008-01-24 18:14:34 -05005100int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102 struct nfs_server *server = NFS_SERVER(inode);
5103 struct nfs4_exception exception = { };
5104 int err;
5105 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005106 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005107 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 switch (err) {
5109 case -NFS4ERR_STALE_STATEID:
5110 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 case 0:
5112 return 0;
5113 }
5114 err = nfs4_handle_exception(server, err, &exception);
5115 } while (exception.retry);
5116 return err;
5117}
5118
5119#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5120#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5121
5122/*
5123 * sleep, with exponential backoff, and retry the LOCK operation.
5124 */
5125static unsigned long
5126nfs4_set_lock_task_retry(unsigned long timeout)
5127{
Colin Cross416ad3c2013-05-06 23:50:06 +00005128 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 timeout <<= 1;
5130 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5131 return NFS4_LOCK_MAXTIMEOUT;
5132 return timeout;
5133}
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5136{
5137 struct inode *inode = state->inode;
5138 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005139 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005140 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005142 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005144 struct nfs_lockt_res res = {
5145 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 };
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5149 .rpc_argp = &arg,
5150 .rpc_resp = &res,
5151 .rpc_cred = state->owner->so_cred,
5152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 struct nfs4_lock_state *lsp;
5154 int status;
5155
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005157 status = nfs4_set_lock_state(state, request);
5158 if (status != 0)
5159 goto out;
5160 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005161 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005162 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005163 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005164 switch (status) {
5165 case 0:
5166 request->fl_type = F_UNLCK;
5167 break;
5168 case -NFS4ERR_DENIED:
5169 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005171 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005172 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return status;
5175}
5176
5177static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5178{
5179 struct nfs4_exception exception = { };
5180 int err;
5181
5182 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005183 err = _nfs4_proc_getlk(state, cmd, request);
5184 trace_nfs4_get_lock(request, state, cmd, err);
5185 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 &exception);
5187 } while (exception.retry);
5188 return err;
5189}
5190
5191static int do_vfs_lock(struct file *file, struct file_lock *fl)
5192{
5193 int res = 0;
5194 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5195 case FL_POSIX:
5196 res = posix_lock_file_wait(file, fl);
5197 break;
5198 case FL_FLOCK:
5199 res = flock_lock_file_wait(file, fl);
5200 break;
5201 default:
5202 BUG();
5203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return res;
5205}
5206
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005207struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005208 struct nfs_locku_args arg;
5209 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210 struct nfs4_lock_state *lsp;
5211 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005212 struct file_lock fl;
5213 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005214 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005215};
5216
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005217static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5218 struct nfs_open_context *ctx,
5219 struct nfs4_lock_state *lsp,
5220 struct nfs_seqid *seqid)
5221{
5222 struct nfs4_unlockdata *p;
5223 struct inode *inode = lsp->ls_state->inode;
5224
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005225 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005226 if (p == NULL)
5227 return NULL;
5228 p->arg.fh = NFS_FH(inode);
5229 p->arg.fl = &p->fl;
5230 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005231 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005232 p->arg.stateid = &lsp->ls_stateid;
5233 p->lsp = lsp;
5234 atomic_inc(&lsp->ls_count);
5235 /* Ensure we don't close file until we're done freeing locks! */
5236 p->ctx = get_nfs_open_context(ctx);
5237 memcpy(&p->fl, fl, sizeof(p->fl));
5238 p->server = NFS_SERVER(inode);
5239 return p;
5240}
5241
Trond Myklebust06f814a2006-01-03 09:55:07 +01005242static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005243{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005244 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005245 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005246 nfs4_put_lock_state(calldata->lsp);
5247 put_nfs_open_context(calldata->ctx);
5248 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005249}
5250
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005251static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005252{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005253 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005254
Trond Myklebust14516c32010-07-31 14:29:06 -04005255 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5256 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005257 switch (task->tk_status) {
5258 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005259 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5260 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005261 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005262 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005263 case -NFS4ERR_BAD_STATEID:
5264 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005265 case -NFS4ERR_STALE_STATEID:
5266 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005267 break;
5268 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005269 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005270 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005271 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005272 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005273}
5274
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005275static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005276{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005277 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005279 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005280 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005281 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005282 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005283 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005284 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005285 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005286 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005287 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005288 &calldata->res.seq_res,
5289 task) != 0)
5290 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005291 return;
5292out_no_action:
5293 task->tk_action = NULL;
5294out_wait:
5295 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005298static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005299 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005300 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005301 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005302};
5303
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005304static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5305 struct nfs_open_context *ctx,
5306 struct nfs4_lock_state *lsp,
5307 struct nfs_seqid *seqid)
5308{
5309 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5312 .rpc_cred = ctx->cred,
5313 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005314 struct rpc_task_setup task_setup_data = {
5315 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005316 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005317 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005318 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005319 .flags = RPC_TASK_ASYNC,
5320 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005321
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005322 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5323 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5324
Frank Filz137d6ac2007-07-09 15:32:29 -07005325 /* Ensure this is an unlock - when canceling a lock, the
5326 * canceled lock is passed in, and it won't be an unlock.
5327 */
5328 fl->fl_type = F_UNLCK;
5329
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5331 if (data == NULL) {
5332 nfs_free_seqid(seqid);
5333 return ERR_PTR(-ENOMEM);
5334 }
5335
Chuck Levera9c92d62013-08-09 12:48:18 -04005336 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005337 msg.rpc_argp = &data->arg;
5338 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005339 task_setup_data.callback_data = data;
5340 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341}
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5344{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005345 struct inode *inode = state->inode;
5346 struct nfs4_state_owner *sp = state->owner;
5347 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005348 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005349 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005350 struct rpc_task *task;
5351 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005352 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Trond Myklebust9b073572006-06-29 16:38:34 -04005354 status = nfs4_set_lock_state(state, request);
5355 /* Unlock _before_ we do the RPC call */
5356 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005357 /* Exclude nfs_delegation_claim_locks() */
5358 mutex_lock(&sp->so_delegreturn_mutex);
5359 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005360 down_read(&nfsi->rwsem);
5361 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5362 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005363 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005364 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005365 }
5366 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005367 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005368 if (status != 0)
5369 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005370 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005371 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005372 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5373 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005374 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005375 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005376 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005377 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005378 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005379 status = PTR_ERR(task);
5380 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005381 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005382 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005383 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005384out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005385 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005386 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388}
5389
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390struct nfs4_lockdata {
5391 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005392 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393 struct nfs4_lock_state *lsp;
5394 struct nfs_open_context *ctx;
5395 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005396 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005397 int rpc_status;
5398 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005399 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005400};
5401
5402static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005403 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5404 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405{
5406 struct nfs4_lockdata *p;
5407 struct inode *inode = lsp->ls_state->inode;
5408 struct nfs_server *server = NFS_SERVER(inode);
5409
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005410 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005411 if (p == NULL)
5412 return NULL;
5413
5414 p->arg.fh = NFS_FH(inode);
5415 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005416 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005417 if (p->arg.open_seqid == NULL)
5418 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005419 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005420 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005421 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005422 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005423 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005424 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005425 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005426 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005428 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005429 atomic_inc(&lsp->ls_count);
5430 p->ctx = get_nfs_open_context(ctx);
5431 memcpy(&p->fl, fl, sizeof(p->fl));
5432 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005433out_free_seqid:
5434 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435out_free:
5436 kfree(p);
5437 return NULL;
5438}
5439
5440static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5441{
5442 struct nfs4_lockdata *data = calldata;
5443 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Harvey Harrison3110ff82008-05-02 13:42:44 -07005445 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005446 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005447 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005448 /* Do we need to do an open_to_lock_owner? */
5449 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005450 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005451 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005452 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005453 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005454 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005455 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005456 } else
5457 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005458 if (!nfs4_valid_open_stateid(state)) {
5459 data->rpc_status = -EBADF;
5460 task->tk_action = NULL;
5461 goto out_release_open_seqid;
5462 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005463 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005464 if (nfs4_setup_sequence(data->server,
5465 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005466 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005467 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005468 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005469out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005470 nfs_release_seqid(data->arg.open_seqid);
5471out_release_lock_seqid:
5472 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005473out_wait:
5474 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005475 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005476}
5477
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005478static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5479{
5480 struct nfs4_lockdata *data = calldata;
5481
Harvey Harrison3110ff82008-05-02 13:42:44 -07005482 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005483
Trond Myklebust14516c32010-07-31 14:29:06 -04005484 if (!nfs4_sequence_done(task, &data->res.seq_res))
5485 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005486
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005487 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005488 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005489 if (data->rpc_status == 0)
5490 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5491 else
5492 goto out;
5493 }
5494 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005495 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005496 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005497 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005499out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005500 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005501}
5502
5503static void nfs4_lock_release(void *calldata)
5504{
5505 struct nfs4_lockdata *data = calldata;
5506
Harvey Harrison3110ff82008-05-02 13:42:44 -07005507 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005508 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005509 if (data->cancelled != 0) {
5510 struct rpc_task *task;
5511 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5512 data->arg.lock_seqid);
5513 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005514 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005515 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 } else
5517 nfs_free_seqid(data->arg.lock_seqid);
5518 nfs4_put_lock_state(data->lsp);
5519 put_nfs_open_context(data->ctx);
5520 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005521 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005522}
5523
5524static const struct rpc_call_ops nfs4_lock_ops = {
5525 .rpc_call_prepare = nfs4_lock_prepare,
5526 .rpc_call_done = nfs4_lock_done,
5527 .rpc_release = nfs4_lock_release,
5528};
5529
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005530static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5531{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005532 switch (error) {
5533 case -NFS4ERR_ADMIN_REVOKED:
5534 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005535 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005536 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005537 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005538 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005539 break;
5540 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005541 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005542 case -NFS4ERR_EXPIRED:
5543 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005544 };
5545}
5546
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005547static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005548{
5549 struct nfs4_lockdata *data;
5550 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5553 .rpc_cred = state->owner->so_cred,
5554 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005555 struct rpc_task_setup task_setup_data = {
5556 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005557 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005558 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005559 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005560 .flags = RPC_TASK_ASYNC,
5561 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005562 int ret;
5563
Harvey Harrison3110ff82008-05-02 13:42:44 -07005564 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005565 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005566 fl->fl_u.nfs4_fl.owner,
5567 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005568 if (data == NULL)
5569 return -ENOMEM;
5570 if (IS_SETLKW(cmd))
5571 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005572 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005573 msg.rpc_argp = &data->arg;
5574 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005575 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005576 if (recovery_type > NFS_LOCK_NEW) {
5577 if (recovery_type == NFS_LOCK_RECLAIM)
5578 data->arg.reclaim = NFS_LOCK_RECLAIM;
5579 nfs4_set_sequence_privileged(&data->arg.seq_args);
5580 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005581 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005582 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005583 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005584 ret = nfs4_wait_for_completion_rpc_task(task);
5585 if (ret == 0) {
5586 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005587 if (ret)
5588 nfs4_handle_setlk_error(data->server, data->lsp,
5589 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005590 } else
5591 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005592 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005593 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005594 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
5597static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5598{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005599 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005600 struct nfs4_exception exception = {
5601 .inode = state->inode,
5602 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005603 int err;
5604
5605 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005606 /* Cache the lock if possible... */
5607 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5608 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005609 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005610 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005611 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005612 break;
5613 nfs4_handle_exception(server, err, &exception);
5614 } while (exception.retry);
5615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
5618static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5619{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005620 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005621 struct nfs4_exception exception = {
5622 .inode = state->inode,
5623 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005624 int err;
5625
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005626 err = nfs4_set_lock_state(state, request);
5627 if (err != 0)
5628 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005629 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005630 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5631 return 0;
5632 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005633 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5635 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005637 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005638 switch (err) {
5639 default:
5640 goto out;
5641 case -NFS4ERR_GRACE:
5642 case -NFS4ERR_DELAY:
5643 nfs4_handle_exception(server, err, &exception);
5644 err = 0;
5645 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005646 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005647out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649}
5650
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005651#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005652/**
5653 * nfs41_check_expired_locks - possibly free a lock stateid
5654 *
5655 * @state: NFSv4 state for an inode
5656 *
5657 * Returns NFS_OK if recovery for this stateid is now finished.
5658 * Otherwise a negative NFS4ERR value is returned.
5659 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005660static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005661{
Chuck Levereb64cf92012-07-11 16:30:05 -04005662 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005663 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005664 struct nfs_server *server = NFS_SERVER(state->inode);
5665
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005666 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005667 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005668 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5669
5670 status = nfs41_test_stateid(server,
5671 &lsp->ls_stateid,
5672 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005673 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005674 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005675 /* Free the stateid unless the server
5676 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005677 if (status != -NFS4ERR_BAD_STATEID)
5678 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005679 &lsp->ls_stateid,
5680 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005681 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005682 ret = status;
5683 }
5684 }
5685 };
5686
5687 return ret;
5688}
5689
5690static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5691{
5692 int status = NFS_OK;
5693
5694 if (test_bit(LK_STATE_IN_USE, &state->flags))
5695 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005696 if (status != NFS_OK)
5697 status = nfs4_lock_expired(state, request);
5698 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005699}
5700#endif
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5703{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005704 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005705 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005706 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005707 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005708 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005710 if ((fl_flags & FL_POSIX) &&
5711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5712 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005713 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005714 status = nfs4_set_lock_state(state, request);
5715 if (status != 0)
5716 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005717 request->fl_flags |= FL_ACCESS;
5718 status = do_vfs_lock(request->fl_file, request);
5719 if (status < 0)
5720 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005721 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005722 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005723 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005724 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005725 request->fl_flags = fl_flags & ~FL_SLEEP;
5726 status = do_vfs_lock(request->fl_file, request);
5727 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005728 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005729 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5730 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005731 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005732 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005733 goto out;
5734 down_read(&nfsi->rwsem);
5735 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5736 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005737 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005738 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005739 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005740 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005741 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005742 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5743 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005744out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005745 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005746out:
5747 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 return status;
5749}
5750
5751static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5752{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005753 struct nfs4_exception exception = {
5754 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005755 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005756 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 int err;
5758
5759 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005760 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005761 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005762 if (err == -NFS4ERR_DENIED)
5763 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005765 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 } while (exception.retry);
5767 return err;
5768}
5769
5770static int
5771nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5772{
5773 struct nfs_open_context *ctx;
5774 struct nfs4_state *state;
5775 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5776 int status;
5777
5778 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005779 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 state = ctx->state;
5781
5782 if (request->fl_start < 0 || request->fl_end < 0)
5783 return -EINVAL;
5784
Trond Myklebustd9531262009-07-21 19:22:38 -04005785 if (IS_GETLK(cmd)) {
5786 if (state != NULL)
5787 return nfs4_proc_getlk(state, F_GETLK, request);
5788 return 0;
5789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5792 return -EINVAL;
5793
Trond Myklebustd9531262009-07-21 19:22:38 -04005794 if (request->fl_type == F_UNLCK) {
5795 if (state != NULL)
5796 return nfs4_proc_unlck(state, cmd, request);
5797 return 0;
5798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Trond Myklebustd9531262009-07-21 19:22:38 -04005800 if (state == NULL)
5801 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005802 /*
5803 * Don't rely on the VFS having checked the file open mode,
5804 * since it won't do this for flock() locks.
5805 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005806 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005807 case F_RDLCK:
5808 if (!(filp->f_mode & FMODE_READ))
5809 return -EBADF;
5810 break;
5811 case F_WRLCK:
5812 if (!(filp->f_mode & FMODE_WRITE))
5813 return -EBADF;
5814 }
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 do {
5817 status = nfs4_proc_setlk(state, cmd, request);
5818 if ((status != -EAGAIN) || IS_SETLK(cmd))
5819 break;
5820 timeout = nfs4_set_lock_task_retry(timeout);
5821 status = -ERESTARTSYS;
5822 if (signalled())
5823 break;
5824 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return status;
5826}
5827
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005828int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005829{
5830 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005831 int err;
5832
5833 err = nfs4_set_lock_state(state, fl);
5834 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005835 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005837 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005838}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005839
Trond Myklebustcf470c32012-03-07 13:49:12 -05005840struct nfs_release_lockowner_data {
5841 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005842 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005843 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005844 struct nfs4_sequence_args seq_args;
5845 struct nfs4_sequence_res seq_res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005846 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005847};
5848
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005849static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5850{
5851 struct nfs_release_lockowner_data *data = calldata;
5852 nfs40_setup_sequence(data->server,
5853 &data->seq_args, &data->seq_res, task);
Chuck Lever60ea6812013-10-17 14:13:47 -04005854 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005855}
5856
5857static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5858{
5859 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005860 struct nfs_server *server = data->server;
5861
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005862 nfs40_sequence_done(task, &data->seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005863
5864 switch (task->tk_status) {
5865 case 0:
5866 renew_lease(server, data->timestamp);
5867 break;
5868 case -NFS4ERR_STALE_CLIENTID:
5869 case -NFS4ERR_EXPIRED:
5870 case -NFS4ERR_LEASE_MOVED:
5871 case -NFS4ERR_DELAY:
5872 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5873 rpc_restart_call_prepare(task);
5874 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005875}
5876
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005877static void nfs4_release_lockowner_release(void *calldata)
5878{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005879 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005880 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005881 kfree(calldata);
5882}
5883
Trond Myklebust17280172012-03-11 13:11:00 -04005884static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005885 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5886 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005887 .rpc_release = nfs4_release_lockowner_release,
5888};
5889
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005890static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005891{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005892 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005893 struct rpc_message msg = {
5894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5895 };
5896
5897 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005898 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005899
Trond Myklebustcf470c32012-03-07 13:49:12 -05005900 data = kmalloc(sizeof(*data), GFP_NOFS);
5901 if (!data)
5902 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005903 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005904 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005905 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005906 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5907 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5908 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005909
Trond Myklebustcf470c32012-03-07 13:49:12 -05005910 msg.rpc_argp = &data->args;
5911 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5912 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005913}
5914
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005915#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5916
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005917static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5918 const void *buf, size_t buflen,
5919 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005920{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005921 if (strcmp(key, "") != 0)
5922 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005923
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005924 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005925}
5926
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005927static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5928 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005929{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005930 if (strcmp(key, "") != 0)
5931 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005932
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005933 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005934}
5935
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005936static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5937 size_t list_len, const char *name,
5938 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005939{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005940 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005941
J. Bruce Fields096455a2006-03-20 23:23:42 -05005942 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5943 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005944
5945 if (list && len <= list_len)
5946 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005947 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005948}
5949
David Quigleyc9bccef2013-05-22 12:50:45 -04005950#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5951static inline int nfs4_server_supports_labels(struct nfs_server *server)
5952{
5953 return server->caps & NFS_CAP_SECURITY_LABEL;
5954}
5955
5956static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5957 const void *buf, size_t buflen,
5958 int flags, int type)
5959{
5960 if (security_ismaclabel(key))
5961 return nfs4_set_security_label(dentry, buf, buflen);
5962
5963 return -EOPNOTSUPP;
5964}
5965
5966static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5967 void *buf, size_t buflen, int type)
5968{
5969 if (security_ismaclabel(key))
5970 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5971 return -EOPNOTSUPP;
5972}
5973
5974static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5975 size_t list_len, const char *name,
5976 size_t name_len, int type)
5977{
5978 size_t len = 0;
5979
5980 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5981 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5982 if (list && len <= list_len)
5983 security_inode_listsecurity(dentry->d_inode, list, len);
5984 }
5985 return len;
5986}
5987
5988static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5989 .prefix = XATTR_SECURITY_PREFIX,
5990 .list = nfs4_xattr_list_nfs4_label,
5991 .get = nfs4_xattr_get_nfs4_label,
5992 .set = nfs4_xattr_set_nfs4_label,
5993};
5994#endif
5995
5996
Andy Adamson533eb462011-06-13 18:25:56 -04005997/*
5998 * nfs_fhget will use either the mounted_on_fileid or the fileid
5999 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006000static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6001{
Andy Adamson533eb462011-06-13 18:25:56 -04006002 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6003 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6004 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006005 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006006 return;
6007
6008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6011 fattr->nlink = 2;
6012}
6013
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006014static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6015 const struct qstr *name,
6016 struct nfs4_fs_locations *fs_locations,
6017 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006018{
6019 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006020 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006021 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006022 };
6023 struct nfs4_fs_locations_arg args = {
6024 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006025 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006026 .page = page,
6027 .bitmask = bitmask,
6028 };
Benny Halevy22958462009-04-01 09:22:02 -04006029 struct nfs4_fs_locations_res res = {
6030 .fs_locations = fs_locations,
6031 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006032 struct rpc_message msg = {
6033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6034 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006035 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006036 };
6037 int status;
6038
Harvey Harrison3110ff82008-05-02 13:42:44 -07006039 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006040
6041 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6042 * is not supported */
6043 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6044 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6045 else
6046 bitmask[0] |= FATTR4_WORD0_FILEID;
6047
Trond Myklebustc228fd32007-01-13 02:28:11 -05006048 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006049 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006050 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006051 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006052 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006053 return status;
6054}
6055
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006056int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6057 const struct qstr *name,
6058 struct nfs4_fs_locations *fs_locations,
6059 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006060{
6061 struct nfs4_exception exception = { };
6062 int err;
6063 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006064 err = _nfs4_proc_fs_locations(client, dir, name,
6065 fs_locations, page);
6066 trace_nfs4_get_fs_locations(dir, name, err);
6067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006068 &exception);
6069 } while (exception.retry);
6070 return err;
6071}
6072
Chuck Leverb03d7352013-10-17 14:12:50 -04006073/*
6074 * This operation also signals the server that this client is
6075 * performing migration recovery. The server can stop returning
6076 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6077 * appended to this compound to identify the client ID which is
6078 * performing recovery.
6079 */
6080static int _nfs40_proc_get_locations(struct inode *inode,
6081 struct nfs4_fs_locations *locations,
6082 struct page *page, struct rpc_cred *cred)
6083{
6084 struct nfs_server *server = NFS_SERVER(inode);
6085 struct rpc_clnt *clnt = server->client;
6086 u32 bitmask[2] = {
6087 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6088 };
6089 struct nfs4_fs_locations_arg args = {
6090 .clientid = server->nfs_client->cl_clientid,
6091 .fh = NFS_FH(inode),
6092 .page = page,
6093 .bitmask = bitmask,
6094 .migration = 1, /* skip LOOKUP */
6095 .renew = 1, /* append RENEW */
6096 };
6097 struct nfs4_fs_locations_res res = {
6098 .fs_locations = locations,
6099 .migration = 1,
6100 .renew = 1,
6101 };
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6104 .rpc_argp = &args,
6105 .rpc_resp = &res,
6106 .rpc_cred = cred,
6107 };
6108 unsigned long now = jiffies;
6109 int status;
6110
6111 nfs_fattr_init(&locations->fattr);
6112 locations->server = server;
6113 locations->nlocations = 0;
6114
6115 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6116 nfs4_set_sequence_privileged(&args.seq_args);
6117 status = nfs4_call_sync_sequence(clnt, server, &msg,
6118 &args.seq_args, &res.seq_res);
6119 if (status)
6120 return status;
6121
6122 renew_lease(server, now);
6123 return 0;
6124}
6125
6126#ifdef CONFIG_NFS_V4_1
6127
6128/*
6129 * This operation also signals the server that this client is
6130 * performing migration recovery. The server can stop asserting
6131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6132 * performing this operation is identified in the SEQUENCE
6133 * operation in this compound.
6134 *
6135 * When the client supports GETATTR(fs_locations_info), it can
6136 * be plumbed in here.
6137 */
6138static int _nfs41_proc_get_locations(struct inode *inode,
6139 struct nfs4_fs_locations *locations,
6140 struct page *page, struct rpc_cred *cred)
6141{
6142 struct nfs_server *server = NFS_SERVER(inode);
6143 struct rpc_clnt *clnt = server->client;
6144 u32 bitmask[2] = {
6145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6146 };
6147 struct nfs4_fs_locations_arg args = {
6148 .fh = NFS_FH(inode),
6149 .page = page,
6150 .bitmask = bitmask,
6151 .migration = 1, /* skip LOOKUP */
6152 };
6153 struct nfs4_fs_locations_res res = {
6154 .fs_locations = locations,
6155 .migration = 1,
6156 };
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6159 .rpc_argp = &args,
6160 .rpc_resp = &res,
6161 .rpc_cred = cred,
6162 };
6163 int status;
6164
6165 nfs_fattr_init(&locations->fattr);
6166 locations->server = server;
6167 locations->nlocations = 0;
6168
6169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6170 nfs4_set_sequence_privileged(&args.seq_args);
6171 status = nfs4_call_sync_sequence(clnt, server, &msg,
6172 &args.seq_args, &res.seq_res);
6173 if (status == NFS4_OK &&
6174 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6175 status = -NFS4ERR_LEASE_MOVED;
6176 return status;
6177}
6178
6179#endif /* CONFIG_NFS_V4_1 */
6180
6181/**
6182 * nfs4_proc_get_locations - discover locations for a migrated FSID
6183 * @inode: inode on FSID that is migrating
6184 * @locations: result of query
6185 * @page: buffer
6186 * @cred: credential to use for this operation
6187 *
6188 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6189 * operation failed, or a negative errno if a local error occurred.
6190 *
6191 * On success, "locations" is filled in, but if the server has
6192 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6193 * asserted.
6194 *
6195 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6196 * from this client that require migration recovery.
6197 */
6198int nfs4_proc_get_locations(struct inode *inode,
6199 struct nfs4_fs_locations *locations,
6200 struct page *page, struct rpc_cred *cred)
6201{
6202 struct nfs_server *server = NFS_SERVER(inode);
6203 struct nfs_client *clp = server->nfs_client;
6204 const struct nfs4_mig_recovery_ops *ops =
6205 clp->cl_mvops->mig_recovery_ops;
6206 struct nfs4_exception exception = { };
6207 int status;
6208
6209 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6210 (unsigned long long)server->fsid.major,
6211 (unsigned long long)server->fsid.minor,
6212 clp->cl_hostname);
6213 nfs_display_fhandle(NFS_FH(inode), __func__);
6214
6215 do {
6216 status = ops->get_locations(inode, locations, page, cred);
6217 if (status != -NFS4ERR_DELAY)
6218 break;
6219 nfs4_handle_exception(server, status, &exception);
6220 } while (exception.retry);
6221 return status;
6222}
6223
Chuck Lever44c99932013-10-17 14:13:30 -04006224/*
6225 * This operation also signals the server that this client is
6226 * performing "lease moved" recovery. The server can stop
6227 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6228 * is appended to this compound to identify the client ID which is
6229 * performing recovery.
6230 */
6231static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6232{
6233 struct nfs_server *server = NFS_SERVER(inode);
6234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6235 struct rpc_clnt *clnt = server->client;
6236 struct nfs4_fsid_present_arg args = {
6237 .fh = NFS_FH(inode),
6238 .clientid = clp->cl_clientid,
6239 .renew = 1, /* append RENEW */
6240 };
6241 struct nfs4_fsid_present_res res = {
6242 .renew = 1,
6243 };
6244 struct rpc_message msg = {
6245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6246 .rpc_argp = &args,
6247 .rpc_resp = &res,
6248 .rpc_cred = cred,
6249 };
6250 unsigned long now = jiffies;
6251 int status;
6252
6253 res.fh = nfs_alloc_fhandle();
6254 if (res.fh == NULL)
6255 return -ENOMEM;
6256
6257 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6258 nfs4_set_sequence_privileged(&args.seq_args);
6259 status = nfs4_call_sync_sequence(clnt, server, &msg,
6260 &args.seq_args, &res.seq_res);
6261 nfs_free_fhandle(res.fh);
6262 if (status)
6263 return status;
6264
6265 do_renew_lease(clp, now);
6266 return 0;
6267}
6268
6269#ifdef CONFIG_NFS_V4_1
6270
6271/*
6272 * This operation also signals the server that this client is
6273 * performing "lease moved" recovery. The server can stop asserting
6274 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6275 * this operation is identified in the SEQUENCE operation in this
6276 * compound.
6277 */
6278static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6279{
6280 struct nfs_server *server = NFS_SERVER(inode);
6281 struct rpc_clnt *clnt = server->client;
6282 struct nfs4_fsid_present_arg args = {
6283 .fh = NFS_FH(inode),
6284 };
6285 struct nfs4_fsid_present_res res = {
6286 };
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6289 .rpc_argp = &args,
6290 .rpc_resp = &res,
6291 .rpc_cred = cred,
6292 };
6293 int status;
6294
6295 res.fh = nfs_alloc_fhandle();
6296 if (res.fh == NULL)
6297 return -ENOMEM;
6298
6299 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6300 nfs4_set_sequence_privileged(&args.seq_args);
6301 status = nfs4_call_sync_sequence(clnt, server, &msg,
6302 &args.seq_args, &res.seq_res);
6303 nfs_free_fhandle(res.fh);
6304 if (status == NFS4_OK &&
6305 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6306 status = -NFS4ERR_LEASE_MOVED;
6307 return status;
6308}
6309
6310#endif /* CONFIG_NFS_V4_1 */
6311
6312/**
6313 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6314 * @inode: inode on FSID to check
6315 * @cred: credential to use for this operation
6316 *
6317 * Server indicates whether the FSID is present, moved, or not
6318 * recognized. This operation is necessary to clear a LEASE_MOVED
6319 * condition for this client ID.
6320 *
6321 * Returns NFS4_OK if the FSID is present on this server,
6322 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6323 * NFS4ERR code if some error occurred on the server, or a
6324 * negative errno if a local failure occurred.
6325 */
6326int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6327{
6328 struct nfs_server *server = NFS_SERVER(inode);
6329 struct nfs_client *clp = server->nfs_client;
6330 const struct nfs4_mig_recovery_ops *ops =
6331 clp->cl_mvops->mig_recovery_ops;
6332 struct nfs4_exception exception = { };
6333 int status;
6334
6335 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6336 (unsigned long long)server->fsid.major,
6337 (unsigned long long)server->fsid.minor,
6338 clp->cl_hostname);
6339 nfs_display_fhandle(NFS_FH(inode), __func__);
6340
6341 do {
6342 status = ops->fsid_present(inode, cred);
6343 if (status != -NFS4ERR_DELAY)
6344 break;
6345 nfs4_handle_exception(server, status, &exception);
6346 } while (exception.retry);
6347 return status;
6348}
6349
Andy Adamson5ec16a82013-08-08 10:57:55 -04006350/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006351 * If 'use_integrity' is true and the state managment nfs_client
6352 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6353 * and the machine credential as per RFC3530bis and RFC5661 Security
6354 * Considerations sections. Otherwise, just use the user cred with the
6355 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006356 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006357static 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 +00006358{
6359 int status;
6360 struct nfs4_secinfo_arg args = {
6361 .dir_fh = NFS_FH(dir),
6362 .name = name,
6363 };
6364 struct nfs4_secinfo_res res = {
6365 .flavors = flavors,
6366 };
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6369 .rpc_argp = &args,
6370 .rpc_resp = &res,
6371 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006372 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006373 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006374
6375 if (use_integrity) {
6376 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006377 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6378 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006379 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006380
6381 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006382
6383 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6384 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6385
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006386 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6387 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006388 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006389
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006390 if (cred)
6391 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006392
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006393 return status;
6394}
6395
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006396int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6397 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006398{
6399 struct nfs4_exception exception = { };
6400 int err;
6401 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006402 err = -NFS4ERR_WRONGSEC;
6403
6404 /* try to use integrity protection with machine cred */
6405 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6406 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6407
6408 /*
6409 * if unable to use integrity protection, or SECINFO with
6410 * integrity protection returns NFS4ERR_WRONGSEC (which is
6411 * disallowed by spec, but exists in deployed servers) use
6412 * the current filesystem's rpc_client and the user cred.
6413 */
6414 if (err == -NFS4ERR_WRONGSEC)
6415 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6416
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006417 trace_nfs4_secinfo(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006419 &exception);
6420 } while (exception.retry);
6421 return err;
6422}
6423
Andy Adamson557134a2009-04-01 09:21:53 -04006424#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006425/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006426 * Check the exchange flags returned by the server for invalid flags, having
6427 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6428 * DS flags set.
6429 */
6430static int nfs4_check_cl_exchange_flags(u32 flags)
6431{
6432 if (flags & ~EXCHGID4_FLAG_MASK_R)
6433 goto out_inval;
6434 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6435 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6436 goto out_inval;
6437 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6438 goto out_inval;
6439 return NFS_OK;
6440out_inval:
6441 return -NFS4ERR_INVAL;
6442}
6443
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006444static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006445nfs41_same_server_scope(struct nfs41_server_scope *a,
6446 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006447{
6448 if (a->server_scope_sz == b->server_scope_sz &&
6449 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6450 return true;
6451
6452 return false;
6453}
6454
Andy Adamson357f54d2010-12-14 10:11:57 -05006455/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006456 * nfs4_proc_bind_conn_to_session()
6457 *
6458 * The 4.1 client currently uses the same TCP connection for the
6459 * fore and backchannel.
6460 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006461int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006462{
6463 int status;
6464 struct nfs41_bind_conn_to_session_res res;
6465 struct rpc_message msg = {
6466 .rpc_proc =
6467 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6468 .rpc_argp = clp,
6469 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006470 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006471 };
6472
6473 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006474
6475 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6476 if (unlikely(res.session == NULL)) {
6477 status = -ENOMEM;
6478 goto out;
6479 }
6480
6481 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006482 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006483 if (status == 0) {
6484 if (memcmp(res.session->sess_id.data,
6485 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6486 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6487 status = -EIO;
6488 goto out_session;
6489 }
6490 if (res.dir != NFS4_CDFS4_BOTH) {
6491 dprintk("NFS: %s: Unexpected direction from server\n",
6492 __func__);
6493 status = -EIO;
6494 goto out_session;
6495 }
6496 if (res.use_conn_in_rdma_mode) {
6497 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6498 __func__);
6499 status = -EIO;
6500 goto out_session;
6501 }
6502 }
6503out_session:
6504 kfree(res.session);
6505out:
6506 dprintk("<-- %s status= %d\n", __func__, status);
6507 return status;
6508}
6509
6510/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006511 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6512 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006513 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006514static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6515 .how = SP4_MACH_CRED,
6516 .enforce.u.words = {
6517 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6518 1 << (OP_EXCHANGE_ID - 32) |
6519 1 << (OP_CREATE_SESSION - 32) |
6520 1 << (OP_DESTROY_SESSION - 32) |
6521 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006522 },
6523 .allow.u.words = {
6524 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006525 1 << (OP_LOCKU) |
6526 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006527 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006528 1 << (OP_SECINFO_NO_NAME - 32) |
6529 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006530 1 << (OP_FREE_STATEID - 32) |
6531 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006532 }
6533};
6534
6535/*
6536 * Select the state protection mode for client `clp' given the server results
6537 * from exchange_id in `sp'.
6538 *
6539 * Returns 0 on success, negative errno otherwise.
6540 */
6541static int nfs4_sp4_select_mode(struct nfs_client *clp,
6542 struct nfs41_state_protection *sp)
6543{
6544 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6545 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6546 1 << (OP_EXCHANGE_ID - 32) |
6547 1 << (OP_CREATE_SESSION - 32) |
6548 1 << (OP_DESTROY_SESSION - 32) |
6549 1 << (OP_DESTROY_CLIENTID - 32)
6550 };
6551 unsigned int i;
6552
6553 if (sp->how == SP4_MACH_CRED) {
6554 /* Print state protect result */
6555 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6556 for (i = 0; i <= LAST_NFS4_OP; i++) {
6557 if (test_bit(i, sp->enforce.u.longs))
6558 dfprintk(MOUNT, " enforce op %d\n", i);
6559 if (test_bit(i, sp->allow.u.longs))
6560 dfprintk(MOUNT, " allow op %d\n", i);
6561 }
6562
6563 /* make sure nothing is on enforce list that isn't supported */
6564 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6565 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6566 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6567 return -EINVAL;
6568 }
6569 }
6570
6571 /*
6572 * Minimal mode - state operations are allowed to use machine
6573 * credential. Note this already happens by default, so the
6574 * client doesn't have to do anything more than the negotiation.
6575 *
6576 * NOTE: we don't care if EXCHANGE_ID is in the list -
6577 * we're already using the machine cred for exchange_id
6578 * and will never use a different cred.
6579 */
6580 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6581 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6582 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6583 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6584 dfprintk(MOUNT, "sp4_mach_cred:\n");
6585 dfprintk(MOUNT, " minimal mode enabled\n");
6586 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6587 } else {
6588 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6589 return -EINVAL;
6590 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006591
6592 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6593 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6594 dfprintk(MOUNT, " cleanup mode enabled\n");
6595 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6596 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006597
6598 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6599 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6600 dfprintk(MOUNT, " secinfo mode enabled\n");
6601 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6602 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006603
6604 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6605 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6606 dfprintk(MOUNT, " stateid mode enabled\n");
6607 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6608 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006609
6610 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " write mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6613 }
6614
6615 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " commit mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6618 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006619 }
6620
6621 return 0;
6622}
6623
6624/*
6625 * _nfs4_proc_exchange_id()
6626 *
6627 * Wrapper for EXCHANGE_ID operation.
6628 */
6629static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6630 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006631{
6632 nfs4_verifier verifier;
6633 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006634 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006635 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006636#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006637 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006638 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6639 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6640#else
6641 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6642 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6643#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006644 };
6645 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006646 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006647 };
6648 int status;
6649 struct rpc_message msg = {
6650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6651 .rpc_argp = &args,
6652 .rpc_resp = &res,
6653 .rpc_cred = cred,
6654 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006655
Chuck Leverf0920752012-05-21 22:45:41 -04006656 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006657 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6658 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006659 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6660 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6661 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006662
Chuck Leveracdeb692012-05-21 22:46:16 -04006663 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006664 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006665 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006666 status = -ENOMEM;
6667 goto out;
6668 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006669
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006670 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006671 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006672 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006673 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006674 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006675 }
6676
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006677 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006678 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006679 status = -ENOMEM;
6680 goto out_server_scope;
6681 }
6682
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006683 switch (sp4_how) {
6684 case SP4_NONE:
6685 args.state_protect.how = SP4_NONE;
6686 break;
6687
6688 case SP4_MACH_CRED:
6689 args.state_protect = nfs4_sp4_mach_cred_request;
6690 break;
6691
6692 default:
6693 /* unsupported! */
6694 WARN_ON_ONCE(1);
6695 status = -EINVAL;
6696 goto out_server_scope;
6697 }
6698
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006699 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006700 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006701 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006702 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006703
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006704 if (status == 0)
6705 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6706
Chuck Lever177313f2012-05-21 22:44:58 -04006707 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006708 clp->cl_clientid = res.clientid;
6709 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6710 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6711 clp->cl_seqid = res.seqid;
6712
Chuck Leveracdeb692012-05-21 22:46:16 -04006713 kfree(clp->cl_serverowner);
6714 clp->cl_serverowner = res.server_owner;
6715 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006716
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006717 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006718 kfree(clp->cl_implid);
6719 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006720
Chuck Lever177313f2012-05-21 22:44:58 -04006721 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006722 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006723 res.server_scope)) {
6724 dprintk("%s: server_scope mismatch detected\n",
6725 __func__);
6726 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006727 kfree(clp->cl_serverscope);
6728 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006729 }
6730
Chuck Lever177313f2012-05-21 22:44:58 -04006731 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006732 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006733 goto out;
6734 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006735 } else
6736 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006737
Chuck Leveracdeb692012-05-21 22:46:16 -04006738out_server_owner:
6739 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006740out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006741 kfree(res.server_scope);
6742out:
Chuck Lever177313f2012-05-21 22:44:58 -04006743 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006744 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006745 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006746 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006747 clp->cl_implid->date.seconds,
6748 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006749 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006750 return status;
6751}
6752
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006753/*
6754 * nfs4_proc_exchange_id()
6755 *
6756 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6757 *
6758 * Since the clientid has expired, all compounds using sessions
6759 * associated with the stale clientid will be returning
6760 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6761 * be in some phase of session reset.
6762 *
6763 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6764 */
6765int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6766{
6767 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6768 int status;
6769
6770 /* try SP4_MACH_CRED if krb5i/p */
6771 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6772 authflavor == RPC_AUTH_GSS_KRB5P) {
6773 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6774 if (!status)
6775 return 0;
6776 }
6777
6778 /* try SP4_NONE */
6779 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6780}
6781
Trond Myklebust66245532012-05-25 17:18:09 -04006782static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6783 struct rpc_cred *cred)
6784{
6785 struct rpc_message msg = {
6786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6787 .rpc_argp = clp,
6788 .rpc_cred = cred,
6789 };
6790 int status;
6791
6792 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006793 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006794 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006795 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006796 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6797 return status;
6798}
6799
6800static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6801 struct rpc_cred *cred)
6802{
6803 unsigned int loop;
6804 int ret;
6805
6806 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6807 ret = _nfs4_proc_destroy_clientid(clp, cred);
6808 switch (ret) {
6809 case -NFS4ERR_DELAY:
6810 case -NFS4ERR_CLIENTID_BUSY:
6811 ssleep(1);
6812 break;
6813 default:
6814 return ret;
6815 }
6816 }
6817 return 0;
6818}
6819
6820int nfs4_destroy_clientid(struct nfs_client *clp)
6821{
6822 struct rpc_cred *cred;
6823 int ret = 0;
6824
6825 if (clp->cl_mvops->minor_version < 1)
6826 goto out;
6827 if (clp->cl_exchange_flags == 0)
6828 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006829 if (clp->cl_preserve_clid)
6830 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006831 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006832 ret = nfs4_proc_destroy_clientid(clp, cred);
6833 if (cred)
6834 put_rpccred(cred);
6835 switch (ret) {
6836 case 0:
6837 case -NFS4ERR_STALE_CLIENTID:
6838 clp->cl_exchange_flags = 0;
6839 }
6840out:
6841 return ret;
6842}
6843
Andy Adamson2050f0c2009-04-01 09:22:30 -04006844struct nfs4_get_lease_time_data {
6845 struct nfs4_get_lease_time_args *args;
6846 struct nfs4_get_lease_time_res *res;
6847 struct nfs_client *clp;
6848};
6849
6850static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6851 void *calldata)
6852{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006853 struct nfs4_get_lease_time_data *data =
6854 (struct nfs4_get_lease_time_data *)calldata;
6855
6856 dprintk("--> %s\n", __func__);
6857 /* just setup sequence, do not trigger session recovery
6858 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006859 nfs41_setup_sequence(data->clp->cl_session,
6860 &data->args->la_seq_args,
6861 &data->res->lr_seq_res,
6862 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006863 dprintk("<-- %s\n", __func__);
6864}
6865
6866/*
6867 * Called from nfs4_state_manager thread for session setup, so don't recover
6868 * from sequence operation or clientid errors.
6869 */
6870static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6871{
6872 struct nfs4_get_lease_time_data *data =
6873 (struct nfs4_get_lease_time_data *)calldata;
6874
6875 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006876 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6877 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006878 switch (task->tk_status) {
6879 case -NFS4ERR_DELAY:
6880 case -NFS4ERR_GRACE:
6881 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6882 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6883 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006884 /* fall through */
6885 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006886 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006887 return;
6888 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006889 dprintk("<-- %s\n", __func__);
6890}
6891
Trond Myklebust17280172012-03-11 13:11:00 -04006892static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006893 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6894 .rpc_call_done = nfs4_get_lease_time_done,
6895};
6896
6897int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6898{
6899 struct rpc_task *task;
6900 struct nfs4_get_lease_time_args args;
6901 struct nfs4_get_lease_time_res res = {
6902 .lr_fsinfo = fsinfo,
6903 };
6904 struct nfs4_get_lease_time_data data = {
6905 .args = &args,
6906 .res = &res,
6907 .clp = clp,
6908 };
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6911 .rpc_argp = &args,
6912 .rpc_resp = &res,
6913 };
6914 struct rpc_task_setup task_setup = {
6915 .rpc_client = clp->cl_rpcclient,
6916 .rpc_message = &msg,
6917 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006918 .callback_data = &data,
6919 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006920 };
6921 int status;
6922
Chuck Levera9c92d62013-08-09 12:48:18 -04006923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006924 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006925 dprintk("--> %s\n", __func__);
6926 task = rpc_run_task(&task_setup);
6927
6928 if (IS_ERR(task))
6929 status = PTR_ERR(task);
6930 else {
6931 status = task->tk_status;
6932 rpc_put_task(task);
6933 }
6934 dprintk("<-- %s return %d\n", __func__, status);
6935
6936 return status;
6937}
6938
Andy Adamsonfc931582009-04-01 09:22:31 -04006939/*
6940 * Initialize the values to be used by the client in CREATE_SESSION
6941 * If nfs4_init_session set the fore channel request and response sizes,
6942 * use them.
6943 *
6944 * Set the back channel max_resp_sz_cached to zero to force the client to
6945 * always set csa_cachethis to FALSE because the current implementation
6946 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6947 */
6948static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6949{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006950 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006951
Andy Adamson18aad3d2013-06-26 12:21:49 -04006952 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6953 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6954
Andy Adamsonfc931582009-04-01 09:22:31 -04006955 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006956 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6957 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006958 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006959 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006960
6961 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006962 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006963 __func__,
6964 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006965 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006966
6967 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006968 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6969 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6970 args->bc_attrs.max_resp_sz_cached = 0;
6971 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6972 args->bc_attrs.max_reqs = 1;
6973
6974 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6975 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6976 __func__,
6977 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6978 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6979 args->bc_attrs.max_reqs);
6980}
6981
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006982static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006983{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006984 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6985 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6986
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006987 if (rcvd->max_resp_sz > sent->max_resp_sz)
6988 return -EINVAL;
6989 /*
6990 * Our requested max_ops is the minimum we need; we're not
6991 * prepared to break up compounds into smaller pieces than that.
6992 * So, no point even trying to continue if the server won't
6993 * cooperate:
6994 */
6995 if (rcvd->max_ops < sent->max_ops)
6996 return -EINVAL;
6997 if (rcvd->max_reqs == 0)
6998 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006999 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7000 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007001 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007002}
7003
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007004static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7005{
7006 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7007 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007008
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007009 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7010 return -EINVAL;
7011 if (rcvd->max_resp_sz < sent->max_resp_sz)
7012 return -EINVAL;
7013 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7014 return -EINVAL;
7015 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007016 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007017 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007018 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007019 return -EINVAL;
7020 return 0;
7021}
Andy Adamson8d353012009-04-01 09:22:32 -04007022
Andy Adamson8d353012009-04-01 09:22:32 -04007023static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7024 struct nfs4_session *session)
7025{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007026 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007027
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007028 ret = nfs4_verify_fore_channel_attrs(args, session);
7029 if (ret)
7030 return ret;
7031 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007032}
7033
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007034static int _nfs4_proc_create_session(struct nfs_client *clp,
7035 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007036{
7037 struct nfs4_session *session = clp->cl_session;
7038 struct nfs41_create_session_args args = {
7039 .client = clp,
7040 .cb_program = NFS4_CALLBACK,
7041 };
7042 struct nfs41_create_session_res res = {
7043 .client = clp,
7044 };
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7047 .rpc_argp = &args,
7048 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007049 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007050 };
7051 int status;
7052
7053 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007054 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007055
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007056 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007057 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007058
Trond Myklebust43095d32012-11-20 11:13:12 -05007059 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007060 /* Verify the session's negotiated channel_attrs values */
7061 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007062 /* Increment the clientid slot sequence id */
7063 clp->cl_seqid++;
7064 }
7065
7066 return status;
7067}
7068
7069/*
7070 * Issues a CREATE_SESSION operation to the server.
7071 * It is the responsibility of the caller to verify the session is
7072 * expired before calling this routine.
7073 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007074int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007075{
7076 int status;
7077 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007078 struct nfs4_session *session = clp->cl_session;
7079
7080 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7081
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007082 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007083 if (status)
7084 goto out;
7085
Andy Adamsonaacd5532011-11-09 13:58:21 -05007086 /* Init or reset the session slot tables */
7087 status = nfs4_setup_session_slot_tables(session);
7088 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007089 if (status)
7090 goto out;
7091
7092 ptr = (unsigned *)&session->sess_id.data[0];
7093 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7094 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007095out:
7096 dprintk("<-- %s\n", __func__);
7097 return status;
7098}
7099
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007100/*
7101 * Issue the over-the-wire RPC DESTROY_SESSION.
7102 * The caller must serialize access to this routine.
7103 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007104int nfs4_proc_destroy_session(struct nfs4_session *session,
7105 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007106{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007107 struct rpc_message msg = {
7108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7109 .rpc_argp = session,
7110 .rpc_cred = cred,
7111 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007112 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007113
7114 dprintk("--> nfs4_proc_destroy_session\n");
7115
7116 /* session is still being setup */
7117 if (session->clp->cl_cons_state != NFS_CS_READY)
7118 return status;
7119
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007120 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007121 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007122
7123 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007124 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007125 "Session has been destroyed regardless...\n", status);
7126
7127 dprintk("<-- nfs4_proc_destroy_session\n");
7128 return status;
7129}
7130
Trond Myklebust7b38c362012-05-23 13:23:31 -04007131/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007132 * Renew the cl_session lease.
7133 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007134struct nfs4_sequence_data {
7135 struct nfs_client *clp;
7136 struct nfs4_sequence_args args;
7137 struct nfs4_sequence_res res;
7138};
7139
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007140static void nfs41_sequence_release(void *data)
7141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007142 struct nfs4_sequence_data *calldata = data;
7143 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007144
Alexandros Batsakis71358402010-02-05 03:45:05 -08007145 if (atomic_read(&clp->cl_count) > 1)
7146 nfs4_schedule_state_renewal(clp);
7147 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007148 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007149}
7150
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007151static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7152{
7153 switch(task->tk_status) {
7154 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7156 return -EAGAIN;
7157 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007158 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007159 }
7160 return 0;
7161}
7162
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007163static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007164{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007165 struct nfs4_sequence_data *calldata = data;
7166 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007167
Trond Myklebust14516c32010-07-31 14:29:06 -04007168 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7169 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007170
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007171 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007172 if (task->tk_status < 0) {
7173 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007174 if (atomic_read(&clp->cl_count) == 1)
7175 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007176
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007177 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7178 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007179 return;
7180 }
7181 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007182 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007183out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007184 dprintk("<-- %s\n", __func__);
7185}
7186
7187static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7188{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007189 struct nfs4_sequence_data *calldata = data;
7190 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007191 struct nfs4_sequence_args *args;
7192 struct nfs4_sequence_res *res;
7193
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007194 args = task->tk_msg.rpc_argp;
7195 res = task->tk_msg.rpc_resp;
7196
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007197 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007198}
7199
7200static const struct rpc_call_ops nfs41_sequence_ops = {
7201 .rpc_call_done = nfs41_sequence_call_done,
7202 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007203 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007204};
7205
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007206static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7207 struct rpc_cred *cred,
7208 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007209{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007210 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7213 .rpc_cred = cred,
7214 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007215 struct rpc_task_setup task_setup_data = {
7216 .rpc_client = clp->cl_rpcclient,
7217 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007218 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007219 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007220 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007221
Alexandros Batsakis71358402010-02-05 03:45:05 -08007222 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007223 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007224 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007225 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007226 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007227 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007228 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007229 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007230 if (is_privileged)
7231 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007232 msg.rpc_argp = &calldata->args;
7233 msg.rpc_resp = &calldata->res;
7234 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007235 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007236
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007237 return rpc_run_task(&task_setup_data);
7238}
7239
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007240static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007241{
7242 struct rpc_task *task;
7243 int ret = 0;
7244
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007245 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7246 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007247 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007248 if (IS_ERR(task))
7249 ret = PTR_ERR(task);
7250 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007251 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007252 dprintk("<-- %s status=%d\n", __func__, ret);
7253 return ret;
7254}
7255
7256static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7257{
7258 struct rpc_task *task;
7259 int ret;
7260
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007261 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007262 if (IS_ERR(task)) {
7263 ret = PTR_ERR(task);
7264 goto out;
7265 }
7266 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007267 if (!ret) {
7268 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7269
7270 if (task->tk_status == 0)
7271 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007272 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007273 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007274 rpc_put_task(task);
7275out:
7276 dprintk("<-- %s status=%d\n", __func__, ret);
7277 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007278}
7279
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007280struct nfs4_reclaim_complete_data {
7281 struct nfs_client *clp;
7282 struct nfs41_reclaim_complete_args arg;
7283 struct nfs41_reclaim_complete_res res;
7284};
7285
7286static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7287{
7288 struct nfs4_reclaim_complete_data *calldata = data;
7289
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007290 nfs41_setup_sequence(calldata->clp->cl_session,
7291 &calldata->arg.seq_args,
7292 &calldata->res.seq_res,
7293 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007294}
7295
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007296static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7297{
7298 switch(task->tk_status) {
7299 case 0:
7300 case -NFS4ERR_COMPLETE_ALREADY:
7301 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7302 break;
7303 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007304 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007305 /* fall through */
7306 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007307 return -EAGAIN;
7308 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007309 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007310 }
7311 return 0;
7312}
7313
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007314static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7315{
7316 struct nfs4_reclaim_complete_data *calldata = data;
7317 struct nfs_client *clp = calldata->clp;
7318 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7319
7320 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007321 if (!nfs41_sequence_done(task, res))
7322 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007323
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007324 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007325 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7326 rpc_restart_call_prepare(task);
7327 return;
7328 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007329 dprintk("<-- %s\n", __func__);
7330}
7331
7332static void nfs4_free_reclaim_complete_data(void *data)
7333{
7334 struct nfs4_reclaim_complete_data *calldata = data;
7335
7336 kfree(calldata);
7337}
7338
7339static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7340 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7341 .rpc_call_done = nfs4_reclaim_complete_done,
7342 .rpc_release = nfs4_free_reclaim_complete_data,
7343};
7344
7345/*
7346 * Issue a global reclaim complete.
7347 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007348static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7349 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007350{
7351 struct nfs4_reclaim_complete_data *calldata;
7352 struct rpc_task *task;
7353 struct rpc_message msg = {
7354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007355 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007356 };
7357 struct rpc_task_setup task_setup_data = {
7358 .rpc_client = clp->cl_rpcclient,
7359 .rpc_message = &msg,
7360 .callback_ops = &nfs4_reclaim_complete_call_ops,
7361 .flags = RPC_TASK_ASYNC,
7362 };
7363 int status = -ENOMEM;
7364
7365 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007366 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007367 if (calldata == NULL)
7368 goto out;
7369 calldata->clp = clp;
7370 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007371
Chuck Levera9c92d62013-08-09 12:48:18 -04007372 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007373 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007374 msg.rpc_argp = &calldata->arg;
7375 msg.rpc_resp = &calldata->res;
7376 task_setup_data.callback_data = calldata;
7377 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007378 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007379 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007380 goto out;
7381 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007382 status = nfs4_wait_for_completion_rpc_task(task);
7383 if (status == 0)
7384 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007385 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007386 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007387out:
7388 dprintk("<-- %s status=%d\n", __func__, status);
7389 return status;
7390}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007391
7392static void
7393nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7394{
7395 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007396 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007397 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007398
7399 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007400 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7401 * right now covering the LAYOUTGET we are about to send.
7402 * However, that is not so catastrophic, and there seems
7403 * to be no way to prevent it completely.
7404 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007405 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007406 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007407 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007408 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7409 NFS_I(lgp->args.inode)->layout,
7410 lgp->args.ctx->state)) {
7411 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007412 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007413}
7414
7415static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7416{
7417 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007418 struct inode *inode = lgp->args.inode;
7419 struct nfs_server *server = NFS_SERVER(inode);
7420 struct pnfs_layout_hdr *lo;
7421 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007422 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007423
Boaz Harroshed7e5422014-01-22 20:34:54 +02007424 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007425
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007426 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007427 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007428
7429 switch (task->tk_status) {
7430 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007431 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007432 /*
7433 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7434 * (or clients) writing to the same RAID stripe
7435 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007436 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007437 /*
7438 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7439 * existing layout before getting a new one).
7440 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007441 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007442 timeo = rpc_get_timeout(task->tk_client);
7443 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007444 now = jiffies;
7445 if (time_after(giveup, now)) {
7446 unsigned long delay;
7447
7448 /* Delay for:
7449 * - Not less then NFS4_POLL_RETRY_MIN.
7450 * - One last time a jiffie before we give up
7451 * - exponential backoff (time_now minus start_attempt)
7452 */
7453 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7454 min((giveup - now - 1),
7455 now - lgp->args.timestamp));
7456
7457 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7458 __func__, delay);
7459 rpc_delay(task, delay);
7460 task->tk_status = 0;
7461 rpc_restart_call_prepare(task);
7462 goto out; /* Do not call nfs4_async_handle_error() */
7463 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007464 break;
7465 case -NFS4ERR_EXPIRED:
7466 case -NFS4ERR_BAD_STATEID:
7467 spin_lock(&inode->i_lock);
7468 lo = NFS_I(inode)->layout;
7469 if (!lo || list_empty(&lo->plh_segs)) {
7470 spin_unlock(&inode->i_lock);
7471 /* If the open stateid was bad, then recover it. */
7472 state = lgp->args.ctx->state;
7473 } else {
7474 LIST_HEAD(head);
7475
7476 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7477 spin_unlock(&inode->i_lock);
7478 /* Mark the bad layout state as invalid, then
7479 * retry using the open stateid. */
7480 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007481 }
7482 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007483 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7484 rpc_restart_call_prepare(task);
7485out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007486 dprintk("<-- %s\n", __func__);
7487}
7488
Idan Kedar85541162012-08-02 11:47:10 +03007489static size_t max_response_pages(struct nfs_server *server)
7490{
7491 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7492 return nfs_page_array_len(0, max_resp_sz);
7493}
7494
7495static void nfs4_free_pages(struct page **pages, size_t size)
7496{
7497 int i;
7498
7499 if (!pages)
7500 return;
7501
7502 for (i = 0; i < size; i++) {
7503 if (!pages[i])
7504 break;
7505 __free_page(pages[i]);
7506 }
7507 kfree(pages);
7508}
7509
7510static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7511{
7512 struct page **pages;
7513 int i;
7514
7515 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7516 if (!pages) {
7517 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7518 return NULL;
7519 }
7520
7521 for (i = 0; i < size; i++) {
7522 pages[i] = alloc_page(gfp_flags);
7523 if (!pages[i]) {
7524 dprintk("%s: failed to allocate page\n", __func__);
7525 nfs4_free_pages(pages, size);
7526 return NULL;
7527 }
7528 }
7529
7530 return pages;
7531}
7532
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007533static void nfs4_layoutget_release(void *calldata)
7534{
7535 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007536 struct inode *inode = lgp->args.inode;
7537 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007538 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007539
7540 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007541 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007542 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007543 put_nfs_open_context(lgp->args.ctx);
7544 kfree(calldata);
7545 dprintk("<-- %s\n", __func__);
7546}
7547
7548static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7549 .rpc_call_prepare = nfs4_layoutget_prepare,
7550 .rpc_call_done = nfs4_layoutget_done,
7551 .rpc_release = nfs4_layoutget_release,
7552};
7553
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007554struct pnfs_layout_segment *
7555nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007556{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007557 struct inode *inode = lgp->args.inode;
7558 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007559 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007560 struct rpc_task *task;
7561 struct rpc_message msg = {
7562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7563 .rpc_argp = &lgp->args,
7564 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007565 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007566 };
7567 struct rpc_task_setup task_setup_data = {
7568 .rpc_client = server->client,
7569 .rpc_message = &msg,
7570 .callback_ops = &nfs4_layoutget_call_ops,
7571 .callback_data = lgp,
7572 .flags = RPC_TASK_ASYNC,
7573 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007574 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007575 int status = 0;
7576
7577 dprintk("--> %s\n", __func__);
7578
Idan Kedar85541162012-08-02 11:47:10 +03007579 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7580 if (!lgp->args.layout.pages) {
7581 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007582 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007583 }
7584 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007585 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007586
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007587 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007588 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007589 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007590
7591 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7592 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7593
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007594 task = rpc_run_task(&task_setup_data);
7595 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007596 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007597 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007598 if (status == 0)
7599 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007600 trace_nfs4_layoutget(lgp->args.ctx,
7601 &lgp->args.range,
7602 &lgp->res.range,
7603 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007604 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7605 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007606 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007607 rpc_put_task(task);
7608 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007609 if (status)
7610 return ERR_PTR(status);
7611 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007612}
7613
Benny Halevycbe82602011-05-22 19:52:37 +03007614static void
7615nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7616{
7617 struct nfs4_layoutreturn *lrp = calldata;
7618
7619 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007620 nfs41_setup_sequence(lrp->clp->cl_session,
7621 &lrp->args.seq_args,
7622 &lrp->res.seq_res,
7623 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007624}
7625
7626static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7627{
7628 struct nfs4_layoutreturn *lrp = calldata;
7629 struct nfs_server *server;
7630
7631 dprintk("--> %s\n", __func__);
7632
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007633 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007634 return;
7635
7636 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007637 switch (task->tk_status) {
7638 default:
7639 task->tk_status = 0;
7640 case 0:
7641 break;
7642 case -NFS4ERR_DELAY:
7643 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7644 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007645 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007646 return;
7647 }
Benny Halevycbe82602011-05-22 19:52:37 +03007648 dprintk("<-- %s\n", __func__);
7649}
7650
7651static void nfs4_layoutreturn_release(void *calldata)
7652{
7653 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007654 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007655
7656 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007657 spin_lock(&lo->plh_inode->i_lock);
7658 if (lrp->res.lrs_present)
7659 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7660 lo->plh_block_lgets--;
7661 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007662 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007663 kfree(calldata);
7664 dprintk("<-- %s\n", __func__);
7665}
7666
7667static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7668 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7669 .rpc_call_done = nfs4_layoutreturn_done,
7670 .rpc_release = nfs4_layoutreturn_release,
7671};
7672
7673int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7674{
7675 struct rpc_task *task;
7676 struct rpc_message msg = {
7677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7678 .rpc_argp = &lrp->args,
7679 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007680 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007681 };
7682 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007683 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007684 .rpc_message = &msg,
7685 .callback_ops = &nfs4_layoutreturn_call_ops,
7686 .callback_data = lrp,
7687 };
7688 int status;
7689
7690 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007691 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007692 task = rpc_run_task(&task_setup_data);
7693 if (IS_ERR(task))
7694 return PTR_ERR(task);
7695 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007696 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007697 dprintk("<-- %s status=%d\n", __func__, status);
7698 rpc_put_task(task);
7699 return status;
7700}
7701
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007702/*
7703 * Retrieve the list of Data Server devices from the MDS.
7704 */
7705static int _nfs4_getdevicelist(struct nfs_server *server,
7706 const struct nfs_fh *fh,
7707 struct pnfs_devicelist *devlist)
7708{
7709 struct nfs4_getdevicelist_args args = {
7710 .fh = fh,
7711 .layoutclass = server->pnfs_curr_ld->id,
7712 };
7713 struct nfs4_getdevicelist_res res = {
7714 .devlist = devlist,
7715 };
7716 struct rpc_message msg = {
7717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7718 .rpc_argp = &args,
7719 .rpc_resp = &res,
7720 };
7721 int status;
7722
7723 dprintk("--> %s\n", __func__);
7724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7725 &res.seq_res, 0);
7726 dprintk("<-- %s status=%d\n", __func__, status);
7727 return status;
7728}
7729
7730int nfs4_proc_getdevicelist(struct nfs_server *server,
7731 const struct nfs_fh *fh,
7732 struct pnfs_devicelist *devlist)
7733{
7734 struct nfs4_exception exception = { };
7735 int err;
7736
7737 do {
7738 err = nfs4_handle_exception(server,
7739 _nfs4_getdevicelist(server, fh, devlist),
7740 &exception);
7741 } while (exception.retry);
7742
7743 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7744 err, devlist->num_devs);
7745
7746 return err;
7747}
7748EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7749
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007750static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007751_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7752 struct pnfs_device *pdev,
7753 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007754{
7755 struct nfs4_getdeviceinfo_args args = {
7756 .pdev = pdev,
7757 };
7758 struct nfs4_getdeviceinfo_res res = {
7759 .pdev = pdev,
7760 };
7761 struct rpc_message msg = {
7762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7763 .rpc_argp = &args,
7764 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007765 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007766 };
7767 int status;
7768
7769 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007771 dprintk("<-- %s status=%d\n", __func__, status);
7772
7773 return status;
7774}
7775
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007776int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7777 struct pnfs_device *pdev,
7778 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007779{
7780 struct nfs4_exception exception = { };
7781 int err;
7782
7783 do {
7784 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007785 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007786 &exception);
7787 } while (exception.retry);
7788 return err;
7789}
7790EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7791
Andy Adamson863a3c62011-03-23 13:27:54 +00007792static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7793{
7794 struct nfs4_layoutcommit_data *data = calldata;
7795 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007796 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007797
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007798 nfs41_setup_sequence(session,
7799 &data->args.seq_args,
7800 &data->res.seq_res,
7801 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007802}
7803
7804static void
7805nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7806{
7807 struct nfs4_layoutcommit_data *data = calldata;
7808 struct nfs_server *server = NFS_SERVER(data->args.inode);
7809
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007810 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007811 return;
7812
7813 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007814 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7815 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7816 case -NFS4ERR_BADLAYOUT: /* no layout */
7817 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007818 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007819 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007820 break;
7821 default:
7822 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7823 rpc_restart_call_prepare(task);
7824 return;
7825 }
7826 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007827}
7828
7829static void nfs4_layoutcommit_release(void *calldata)
7830{
7831 struct nfs4_layoutcommit_data *data = calldata;
7832
Andy Adamsondb29c082011-07-30 20:52:38 -04007833 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007834 nfs_post_op_update_inode_force_wcc(data->args.inode,
7835 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007836 put_rpccred(data->cred);
7837 kfree(data);
7838}
7839
7840static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7841 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7842 .rpc_call_done = nfs4_layoutcommit_done,
7843 .rpc_release = nfs4_layoutcommit_release,
7844};
7845
7846int
Andy Adamsonef311532011-03-12 02:58:10 -05007847nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007848{
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7851 .rpc_argp = &data->args,
7852 .rpc_resp = &data->res,
7853 .rpc_cred = data->cred,
7854 };
7855 struct rpc_task_setup task_setup_data = {
7856 .task = &data->task,
7857 .rpc_client = NFS_CLIENT(data->args.inode),
7858 .rpc_message = &msg,
7859 .callback_ops = &nfs4_layoutcommit_ops,
7860 .callback_data = data,
7861 .flags = RPC_TASK_ASYNC,
7862 };
7863 struct rpc_task *task;
7864 int status = 0;
7865
7866 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7867 "lbw: %llu inode %lu\n",
7868 data->task.tk_pid, sync,
7869 data->args.lastbytewritten,
7870 data->args.inode->i_ino);
7871
Chuck Levera9c92d62013-08-09 12:48:18 -04007872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007873 task = rpc_run_task(&task_setup_data);
7874 if (IS_ERR(task))
7875 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007876 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007877 goto out;
7878 status = nfs4_wait_for_completion_rpc_task(task);
7879 if (status != 0)
7880 goto out;
7881 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007882 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007883out:
7884 dprintk("%s: status %d\n", __func__, status);
7885 rpc_put_task(task);
7886 return status;
7887}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007888
Andy Adamson97431202013-08-08 10:57:56 -04007889/**
7890 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7891 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7892 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007893static int
7894_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007895 struct nfs_fsinfo *info,
7896 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007897{
7898 struct nfs41_secinfo_no_name_args args = {
7899 .style = SECINFO_STYLE_CURRENT_FH,
7900 };
7901 struct nfs4_secinfo_res res = {
7902 .flavors = flavors,
7903 };
7904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7906 .rpc_argp = &args,
7907 .rpc_resp = &res,
7908 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007909 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007910 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007911 int status;
7912
7913 if (use_integrity) {
7914 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007915 cred = nfs4_get_clid_cred(server->nfs_client);
7916 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007917 }
7918
7919 dprintk("--> %s\n", __func__);
7920 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7921 &res.seq_res, 0);
7922 dprintk("<-- %s status=%d\n", __func__, status);
7923
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007924 if (cred)
7925 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007926
7927 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007928}
7929
7930static int
7931nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7932 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7933{
7934 struct nfs4_exception exception = { };
7935 int err;
7936 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007937 /* first try using integrity protection */
7938 err = -NFS4ERR_WRONGSEC;
7939
7940 /* try to use integrity protection with machine cred */
7941 if (_nfs4_is_integrity_protected(server->nfs_client))
7942 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7943 flavors, true);
7944
7945 /*
7946 * if unable to use integrity protection, or SECINFO with
7947 * integrity protection returns NFS4ERR_WRONGSEC (which is
7948 * disallowed by spec, but exists in deployed servers) use
7949 * the current filesystem's rpc_client and the user cred.
7950 */
7951 if (err == -NFS4ERR_WRONGSEC)
7952 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7953 flavors, false);
7954
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007955 switch (err) {
7956 case 0:
7957 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007958 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007959 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007960 default:
7961 err = nfs4_handle_exception(server, err, &exception);
7962 }
7963 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007964out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007965 return err;
7966}
7967
7968static int
7969nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7970 struct nfs_fsinfo *info)
7971{
7972 int err;
7973 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007974 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007975 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007976 struct nfs4_secinfo4 *secinfo;
7977 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007978
7979 page = alloc_page(GFP_KERNEL);
7980 if (!page) {
7981 err = -ENOMEM;
7982 goto out;
7983 }
7984
7985 flavors = page_address(page);
7986 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7987
7988 /*
7989 * Fall back on "guess and check" method if
7990 * the server doesn't support SECINFO_NO_NAME
7991 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007992 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007993 err = nfs4_find_root_sec(server, fhandle, info);
7994 goto out_freepage;
7995 }
7996 if (err)
7997 goto out_freepage;
7998
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007999 for (i = 0; i < flavors->num_flavors; i++) {
8000 secinfo = &flavors->flavors[i];
8001
8002 switch (secinfo->flavor) {
8003 case RPC_AUTH_NULL:
8004 case RPC_AUTH_UNIX:
8005 case RPC_AUTH_GSS:
8006 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8007 &secinfo->flavor_info);
8008 break;
8009 default:
8010 flavor = RPC_AUTH_MAXFLAVOR;
8011 break;
8012 }
8013
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008014 if (!nfs_auth_info_match(&server->auth_info, flavor))
8015 flavor = RPC_AUTH_MAXFLAVOR;
8016
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008017 if (flavor != RPC_AUTH_MAXFLAVOR) {
8018 err = nfs4_lookup_root_sec(server, fhandle,
8019 info, flavor);
8020 if (!err)
8021 break;
8022 }
8023 }
8024
8025 if (flavor == RPC_AUTH_MAXFLAVOR)
8026 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008027
8028out_freepage:
8029 put_page(page);
8030 if (err == -EACCES)
8031 return -EPERM;
8032out:
8033 return err;
8034}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008035
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008036static int _nfs41_test_stateid(struct nfs_server *server,
8037 nfs4_stateid *stateid,
8038 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008039{
8040 int status;
8041 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008042 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008043 };
8044 struct nfs41_test_stateid_res res;
8045 struct rpc_message msg = {
8046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8047 .rpc_argp = &args,
8048 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008049 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008050 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008051 struct rpc_clnt *rpc_client = server->client;
8052
8053 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8054 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008055
Chuck Lever38527b12012-07-11 16:30:23 -04008056 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008058 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008059 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008060 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008061 if (status != NFS_OK) {
8062 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008063 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008064 }
8065 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008066 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008067}
8068
Chuck Lever38527b12012-07-11 16:30:23 -04008069/**
8070 * nfs41_test_stateid - perform a TEST_STATEID operation
8071 *
8072 * @server: server / transport on which to perform the operation
8073 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008074 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008075 *
8076 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8077 * Otherwise a negative NFS4ERR value is returned if the operation
8078 * failed or the state ID is not currently valid.
8079 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008080static int nfs41_test_stateid(struct nfs_server *server,
8081 nfs4_stateid *stateid,
8082 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008083{
8084 struct nfs4_exception exception = { };
8085 int err;
8086 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008087 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008088 if (err != -NFS4ERR_DELAY)
8089 break;
8090 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008091 } while (exception.retry);
8092 return err;
8093}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008094
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008095struct nfs_free_stateid_data {
8096 struct nfs_server *server;
8097 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008098 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008099};
8100
8101static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8102{
8103 struct nfs_free_stateid_data *data = calldata;
8104 nfs41_setup_sequence(nfs4_get_session(data->server),
8105 &data->args.seq_args,
8106 &data->res.seq_res,
8107 task);
8108}
8109
8110static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8111{
8112 struct nfs_free_stateid_data *data = calldata;
8113
8114 nfs41_sequence_done(task, &data->res.seq_res);
8115
8116 switch (task->tk_status) {
8117 case -NFS4ERR_DELAY:
8118 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8119 rpc_restart_call_prepare(task);
8120 }
8121}
8122
8123static void nfs41_free_stateid_release(void *calldata)
8124{
8125 kfree(calldata);
8126}
8127
Trond Myklebust17f26b12013-08-21 15:48:42 -04008128static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008129 .rpc_call_prepare = nfs41_free_stateid_prepare,
8130 .rpc_call_done = nfs41_free_stateid_done,
8131 .rpc_release = nfs41_free_stateid_release,
8132};
8133
8134static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8135 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008136 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008137 bool privileged)
8138{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008139 struct rpc_message msg = {
8140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008141 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008142 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008143 struct rpc_task_setup task_setup = {
8144 .rpc_client = server->client,
8145 .rpc_message = &msg,
8146 .callback_ops = &nfs41_free_stateid_ops,
8147 .flags = RPC_TASK_ASYNC,
8148 };
8149 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008150
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008151 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8152 &task_setup.rpc_client, &msg);
8153
Chuck Lever38527b12012-07-11 16:30:23 -04008154 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008155 data = kmalloc(sizeof(*data), GFP_NOFS);
8156 if (!data)
8157 return ERR_PTR(-ENOMEM);
8158 data->server = server;
8159 nfs4_stateid_copy(&data->args.stateid, stateid);
8160
8161 task_setup.callback_data = data;
8162
8163 msg.rpc_argp = &data->args;
8164 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008165 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008166 if (privileged)
8167 nfs4_set_sequence_privileged(&data->args.seq_args);
8168
8169 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008170}
8171
Chuck Lever38527b12012-07-11 16:30:23 -04008172/**
8173 * nfs41_free_stateid - perform a FREE_STATEID operation
8174 *
8175 * @server: server / transport on which to perform the operation
8176 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008177 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008178 *
8179 * Returns NFS_OK if the server freed "stateid". Otherwise a
8180 * negative NFS4ERR value is returned.
8181 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008182static int nfs41_free_stateid(struct nfs_server *server,
8183 nfs4_stateid *stateid,
8184 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008185{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008186 struct rpc_task *task;
8187 int ret;
8188
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008189 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008190 if (IS_ERR(task))
8191 return PTR_ERR(task);
8192 ret = rpc_wait_for_completion_task(task);
8193 if (!ret)
8194 ret = task->tk_status;
8195 rpc_put_task(task);
8196 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008197}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008198
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008199static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8200{
8201 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008202 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008203
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008204 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008205 nfs4_free_lock_state(server, lsp);
8206 if (IS_ERR(task))
8207 return PTR_ERR(task);
8208 rpc_put_task(task);
8209 return 0;
8210}
8211
Trond Myklebust36281ca2012-03-04 18:13:56 -05008212static bool nfs41_match_stateid(const nfs4_stateid *s1,
8213 const nfs4_stateid *s2)
8214{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008215 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008216 return false;
8217
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008218 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008219 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008220 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008221 return true;
8222
8223 return false;
8224}
8225
Andy Adamson557134a2009-04-01 09:21:53 -04008226#endif /* CONFIG_NFS_V4_1 */
8227
Trond Myklebust36281ca2012-03-04 18:13:56 -05008228static bool nfs4_match_stateid(const nfs4_stateid *s1,
8229 const nfs4_stateid *s2)
8230{
Trond Myklebustf597c532012-03-04 18:13:56 -05008231 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008232}
8233
8234
Trond Myklebust17280172012-03-11 13:11:00 -04008235static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008236 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008237 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 .recover_open = nfs4_open_reclaim,
8239 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008240 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008241 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242};
8243
Andy Adamson591d71c2009-04-01 09:22:47 -04008244#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008245static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008246 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8247 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8248 .recover_open = nfs4_open_reclaim,
8249 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008250 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008251 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008252 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008253};
8254#endif /* CONFIG_NFS_V4_1 */
8255
Trond Myklebust17280172012-03-11 13:11:00 -04008256static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008257 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008258 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 .recover_open = nfs4_open_expired,
8260 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008261 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262};
8263
Andy Adamson591d71c2009-04-01 09:22:47 -04008264#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008265static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008266 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8267 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008268 .recover_open = nfs41_open_expired,
8269 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008270 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008271};
8272#endif /* CONFIG_NFS_V4_1 */
8273
Trond Myklebust17280172012-03-11 13:11:00 -04008274static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008275 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008276 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008277 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008278};
8279
8280#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008281static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008282 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008283 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008284 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008285};
8286#endif
8287
Chuck Leverec011fe2013-10-17 14:12:39 -04008288static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008289 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008290 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008291};
8292
8293#if defined(CONFIG_NFS_V4_1)
8294static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008295 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008296 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008297};
8298#endif /* CONFIG_NFS_V4_1 */
8299
Trond Myklebust97dc1352010-06-16 09:52:26 -04008300static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8301 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008302 .init_caps = NFS_CAP_READDIRPLUS
8303 | NFS_CAP_ATOMIC_OPEN
8304 | NFS_CAP_CHANGE_ATTR
8305 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008306 .init_client = nfs40_init_client,
8307 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008308 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008309 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008310 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008311 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008312 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8313 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8314 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008315 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008316};
8317
8318#if defined(CONFIG_NFS_V4_1)
8319static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8320 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008321 .init_caps = NFS_CAP_READDIRPLUS
8322 | NFS_CAP_ATOMIC_OPEN
8323 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008324 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008325 | NFS_CAP_STATEID_NFSV41
8326 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008327 .init_client = nfs41_init_client,
8328 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008329 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008330 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008331 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008332 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008333 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8334 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8335 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008336 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008337};
8338#endif
8339
Steve Dickson42c2c422013-05-22 12:50:38 -04008340#if defined(CONFIG_NFS_V4_2)
8341static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8342 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008343 .init_caps = NFS_CAP_READDIRPLUS
8344 | NFS_CAP_ATOMIC_OPEN
8345 | NFS_CAP_CHANGE_ATTR
8346 | NFS_CAP_POSIX_LOCK
8347 | NFS_CAP_STATEID_NFSV41
8348 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008349 .init_client = nfs41_init_client,
8350 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008351 .match_stateid = nfs41_match_stateid,
8352 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008353 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008354 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8357 .state_renewal_ops = &nfs41_state_renewal_ops,
8358};
8359#endif
8360
Trond Myklebust97dc1352010-06-16 09:52:26 -04008361const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8362 [0] = &nfs_v4_0_minor_ops,
8363#if defined(CONFIG_NFS_V4_1)
8364 [1] = &nfs_v4_1_minor_ops,
8365#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008366#if defined(CONFIG_NFS_V4_2)
8367 [2] = &nfs_v4_2_minor_ops,
8368#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008369};
8370
Trond Myklebust17f26b12013-08-21 15:48:42 -04008371static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008372 .create = nfs_create,
8373 .lookup = nfs_lookup,
8374 .atomic_open = nfs_atomic_open,
8375 .link = nfs_link,
8376 .unlink = nfs_unlink,
8377 .symlink = nfs_symlink,
8378 .mkdir = nfs_mkdir,
8379 .rmdir = nfs_rmdir,
8380 .mknod = nfs_mknod,
8381 .rename = nfs_rename,
8382 .permission = nfs_permission,
8383 .getattr = nfs_getattr,
8384 .setattr = nfs_setattr,
8385 .getxattr = generic_getxattr,
8386 .setxattr = generic_setxattr,
8387 .listxattr = generic_listxattr,
8388 .removexattr = generic_removexattr,
8389};
8390
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008391static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008392 .permission = nfs_permission,
8393 .getattr = nfs_getattr,
8394 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008395 .getxattr = generic_getxattr,
8396 .setxattr = generic_setxattr,
8397 .listxattr = generic_listxattr,
8398 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008399};
8400
David Howells509de812006-08-22 20:06:11 -04008401const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 .version = 4, /* protocol version */
8403 .dentry_ops = &nfs4_dentry_operations,
8404 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008405 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008406 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008408 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008409 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 .getattr = nfs4_proc_getattr,
8411 .setattr = nfs4_proc_setattr,
8412 .lookup = nfs4_proc_lookup,
8413 .access = nfs4_proc_access,
8414 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 .create = nfs4_proc_create,
8416 .remove = nfs4_proc_remove,
8417 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008418 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 .unlink_done = nfs4_proc_unlink_done,
8420 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008421 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008422 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008423 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008424 .link = nfs4_proc_link,
8425 .symlink = nfs4_proc_symlink,
8426 .mkdir = nfs4_proc_mkdir,
8427 .rmdir = nfs4_proc_remove,
8428 .readdir = nfs4_proc_readdir,
8429 .mknod = nfs4_proc_mknod,
8430 .statfs = nfs4_proc_statfs,
8431 .fsinfo = nfs4_proc_fsinfo,
8432 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008433 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 .decode_dirent = nfs4_decode_dirent,
8435 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008436 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008437 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008438 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008439 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008440 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008441 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008442 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008443 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008444 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008445 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008447 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008448 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008449 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008450 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008451 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008452 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008453 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008454 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008455 .create_server = nfs4_create_server,
8456 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008457};
8458
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008459static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8460 .prefix = XATTR_NAME_NFSV4_ACL,
8461 .list = nfs4_xattr_list_nfs4_acl,
8462 .get = nfs4_xattr_get_nfs4_acl,
8463 .set = nfs4_xattr_set_nfs4_acl,
8464};
8465
8466const struct xattr_handler *nfs4_xattr_handlers[] = {
8467 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008468#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8469 &nfs4_xattr_nfs4_label_handler,
8470#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008471 NULL
8472};
8473
Linus Torvalds1da177e2005-04-16 15:20:36 -07008474/*
8475 * Local variables:
8476 * c-basic-offset: 8
8477 * End:
8478 */