blob: 42da6af77587bef4972ef8a84fc005b07aa4136f [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 Myklebustdc0b0272008-12-23 15:21:56 -05001140static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001141{
1142 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001143 nfs4_stateid_copy(&state->stateid, stateid);
1144 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001145 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001146 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001147 case FMODE_READ:
1148 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1149 break;
1150 case FMODE_WRITE:
1151 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1152 break;
1153 case FMODE_READ|FMODE_WRITE:
1154 set_bit(NFS_O_RDWR_STATE, &state->flags);
1155 }
1156}
1157
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001159{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001160 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001161 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001162 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001163}
1164
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165static 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 -07001166{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001167 /*
1168 * Protect the call to nfs4_state_set_mode_locked and
1169 * serialise the stateid update
1170 */
1171 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001172 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001173 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001174 set_bit(NFS_DELEGATED_STATE, &state->flags);
1175 }
1176 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001177 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001178 write_sequnlock(&state->seqlock);
1179 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001180 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001181 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184static 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 -05001185{
1186 struct nfs_inode *nfsi = NFS_I(state->inode);
1187 struct nfs_delegation *deleg_cur;
1188 int ret = 0;
1189
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001190 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001191
1192 rcu_read_lock();
1193 deleg_cur = rcu_dereference(nfsi->delegation);
1194 if (deleg_cur == NULL)
1195 goto no_delegation;
1196
1197 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001198 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001199 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001200 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001201 goto no_delegation_unlock;
1202
1203 if (delegation == NULL)
1204 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001205 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001206 goto no_delegation_unlock;
1207
Trond Myklebustb7391f42008-12-23 15:21:52 -05001208 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001209 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001210 ret = 1;
1211no_delegation_unlock:
1212 spin_unlock(&deleg_cur->lock);
1213no_delegation:
1214 rcu_read_unlock();
1215
1216 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001217 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001218 ret = 1;
1219 }
1220
1221 return ret;
1222}
1223
1224
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001226{
1227 struct nfs_delegation *delegation;
1228
1229 rcu_read_lock();
1230 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001231 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001232 rcu_read_unlock();
1233 return;
1234 }
1235 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001236 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001237}
1238
Trond Myklebust6ee41262007-07-08 14:11:36 -04001239static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001240{
1241 struct nfs4_state *state = opendata->state;
1242 struct nfs_inode *nfsi = NFS_I(state->inode);
1243 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001244 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001245 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001246 nfs4_stateid stateid;
1247 int ret = -EAGAIN;
1248
Trond Myklebustaac00a82007-07-05 19:02:21 -04001249 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001250 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001251 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001252 if (can_open_cached(state, fmode, open_mode)) {
1253 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001254 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001255 goto out_return_state;
1256 }
1257 spin_unlock(&state->owner->so_lock);
1258 }
Trond Myklebust34310432008-12-23 15:21:38 -05001259 rcu_read_lock();
1260 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001261 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001262 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001263 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001264 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001265 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001266 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001267 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001268 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001269 if (!opendata->is_recover) {
1270 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1271 if (ret != 0)
1272 goto out;
1273 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001274 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001275
1276 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001277 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001278 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001280out:
1281 return ERR_PTR(ret);
1282out_return_state:
1283 atomic_inc(&state->count);
1284 return state;
1285}
1286
Andy Adamsone23008e2012-10-02 21:07:32 -04001287static void
1288nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1289{
1290 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1291 struct nfs_delegation *delegation;
1292 int delegation_flags = 0;
1293
1294 rcu_read_lock();
1295 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1296 if (delegation)
1297 delegation_flags = delegation->flags;
1298 rcu_read_unlock();
1299 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1300 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1301 "returning a delegation for "
1302 "OPEN(CLAIM_DELEGATE_CUR)\n",
1303 clp->cl_hostname);
1304 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1305 nfs_inode_set_delegation(state->inode,
1306 data->owner->so_cred,
1307 &data->o_res);
1308 else
1309 nfs_inode_reclaim_delegation(state->inode,
1310 data->owner->so_cred,
1311 &data->o_res);
1312}
1313
1314/*
1315 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1316 * and update the nfs4_state.
1317 */
1318static struct nfs4_state *
1319_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1320{
1321 struct inode *inode = data->state->inode;
1322 struct nfs4_state *state = data->state;
1323 int ret;
1324
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001325 if (!data->rpc_done) {
1326 if (data->rpc_status) {
1327 ret = data->rpc_status;
1328 goto err;
1329 }
1330 /* cached opens have already been processed */
1331 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001332 }
1333
Andy Adamsone23008e2012-10-02 21:07:32 -04001334 ret = nfs_refresh_inode(inode, &data->f_attr);
1335 if (ret)
1336 goto err;
1337
1338 if (data->o_res.delegation_type != 0)
1339 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001340update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001341 update_open_stateid(state, &data->o_res.stateid, NULL,
1342 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001343 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001344
1345 return state;
1346err:
1347 return ERR_PTR(ret);
1348
1349}
1350
1351static struct nfs4_state *
1352_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353{
1354 struct inode *inode;
1355 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360 goto out;
1361 }
1362
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001363 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001364 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001365 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001366 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001367 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001368 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001369 goto err;
1370 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 state = nfs4_get_open_state(inode, data->owner);
1372 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001373 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001374 if (data->o_res.delegation_type != 0)
1375 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001376 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001377 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001380 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001382err_put_inode:
1383 iput(inode);
1384err:
1385 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386}
1387
Andy Adamsone23008e2012-10-02 21:07:32 -04001388static struct nfs4_state *
1389nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1390{
1391 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1392 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1393 return _nfs4_opendata_to_nfs4_state(data);
1394}
1395
Trond Myklebust864472e2006-01-03 09:55:15 +01001396static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1397{
1398 struct nfs_inode *nfsi = NFS_I(state->inode);
1399 struct nfs_open_context *ctx;
1400
1401 spin_lock(&state->inode->i_lock);
1402 list_for_each_entry(ctx, &nfsi->open_files, list) {
1403 if (ctx->state != state)
1404 continue;
1405 get_nfs_open_context(ctx);
1406 spin_unlock(&state->inode->i_lock);
1407 return ctx;
1408 }
1409 spin_unlock(&state->inode->i_lock);
1410 return ERR_PTR(-ENOENT);
1411}
1412
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001413static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1414 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415{
1416 struct nfs4_opendata *opendata;
1417
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001418 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001419 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001420 if (opendata == NULL)
1421 return ERR_PTR(-ENOMEM);
1422 opendata->state = state;
1423 atomic_inc(&state->count);
1424 return opendata;
1425}
1426
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001427static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001428{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001429 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 int ret;
1431
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 opendata->o_arg.open_flags = 0;
1433 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001434 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1435 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1436 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001437 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001438 if (ret != 0)
1439 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001440 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441 if (IS_ERR(newstate))
1442 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001443 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001444 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001445 return 0;
1446}
1447
1448static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1449{
Trond Myklebust864472e2006-01-03 09:55:15 +01001450 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001451 int ret;
1452
1453 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001454 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001455 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001456 smp_rmb();
1457 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001458 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001459 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001460 if (ret != 0)
1461 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001462 if (newstate != state)
1463 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001464 }
1465 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001467 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001468 if (ret != 0)
1469 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001470 if (newstate != state)
1471 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001472 }
1473 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001474 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001475 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001476 if (ret != 0)
1477 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001478 if (newstate != state)
1479 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001480 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001481 /*
1482 * We may have performed cached opens for all three recoveries.
1483 * Check if we need to update the current stateid.
1484 */
1485 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001486 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001487 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001490 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001491 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001492 return 0;
1493}
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495/*
1496 * OPEN_RECLAIM:
1497 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001499static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001501 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001502 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int status;
1505
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001506 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1507 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001508 if (IS_ERR(opendata))
1509 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001510 rcu_read_lock();
1511 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001512 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001513 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001514 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001515 opendata->o_arg.u.delegation_type = delegation_type;
1516 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001517 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return status;
1519}
1520
Trond Myklebust539cd032007-06-05 11:46:42 -04001521static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
1523 struct nfs_server *server = NFS_SERVER(state->inode);
1524 struct nfs4_exception exception = { };
1525 int err;
1526 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001527 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001528 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001529 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1530 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001531 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001532 break;
1533 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 } while (exception.retry);
1535 return err;
1536}
1537
Trond Myklebust864472e2006-01-03 09:55:15 +01001538static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1539{
1540 struct nfs_open_context *ctx;
1541 int ret;
1542
1543 ctx = nfs4_state_find_open_context(state);
1544 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001545 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001546 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001547 put_nfs_open_context(ctx);
1548 return ret;
1549}
1550
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001551static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001553 switch (err) {
1554 default:
1555 printk(KERN_ERR "NFS: %s: unhandled error "
1556 "%d.\n", __func__, err);
1557 case 0:
1558 case -ENOENT:
1559 case -ESTALE:
1560 break;
1561 case -NFS4ERR_BADSESSION:
1562 case -NFS4ERR_BADSLOT:
1563 case -NFS4ERR_BAD_HIGH_SLOT:
1564 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1565 case -NFS4ERR_DEADSESSION:
1566 set_bit(NFS_DELEGATED_STATE, &state->flags);
1567 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1568 return -EAGAIN;
1569 case -NFS4ERR_STALE_CLIENTID:
1570 case -NFS4ERR_STALE_STATEID:
1571 set_bit(NFS_DELEGATED_STATE, &state->flags);
1572 case -NFS4ERR_EXPIRED:
1573 /* Don't recall a delegation if it was lost */
1574 nfs4_schedule_lease_recovery(server->nfs_client);
1575 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001576 case -NFS4ERR_MOVED:
1577 nfs4_schedule_migration_recovery(server);
1578 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001579 case -NFS4ERR_LEASE_MOVED:
1580 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1581 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001582 case -NFS4ERR_DELEG_REVOKED:
1583 case -NFS4ERR_ADMIN_REVOKED:
1584 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001585 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001586 nfs_inode_find_state_and_recover(state->inode,
1587 stateid);
1588 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001589 return 0;
1590 case -NFS4ERR_DELAY:
1591 case -NFS4ERR_GRACE:
1592 set_bit(NFS_DELEGATED_STATE, &state->flags);
1593 ssleep(1);
1594 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001595 case -ENOMEM:
1596 case -NFS4ERR_DENIED:
1597 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1598 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return err;
1601}
1602
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001603int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1604{
1605 struct nfs_server *server = NFS_SERVER(state->inode);
1606 struct nfs4_opendata *opendata;
1607 int err;
1608
1609 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1610 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1611 if (IS_ERR(opendata))
1612 return PTR_ERR(opendata);
1613 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1614 err = nfs4_open_recover(opendata, state);
1615 nfs4_opendata_put(opendata);
1616 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1617}
1618
Chuck Leverbe05c862013-08-09 12:49:47 -04001619static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1620{
1621 struct nfs4_opendata *data = calldata;
1622
1623 nfs40_setup_sequence(data->o_arg.server, &data->o_arg.seq_args,
1624 &data->o_res.seq_res, task);
1625}
1626
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001627static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1628{
1629 struct nfs4_opendata *data = calldata;
1630
Chuck Leverbe05c862013-08-09 12:49:47 -04001631 nfs40_sequence_done(task, &data->o_res.seq_res);
1632
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001633 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001634 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001635 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001636 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001637 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001638 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001639 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001640}
1641
1642static void nfs4_open_confirm_release(void *calldata)
1643{
1644 struct nfs4_opendata *data = calldata;
1645 struct nfs4_state *state = NULL;
1646
1647 /* If this request hasn't been cancelled, do nothing */
1648 if (data->cancelled == 0)
1649 goto out_free;
1650 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001651 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001652 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001653 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001654 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001655 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001656out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001657 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001658}
1659
1660static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001661 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001662 .rpc_call_done = nfs4_open_confirm_done,
1663 .rpc_release = nfs4_open_confirm_release,
1664};
1665
1666/*
1667 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1668 */
1669static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1670{
1671 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1672 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001673 struct rpc_message msg = {
1674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1675 .rpc_argp = &data->c_arg,
1676 .rpc_resp = &data->c_res,
1677 .rpc_cred = data->owner->so_cred,
1678 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001679 struct rpc_task_setup task_setup_data = {
1680 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001681 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001682 .callback_ops = &nfs4_open_confirm_ops,
1683 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001684 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001685 .flags = RPC_TASK_ASYNC,
1686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int status;
1688
Chuck Leverbe05c862013-08-09 12:49:47 -04001689 nfs4_init_sequence(&data->o_arg.seq_args, &data->o_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001690 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001691 data->rpc_done = 0;
1692 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001693 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001694 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001695 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001696 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001697 status = nfs4_wait_for_completion_rpc_task(task);
1698 if (status != 0) {
1699 data->cancelled = 1;
1700 smp_wmb();
1701 } else
1702 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001703 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return status;
1705}
1706
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001707static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 struct nfs4_opendata *data = calldata;
1710 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001711 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001712
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001713 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001714 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 /*
1716 * Check if we still need to send an OPEN call, or if we can use
1717 * a delegation instead.
1718 */
1719 if (data->state != NULL) {
1720 struct nfs_delegation *delegation;
1721
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001723 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724 rcu_read_lock();
1725 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001726 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001727 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001728 can_open_delegated(delegation, data->o_arg.fmode))
1729 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 rcu_read_unlock();
1731 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001732 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001733 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001734 switch (data->o_arg.claim) {
1735 case NFS4_OPEN_CLAIM_PREVIOUS:
1736 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1737 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001738 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001739 case NFS4_OPEN_CLAIM_FH:
1740 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001741 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1742 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001743 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001744 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001745 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001746 &data->o_res.seq_res,
1747 task) != 0)
1748 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001749
1750 /* Set the create mode (note dependency on the session type) */
1751 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1752 if (data->o_arg.open_flags & O_EXCL) {
1753 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1754 if (nfs4_has_persistent_session(clp))
1755 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1756 else if (clp->cl_mvops->minor_version > 0)
1757 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1758 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001759 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001760unlock_no_action:
1761 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001762out_no_action:
1763 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001764out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001765 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001766}
1767
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768static void nfs4_open_done(struct rpc_task *task, void *calldata)
1769{
1770 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001773
Trond Myklebust14516c32010-07-31 14:29:06 -04001774 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1775 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001776
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001778 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1779 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001780 case S_IFREG:
1781 break;
1782 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001784 break;
1785 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001786 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001787 break;
1788 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001789 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001790 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001791 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001792 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001793 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1794 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001795 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001796 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001797}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001798
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001799static void nfs4_open_release(void *calldata)
1800{
1801 struct nfs4_opendata *data = calldata;
1802 struct nfs4_state *state = NULL;
1803
1804 /* If this request hasn't been cancelled, do nothing */
1805 if (data->cancelled == 0)
1806 goto out_free;
1807 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001808 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001809 goto out_free;
1810 /* In case we need an open_confirm, no cleanup! */
1811 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1812 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001813 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001814 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001815 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001816out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001817 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001818}
1819
1820static const struct rpc_call_ops nfs4_open_ops = {
1821 .rpc_call_prepare = nfs4_open_prepare,
1822 .rpc_call_done = nfs4_open_done,
1823 .rpc_release = nfs4_open_release,
1824};
1825
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001826static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001827{
1828 struct inode *dir = data->dir->d_inode;
1829 struct nfs_server *server = NFS_SERVER(dir);
1830 struct nfs_openargs *o_arg = &data->o_arg;
1831 struct nfs_openres *o_res = &data->o_res;
1832 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001833 struct rpc_message msg = {
1834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1835 .rpc_argp = o_arg,
1836 .rpc_resp = o_res,
1837 .rpc_cred = data->owner->so_cred,
1838 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001839 struct rpc_task_setup task_setup_data = {
1840 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001841 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001842 .callback_ops = &nfs4_open_ops,
1843 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001844 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001845 .flags = RPC_TASK_ASYNC,
1846 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 int status;
1848
Chuck Levera9c92d62013-08-09 12:48:18 -04001849 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001850 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001851 data->rpc_done = 0;
1852 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001853 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001854 data->is_recover = 0;
1855 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001856 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001857 data->is_recover = 1;
1858 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001859 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001860 if (IS_ERR(task))
1861 return PTR_ERR(task);
1862 status = nfs4_wait_for_completion_rpc_task(task);
1863 if (status != 0) {
1864 data->cancelled = 1;
1865 smp_wmb();
1866 } else
1867 status = data->rpc_status;
1868 rpc_put_task(task);
1869
1870 return status;
1871}
1872
1873static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1874{
1875 struct inode *dir = data->dir->d_inode;
1876 struct nfs_openres *o_res = &data->o_res;
1877 int status;
1878
1879 status = nfs4_run_open_task(data, 1);
1880 if (status != 0 || !data->rpc_done)
1881 return status;
1882
Trond Myklebust6926afd2012-01-07 13:22:46 -05001883 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1884
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001885 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1886 status = _nfs4_proc_open_confirm(data);
1887 if (status != 0)
1888 return status;
1889 }
1890
1891 return status;
1892}
1893
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001894static int nfs4_opendata_access(struct rpc_cred *cred,
1895 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001896 struct nfs4_state *state, fmode_t fmode,
1897 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001898{
1899 struct nfs_access_entry cache;
1900 u32 mask;
1901
1902 /* access call failed or for some reason the server doesn't
1903 * support any access modes -- defer access call until later */
1904 if (opendata->o_res.access_supported == 0)
1905 return 0;
1906
1907 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001908 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001909 * write mode bits, but POSIX allows the creating process to write.
1910 * use openflags to check for exec, because fmode won't
1911 * always have FMODE_EXEC set when file open for exec. */
1912 if (openflags & __FMODE_EXEC) {
1913 /* ONLY check for exec rights */
1914 mask = MAY_EXEC;
1915 } else if (fmode & FMODE_READ)
1916 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001917
1918 cache.cred = cred;
1919 cache.jiffies = jiffies;
1920 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1921 nfs_access_add_cache(state->inode, &cache);
1922
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001923 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001924 return 0;
1925
1926 /* even though OPEN succeeded, access is denied. Close the file */
1927 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001928 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001929}
1930
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001931/*
1932 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1933 */
1934static int _nfs4_proc_open(struct nfs4_opendata *data)
1935{
1936 struct inode *dir = data->dir->d_inode;
1937 struct nfs_server *server = NFS_SERVER(dir);
1938 struct nfs_openargs *o_arg = &data->o_arg;
1939 struct nfs_openres *o_res = &data->o_res;
1940 int status;
1941
1942 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001943 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001944 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001945 if (status != 0) {
1946 if (status == -NFS4ERR_BADNAME &&
1947 !(o_arg->open_flags & O_CREAT))
1948 return -ENOENT;
1949 return status;
1950 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001951
Trond Myklebust6926afd2012-01-07 13:22:46 -05001952 nfs_fattr_map_and_free_names(server, &data->f_attr);
1953
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001954 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001955 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001956 if (o_arg->open_flags & O_EXCL)
1957 data->file_created = 1;
1958 else if (o_res->cinfo.before != o_res->cinfo.after)
1959 data->file_created = 1;
1960 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001961 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1962 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001964 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001966 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001969 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Andy Adamsond83217c2011-03-01 01:34:17 +00001973static int nfs4_recover_expired_lease(struct nfs_server *server)
1974{
1975 return nfs4_client_recover_expired_lease(server->nfs_client);
1976}
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978/*
1979 * OPEN_EXPIRED:
1980 * reclaim state on the server after a network partition.
1981 * Assumes caller holds the appropriate lock
1982 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001983static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001985 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001986 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001988 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001989 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001990 if (IS_ERR(opendata))
1991 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001992 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001993 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001994 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001995 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001996 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001999static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002000{
Trond Myklebust539cd032007-06-05 11:46:42 -04002001 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002002 struct nfs4_exception exception = { };
2003 int err;
2004
2005 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002006 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002007 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002008 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2009 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002010 switch (err) {
2011 default:
2012 goto out;
2013 case -NFS4ERR_GRACE:
2014 case -NFS4ERR_DELAY:
2015 nfs4_handle_exception(server, err, &exception);
2016 err = 0;
2017 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002018 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002019out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002020 return err;
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002026 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 ctx = nfs4_state_find_open_context(state);
2029 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002030 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002031 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002032 put_nfs_open_context(ctx);
2033 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002036#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002037static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002038{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002039 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002040 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002041 struct nfs_delegation *delegation;
2042 struct rpc_cred *cred = NULL;
2043 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002044
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002045 /* If a state reset has been done, test_stateid is unneeded */
2046 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2047 return;
2048
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002049 /* Get the delegation credential for use by test/free_stateid */
2050 rcu_read_lock();
2051 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2052 if (delegation != NULL &&
2053 nfs4_stateid_match(&delegation->stateid, stateid)) {
2054 cred = get_rpccred(delegation->cred);
2055 rcu_read_unlock();
2056 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002057 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002058 } else
2059 rcu_read_unlock();
2060
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002061 if (status != NFS_OK) {
2062 /* Free the stateid unless the server explicitly
2063 * informs us the stateid is unrecognized. */
2064 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002065 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002066 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002067
Bryan Schumaker69388672012-09-26 15:25:52 -04002068 write_seqlock(&state->seqlock);
2069 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2070 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002071 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2072 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002073
2074 if (cred != NULL)
2075 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002076}
2077
2078/**
2079 * nfs41_check_open_stateid - possibly free an open stateid
2080 *
2081 * @state: NFSv4 state for an inode
2082 *
2083 * Returns NFS_OK if recovery for this stateid is now finished.
2084 * Otherwise a negative NFS4ERR value is returned.
2085 */
2086static int nfs41_check_open_stateid(struct nfs4_state *state)
2087{
2088 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002089 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002090 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002091 int status;
2092
2093 /* If a state reset has been done, test_stateid is unneeded */
2094 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2095 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2096 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2097 return -NFS4ERR_BAD_STATEID;
2098
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002099 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002100 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002101 if (status != NFS_OK) {
2102 /* Free the stateid unless the server explicitly
2103 * informs us the stateid is unrecognized. */
2104 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002105 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002106
2107 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2108 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2109 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002110 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002111 }
2112 return status;
2113}
2114
2115static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2116{
Chuck Levereb64cf92012-07-11 16:30:05 -04002117 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002118
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002119 nfs41_clear_delegation_stateid(state);
2120 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002121 if (status != NFS_OK)
2122 status = nfs4_open_expired(sp, state);
2123 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002124}
2125#endif
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002128 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2129 * fields corresponding to attributes that were used to store the verifier.
2130 * Make sure we clobber those fields in the later setattr call
2131 */
2132static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2133{
2134 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2135 !(sattr->ia_valid & ATTR_ATIME_SET))
2136 sattr->ia_valid |= ATTR_ATIME;
2137
2138 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2139 !(sattr->ia_valid & ATTR_MTIME_SET))
2140 sattr->ia_valid |= ATTR_MTIME;
2141}
2142
Trond Myklebustc21443c2013-02-07 14:26:21 -05002143static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2144 fmode_t fmode,
2145 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002146 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002147{
2148 struct nfs4_state_owner *sp = opendata->owner;
2149 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002150 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002151 struct nfs4_state *state;
2152 unsigned int seq;
2153 int ret;
2154
2155 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2156
2157 ret = _nfs4_proc_open(opendata);
2158 if (ret != 0)
2159 goto out;
2160
2161 state = nfs4_opendata_to_nfs4_state(opendata);
2162 ret = PTR_ERR(state);
2163 if (IS_ERR(state))
2164 goto out;
2165 if (server->caps & NFS_CAP_POSIX_LOCK)
2166 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2167
Trond Myklebust275bb302013-05-29 13:11:28 -04002168 dentry = opendata->dentry;
2169 if (dentry->d_inode == NULL) {
2170 /* FIXME: Is this d_drop() ever needed? */
2171 d_drop(dentry);
2172 dentry = d_add_unique(dentry, igrab(state->inode));
2173 if (dentry == NULL) {
2174 dentry = opendata->dentry;
2175 } else if (dentry != ctx->dentry) {
2176 dput(ctx->dentry);
2177 ctx->dentry = dget(dentry);
2178 }
2179 nfs_set_verifier(dentry,
2180 nfs_save_change_attribute(opendata->dir->d_inode));
2181 }
2182
Trond Myklebustc21443c2013-02-07 14:26:21 -05002183 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2184 if (ret != 0)
2185 goto out;
2186
Trond Myklebust3efb9722013-05-29 13:17:04 -04002187 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002188 if (dentry->d_inode == state->inode) {
2189 nfs_inode_attach_open_context(ctx);
2190 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2191 nfs4_schedule_stateid_recovery(server, state);
2192 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002193out:
2194 return ret;
2195}
2196
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002197/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002198 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 */
Andy Adamson82be4172012-05-23 05:02:35 -04002200static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002201 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002202 int flags,
2203 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002204 struct nfs4_label *label,
2205 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 struct nfs4_state_owner *sp;
2208 struct nfs4_state *state = NULL;
2209 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002210 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002211 struct dentry *dentry = ctx->dentry;
2212 struct rpc_cred *cred = ctx->cred;
2213 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2214 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002215 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002216 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002217 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002221 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2222 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2224 goto out_err;
2225 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002226 status = nfs4_recover_expired_lease(server);
2227 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002228 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002229 if (dentry->d_inode != NULL)
2230 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002231 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002232 if (dentry->d_inode)
2233 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002234 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002235 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002236 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002237 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
David Quigley14c43f72013-05-22 12:50:43 -04002239 if (label) {
2240 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2241 if (IS_ERR(olabel)) {
2242 status = PTR_ERR(olabel);
2243 goto err_opendata_put;
2244 }
2245 }
2246
Andy Adamson82be4172012-05-23 05:02:35 -04002247 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2248 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2249 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002250 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002251 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002252 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002253 if (dentry->d_inode != NULL)
2254 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002255
Trond Myklebust3efb9722013-05-29 13:17:04 -04002256 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002257 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002258 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002259 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002260
Trond Myklebust549b19c2013-04-16 18:42:34 -04002261 if ((opendata->o_arg.open_flags & O_EXCL) &&
2262 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002263 nfs4_exclusive_attrset(opendata, sattr);
2264
2265 nfs_fattr_init(opendata->o_res.f_attr);
2266 status = nfs4_do_setattr(state->inode, cred,
2267 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002268 state, label, olabel);
2269 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002270 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002271 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002272 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002273 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002274 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002275 if (opendata->file_created)
2276 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002277
2278 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2279 *ctx_th = opendata->f_attr.mdsthreshold;
2280 else
2281 kfree(opendata->f_attr.mdsthreshold);
2282 opendata->f_attr.mdsthreshold = NULL;
2283
David Quigley14c43f72013-05-22 12:50:43 -04002284 nfs4_label_free(olabel);
2285
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002286 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002289err_free_label:
2290 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002291err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002292 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002293 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002294err_put_state_owner:
2295 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return status;
2298}
2299
2300
Andy Adamson82be4172012-05-23 05:02:35 -04002301static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002302 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002303 int flags,
2304 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002305 struct nfs4_label *label,
2306 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002308 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct nfs4_exception exception = { };
2310 struct nfs4_state *res;
2311 int status;
2312
2313 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002314 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002315 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002316 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (status == 0)
2318 break;
2319 /* NOTE: BAD_SEQID means the server and client disagree about the
2320 * book-keeping w.r.t. state-changing operations
2321 * (OPEN/CLOSE/LOCK/LOCKU...)
2322 * It is actually a sign of a bug on the client or on the server.
2323 *
2324 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002325 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 * have unhashed the old state_owner for us, and that we can
2327 * therefore safely retry using a new one. We should still warn
2328 * the user though...
2329 */
2330 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002331 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002332 " returned a bad sequence-id error!\n",
2333 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 exception.retry = 1;
2335 continue;
2336 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002337 /*
2338 * BAD_STATEID on OPEN means that the server cancelled our
2339 * state before it received the OPEN_CONFIRM.
2340 * Recover by retrying the request as per the discussion
2341 * on Page 181 of RFC3530.
2342 */
2343 if (status == -NFS4ERR_BAD_STATEID) {
2344 exception.retry = 1;
2345 continue;
2346 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002347 if (status == -EAGAIN) {
2348 /* We must have found a delegation */
2349 exception.retry = 1;
2350 continue;
2351 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002352 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2353 continue;
2354 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 status, &exception));
2356 } while (exception.retry);
2357 return res;
2358}
2359
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002360static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2361 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002362 struct nfs4_state *state, struct nfs4_label *ilabel,
2363 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002365 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002367 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 .iap = sattr,
2369 .server = server,
2370 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002371 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 };
2373 struct nfs_setattrres res = {
2374 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002375 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 .server = server,
2377 };
2378 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2380 .rpc_argp = &arg,
2381 .rpc_resp = &res,
2382 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002384 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002385 fmode_t fmode;
2386 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002387 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
David Quigleyaa9c2662013-05-22 12:50:44 -04002389 arg.bitmask = nfs4_bitmask(server, ilabel);
2390 if (ilabel)
2391 arg.bitmask = nfs4_bitmask(server, olabel);
2392
Trond Myklebust0e574af2005-10-27 22:12:38 -04002393 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
Trond Myklebustee3ae842013-04-29 10:35:36 -04002395 /* Servers should only apply open mode checks for file size changes */
2396 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2397 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2398
2399 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2400 /* Use that stateid */
2401 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002402 struct nfs_lockowner lockowner = {
2403 .l_owner = current->files,
2404 .l_pid = current->tgid,
2405 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002406 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002407 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002408 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002409 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Bryan Schumaker7c513052011-03-24 17:12:24 +00002411 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002412 if (status == 0 && state != NULL)
2413 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002417static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2418 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002419 struct nfs4_state *state, struct nfs4_label *ilabel,
2420 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002422 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002423 struct nfs4_exception exception = {
2424 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002425 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002426 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 int err;
2428 do {
David Quigley1775fd32013-05-22 12:50:42 -04002429 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002430 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002431 switch (err) {
2432 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002433 if (!(sattr->ia_valid & ATTR_SIZE)) {
2434 pr_warn_once("NFSv4: server %s is incorrectly "
2435 "applying open mode checks to "
2436 "a SETATTR that is not "
2437 "changing file size.\n",
2438 server->nfs_client->cl_hostname);
2439 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002440 if (state && !(state->state & FMODE_WRITE)) {
2441 err = -EBADF;
2442 if (sattr->ia_valid & ATTR_OPEN)
2443 err = -EACCES;
2444 goto out;
2445 }
2446 }
2447 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002449out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return err;
2451}
2452
2453struct nfs4_closedata {
2454 struct inode *inode;
2455 struct nfs4_state *state;
2456 struct nfs_closeargs arg;
2457 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002458 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002459 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002460 bool roc;
2461 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462};
2463
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002464static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002465{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002466 struct nfs4_closedata *calldata = data;
2467 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002468 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002469
Fred Isamanf7e89172011-01-06 11:36:32 +00002470 if (calldata->roc)
2471 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002472 nfs4_put_open_state(calldata->state);
2473 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002474 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002475 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002476 kfree(calldata);
2477}
2478
Trond Myklebust88069f72009-12-08 08:33:16 -05002479static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2480 fmode_t fmode)
2481{
2482 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002483 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002484 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2485 case FMODE_WRITE:
2486 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2487 break;
2488 case FMODE_READ:
2489 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2490 break;
2491 case 0:
2492 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2493 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2494 clear_bit(NFS_OPEN_STATE, &state->flags);
2495 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002496 spin_unlock(&state->owner->so_lock);
2497}
2498
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002499static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002501 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct nfs_server *server = NFS_SERVER(calldata->inode);
2504
Chuck Levera3ca5652012-03-01 17:00:40 -05002505 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002506 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2507 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002508 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /* hmm. we are done with the inode, and in the process of freeing
2510 * the state_owner. we keep this around to process errors
2511 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 switch (task->tk_status) {
2513 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002514 if (calldata->roc)
2515 pnfs_roc_set_barrier(state->inode,
2516 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002517 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002518 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002520 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002522 case -NFS4ERR_OLD_STATEID:
2523 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002525 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 default:
Trond Myklebust69794ad2013-11-20 12:57:19 -05002528 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002529 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002530 goto out_release;
2531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Trond Myklebust69794ad2013-11-20 12:57:19 -05002533 nfs4_close_clear_stateid_flags(state, calldata->arg.fmode);
2534out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002535 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002536 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002537 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002540static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002542 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002543 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002544 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002545 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002546
Chuck Levera3ca5652012-03-01 17:00:40 -05002547 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002548 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002549 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002550
Trond Myklebust88069f72009-12-08 08:33:16 -05002551 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2552 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002553 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002554 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002555 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002556 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002557 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2558 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2559 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002560 }
2561 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002562 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2563 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2564 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002565 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002566 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002567 if (!nfs4_valid_open_stateid(state))
2568 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002569 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002570
2571 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002572 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002573 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002574 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002575
Fred Isamanf7e89172011-01-06 11:36:32 +00002576 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002577 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002578 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002579 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2580 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002581 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002582 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002583 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002584
Trond Myklebust516a6af2005-10-27 22:12:41 -04002585 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002586 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002587 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002588 &calldata->arg.seq_args,
2589 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002590 task) != 0)
2591 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002592 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002593 return;
2594out_no_action:
2595 task->tk_action = NULL;
2596out_wait:
2597 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
2599
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002600static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002601 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002602 .rpc_call_done = nfs4_close_done,
2603 .rpc_release = nfs4_free_closedata,
2604};
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606/*
2607 * It is possible for data to be read/written from a mem-mapped file
2608 * after the sys_close call (which hits the vfs layer as a flush).
2609 * This means that we can't safely call nfsv4 close on a file until
2610 * the inode is cleared. This in turn means that we are not good
2611 * NFSv4 citizens - we do not indicate to the server to update the file's
2612 * share state even when we are done with one of the three share
2613 * stateid's in the inode.
2614 *
2615 * NOTE: Caller must be holding the sp->so_owner semaphore!
2616 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002617int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002619 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002621 struct nfs4_state_owner *sp = state->owner;
2622 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002623 struct rpc_message msg = {
2624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2625 .rpc_cred = state->owner->so_cred,
2626 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002627 struct rpc_task_setup task_setup_data = {
2628 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002629 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002630 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002631 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002632 .flags = RPC_TASK_ASYNC,
2633 };
Trond Myklebust95121352005-10-18 14:20:12 -07002634 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002636 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2637 &task_setup_data.rpc_client, &msg);
2638
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002639 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002641 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002642 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002643 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002645 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002646 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002648 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002649 if (calldata->arg.seqid == NULL)
2650 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002651 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002652 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002653 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002654 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002655 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002656 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002657 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002658
Trond Myklebust1174dd12010-12-21 10:52:24 -05002659 msg.rpc_argp = &calldata->arg;
2660 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002661 task_setup_data.callback_data = calldata;
2662 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002663 if (IS_ERR(task))
2664 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002665 status = 0;
2666 if (wait)
2667 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002668 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002669 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002670out_free_calldata:
2671 kfree(calldata);
2672out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002673 nfs4_put_open_state(state);
2674 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002675 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Trond Myklebust2b484292010-09-17 10:56:51 -04002678static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002679nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2680 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002683 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2684
2685 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Trond Myklebust565277f2007-10-15 18:17:53 -04002687 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002688 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002689
2690 nfs4_label_release_security(label);
2691
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002692 if (IS_ERR(state))
2693 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002694 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Trond Myklebust1185a552009-12-03 15:54:02 -05002697static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002698{
2699 if (ctx->state == NULL)
2700 return;
2701 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002702 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002703 else
Al Viro643168c2011-06-22 18:20:23 -04002704 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Trond Myklebustb944dba2013-11-04 15:20:20 -05002707#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2708#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2709#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2712{
Benny Halevy43652ad2009-04-01 09:21:54 -04002713 struct nfs4_server_caps_arg args = {
2714 .fhandle = fhandle,
2715 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 struct nfs4_server_caps_res res = {};
2717 struct rpc_message msg = {
2718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002719 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 .rpc_resp = &res,
2721 };
2722 int status;
2723
Bryan Schumaker7c513052011-03-24 17:12:24 +00002724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002726 /* Sanity check the server answers */
2727 switch (server->nfs_client->cl_minorversion) {
2728 case 0:
2729 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2730 res.attr_bitmask[2] = 0;
2731 break;
2732 case 1:
2733 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2734 break;
2735 case 2:
2736 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002739 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2740 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2741 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2742 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002743 NFS_CAP_CTIME|NFS_CAP_MTIME|
2744 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002745 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2746 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 server->caps |= NFS_CAP_ACLS;
2748 if (res.has_links != 0)
2749 server->caps |= NFS_CAP_HARDLINKS;
2750 if (res.has_symlinks != 0)
2751 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002752 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2753 server->caps |= NFS_CAP_FILEID;
2754 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2755 server->caps |= NFS_CAP_MODE;
2756 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2757 server->caps |= NFS_CAP_NLINK;
2758 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2759 server->caps |= NFS_CAP_OWNER;
2760 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2761 server->caps |= NFS_CAP_OWNER_GROUP;
2762 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2763 server->caps |= NFS_CAP_ATIME;
2764 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2765 server->caps |= NFS_CAP_CTIME;
2766 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2767 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002768#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2769 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2770 server->caps |= NFS_CAP_SECURITY_LABEL;
2771#endif
2772 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2773 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002774 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002775
Trond Myklebusta65318b2009-03-11 14:10:28 -04002776 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2777 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2778 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002779 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002781 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002783
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return status;
2785}
2786
Trond Myklebust55a97592006-06-09 09:34:19 -04002787int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788{
2789 struct nfs4_exception exception = { };
2790 int err;
2791 do {
2792 err = nfs4_handle_exception(server,
2793 _nfs4_server_capabilities(server, fhandle),
2794 &exception);
2795 } while (exception.retry);
2796 return err;
2797}
2798
2799static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2800 struct nfs_fsinfo *info)
2801{
David Quigleyaa9c2662013-05-22 12:50:44 -04002802 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002804 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 };
2806 struct nfs4_lookup_res res = {
2807 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002808 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 .fh = fhandle,
2810 };
2811 struct rpc_message msg = {
2812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2813 .rpc_argp = &args,
2814 .rpc_resp = &res,
2815 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002816
David Quigleyaa9c2662013-05-22 12:50:44 -04002817 bitmask[0] = nfs4_fattr_bitmap[0];
2818 bitmask[1] = nfs4_fattr_bitmap[1];
2819 /*
2820 * Process the label in the upcoming getfattr
2821 */
2822 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2823
Trond Myklebust0e574af2005-10-27 22:12:38 -04002824 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002825 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2829 struct nfs_fsinfo *info)
2830{
2831 struct nfs4_exception exception = { };
2832 int err;
2833 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002834 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002835 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002836 switch (err) {
2837 case 0:
2838 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002839 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002840 default:
2841 err = nfs4_handle_exception(server, err, &exception);
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002844out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 return err;
2846}
2847
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002848static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2849 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2850{
Trond Myklebustc2190662013-08-26 19:23:04 -04002851 struct rpc_auth_create_args auth_args = {
2852 .pseudoflavor = flavor,
2853 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002854 struct rpc_auth *auth;
2855 int ret;
2856
Trond Myklebustc2190662013-08-26 19:23:04 -04002857 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002858 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002859 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002860 goto out;
2861 }
2862 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002863out:
2864 return ret;
2865}
2866
Chuck Lever9a744ba2013-03-16 15:56:02 -04002867/*
2868 * Retry pseudoroot lookup with various security flavors. We do this when:
2869 *
2870 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2871 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2872 *
2873 * Returns zero on success, or a negative NFS4ERR value, or a
2874 * negative errno value.
2875 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002876static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002877 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002879 /* Per 3530bis 15.33.5 */
2880 static const rpc_authflavor_t flav_array[] = {
2881 RPC_AUTH_GSS_KRB5P,
2882 RPC_AUTH_GSS_KRB5I,
2883 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002884 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002885 RPC_AUTH_NULL,
2886 };
2887 int status = -EPERM;
2888 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04002890 if (server->auth_info.flavor_len > 0) {
2891 /* try each flavor specified by user */
2892 for (i = 0; i < server->auth_info.flavor_len; i++) {
2893 status = nfs4_lookup_root_sec(server, fhandle, info,
2894 server->auth_info.flavors[i]);
2895 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2896 continue;
2897 break;
2898 }
2899 } else {
2900 /* no flavors specified by user, try default list */
2901 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2902 status = nfs4_lookup_root_sec(server, fhandle, info,
2903 flav_array[i]);
2904 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2905 continue;
2906 break;
2907 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002908 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002909
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002910 /*
2911 * -EACCESS could mean that the user doesn't have correct permissions
2912 * to access the mount. It could also mean that we tried to mount
2913 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2914 * existing mount programs don't handle -EACCES very well so it should
2915 * be mapped to -EPERM instead.
2916 */
2917 if (status == -EACCES)
2918 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002919 return status;
2920}
2921
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002922static int nfs4_do_find_root_sec(struct nfs_server *server,
2923 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2924{
2925 int mv = server->nfs_client->cl_minorversion;
2926 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2927}
2928
2929/**
2930 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2931 * @server: initialized nfs_server handle
2932 * @fhandle: we fill in the pseudo-fs root file handle
2933 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002934 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002935 *
2936 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002937 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002938int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002939 struct nfs_fsinfo *info,
2940 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002941{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002942 int status;
2943
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002944 switch (auth_probe) {
2945 case false:
2946 status = nfs4_lookup_root(server, fhandle, info);
2947 if (status != -NFS4ERR_WRONGSEC)
2948 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002949 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002950 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002951 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (status == 0)
2954 status = nfs4_server_capabilities(server, fhandle);
2955 if (status == 0)
2956 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002957
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002958 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
Bryan Schumakerbae36242012-05-10 15:07:31 -04002961static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2962 struct nfs_fsinfo *info)
2963{
2964 int error;
2965 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002966 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002967
2968 error = nfs4_server_capabilities(server, mntfh);
2969 if (error < 0) {
2970 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2971 return error;
2972 }
2973
David Quigley14c43f72013-05-22 12:50:43 -04002974 label = nfs4_label_alloc(server, GFP_KERNEL);
2975 if (IS_ERR(label))
2976 return PTR_ERR(label);
2977
David Quigley1775fd32013-05-22 12:50:42 -04002978 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002979 if (error < 0) {
2980 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002981 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002982 }
2983
2984 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2985 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2986 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2987
David Quigley14c43f72013-05-22 12:50:43 -04002988err_free_label:
2989 nfs4_label_free(label);
2990
Bryan Schumakerbae36242012-05-10 15:07:31 -04002991 return error;
2992}
2993
Manoj Naik6b97fd32006-06-09 09:34:29 -04002994/*
2995 * Get locations and (maybe) other attributes of a referral.
2996 * Note that we'll actually follow the referral later when
2997 * we detect fsid mismatch in inode revalidation
2998 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002999static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3000 const struct qstr *name, struct nfs_fattr *fattr,
3001 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003002{
3003 int status = -ENOMEM;
3004 struct page *page = NULL;
3005 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003006
3007 page = alloc_page(GFP_KERNEL);
3008 if (page == NULL)
3009 goto out;
3010 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3011 if (locations == NULL)
3012 goto out;
3013
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003014 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003015 if (status != 0)
3016 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003017
3018 /*
3019 * If the fsid didn't change, this is a migration event, not a
3020 * referral. Cause us to drop into the exception handler, which
3021 * will kick off migration recovery.
3022 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003023 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003024 dprintk("%s: server did not return a different fsid for"
3025 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003026 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003027 goto out;
3028 }
Andy Adamson533eb462011-06-13 18:25:56 -04003029 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3030 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003031
Andy Adamson533eb462011-06-13 18:25:56 -04003032 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003033 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003034 memset(fhandle, 0, sizeof(struct nfs_fh));
3035out:
3036 if (page)
3037 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003038 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003039 return status;
3040}
3041
David Quigley1775fd32013-05-22 12:50:42 -04003042static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3043 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
3045 struct nfs4_getattr_arg args = {
3046 .fh = fhandle,
3047 .bitmask = server->attr_bitmask,
3048 };
3049 struct nfs4_getattr_res res = {
3050 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003051 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 .server = server,
3053 };
3054 struct rpc_message msg = {
3055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3056 .rpc_argp = &args,
3057 .rpc_resp = &res,
3058 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003059
3060 args.bitmask = nfs4_bitmask(server, label);
3061
Trond Myklebust0e574af2005-10-27 22:12:38 -04003062 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003063 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
David Quigley1775fd32013-05-22 12:50:42 -04003066static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3067 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
3069 struct nfs4_exception exception = { };
3070 int err;
3071 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003072 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3073 trace_nfs4_getattr(server, fhandle, fattr, err);
3074 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 &exception);
3076 } while (exception.retry);
3077 return err;
3078}
3079
3080/*
3081 * The file is not closed if it is opened due to the a request to change
3082 * the size of the file. The open call will not be needed once the
3083 * VFS layer lookup-intents are implemented.
3084 *
3085 * Close is called when the inode is destroyed.
3086 * If we haven't opened the file for O_WRONLY, we
3087 * need to in the size_change case to obtain a stateid.
3088 *
3089 * Got race?
3090 * Because OPEN is always done by name in nfsv4, it is
3091 * possible that we opened a different file by the same
3092 * name. We can recognize this race condition, but we
3093 * can't do anything about it besides returning an error.
3094 *
3095 * This will be fixed with VFS changes (lookup-intent).
3096 */
3097static int
3098nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3099 struct iattr *sattr)
3100{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003101 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003102 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003103 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003104 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 int status;
3106
Benny Halevy8a1636c2010-07-14 15:43:57 -04003107 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003108 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003109
Trond Myklebust0e574af2005-10-27 22:12:38 -04003110 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Andy Adamson26699402012-05-30 16:12:24 -04003112 /* Deal with open(O_TRUNC) */
3113 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003114 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003115
3116 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003117 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003118 return 0;
3119
Trond Myklebustd5308382005-11-04 15:33:38 -05003120 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003121 if (sattr->ia_valid & ATTR_FILE) {
3122 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003123
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003124 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003125 if (ctx) {
3126 cred = ctx->cred;
3127 state = ctx->state;
3128 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003129 }
3130
David Quigley14c43f72013-05-22 12:50:43 -04003131 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3132 if (IS_ERR(label))
3133 return PTR_ERR(label);
3134
3135 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003136 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003137 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003138 nfs_setsecurity(inode, fattr, label);
3139 }
David Quigley14c43f72013-05-22 12:50:43 -04003140 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return status;
3142}
3143
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003144static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3145 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003146 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003147{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003148 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003149 int status;
3150 struct nfs4_lookup_arg args = {
3151 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003152 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003153 .name = name,
3154 };
3155 struct nfs4_lookup_res res = {
3156 .server = server,
3157 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003158 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003159 .fh = fhandle,
3160 };
3161 struct rpc_message msg = {
3162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3163 .rpc_argp = &args,
3164 .rpc_resp = &res,
3165 };
3166
David Quigleyaa9c2662013-05-22 12:50:44 -04003167 args.bitmask = nfs4_bitmask(server, label);
3168
David Howells2b3de442006-08-22 20:06:09 -04003169 nfs_fattr_init(fattr);
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003172 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 dprintk("NFS reply lookup: %d\n", status);
3174 return status;
3175}
3176
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003177static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003178{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003179 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003180 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003181 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3182 fattr->nlink = 2;
3183}
3184
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003185static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3186 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003187 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003188{
3189 struct nfs4_exception exception = { };
3190 struct rpc_clnt *client = *clnt;
3191 int err;
3192 do {
David Quigley1775fd32013-05-22 12:50:42 -04003193 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003194 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003195 switch (err) {
3196 case -NFS4ERR_BADNAME:
3197 err = -ENOENT;
3198 goto out;
3199 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003200 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003201 goto out;
3202 case -NFS4ERR_WRONGSEC:
3203 err = -EPERM;
3204 if (client != *clnt)
3205 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003206 client = nfs4_create_sec_client(client, dir, name);
3207 if (IS_ERR(client))
3208 return PTR_ERR(client);
3209
3210 exception.retry = 1;
3211 break;
3212 default:
3213 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3214 }
3215 } while (exception.retry);
3216
3217out:
3218 if (err == 0)
3219 *clnt = client;
3220 else if (client != *clnt)
3221 rpc_shutdown_client(client);
3222
3223 return err;
3224}
3225
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003226static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003227 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3228 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003230 int status;
3231 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003232
David Quigley1775fd32013-05-22 12:50:42 -04003233 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003234 if (client != NFS_CLIENT(dir)) {
3235 rpc_shutdown_client(client);
3236 nfs_fixup_secinfo_attributes(fattr);
3237 }
3238 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003241struct rpc_clnt *
3242nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3243 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3244{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003245 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003246 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003247
David Quigley1775fd32013-05-22 12:50:42 -04003248 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003249 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003250 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003251 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003252}
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3255{
Trond Myklebust76b32992007-08-10 17:45:11 -04003256 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct nfs4_accessargs args = {
3258 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003259 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003261 struct nfs4_accessres res = {
3262 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 struct rpc_message msg = {
3265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3266 .rpc_argp = &args,
3267 .rpc_resp = &res,
3268 .rpc_cred = entry->cred,
3269 };
3270 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003271 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /*
3274 * Determine which access bits we want to ask for...
3275 */
3276 if (mode & MAY_READ)
3277 args.access |= NFS4_ACCESS_READ;
3278 if (S_ISDIR(inode->i_mode)) {
3279 if (mode & MAY_WRITE)
3280 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3281 if (mode & MAY_EXEC)
3282 args.access |= NFS4_ACCESS_LOOKUP;
3283 } else {
3284 if (mode & MAY_WRITE)
3285 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3286 if (mode & MAY_EXEC)
3287 args.access |= NFS4_ACCESS_EXECUTE;
3288 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003289
3290 res.fattr = nfs_alloc_fattr();
3291 if (res.fattr == NULL)
3292 return -ENOMEM;
3293
Bryan Schumaker7c513052011-03-24 17:12:24 +00003294 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003296 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003297 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003299 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 return status;
3301}
3302
3303static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3304{
3305 struct nfs4_exception exception = { };
3306 int err;
3307 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003308 err = _nfs4_proc_access(inode, entry);
3309 trace_nfs4_access(inode, err);
3310 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 &exception);
3312 } while (exception.retry);
3313 return err;
3314}
3315
3316/*
3317 * TODO: For the time being, we don't try to get any attributes
3318 * along with any of the zero-copy operations READ, READDIR,
3319 * READLINK, WRITE.
3320 *
3321 * In the case of the first three, we want to put the GETATTR
3322 * after the read-type operation -- this is because it is hard
3323 * to predict the length of a GETATTR response in v4, and thus
3324 * align the READ data correctly. This means that the GETATTR
3325 * may end up partially falling into the page cache, and we should
3326 * shift it into the 'tail' of the xdr_buf before processing.
3327 * To do this efficiently, we need to know the total length
3328 * of data received, which doesn't seem to be available outside
3329 * of the RPC layer.
3330 *
3331 * In the case of WRITE, we also want to put the GETATTR after
3332 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003333 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 *
3335 * Both of these changes to the XDR layer would in fact be quite
3336 * minor, but I decided to leave them for a subsequent patch.
3337 */
3338static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3339 unsigned int pgbase, unsigned int pglen)
3340{
3341 struct nfs4_readlink args = {
3342 .fh = NFS_FH(inode),
3343 .pgbase = pgbase,
3344 .pglen = pglen,
3345 .pages = &page,
3346 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003347 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 struct rpc_message msg = {
3349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3350 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003351 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 };
3353
Bryan Schumaker7c513052011-03-24 17:12:24 +00003354 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 -07003355}
3356
3357static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3358 unsigned int pgbase, unsigned int pglen)
3359{
3360 struct nfs4_exception exception = { };
3361 int err;
3362 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003363 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3364 trace_nfs4_readlink(inode, err);
3365 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 &exception);
3367 } while (exception.retry);
3368 return err;
3369}
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003372 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374static int
3375nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003376 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
David Quigleyaa9c2662013-05-22 12:50:44 -04003378 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003379 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003381 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 int status = 0;
3383
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003384 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3385 if (IS_ERR(ctx))
3386 return PTR_ERR(ctx);
3387
David Quigleyaa9c2662013-05-22 12:50:44 -04003388 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3389
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003390 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003391 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (IS_ERR(state)) {
3393 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003397 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003398 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return status;
3400}
3401
3402static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3403{
Trond Myklebust16e42952005-10-27 22:12:44 -04003404 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003405 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003407 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003409 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003410 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003411 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3414 .rpc_argp = &args,
3415 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003417 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003418
Bryan Schumaker7c513052011-03-24 17:12:24 +00003419 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003420 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003421 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return status;
3423}
3424
3425static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3426{
3427 struct nfs4_exception exception = { };
3428 int err;
3429 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003430 err = _nfs4_proc_remove(dir, name);
3431 trace_nfs4_remove(dir, name, err);
3432 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 &exception);
3434 } while (exception.retry);
3435 return err;
3436}
3437
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003438static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003440 struct nfs_server *server = NFS_SERVER(dir);
3441 struct nfs_removeargs *args = msg->rpc_argp;
3442 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003444 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003446 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003447
3448 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003451static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3452{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003453 nfs4_setup_sequence(NFS_SERVER(data->dir),
3454 &data->args.seq_args,
3455 &data->res.seq_res,
3456 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003459static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003461 struct nfs_unlinkdata *data = task->tk_calldata;
3462 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003463
Trond Myklebust14516c32010-07-31 14:29:06 -04003464 if (!nfs4_sequence_done(task, &res->seq_res))
3465 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003466 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003467 return 0;
3468 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003469 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
Jeff Laytond3d41522010-09-17 17:31:57 -04003472static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3473{
3474 struct nfs_server *server = NFS_SERVER(dir);
3475 struct nfs_renameargs *arg = msg->rpc_argp;
3476 struct nfs_renameres *res = msg->rpc_resp;
3477
3478 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003479 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003480 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003481}
3482
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003483static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3484{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003485 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3486 &data->args.seq_args,
3487 &data->res.seq_res,
3488 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003489}
3490
3491static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3492 struct inode *new_dir)
3493{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003494 struct nfs_renamedata *data = task->tk_calldata;
3495 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003496
3497 if (!nfs4_sequence_done(task, &res->seq_res))
3498 return 0;
3499 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3500 return 0;
3501
3502 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003503 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003504 return 1;
3505}
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3508 struct inode *new_dir, struct qstr *new_name)
3509{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003510 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003511 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 .old_dir = NFS_FH(old_dir),
3513 .new_dir = NFS_FH(new_dir),
3514 .old_name = old_name,
3515 .new_name = new_name,
3516 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003517 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003518 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 struct rpc_message msg = {
3521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3522 .rpc_argp = &arg,
3523 .rpc_resp = &res,
3524 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003525 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003526
Bryan Schumaker7c513052011-03-24 17:12:24 +00003527 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 if (!status) {
3529 update_changeattr(old_dir, &res.old_cinfo);
3530 update_changeattr(new_dir, &res.new_cinfo);
3531 }
3532 return status;
3533}
3534
3535static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3536 struct inode *new_dir, struct qstr *new_name)
3537{
3538 struct nfs4_exception exception = { };
3539 int err;
3540 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003541 err = _nfs4_proc_rename(old_dir, old_name,
3542 new_dir, new_name);
3543 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3544 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 &exception);
3546 } while (exception.retry);
3547 return err;
3548}
3549
3550static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3551{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003552 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 struct nfs4_link_arg arg = {
3554 .fh = NFS_FH(inode),
3555 .dir_fh = NFS_FH(dir),
3556 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003557 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003559 struct nfs4_link_res res = {
3560 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003561 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 struct rpc_message msg = {
3564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3565 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003566 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003568 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Trond Myklebust136f2622010-04-16 16:22:49 -04003570 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003571 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003572 goto out;
3573
David Quigley14c43f72013-05-22 12:50:43 -04003574 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3575 if (IS_ERR(res.label)) {
3576 status = PTR_ERR(res.label);
3577 goto out;
3578 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003579 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003580
Bryan Schumaker7c513052011-03-24 17:12:24 +00003581 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003582 if (!status) {
3583 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003584 status = nfs_post_op_update_inode(inode, res.fattr);
3585 if (!status)
3586 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003587 }
David Quigley14c43f72013-05-22 12:50:43 -04003588
3589
3590 nfs4_label_free(res.label);
3591
Trond Myklebust136f2622010-04-16 16:22:49 -04003592out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003593 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return status;
3595}
3596
3597static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3598{
3599 struct nfs4_exception exception = { };
3600 int err;
3601 do {
3602 err = nfs4_handle_exception(NFS_SERVER(inode),
3603 _nfs4_proc_link(inode, dir, name),
3604 &exception);
3605 } while (exception.retry);
3606 return err;
3607}
3608
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003609struct nfs4_createdata {
3610 struct rpc_message msg;
3611 struct nfs4_create_arg arg;
3612 struct nfs4_create_res res;
3613 struct nfs_fh fh;
3614 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003615 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003616};
3617
3618static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3619 struct qstr *name, struct iattr *sattr, u32 ftype)
3620{
3621 struct nfs4_createdata *data;
3622
3623 data = kzalloc(sizeof(*data), GFP_KERNEL);
3624 if (data != NULL) {
3625 struct nfs_server *server = NFS_SERVER(dir);
3626
David Quigley14c43f72013-05-22 12:50:43 -04003627 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3628 if (IS_ERR(data->label))
3629 goto out_free;
3630
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3632 data->msg.rpc_argp = &data->arg;
3633 data->msg.rpc_resp = &data->res;
3634 data->arg.dir_fh = NFS_FH(dir);
3635 data->arg.server = server;
3636 data->arg.name = name;
3637 data->arg.attrs = sattr;
3638 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003639 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003640 data->res.server = server;
3641 data->res.fh = &data->fh;
3642 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003643 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003644 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003645 }
3646 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003647out_free:
3648 kfree(data);
3649 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003650}
3651
3652static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3653{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003654 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003655 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656 if (status == 0) {
3657 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003658 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 }
3660 return status;
3661}
3662
3663static void nfs4_free_createdata(struct nfs4_createdata *data)
3664{
David Quigley14c43f72013-05-22 12:50:43 -04003665 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003666 kfree(data);
3667}
3668
Chuck Lever4f390c12006-08-22 20:06:22 -04003669static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003670 struct page *page, unsigned int len, struct iattr *sattr,
3671 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003673 struct nfs4_createdata *data;
3674 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Chuck Lever94a6d752006-08-22 20:06:23 -04003676 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003678
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 status = -ENOMEM;
3680 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3681 if (data == NULL)
3682 goto out;
3683
3684 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3685 data->arg.u.symlink.pages = &page;
3686 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003687 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689 status = nfs4_do_create(dir, dentry, data);
3690
3691 nfs4_free_createdata(data);
3692out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return status;
3694}
3695
Chuck Lever4f390c12006-08-22 20:06:22 -04003696static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003697 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
3699 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003700 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003702
3703 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003706 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3707 trace_nfs4_symlink(dir, &dentry->d_name, err);
3708 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 &exception);
3710 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003711
3712 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return err;
3714}
3715
3716static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003717 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003719 struct nfs4_createdata *data;
3720 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003722 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3723 if (data == NULL)
3724 goto out;
3725
David Quigley1775fd32013-05-22 12:50:42 -04003726 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003727 status = nfs4_do_create(dir, dentry, data);
3728
3729 nfs4_free_createdata(data);
3730out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return status;
3732}
3733
3734static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3735 struct iattr *sattr)
3736{
3737 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003738 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003740
David Quigleyaa9c2662013-05-22 12:50:44 -04003741 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3742
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003743 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003745 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3746 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3747 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 &exception);
3749 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 nfs4_label_release_security(label);
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return err;
3753}
3754
3755static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003756 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 struct inode *dir = dentry->d_inode;
3759 struct nfs4_readdir_arg args = {
3760 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003761 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 .pgbase = 0,
3763 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003764 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003765 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 };
3767 struct nfs4_readdir_res res;
3768 struct rpc_message msg = {
3769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3770 .rpc_argp = &args,
3771 .rpc_resp = &res,
3772 .rpc_cred = cred,
3773 };
3774 int status;
3775
Al Viro6de14722013-09-16 10:53:17 -04003776 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3777 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003778 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003779 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003781 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 -05003782 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003783 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003784 status += args.pgbase;
3785 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003786
3787 nfs_invalidate_atime(dir);
3788
Harvey Harrison3110ff82008-05-02 13:42:44 -07003789 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return status;
3791}
3792
3793static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003794 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 struct nfs4_exception exception = { };
3797 int err;
3798 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003799 err = _nfs4_proc_readdir(dentry, cred, cookie,
3800 pages, count, plus);
3801 trace_nfs4_readdir(dentry->d_inode, err);
3802 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 &exception);
3804 } while (exception.retry);
3805 return err;
3806}
3807
3808static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003811 struct nfs4_createdata *data;
3812 int mode = sattr->ia_mode;
3813 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003815 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3816 if (data == NULL)
3817 goto out;
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003820 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003822 data->arg.ftype = NF4BLK;
3823 data->arg.u.device.specdata1 = MAJOR(rdev);
3824 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003827 data->arg.ftype = NF4CHR;
3828 data->arg.u.device.specdata1 = MAJOR(rdev);
3829 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003830 } else if (!S_ISSOCK(mode)) {
3831 status = -EINVAL;
3832 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
David Quigley1775fd32013-05-22 12:50:42 -04003834
David Quigleyaa9c2662013-05-22 12:50:44 -04003835 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003836 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003837out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 nfs4_free_createdata(data);
3839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return status;
3841}
3842
3843static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3844 struct iattr *sattr, dev_t rdev)
3845{
3846 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003847 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003849
David Quigleyaa9c2662013-05-22 12:50:44 -04003850 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3851
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003852 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003854 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3855 trace_nfs4_mknod(dir, &dentry->d_name, err);
3856 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 &exception);
3858 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003859
3860 nfs4_label_release_security(label);
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return err;
3863}
3864
3865static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3866 struct nfs_fsstat *fsstat)
3867{
3868 struct nfs4_statfs_arg args = {
3869 .fh = fhandle,
3870 .bitmask = server->attr_bitmask,
3871 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003872 struct nfs4_statfs_res res = {
3873 .fsstat = fsstat,
3874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3877 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003878 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
3880
Trond Myklebust0e574af2005-10-27 22:12:38 -04003881 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003882 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
3885static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3886{
3887 struct nfs4_exception exception = { };
3888 int err;
3889 do {
3890 err = nfs4_handle_exception(server,
3891 _nfs4_proc_statfs(server, fhandle, fsstat),
3892 &exception);
3893 } while (exception.retry);
3894 return err;
3895}
3896
3897static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3898 struct nfs_fsinfo *fsinfo)
3899{
3900 struct nfs4_fsinfo_arg args = {
3901 .fh = fhandle,
3902 .bitmask = server->attr_bitmask,
3903 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003904 struct nfs4_fsinfo_res res = {
3905 .fsinfo = fsinfo,
3906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 struct rpc_message msg = {
3908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3909 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003910 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 };
3912
Bryan Schumaker7c513052011-03-24 17:12:24 +00003913 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
3916static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3917{
3918 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003919 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 int err;
3921
3922 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003923 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003924 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003925 if (err == 0) {
3926 struct nfs_client *clp = server->nfs_client;
3927
3928 spin_lock(&clp->cl_lock);
3929 clp->cl_lease_time = fsinfo->lease_time * HZ;
3930 clp->cl_last_renewal = now;
3931 spin_unlock(&clp->cl_lock);
3932 break;
3933 }
3934 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 } while (exception.retry);
3936 return err;
3937}
3938
3939static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3940{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003941 int error;
3942
Trond Myklebust0e574af2005-10-27 22:12:38 -04003943 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003944 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003945 if (error == 0) {
3946 /* block layout checks this! */
3947 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003948 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003949 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003950
3951 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
3954static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3955 struct nfs_pathconf *pathconf)
3956{
3957 struct nfs4_pathconf_arg args = {
3958 .fh = fhandle,
3959 .bitmask = server->attr_bitmask,
3960 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003961 struct nfs4_pathconf_res res = {
3962 .pathconf = pathconf,
3963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3966 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003967 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 };
3969
3970 /* None of the pathconf attributes are mandatory to implement */
3971 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3972 memset(pathconf, 0, sizeof(*pathconf));
3973 return 0;
3974 }
3975
Trond Myklebust0e574af2005-10-27 22:12:38 -04003976 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003977 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
3980static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3981 struct nfs_pathconf *pathconf)
3982{
3983 struct nfs4_exception exception = { };
3984 int err;
3985
3986 do {
3987 err = nfs4_handle_exception(server,
3988 _nfs4_proc_pathconf(server, fhandle, pathconf),
3989 &exception);
3990 } while (exception.retry);
3991 return err;
3992}
3993
Trond Myklebust5521abf2013-03-16 20:54:34 -04003994int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003995 const struct nfs_open_context *ctx,
3996 const struct nfs_lock_context *l_ctx,
3997 fmode_t fmode)
3998{
3999 const struct nfs_lockowner *lockowner = NULL;
4000
4001 if (l_ctx != NULL)
4002 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004003 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004004}
4005EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4006
Trond Myklebust5521abf2013-03-16 20:54:34 -04004007static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4008 const struct nfs_open_context *ctx,
4009 const struct nfs_lock_context *l_ctx,
4010 fmode_t fmode)
4011{
4012 nfs4_stateid current_stateid;
4013
4014 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
4015 return false;
4016 return nfs4_stateid_match(stateid, &current_stateid);
4017}
4018
4019static bool nfs4_error_stateid_expired(int err)
4020{
4021 switch (err) {
4022 case -NFS4ERR_DELEG_REVOKED:
4023 case -NFS4ERR_ADMIN_REVOKED:
4024 case -NFS4ERR_BAD_STATEID:
4025 case -NFS4ERR_STALE_STATEID:
4026 case -NFS4ERR_OLD_STATEID:
4027 case -NFS4ERR_OPENMODE:
4028 case -NFS4ERR_EXPIRED:
4029 return true;
4030 }
4031 return false;
4032}
4033
Benny Halevyd20581a2011-05-22 19:52:03 +03004034void __nfs4_read_done_cb(struct nfs_read_data *data)
4035{
Fred Isamancd841602012-04-20 14:47:44 -04004036 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004037}
4038
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004039static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Fred Isamancd841602012-04-20 14:47:44 -04004041 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004043 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004044 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004045 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004046 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004048
Benny Halevyd20581a2011-05-22 19:52:03 +03004049 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004051 renew_lease(server, data->timestamp);
4052 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053}
4054
Trond Myklebust5521abf2013-03-16 20:54:34 -04004055static bool nfs4_read_stateid_changed(struct rpc_task *task,
4056 struct nfs_readargs *args)
4057{
4058
4059 if (!nfs4_error_stateid_expired(task->tk_status) ||
4060 nfs4_stateid_is_current(&args->stateid,
4061 args->context,
4062 args->lock_context,
4063 FMODE_READ))
4064 return false;
4065 rpc_restart_call_prepare(task);
4066 return true;
4067}
4068
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004069static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4070{
4071
4072 dprintk("--> %s\n", __func__);
4073
4074 if (!nfs4_sequence_done(task, &data->res.seq_res))
4075 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004076 if (nfs4_read_stateid_changed(task, &data->args))
4077 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004078 return data->read_done_cb ? data->read_done_cb(task, data) :
4079 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004080}
4081
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004082static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004085 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004086 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004087 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
NeilBrownef1820f2013-09-04 17:04:49 +10004090static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004091{
Trond Myklebust9b206142013-03-17 15:52:00 -04004092 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004093 &data->args.seq_args,
4094 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004095 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004096 return 0;
4097 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4098 data->args.lock_context, FMODE_READ) == -EIO)
4099 return -EIO;
4100 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4101 return -EIO;
4102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
Fred Isamanb029bc92011-03-03 15:13:42 +00004105static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106{
Fred Isamancd841602012-04-20 14:47:44 -04004107 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004109 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004110 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004111 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004112 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004114 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004116 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004117 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
Trond Myklebust5521abf2013-03-16 20:54:34 -04004121static bool nfs4_write_stateid_changed(struct rpc_task *task,
4122 struct nfs_writeargs *args)
4123{
4124
4125 if (!nfs4_error_stateid_expired(task->tk_status) ||
4126 nfs4_stateid_is_current(&args->stateid,
4127 args->context,
4128 args->lock_context,
4129 FMODE_WRITE))
4130 return false;
4131 rpc_restart_call_prepare(task);
4132 return true;
4133}
4134
Fred Isamanb029bc92011-03-03 15:13:42 +00004135static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4136{
4137 if (!nfs4_sequence_done(task, &data->res.seq_res))
4138 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004139 if (nfs4_write_stateid_changed(task, &data->args))
4140 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004141 return data->write_done_cb ? data->write_done_cb(task, data) :
4142 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004143}
4144
Trond Myklebust5a37f852012-04-28 14:55:16 -04004145static
4146bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004147{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004148 const struct nfs_pgio_header *hdr = data->header;
4149
4150 /* Don't request attributes for pNFS or O_DIRECT writes */
4151 if (data->ds_clp != NULL || hdr->dreq != NULL)
4152 return false;
4153 /* Otherwise, request attributes if and only if we don't hold
4154 * a delegation
4155 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004156 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004157}
Fred Isamana69aef12011-03-03 15:13:47 +00004158
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004159static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
Fred Isamancd841602012-04-20 14:47:44 -04004161 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004162
Trond Myklebust5a37f852012-04-28 14:55:16 -04004163 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004164 data->args.bitmask = NULL;
4165 data->res.fattr = NULL;
4166 } else
4167 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004168
Fred Isamanb029bc92011-03-03 15:13:42 +00004169 if (!data->write_done_cb)
4170 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004171 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 data->timestamp = jiffies;
4173
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004174 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004175 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
NeilBrownef1820f2013-09-04 17:04:49 +10004178static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004179{
Trond Myklebust9b206142013-03-17 15:52:00 -04004180 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004181 &data->args.seq_args,
4182 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004183 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004184 return 0;
4185 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4186 data->args.lock_context, FMODE_WRITE) == -EIO)
4187 return -EIO;
4188 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4189 return -EIO;
4190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Fred Isaman0b7c0152012-04-20 14:47:39 -04004193static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4194{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004195 nfs4_setup_sequence(NFS_SERVER(data->inode),
4196 &data->args.seq_args,
4197 &data->res.seq_res,
4198 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
Fred Isaman0b7c0152012-04-20 14:47:39 -04004201static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004204
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004205 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004206 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004207 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004208 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Fred Isaman0b7c0152012-04-20 14:47:39 -04004213static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004214{
4215 if (!nfs4_sequence_done(task, &data->res.seq_res))
4216 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004217 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004218}
4219
Fred Isaman0b7c0152012-04-20 14:47:39 -04004220static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004222 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004223
Fred Isaman0b7c0152012-04-20 14:47:39 -04004224 if (data->commit_done_cb == NULL)
4225 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004226 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004227 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004228 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229}
4230
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004231struct nfs4_renewdata {
4232 struct nfs_client *client;
4233 unsigned long timestamp;
4234};
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236/*
4237 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4238 * standalone procedure for queueing an asynchronous RENEW.
4239 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004240static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004241{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004242 struct nfs4_renewdata *data = calldata;
4243 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004244
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004245 if (atomic_read(&clp->cl_count) > 1)
4246 nfs4_schedule_state_renewal(clp);
4247 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004248 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004249}
4250
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004251static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004253 struct nfs4_renewdata *data = calldata;
4254 struct nfs_client *clp = data->client;
4255 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004257 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004258 switch (task->tk_status) {
4259 case 0:
4260 break;
4261 case -NFS4ERR_LEASE_MOVED:
4262 nfs4_schedule_lease_moved_recovery(clp);
4263 break;
4264 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004265 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004266 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4267 return;
4268 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004269 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004270 return;
4271 }
4272 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004274 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004277static const struct rpc_call_ops nfs4_renew_ops = {
4278 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004279 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004280};
4281
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004282static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 struct rpc_message msg = {
4285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4286 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004287 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004289 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004291 if (renew_flags == 0)
4292 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004293 if (!atomic_inc_not_zero(&clp->cl_count))
4294 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004295 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004296 if (data == NULL)
4297 return -ENOMEM;
4298 data->client = clp;
4299 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004300 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004301 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302}
4303
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004304static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305{
4306 struct rpc_message msg = {
4307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4308 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004309 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 };
4311 unsigned long now = jiffies;
4312 int status;
4313
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004314 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (status < 0)
4316 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004317 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return 0;
4319}
4320
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004321static inline int nfs4_server_supports_acls(struct nfs_server *server)
4322{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004323 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004324}
4325
Trond Myklebust21f498c2012-08-24 10:59:25 -04004326/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4327 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004328 * the stack.
4329 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004330#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004331
Neil Hormane9e3d722011-03-04 19:26:03 -05004332static int buf_to_pages_noslab(const void *buf, size_t buflen,
4333 struct page **pages, unsigned int *pgbase)
4334{
4335 struct page *newpage, **spages;
4336 int rc = 0;
4337 size_t len;
4338 spages = pages;
4339
4340 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004341 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004342 newpage = alloc_page(GFP_KERNEL);
4343
4344 if (newpage == NULL)
4345 goto unwind;
4346 memcpy(page_address(newpage), buf, len);
4347 buf += len;
4348 buflen -= len;
4349 *pages++ = newpage;
4350 rc++;
4351 } while (buflen != 0);
4352
4353 return rc;
4354
4355unwind:
4356 for(; rc > 0; rc--)
4357 __free_page(spages[rc-1]);
4358 return -ENOMEM;
4359}
4360
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004361struct nfs4_cached_acl {
4362 int cached;
4363 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004364 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004365};
4366
4367static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004368{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004369 struct nfs_inode *nfsi = NFS_I(inode);
4370
4371 spin_lock(&inode->i_lock);
4372 kfree(nfsi->nfs4_acl);
4373 nfsi->nfs4_acl = acl;
4374 spin_unlock(&inode->i_lock);
4375}
4376
4377static void nfs4_zap_acl_attr(struct inode *inode)
4378{
4379 nfs4_set_cached_acl(inode, NULL);
4380}
4381
4382static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4383{
4384 struct nfs_inode *nfsi = NFS_I(inode);
4385 struct nfs4_cached_acl *acl;
4386 int ret = -ENOENT;
4387
4388 spin_lock(&inode->i_lock);
4389 acl = nfsi->nfs4_acl;
4390 if (acl == NULL)
4391 goto out;
4392 if (buf == NULL) /* user is just asking for length */
4393 goto out_len;
4394 if (acl->cached == 0)
4395 goto out;
4396 ret = -ERANGE; /* see getxattr(2) man page */
4397 if (acl->len > buflen)
4398 goto out;
4399 memcpy(buf, acl->data, acl->len);
4400out_len:
4401 ret = acl->len;
4402out:
4403 spin_unlock(&inode->i_lock);
4404 return ret;
4405}
4406
Sachin Prabhu5794d212012-04-17 14:36:40 +01004407static 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 +00004408{
4409 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004410 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004411
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004412 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004413 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004414 if (acl == NULL)
4415 goto out;
4416 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004417 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004418 } else {
4419 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4420 if (acl == NULL)
4421 goto out;
4422 acl->cached = 0;
4423 }
4424 acl->len = acl_len;
4425out:
4426 nfs4_set_cached_acl(inode, acl);
4427}
4428
Andy Adamsonbf118a32011-12-07 11:55:27 -05004429/*
4430 * The getxattr API returns the required buffer length when called with a
4431 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4432 * the required buf. On a NULL buf, we send a page of data to the server
4433 * guessing that the ACL request can be serviced by a page. If so, we cache
4434 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4435 * the cache. If not so, we throw away the page, and cache the required
4436 * length. The next getxattr call will then produce another round trip to
4437 * the server, this time with the input buf of the required size.
4438 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004439static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004440{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004441 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004442 struct nfs_getaclargs args = {
4443 .fh = NFS_FH(inode),
4444 .acl_pages = pages,
4445 .acl_len = buflen,
4446 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004447 struct nfs_getaclres res = {
4448 .acl_len = buflen,
4449 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004450 struct rpc_message msg = {
4451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4452 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004453 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004454 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004455 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4456 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004457
Andy Adamsonbf118a32011-12-07 11:55:27 -05004458 /* As long as we're doing a round trip to the server anyway,
4459 * let's be prepared for a page of acl data. */
4460 if (npages == 0)
4461 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004462 if (npages > ARRAY_SIZE(pages))
4463 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004464
Andy Adamsonbf118a32011-12-07 11:55:27 -05004465 for (i = 0; i < npages; i++) {
4466 pages[i] = alloc_page(GFP_KERNEL);
4467 if (!pages[i])
4468 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004469 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004470
4471 /* for decoding across pages */
4472 res.acl_scratch = alloc_page(GFP_KERNEL);
4473 if (!res.acl_scratch)
4474 goto out_free;
4475
Andy Adamsonbf118a32011-12-07 11:55:27 -05004476 args.acl_len = npages * PAGE_SIZE;
4477 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004478
Peng Taode040be2012-01-10 22:42:47 +08004479 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004480 __func__, buf, buflen, npages, args.acl_len);
4481 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4482 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004483 if (ret)
4484 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004485
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004486 /* Handle the case where the passed-in buffer is too short */
4487 if (res.acl_flags & NFS4_ACL_TRUNC) {
4488 /* Did the user only issue a request for the acl length? */
4489 if (buf == NULL)
4490 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004491 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004492 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004493 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004494 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004495 if (buf) {
4496 if (res.acl_len > buflen) {
4497 ret = -ERANGE;
4498 goto out_free;
4499 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004500 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004501 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004502out_ok:
4503 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004504out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004505 for (i = 0; i < npages; i++)
4506 if (pages[i])
4507 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004508 if (res.acl_scratch)
4509 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004510 return ret;
4511}
4512
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004513static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4514{
4515 struct nfs4_exception exception = { };
4516 ssize_t ret;
4517 do {
4518 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004519 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004520 if (ret >= 0)
4521 break;
4522 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4523 } while (exception.retry);
4524 return ret;
4525}
4526
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004527static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4528{
4529 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004530 int ret;
4531
4532 if (!nfs4_server_supports_acls(server))
4533 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004534 ret = nfs_revalidate_inode(server, inode);
4535 if (ret < 0)
4536 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004537 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4538 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004539 ret = nfs4_read_cached_acl(inode, buf, buflen);
4540 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004541 /* -ENOENT is returned if there is no ACL or if there is an ACL
4542 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004543 return ret;
4544 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004545}
4546
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004547static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004548{
4549 struct nfs_server *server = NFS_SERVER(inode);
4550 struct page *pages[NFS4ACL_MAXPAGES];
4551 struct nfs_setaclargs arg = {
4552 .fh = NFS_FH(inode),
4553 .acl_pages = pages,
4554 .acl_len = buflen,
4555 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004556 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004557 struct rpc_message msg = {
4558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4559 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004560 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004561 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004562 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004563 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004564
4565 if (!nfs4_server_supports_acls(server))
4566 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004567 if (npages > ARRAY_SIZE(pages))
4568 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004569 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4570 if (i < 0)
4571 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004572 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004573 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004574
4575 /*
4576 * Free each page after tx, so the only ref left is
4577 * held by the network stack
4578 */
4579 for (; i > 0; i--)
4580 put_page(pages[i-1]);
4581
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004582 /*
4583 * Acl update can result in inode attribute update.
4584 * so mark the attribute cache invalid.
4585 */
4586 spin_lock(&inode->i_lock);
4587 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4588 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004589 nfs_access_zap_cache(inode);
4590 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004591 return ret;
4592}
4593
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004594static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4595{
4596 struct nfs4_exception exception = { };
4597 int err;
4598 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004599 err = __nfs4_proc_set_acl(inode, buf, buflen);
4600 trace_nfs4_set_acl(inode, err);
4601 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004602 &exception);
4603 } while (exception.retry);
4604 return err;
4605}
4606
David Quigleyaa9c2662013-05-22 12:50:44 -04004607#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4608static int _nfs4_get_security_label(struct inode *inode, void *buf,
4609 size_t buflen)
4610{
4611 struct nfs_server *server = NFS_SERVER(inode);
4612 struct nfs_fattr fattr;
4613 struct nfs4_label label = {0, 0, buflen, buf};
4614
4615 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004616 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004617 .fh = NFS_FH(inode),
4618 .bitmask = bitmask,
4619 };
4620 struct nfs4_getattr_res res = {
4621 .fattr = &fattr,
4622 .label = &label,
4623 .server = server,
4624 };
4625 struct rpc_message msg = {
4626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004627 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004628 .rpc_resp = &res,
4629 };
4630 int ret;
4631
4632 nfs_fattr_init(&fattr);
4633
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004634 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004635 if (ret)
4636 return ret;
4637 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4638 return -ENOENT;
4639 if (buflen < label.len)
4640 return -ERANGE;
4641 return 0;
4642}
4643
4644static int nfs4_get_security_label(struct inode *inode, void *buf,
4645 size_t buflen)
4646{
4647 struct nfs4_exception exception = { };
4648 int err;
4649
4650 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4651 return -EOPNOTSUPP;
4652
4653 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004654 err = _nfs4_get_security_label(inode, buf, buflen);
4655 trace_nfs4_get_security_label(inode, err);
4656 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004657 &exception);
4658 } while (exception.retry);
4659 return err;
4660}
4661
4662static int _nfs4_do_set_security_label(struct inode *inode,
4663 struct nfs4_label *ilabel,
4664 struct nfs_fattr *fattr,
4665 struct nfs4_label *olabel)
4666{
4667
4668 struct iattr sattr = {0};
4669 struct nfs_server *server = NFS_SERVER(inode);
4670 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004671 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004672 .fh = NFS_FH(inode),
4673 .iap = &sattr,
4674 .server = server,
4675 .bitmask = bitmask,
4676 .label = ilabel,
4677 };
4678 struct nfs_setattrres res = {
4679 .fattr = fattr,
4680 .label = olabel,
4681 .server = server,
4682 };
4683 struct rpc_message msg = {
4684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004685 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004686 .rpc_resp = &res,
4687 };
4688 int status;
4689
Jeff Layton12207f62013-11-01 10:49:32 -04004690 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004691
Jeff Layton12207f62013-11-01 10:49:32 -04004692 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004693 if (status)
4694 dprintk("%s failed: %d\n", __func__, status);
4695
4696 return status;
4697}
4698
4699static int nfs4_do_set_security_label(struct inode *inode,
4700 struct nfs4_label *ilabel,
4701 struct nfs_fattr *fattr,
4702 struct nfs4_label *olabel)
4703{
4704 struct nfs4_exception exception = { };
4705 int err;
4706
4707 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004708 err = _nfs4_do_set_security_label(inode, ilabel,
4709 fattr, olabel);
4710 trace_nfs4_set_security_label(inode, err);
4711 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004712 &exception);
4713 } while (exception.retry);
4714 return err;
4715}
4716
4717static int
4718nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4719{
4720 struct nfs4_label ilabel, *olabel = NULL;
4721 struct nfs_fattr fattr;
4722 struct rpc_cred *cred;
4723 struct inode *inode = dentry->d_inode;
4724 int status;
4725
4726 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4727 return -EOPNOTSUPP;
4728
4729 nfs_fattr_init(&fattr);
4730
4731 ilabel.pi = 0;
4732 ilabel.lfs = 0;
4733 ilabel.label = (char *)buf;
4734 ilabel.len = buflen;
4735
4736 cred = rpc_lookup_cred();
4737 if (IS_ERR(cred))
4738 return PTR_ERR(cred);
4739
4740 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4741 if (IS_ERR(olabel)) {
4742 status = -PTR_ERR(olabel);
4743 goto out;
4744 }
4745
4746 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4747 if (status == 0)
4748 nfs_setsecurity(inode, &fattr, olabel);
4749
4750 nfs4_label_free(olabel);
4751out:
4752 put_rpccred(cred);
4753 return status;
4754}
4755#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4756
4757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004759nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004761 struct nfs_client *clp = server->nfs_client;
4762
4763 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 return 0;
4765 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004766 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004767 case -NFS4ERR_ADMIN_REVOKED:
4768 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004769 if (state == NULL)
4770 break;
4771 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004772 case -NFS4ERR_OPENMODE:
4773 if (state == NULL)
4774 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004775 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004776 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004777 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004778 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004779 if (state != NULL) {
4780 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004781 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004784 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004785 nfs4_schedule_lease_recovery(clp);
4786 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004787 case -NFS4ERR_MOVED:
4788 if (nfs4_schedule_migration_recovery(server) < 0)
4789 goto recovery_failed;
4790 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004791 case -NFS4ERR_LEASE_MOVED:
4792 nfs4_schedule_lease_moved_recovery(clp);
4793 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004794#if defined(CONFIG_NFS_V4_1)
4795 case -NFS4ERR_BADSESSION:
4796 case -NFS4ERR_BADSLOT:
4797 case -NFS4ERR_BAD_HIGH_SLOT:
4798 case -NFS4ERR_DEADSESSION:
4799 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4800 case -NFS4ERR_SEQ_FALSE_RETRY:
4801 case -NFS4ERR_SEQ_MISORDERED:
4802 dprintk("%s ERROR %d, Reset session\n", __func__,
4803 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004804 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004805 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004806#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004808 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004809 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004811 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004813 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 }
4815 task->tk_status = nfs4_map_errors(task->tk_status);
4816 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004817recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004818 task->tk_status = -EIO;
4819 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004820wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004821 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004822 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4823 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004824 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4825 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004826restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004827 task->tk_status = 0;
4828 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
Chuck Leverf0920752012-05-21 22:45:41 -04004831static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4832 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004833{
4834 __be32 verf[2];
4835
Chuck Lever2c820d92012-05-21 22:45:33 -04004836 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4837 /* An impossible timestamp guarantees this value
4838 * will never match a generated boot time. */
4839 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004840 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004841 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004842 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004843 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4844 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004845 }
Chuck Levercd937102012-03-02 17:14:31 -05004846 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4847}
4848
Chuck Levere984a552012-09-14 17:24:21 -04004849static unsigned int
4850nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4851 char *buf, size_t len)
4852{
4853 unsigned int result;
4854
4855 rcu_read_lock();
4856 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4857 clp->cl_ipaddr,
4858 rpc_peeraddr2str(clp->cl_rpcclient,
4859 RPC_DISPLAY_ADDR),
4860 rpc_peeraddr2str(clp->cl_rpcclient,
4861 RPC_DISPLAY_PROTO));
4862 rcu_read_unlock();
4863 return result;
4864}
4865
4866static unsigned int
4867nfs4_init_uniform_client_string(const struct nfs_client *clp,
4868 char *buf, size_t len)
4869{
Trond Myklebust55b59292013-07-24 16:36:35 -04004870 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004871
4872 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004873 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4874 clp->rpc_ops->version,
4875 clp->cl_minorversion,
4876 nfs4_client_id_uniquifier,
4877 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004878 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4879 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004880 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004881}
4882
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004883/**
4884 * nfs4_proc_setclientid - Negotiate client ID
4885 * @clp: state data structure
4886 * @program: RPC program for NFSv4 callback service
4887 * @port: IP port number for NFS4 callback service
4888 * @cred: RPC credential to use for this call
4889 * @res: where to place the result
4890 *
4891 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4892 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004893int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4894 unsigned short port, struct rpc_cred *cred,
4895 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
4897 nfs4_verifier sc_verifier;
4898 struct nfs4_setclientid setclientid = {
4899 .sc_verifier = &sc_verifier,
4900 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004901 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 };
4903 struct rpc_message msg = {
4904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4905 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004906 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004907 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004909 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Chuck Leverde734832012-07-11 16:30:50 -04004911 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004912 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004913 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4914 setclientid.sc_name_len =
4915 nfs4_init_uniform_client_string(clp,
4916 setclientid.sc_name,
4917 sizeof(setclientid.sc_name));
4918 else
4919 setclientid.sc_name_len =
4920 nfs4_init_nonuniform_client_string(clp,
4921 setclientid.sc_name,
4922 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004923 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004924 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004925 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004926 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004927 rpc_peeraddr2str(clp->cl_rpcclient,
4928 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004929 rcu_read_unlock();
4930 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004931 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 clp->cl_ipaddr, port >> 8, port & 255);
4933
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004934 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4935 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4936 setclientid.sc_name_len, setclientid.sc_name);
4937 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004938 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004939 dprintk("NFS reply setclientid: %d\n", status);
4940 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004943/**
4944 * nfs4_proc_setclientid_confirm - Confirm client ID
4945 * @clp: state data structure
4946 * @res: result of a previous SETCLIENTID
4947 * @cred: RPC credential to use for this call
4948 *
4949 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4950 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004951int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004952 struct nfs4_setclientid_res *arg,
4953 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 struct rpc_message msg = {
4956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004957 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004958 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 int status;
4961
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004962 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4963 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4964 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004965 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004966 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004967 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 return status;
4969}
4970
Trond Myklebustfe650402006-01-03 09:55:18 +01004971struct nfs4_delegreturndata {
4972 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004973 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004974 struct nfs_fh fh;
4975 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004976 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004977 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004978 int rpc_status;
4979};
4980
Trond Myklebustfe650402006-01-03 09:55:18 +01004981static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4982{
4983 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004984
Trond Myklebust14516c32010-07-31 14:29:06 -04004985 if (!nfs4_sequence_done(task, &data->res.seq_res))
4986 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004987
Trond Myklebustca8acf82013-08-13 10:36:56 -04004988 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004989 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05004990 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004991 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004992 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05004993 case -NFS4ERR_ADMIN_REVOKED:
4994 case -NFS4ERR_DELEG_REVOKED:
4995 case -NFS4ERR_BAD_STATEID:
4996 case -NFS4ERR_OLD_STATEID:
4997 case -NFS4ERR_STALE_STATEID:
4998 case -NFS4ERR_EXPIRED:
4999 task->tk_status = 0;
5000 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005001 default:
5002 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5003 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005004 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005005 return;
5006 }
5007 }
5008 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005009}
5010
5011static void nfs4_delegreturn_release(void *calldata)
5012{
Trond Myklebustfe650402006-01-03 09:55:18 +01005013 kfree(calldata);
5014}
5015
Andy Adamson938e1012009-04-01 09:22:28 -04005016static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5017{
5018 struct nfs4_delegreturndata *d_data;
5019
5020 d_data = (struct nfs4_delegreturndata *)data;
5021
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005022 nfs4_setup_sequence(d_data->res.server,
5023 &d_data->args.seq_args,
5024 &d_data->res.seq_res,
5025 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005026}
Andy Adamson938e1012009-04-01 09:22:28 -04005027
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005028static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005029 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005030 .rpc_call_done = nfs4_delegreturn_done,
5031 .rpc_release = nfs4_delegreturn_release,
5032};
5033
Trond Myklebuste6f81072008-01-24 18:14:34 -05005034static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005035{
5036 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005037 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005038 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5041 .rpc_cred = cred,
5042 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005043 struct rpc_task_setup task_setup_data = {
5044 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005045 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005046 .callback_ops = &nfs4_delegreturn_ops,
5047 .flags = RPC_TASK_ASYNC,
5048 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005049 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005050
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005051 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005052 if (data == NULL)
5053 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005054 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005055 data->args.fhandle = &data->fh;
5056 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005057 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005058 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005059 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005060 data->res.fattr = &data->fattr;
5061 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005062 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005063 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005064 data->rpc_status = 0;
5065
Trond Myklebustc970aa82007-07-14 15:39:59 -04005066 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005067 msg.rpc_argp = &data->args;
5068 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005069 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005070 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005071 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005072 if (!issync)
5073 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005074 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005075 if (status != 0)
5076 goto out;
5077 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005078 if (status == 0)
5079 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5080 else
5081 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005082out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005083 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005084 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085}
5086
Trond Myklebuste6f81072008-01-24 18:14:34 -05005087int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088{
5089 struct nfs_server *server = NFS_SERVER(inode);
5090 struct nfs4_exception exception = { };
5091 int err;
5092 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005093 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005094 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 switch (err) {
5096 case -NFS4ERR_STALE_STATEID:
5097 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 case 0:
5099 return 0;
5100 }
5101 err = nfs4_handle_exception(server, err, &exception);
5102 } while (exception.retry);
5103 return err;
5104}
5105
5106#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5107#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5108
5109/*
5110 * sleep, with exponential backoff, and retry the LOCK operation.
5111 */
5112static unsigned long
5113nfs4_set_lock_task_retry(unsigned long timeout)
5114{
Colin Cross416ad3c2013-05-06 23:50:06 +00005115 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 timeout <<= 1;
5117 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5118 return NFS4_LOCK_MAXTIMEOUT;
5119 return timeout;
5120}
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5123{
5124 struct inode *inode = state->inode;
5125 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005126 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005127 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005129 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005131 struct nfs_lockt_res res = {
5132 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 };
5134 struct rpc_message msg = {
5135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5136 .rpc_argp = &arg,
5137 .rpc_resp = &res,
5138 .rpc_cred = state->owner->so_cred,
5139 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 struct nfs4_lock_state *lsp;
5141 int status;
5142
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005143 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005144 status = nfs4_set_lock_state(state, request);
5145 if (status != 0)
5146 goto out;
5147 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005148 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005149 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005150 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005151 switch (status) {
5152 case 0:
5153 request->fl_type = F_UNLCK;
5154 break;
5155 case -NFS4ERR_DENIED:
5156 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005158 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005159 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005160out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return status;
5162}
5163
5164static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5165{
5166 struct nfs4_exception exception = { };
5167 int err;
5168
5169 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005170 err = _nfs4_proc_getlk(state, cmd, request);
5171 trace_nfs4_get_lock(request, state, cmd, err);
5172 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 &exception);
5174 } while (exception.retry);
5175 return err;
5176}
5177
5178static int do_vfs_lock(struct file *file, struct file_lock *fl)
5179{
5180 int res = 0;
5181 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5182 case FL_POSIX:
5183 res = posix_lock_file_wait(file, fl);
5184 break;
5185 case FL_FLOCK:
5186 res = flock_lock_file_wait(file, fl);
5187 break;
5188 default:
5189 BUG();
5190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 return res;
5192}
5193
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005194struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005195 struct nfs_locku_args arg;
5196 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005197 struct nfs4_lock_state *lsp;
5198 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005199 struct file_lock fl;
5200 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005201 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005202};
5203
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005204static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5205 struct nfs_open_context *ctx,
5206 struct nfs4_lock_state *lsp,
5207 struct nfs_seqid *seqid)
5208{
5209 struct nfs4_unlockdata *p;
5210 struct inode *inode = lsp->ls_state->inode;
5211
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005212 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005213 if (p == NULL)
5214 return NULL;
5215 p->arg.fh = NFS_FH(inode);
5216 p->arg.fl = &p->fl;
5217 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005218 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005219 p->arg.stateid = &lsp->ls_stateid;
5220 p->lsp = lsp;
5221 atomic_inc(&lsp->ls_count);
5222 /* Ensure we don't close file until we're done freeing locks! */
5223 p->ctx = get_nfs_open_context(ctx);
5224 memcpy(&p->fl, fl, sizeof(p->fl));
5225 p->server = NFS_SERVER(inode);
5226 return p;
5227}
5228
Trond Myklebust06f814a2006-01-03 09:55:07 +01005229static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005230{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005231 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005232 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005233 nfs4_put_lock_state(calldata->lsp);
5234 put_nfs_open_context(calldata->ctx);
5235 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005236}
5237
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005238static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005239{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005240 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005241
Trond Myklebust14516c32010-07-31 14:29:06 -04005242 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5243 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005244 switch (task->tk_status) {
5245 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005246 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5247 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005248 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005249 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005250 case -NFS4ERR_BAD_STATEID:
5251 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005252 case -NFS4ERR_STALE_STATEID:
5253 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005254 break;
5255 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005256 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005257 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005258 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005259 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005260}
5261
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005262static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005263{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005264 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005266 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005267 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005268 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005269 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005270 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005271 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005272 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005273 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005274 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005275 &calldata->res.seq_res,
5276 task) != 0)
5277 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005278 return;
5279out_no_action:
5280 task->tk_action = NULL;
5281out_wait:
5282 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005285static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005286 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005287 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005288 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005289};
5290
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005291static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5292 struct nfs_open_context *ctx,
5293 struct nfs4_lock_state *lsp,
5294 struct nfs_seqid *seqid)
5295{
5296 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005297 struct rpc_message msg = {
5298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5299 .rpc_cred = ctx->cred,
5300 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005301 struct rpc_task_setup task_setup_data = {
5302 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005303 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005304 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005305 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005306 .flags = RPC_TASK_ASYNC,
5307 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005308
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005309 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5310 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5311
Frank Filz137d6ac2007-07-09 15:32:29 -07005312 /* Ensure this is an unlock - when canceling a lock, the
5313 * canceled lock is passed in, and it won't be an unlock.
5314 */
5315 fl->fl_type = F_UNLCK;
5316
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005317 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5318 if (data == NULL) {
5319 nfs_free_seqid(seqid);
5320 return ERR_PTR(-ENOMEM);
5321 }
5322
Chuck Levera9c92d62013-08-09 12:48:18 -04005323 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005324 msg.rpc_argp = &data->arg;
5325 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005326 task_setup_data.callback_data = data;
5327 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005328}
5329
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5331{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005332 struct inode *inode = state->inode;
5333 struct nfs4_state_owner *sp = state->owner;
5334 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005335 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005336 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005337 struct rpc_task *task;
5338 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005339 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
Trond Myklebust9b073572006-06-29 16:38:34 -04005341 status = nfs4_set_lock_state(state, request);
5342 /* Unlock _before_ we do the RPC call */
5343 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005344 /* Exclude nfs_delegation_claim_locks() */
5345 mutex_lock(&sp->so_delegreturn_mutex);
5346 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005347 down_read(&nfsi->rwsem);
5348 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5349 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005350 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005351 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005352 }
5353 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005354 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005355 if (status != 0)
5356 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005357 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005358 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005359 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5360 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005361 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005362 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005363 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005364 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005365 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005366 status = PTR_ERR(task);
5367 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005368 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005369 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005370 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005371out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005372 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005373 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005374 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375}
5376
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005377struct nfs4_lockdata {
5378 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005379 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005380 struct nfs4_lock_state *lsp;
5381 struct nfs_open_context *ctx;
5382 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005383 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005384 int rpc_status;
5385 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005386 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005387};
5388
5389static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005390 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5391 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005392{
5393 struct nfs4_lockdata *p;
5394 struct inode *inode = lsp->ls_state->inode;
5395 struct nfs_server *server = NFS_SERVER(inode);
5396
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005397 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005398 if (p == NULL)
5399 return NULL;
5400
5401 p->arg.fh = NFS_FH(inode);
5402 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005403 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005404 if (p->arg.open_seqid == NULL)
5405 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005406 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005407 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005408 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005409 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005410 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005411 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005412 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005413 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005414 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005415 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005416 atomic_inc(&lsp->ls_count);
5417 p->ctx = get_nfs_open_context(ctx);
5418 memcpy(&p->fl, fl, sizeof(p->fl));
5419 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005420out_free_seqid:
5421 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005422out_free:
5423 kfree(p);
5424 return NULL;
5425}
5426
5427static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5428{
5429 struct nfs4_lockdata *data = calldata;
5430 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
Harvey Harrison3110ff82008-05-02 13:42:44 -07005432 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005433 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005434 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435 /* Do we need to do an open_to_lock_owner? */
5436 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005437 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005438 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005439 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005440 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005441 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005442 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005443 } else
5444 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005445 if (!nfs4_valid_open_stateid(state)) {
5446 data->rpc_status = -EBADF;
5447 task->tk_action = NULL;
5448 goto out_release_open_seqid;
5449 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005450 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005451 if (nfs4_setup_sequence(data->server,
5452 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005453 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005454 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005455 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005456out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005457 nfs_release_seqid(data->arg.open_seqid);
5458out_release_lock_seqid:
5459 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005460out_wait:
5461 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005462 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005463}
5464
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005465static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5466{
5467 struct nfs4_lockdata *data = calldata;
5468
Harvey Harrison3110ff82008-05-02 13:42:44 -07005469 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005470
Trond Myklebust14516c32010-07-31 14:29:06 -04005471 if (!nfs4_sequence_done(task, &data->res.seq_res))
5472 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005473
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005474 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005475 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005476 if (data->rpc_status == 0)
5477 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5478 else
5479 goto out;
5480 }
5481 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005482 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005483 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005484 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005485 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005486out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005487 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005488}
5489
5490static void nfs4_lock_release(void *calldata)
5491{
5492 struct nfs4_lockdata *data = calldata;
5493
Harvey Harrison3110ff82008-05-02 13:42:44 -07005494 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005495 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005496 if (data->cancelled != 0) {
5497 struct rpc_task *task;
5498 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5499 data->arg.lock_seqid);
5500 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005501 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005502 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005503 } else
5504 nfs_free_seqid(data->arg.lock_seqid);
5505 nfs4_put_lock_state(data->lsp);
5506 put_nfs_open_context(data->ctx);
5507 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005508 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005509}
5510
5511static const struct rpc_call_ops nfs4_lock_ops = {
5512 .rpc_call_prepare = nfs4_lock_prepare,
5513 .rpc_call_done = nfs4_lock_done,
5514 .rpc_release = nfs4_lock_release,
5515};
5516
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005517static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5518{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005519 switch (error) {
5520 case -NFS4ERR_ADMIN_REVOKED:
5521 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005522 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005523 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005524 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005525 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005526 break;
5527 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005528 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005529 case -NFS4ERR_EXPIRED:
5530 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005531 };
5532}
5533
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005534static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005535{
5536 struct nfs4_lockdata *data;
5537 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5540 .rpc_cred = state->owner->so_cred,
5541 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005542 struct rpc_task_setup task_setup_data = {
5543 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005544 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005545 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005546 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005547 .flags = RPC_TASK_ASYNC,
5548 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005549 int ret;
5550
Harvey Harrison3110ff82008-05-02 13:42:44 -07005551 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005552 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005553 fl->fl_u.nfs4_fl.owner,
5554 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005555 if (data == NULL)
5556 return -ENOMEM;
5557 if (IS_SETLKW(cmd))
5558 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005559 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005560 msg.rpc_argp = &data->arg;
5561 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005562 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005563 if (recovery_type > NFS_LOCK_NEW) {
5564 if (recovery_type == NFS_LOCK_RECLAIM)
5565 data->arg.reclaim = NFS_LOCK_RECLAIM;
5566 nfs4_set_sequence_privileged(&data->arg.seq_args);
5567 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005568 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005569 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005570 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005571 ret = nfs4_wait_for_completion_rpc_task(task);
5572 if (ret == 0) {
5573 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005574 if (ret)
5575 nfs4_handle_setlk_error(data->server, data->lsp,
5576 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005577 } else
5578 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005579 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005580 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582}
5583
5584static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5585{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005586 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005587 struct nfs4_exception exception = {
5588 .inode = state->inode,
5589 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005590 int err;
5591
5592 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005593 /* Cache the lock if possible... */
5594 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5595 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005596 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005597 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005598 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005599 break;
5600 nfs4_handle_exception(server, err, &exception);
5601 } while (exception.retry);
5602 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603}
5604
5605static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5606{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005607 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005608 struct nfs4_exception exception = {
5609 .inode = state->inode,
5610 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005611 int err;
5612
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005613 err = nfs4_set_lock_state(state, request);
5614 if (err != 0)
5615 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005616 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005617 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5618 return 0;
5619 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005620 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005621 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5622 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005623 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005624 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005625 switch (err) {
5626 default:
5627 goto out;
5628 case -NFS4ERR_GRACE:
5629 case -NFS4ERR_DELAY:
5630 nfs4_handle_exception(server, err, &exception);
5631 err = 0;
5632 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005633 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005634out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005635 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005638#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005639/**
5640 * nfs41_check_expired_locks - possibly free a lock stateid
5641 *
5642 * @state: NFSv4 state for an inode
5643 *
5644 * Returns NFS_OK if recovery for this stateid is now finished.
5645 * Otherwise a negative NFS4ERR value is returned.
5646 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005647static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005648{
Chuck Levereb64cf92012-07-11 16:30:05 -04005649 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005650 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005651 struct nfs_server *server = NFS_SERVER(state->inode);
5652
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005653 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005654 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005655 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5656
5657 status = nfs41_test_stateid(server,
5658 &lsp->ls_stateid,
5659 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005660 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005661 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005662 /* Free the stateid unless the server
5663 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005664 if (status != -NFS4ERR_BAD_STATEID)
5665 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005666 &lsp->ls_stateid,
5667 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005668 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005669 ret = status;
5670 }
5671 }
5672 };
5673
5674 return ret;
5675}
5676
5677static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5678{
5679 int status = NFS_OK;
5680
5681 if (test_bit(LK_STATE_IN_USE, &state->flags))
5682 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005683 if (status != NFS_OK)
5684 status = nfs4_lock_expired(state, request);
5685 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005686}
5687#endif
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5690{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005691 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005692 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005693 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005694 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005695 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005697 if ((fl_flags & FL_POSIX) &&
5698 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5699 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005700 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005701 status = nfs4_set_lock_state(state, request);
5702 if (status != 0)
5703 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005704 request->fl_flags |= FL_ACCESS;
5705 status = do_vfs_lock(request->fl_file, request);
5706 if (status < 0)
5707 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005708 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005709 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005710 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005711 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005712 request->fl_flags = fl_flags & ~FL_SLEEP;
5713 status = do_vfs_lock(request->fl_file, request);
5714 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005715 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005716 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5717 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005718 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005719 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005720 goto out;
5721 down_read(&nfsi->rwsem);
5722 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5723 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005724 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005725 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005726 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005727 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005728 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005729 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5730 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005731out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005732 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005733out:
5734 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 return status;
5736}
5737
5738static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5739{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005740 struct nfs4_exception exception = {
5741 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005742 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005743 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 int err;
5745
5746 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005747 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005748 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005749 if (err == -NFS4ERR_DENIED)
5750 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005752 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 } while (exception.retry);
5754 return err;
5755}
5756
5757static int
5758nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5759{
5760 struct nfs_open_context *ctx;
5761 struct nfs4_state *state;
5762 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5763 int status;
5764
5765 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005766 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 state = ctx->state;
5768
5769 if (request->fl_start < 0 || request->fl_end < 0)
5770 return -EINVAL;
5771
Trond Myklebustd9531262009-07-21 19:22:38 -04005772 if (IS_GETLK(cmd)) {
5773 if (state != NULL)
5774 return nfs4_proc_getlk(state, F_GETLK, request);
5775 return 0;
5776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777
5778 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5779 return -EINVAL;
5780
Trond Myklebustd9531262009-07-21 19:22:38 -04005781 if (request->fl_type == F_UNLCK) {
5782 if (state != NULL)
5783 return nfs4_proc_unlck(state, cmd, request);
5784 return 0;
5785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
Trond Myklebustd9531262009-07-21 19:22:38 -04005787 if (state == NULL)
5788 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005789 /*
5790 * Don't rely on the VFS having checked the file open mode,
5791 * since it won't do this for flock() locks.
5792 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005793 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005794 case F_RDLCK:
5795 if (!(filp->f_mode & FMODE_READ))
5796 return -EBADF;
5797 break;
5798 case F_WRLCK:
5799 if (!(filp->f_mode & FMODE_WRITE))
5800 return -EBADF;
5801 }
5802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 do {
5804 status = nfs4_proc_setlk(state, cmd, request);
5805 if ((status != -EAGAIN) || IS_SETLK(cmd))
5806 break;
5807 timeout = nfs4_set_lock_task_retry(timeout);
5808 status = -ERESTARTSYS;
5809 if (signalled())
5810 break;
5811 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return status;
5813}
5814
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005815int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005816{
5817 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005818 int err;
5819
5820 err = nfs4_set_lock_state(state, fl);
5821 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005822 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005823 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005824 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005825}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005826
Trond Myklebustcf470c32012-03-07 13:49:12 -05005827struct nfs_release_lockowner_data {
5828 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005829 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005830 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005831 struct nfs4_sequence_args seq_args;
5832 struct nfs4_sequence_res seq_res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005833 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005834};
5835
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005836static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5837{
5838 struct nfs_release_lockowner_data *data = calldata;
5839 nfs40_setup_sequence(data->server,
5840 &data->seq_args, &data->seq_res, task);
Chuck Lever60ea6812013-10-17 14:13:47 -04005841 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005842}
5843
5844static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5845{
5846 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005847 struct nfs_server *server = data->server;
5848
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005849 nfs40_sequence_done(task, &data->seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005850
5851 switch (task->tk_status) {
5852 case 0:
5853 renew_lease(server, data->timestamp);
5854 break;
5855 case -NFS4ERR_STALE_CLIENTID:
5856 case -NFS4ERR_EXPIRED:
5857 case -NFS4ERR_LEASE_MOVED:
5858 case -NFS4ERR_DELAY:
5859 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5860 rpc_restart_call_prepare(task);
5861 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005862}
5863
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005864static void nfs4_release_lockowner_release(void *calldata)
5865{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005866 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005867 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005868 kfree(calldata);
5869}
5870
Trond Myklebust17280172012-03-11 13:11:00 -04005871static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005872 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5873 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005874 .rpc_release = nfs4_release_lockowner_release,
5875};
5876
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005877static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005878{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005879 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005880 struct rpc_message msg = {
5881 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5882 };
5883
5884 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005885 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005886
Trond Myklebustcf470c32012-03-07 13:49:12 -05005887 data = kmalloc(sizeof(*data), GFP_NOFS);
5888 if (!data)
5889 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005890 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005891 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005892 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005893 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5894 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5895 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005896
Trond Myklebustcf470c32012-03-07 13:49:12 -05005897 msg.rpc_argp = &data->args;
5898 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5899 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005900}
5901
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005902#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5903
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005904static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5905 const void *buf, size_t buflen,
5906 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005907{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005908 if (strcmp(key, "") != 0)
5909 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005910
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005911 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005912}
5913
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005914static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5915 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005916{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005917 if (strcmp(key, "") != 0)
5918 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005919
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005920 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005921}
5922
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005923static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5924 size_t list_len, const char *name,
5925 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005926{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005927 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005928
J. Bruce Fields096455a2006-03-20 23:23:42 -05005929 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5930 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005931
5932 if (list && len <= list_len)
5933 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005934 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005935}
5936
David Quigleyc9bccef2013-05-22 12:50:45 -04005937#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5938static inline int nfs4_server_supports_labels(struct nfs_server *server)
5939{
5940 return server->caps & NFS_CAP_SECURITY_LABEL;
5941}
5942
5943static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5944 const void *buf, size_t buflen,
5945 int flags, int type)
5946{
5947 if (security_ismaclabel(key))
5948 return nfs4_set_security_label(dentry, buf, buflen);
5949
5950 return -EOPNOTSUPP;
5951}
5952
5953static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5954 void *buf, size_t buflen, int type)
5955{
5956 if (security_ismaclabel(key))
5957 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5958 return -EOPNOTSUPP;
5959}
5960
5961static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5962 size_t list_len, const char *name,
5963 size_t name_len, int type)
5964{
5965 size_t len = 0;
5966
5967 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5968 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5969 if (list && len <= list_len)
5970 security_inode_listsecurity(dentry->d_inode, list, len);
5971 }
5972 return len;
5973}
5974
5975static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5976 .prefix = XATTR_SECURITY_PREFIX,
5977 .list = nfs4_xattr_list_nfs4_label,
5978 .get = nfs4_xattr_get_nfs4_label,
5979 .set = nfs4_xattr_set_nfs4_label,
5980};
5981#endif
5982
5983
Andy Adamson533eb462011-06-13 18:25:56 -04005984/*
5985 * nfs_fhget will use either the mounted_on_fileid or the fileid
5986 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005987static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5988{
Andy Adamson533eb462011-06-13 18:25:56 -04005989 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5990 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5991 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005992 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005993 return;
5994
5995 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005996 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005997 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5998 fattr->nlink = 2;
5999}
6000
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006001static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6002 const struct qstr *name,
6003 struct nfs4_fs_locations *fs_locations,
6004 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006005{
6006 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006007 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006008 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006009 };
6010 struct nfs4_fs_locations_arg args = {
6011 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006012 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006013 .page = page,
6014 .bitmask = bitmask,
6015 };
Benny Halevy22958462009-04-01 09:22:02 -04006016 struct nfs4_fs_locations_res res = {
6017 .fs_locations = fs_locations,
6018 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006019 struct rpc_message msg = {
6020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6021 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006022 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006023 };
6024 int status;
6025
Harvey Harrison3110ff82008-05-02 13:42:44 -07006026 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006027
6028 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6029 * is not supported */
6030 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6031 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6032 else
6033 bitmask[0] |= FATTR4_WORD0_FILEID;
6034
Trond Myklebustc228fd32007-01-13 02:28:11 -05006035 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006036 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006037 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006038 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006039 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006040 return status;
6041}
6042
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006043int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6044 const struct qstr *name,
6045 struct nfs4_fs_locations *fs_locations,
6046 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006047{
6048 struct nfs4_exception exception = { };
6049 int err;
6050 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006051 err = _nfs4_proc_fs_locations(client, dir, name,
6052 fs_locations, page);
6053 trace_nfs4_get_fs_locations(dir, name, err);
6054 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006055 &exception);
6056 } while (exception.retry);
6057 return err;
6058}
6059
Chuck Leverb03d7352013-10-17 14:12:50 -04006060/*
6061 * This operation also signals the server that this client is
6062 * performing migration recovery. The server can stop returning
6063 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6064 * appended to this compound to identify the client ID which is
6065 * performing recovery.
6066 */
6067static int _nfs40_proc_get_locations(struct inode *inode,
6068 struct nfs4_fs_locations *locations,
6069 struct page *page, struct rpc_cred *cred)
6070{
6071 struct nfs_server *server = NFS_SERVER(inode);
6072 struct rpc_clnt *clnt = server->client;
6073 u32 bitmask[2] = {
6074 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6075 };
6076 struct nfs4_fs_locations_arg args = {
6077 .clientid = server->nfs_client->cl_clientid,
6078 .fh = NFS_FH(inode),
6079 .page = page,
6080 .bitmask = bitmask,
6081 .migration = 1, /* skip LOOKUP */
6082 .renew = 1, /* append RENEW */
6083 };
6084 struct nfs4_fs_locations_res res = {
6085 .fs_locations = locations,
6086 .migration = 1,
6087 .renew = 1,
6088 };
6089 struct rpc_message msg = {
6090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6091 .rpc_argp = &args,
6092 .rpc_resp = &res,
6093 .rpc_cred = cred,
6094 };
6095 unsigned long now = jiffies;
6096 int status;
6097
6098 nfs_fattr_init(&locations->fattr);
6099 locations->server = server;
6100 locations->nlocations = 0;
6101
6102 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6103 nfs4_set_sequence_privileged(&args.seq_args);
6104 status = nfs4_call_sync_sequence(clnt, server, &msg,
6105 &args.seq_args, &res.seq_res);
6106 if (status)
6107 return status;
6108
6109 renew_lease(server, now);
6110 return 0;
6111}
6112
6113#ifdef CONFIG_NFS_V4_1
6114
6115/*
6116 * This operation also signals the server that this client is
6117 * performing migration recovery. The server can stop asserting
6118 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6119 * performing this operation is identified in the SEQUENCE
6120 * operation in this compound.
6121 *
6122 * When the client supports GETATTR(fs_locations_info), it can
6123 * be plumbed in here.
6124 */
6125static int _nfs41_proc_get_locations(struct inode *inode,
6126 struct nfs4_fs_locations *locations,
6127 struct page *page, struct rpc_cred *cred)
6128{
6129 struct nfs_server *server = NFS_SERVER(inode);
6130 struct rpc_clnt *clnt = server->client;
6131 u32 bitmask[2] = {
6132 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6133 };
6134 struct nfs4_fs_locations_arg args = {
6135 .fh = NFS_FH(inode),
6136 .page = page,
6137 .bitmask = bitmask,
6138 .migration = 1, /* skip LOOKUP */
6139 };
6140 struct nfs4_fs_locations_res res = {
6141 .fs_locations = locations,
6142 .migration = 1,
6143 };
6144 struct rpc_message msg = {
6145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6146 .rpc_argp = &args,
6147 .rpc_resp = &res,
6148 .rpc_cred = cred,
6149 };
6150 int status;
6151
6152 nfs_fattr_init(&locations->fattr);
6153 locations->server = server;
6154 locations->nlocations = 0;
6155
6156 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6157 nfs4_set_sequence_privileged(&args.seq_args);
6158 status = nfs4_call_sync_sequence(clnt, server, &msg,
6159 &args.seq_args, &res.seq_res);
6160 if (status == NFS4_OK &&
6161 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6162 status = -NFS4ERR_LEASE_MOVED;
6163 return status;
6164}
6165
6166#endif /* CONFIG_NFS_V4_1 */
6167
6168/**
6169 * nfs4_proc_get_locations - discover locations for a migrated FSID
6170 * @inode: inode on FSID that is migrating
6171 * @locations: result of query
6172 * @page: buffer
6173 * @cred: credential to use for this operation
6174 *
6175 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6176 * operation failed, or a negative errno if a local error occurred.
6177 *
6178 * On success, "locations" is filled in, but if the server has
6179 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6180 * asserted.
6181 *
6182 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6183 * from this client that require migration recovery.
6184 */
6185int nfs4_proc_get_locations(struct inode *inode,
6186 struct nfs4_fs_locations *locations,
6187 struct page *page, struct rpc_cred *cred)
6188{
6189 struct nfs_server *server = NFS_SERVER(inode);
6190 struct nfs_client *clp = server->nfs_client;
6191 const struct nfs4_mig_recovery_ops *ops =
6192 clp->cl_mvops->mig_recovery_ops;
6193 struct nfs4_exception exception = { };
6194 int status;
6195
6196 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6197 (unsigned long long)server->fsid.major,
6198 (unsigned long long)server->fsid.minor,
6199 clp->cl_hostname);
6200 nfs_display_fhandle(NFS_FH(inode), __func__);
6201
6202 do {
6203 status = ops->get_locations(inode, locations, page, cred);
6204 if (status != -NFS4ERR_DELAY)
6205 break;
6206 nfs4_handle_exception(server, status, &exception);
6207 } while (exception.retry);
6208 return status;
6209}
6210
Chuck Lever44c99932013-10-17 14:13:30 -04006211/*
6212 * This operation also signals the server that this client is
6213 * performing "lease moved" recovery. The server can stop
6214 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6215 * is appended to this compound to identify the client ID which is
6216 * performing recovery.
6217 */
6218static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6219{
6220 struct nfs_server *server = NFS_SERVER(inode);
6221 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6222 struct rpc_clnt *clnt = server->client;
6223 struct nfs4_fsid_present_arg args = {
6224 .fh = NFS_FH(inode),
6225 .clientid = clp->cl_clientid,
6226 .renew = 1, /* append RENEW */
6227 };
6228 struct nfs4_fsid_present_res res = {
6229 .renew = 1,
6230 };
6231 struct rpc_message msg = {
6232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6233 .rpc_argp = &args,
6234 .rpc_resp = &res,
6235 .rpc_cred = cred,
6236 };
6237 unsigned long now = jiffies;
6238 int status;
6239
6240 res.fh = nfs_alloc_fhandle();
6241 if (res.fh == NULL)
6242 return -ENOMEM;
6243
6244 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6245 nfs4_set_sequence_privileged(&args.seq_args);
6246 status = nfs4_call_sync_sequence(clnt, server, &msg,
6247 &args.seq_args, &res.seq_res);
6248 nfs_free_fhandle(res.fh);
6249 if (status)
6250 return status;
6251
6252 do_renew_lease(clp, now);
6253 return 0;
6254}
6255
6256#ifdef CONFIG_NFS_V4_1
6257
6258/*
6259 * This operation also signals the server that this client is
6260 * performing "lease moved" recovery. The server can stop asserting
6261 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6262 * this operation is identified in the SEQUENCE operation in this
6263 * compound.
6264 */
6265static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6266{
6267 struct nfs_server *server = NFS_SERVER(inode);
6268 struct rpc_clnt *clnt = server->client;
6269 struct nfs4_fsid_present_arg args = {
6270 .fh = NFS_FH(inode),
6271 };
6272 struct nfs4_fsid_present_res res = {
6273 };
6274 struct rpc_message msg = {
6275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6276 .rpc_argp = &args,
6277 .rpc_resp = &res,
6278 .rpc_cred = cred,
6279 };
6280 int status;
6281
6282 res.fh = nfs_alloc_fhandle();
6283 if (res.fh == NULL)
6284 return -ENOMEM;
6285
6286 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6287 nfs4_set_sequence_privileged(&args.seq_args);
6288 status = nfs4_call_sync_sequence(clnt, server, &msg,
6289 &args.seq_args, &res.seq_res);
6290 nfs_free_fhandle(res.fh);
6291 if (status == NFS4_OK &&
6292 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6293 status = -NFS4ERR_LEASE_MOVED;
6294 return status;
6295}
6296
6297#endif /* CONFIG_NFS_V4_1 */
6298
6299/**
6300 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6301 * @inode: inode on FSID to check
6302 * @cred: credential to use for this operation
6303 *
6304 * Server indicates whether the FSID is present, moved, or not
6305 * recognized. This operation is necessary to clear a LEASE_MOVED
6306 * condition for this client ID.
6307 *
6308 * Returns NFS4_OK if the FSID is present on this server,
6309 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6310 * NFS4ERR code if some error occurred on the server, or a
6311 * negative errno if a local failure occurred.
6312 */
6313int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6314{
6315 struct nfs_server *server = NFS_SERVER(inode);
6316 struct nfs_client *clp = server->nfs_client;
6317 const struct nfs4_mig_recovery_ops *ops =
6318 clp->cl_mvops->mig_recovery_ops;
6319 struct nfs4_exception exception = { };
6320 int status;
6321
6322 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6323 (unsigned long long)server->fsid.major,
6324 (unsigned long long)server->fsid.minor,
6325 clp->cl_hostname);
6326 nfs_display_fhandle(NFS_FH(inode), __func__);
6327
6328 do {
6329 status = ops->fsid_present(inode, cred);
6330 if (status != -NFS4ERR_DELAY)
6331 break;
6332 nfs4_handle_exception(server, status, &exception);
6333 } while (exception.retry);
6334 return status;
6335}
6336
Andy Adamson5ec16a82013-08-08 10:57:55 -04006337/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006338 * If 'use_integrity' is true and the state managment nfs_client
6339 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6340 * and the machine credential as per RFC3530bis and RFC5661 Security
6341 * Considerations sections. Otherwise, just use the user cred with the
6342 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006343 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006344static 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 +00006345{
6346 int status;
6347 struct nfs4_secinfo_arg args = {
6348 .dir_fh = NFS_FH(dir),
6349 .name = name,
6350 };
6351 struct nfs4_secinfo_res res = {
6352 .flavors = flavors,
6353 };
6354 struct rpc_message msg = {
6355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6356 .rpc_argp = &args,
6357 .rpc_resp = &res,
6358 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006359 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006360 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006361
6362 if (use_integrity) {
6363 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006364 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6365 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006366 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006367
6368 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006369
6370 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6371 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6372
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006373 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6374 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006375 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006376
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006377 if (cred)
6378 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006379
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006380 return status;
6381}
6382
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006383int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6384 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006385{
6386 struct nfs4_exception exception = { };
6387 int err;
6388 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006389 err = -NFS4ERR_WRONGSEC;
6390
6391 /* try to use integrity protection with machine cred */
6392 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6393 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6394
6395 /*
6396 * if unable to use integrity protection, or SECINFO with
6397 * integrity protection returns NFS4ERR_WRONGSEC (which is
6398 * disallowed by spec, but exists in deployed servers) use
6399 * the current filesystem's rpc_client and the user cred.
6400 */
6401 if (err == -NFS4ERR_WRONGSEC)
6402 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6403
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006404 trace_nfs4_secinfo(dir, name, err);
6405 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006406 &exception);
6407 } while (exception.retry);
6408 return err;
6409}
6410
Andy Adamson557134a2009-04-01 09:21:53 -04006411#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006412/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006413 * Check the exchange flags returned by the server for invalid flags, having
6414 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6415 * DS flags set.
6416 */
6417static int nfs4_check_cl_exchange_flags(u32 flags)
6418{
6419 if (flags & ~EXCHGID4_FLAG_MASK_R)
6420 goto out_inval;
6421 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6422 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6423 goto out_inval;
6424 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6425 goto out_inval;
6426 return NFS_OK;
6427out_inval:
6428 return -NFS4ERR_INVAL;
6429}
6430
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006431static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006432nfs41_same_server_scope(struct nfs41_server_scope *a,
6433 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006434{
6435 if (a->server_scope_sz == b->server_scope_sz &&
6436 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6437 return true;
6438
6439 return false;
6440}
6441
Andy Adamson357f54d2010-12-14 10:11:57 -05006442/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006443 * nfs4_proc_bind_conn_to_session()
6444 *
6445 * The 4.1 client currently uses the same TCP connection for the
6446 * fore and backchannel.
6447 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006448int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006449{
6450 int status;
6451 struct nfs41_bind_conn_to_session_res res;
6452 struct rpc_message msg = {
6453 .rpc_proc =
6454 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6455 .rpc_argp = clp,
6456 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006457 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006458 };
6459
6460 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006461
6462 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6463 if (unlikely(res.session == NULL)) {
6464 status = -ENOMEM;
6465 goto out;
6466 }
6467
6468 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006469 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006470 if (status == 0) {
6471 if (memcmp(res.session->sess_id.data,
6472 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6473 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6474 status = -EIO;
6475 goto out_session;
6476 }
6477 if (res.dir != NFS4_CDFS4_BOTH) {
6478 dprintk("NFS: %s: Unexpected direction from server\n",
6479 __func__);
6480 status = -EIO;
6481 goto out_session;
6482 }
6483 if (res.use_conn_in_rdma_mode) {
6484 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6485 __func__);
6486 status = -EIO;
6487 goto out_session;
6488 }
6489 }
6490out_session:
6491 kfree(res.session);
6492out:
6493 dprintk("<-- %s status= %d\n", __func__, status);
6494 return status;
6495}
6496
6497/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006498 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6499 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006500 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006501static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6502 .how = SP4_MACH_CRED,
6503 .enforce.u.words = {
6504 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6505 1 << (OP_EXCHANGE_ID - 32) |
6506 1 << (OP_CREATE_SESSION - 32) |
6507 1 << (OP_DESTROY_SESSION - 32) |
6508 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006509 },
6510 .allow.u.words = {
6511 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006512 1 << (OP_LOCKU) |
6513 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006514 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006515 1 << (OP_SECINFO_NO_NAME - 32) |
6516 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006517 1 << (OP_FREE_STATEID - 32) |
6518 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006519 }
6520};
6521
6522/*
6523 * Select the state protection mode for client `clp' given the server results
6524 * from exchange_id in `sp'.
6525 *
6526 * Returns 0 on success, negative errno otherwise.
6527 */
6528static int nfs4_sp4_select_mode(struct nfs_client *clp,
6529 struct nfs41_state_protection *sp)
6530{
6531 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6532 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6533 1 << (OP_EXCHANGE_ID - 32) |
6534 1 << (OP_CREATE_SESSION - 32) |
6535 1 << (OP_DESTROY_SESSION - 32) |
6536 1 << (OP_DESTROY_CLIENTID - 32)
6537 };
6538 unsigned int i;
6539
6540 if (sp->how == SP4_MACH_CRED) {
6541 /* Print state protect result */
6542 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6543 for (i = 0; i <= LAST_NFS4_OP; i++) {
6544 if (test_bit(i, sp->enforce.u.longs))
6545 dfprintk(MOUNT, " enforce op %d\n", i);
6546 if (test_bit(i, sp->allow.u.longs))
6547 dfprintk(MOUNT, " allow op %d\n", i);
6548 }
6549
6550 /* make sure nothing is on enforce list that isn't supported */
6551 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6552 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6553 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6554 return -EINVAL;
6555 }
6556 }
6557
6558 /*
6559 * Minimal mode - state operations are allowed to use machine
6560 * credential. Note this already happens by default, so the
6561 * client doesn't have to do anything more than the negotiation.
6562 *
6563 * NOTE: we don't care if EXCHANGE_ID is in the list -
6564 * we're already using the machine cred for exchange_id
6565 * and will never use a different cred.
6566 */
6567 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6568 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6569 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6570 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6571 dfprintk(MOUNT, "sp4_mach_cred:\n");
6572 dfprintk(MOUNT, " minimal mode enabled\n");
6573 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6574 } else {
6575 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6576 return -EINVAL;
6577 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006578
6579 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6580 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6581 dfprintk(MOUNT, " cleanup mode enabled\n");
6582 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6583 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006584
6585 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6586 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6587 dfprintk(MOUNT, " secinfo mode enabled\n");
6588 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6589 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006590
6591 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6592 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6593 dfprintk(MOUNT, " stateid mode enabled\n");
6594 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6595 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006596
6597 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6598 dfprintk(MOUNT, " write mode enabled\n");
6599 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6600 }
6601
6602 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6603 dfprintk(MOUNT, " commit mode enabled\n");
6604 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6605 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006606 }
6607
6608 return 0;
6609}
6610
6611/*
6612 * _nfs4_proc_exchange_id()
6613 *
6614 * Wrapper for EXCHANGE_ID operation.
6615 */
6616static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6617 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006618{
6619 nfs4_verifier verifier;
6620 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006621 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006622 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006623#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006624 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006625 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6626 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6627#else
6628 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6629 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6630#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006631 };
6632 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006633 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006634 };
6635 int status;
6636 struct rpc_message msg = {
6637 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6638 .rpc_argp = &args,
6639 .rpc_resp = &res,
6640 .rpc_cred = cred,
6641 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006642
Chuck Leverf0920752012-05-21 22:45:41 -04006643 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006644 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6645 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006646 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6647 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6648 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006649
Chuck Leveracdeb692012-05-21 22:46:16 -04006650 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006651 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006652 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006653 status = -ENOMEM;
6654 goto out;
6655 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006656
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006657 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006658 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006659 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006660 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006661 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006662 }
6663
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006664 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006665 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006666 status = -ENOMEM;
6667 goto out_server_scope;
6668 }
6669
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006670 switch (sp4_how) {
6671 case SP4_NONE:
6672 args.state_protect.how = SP4_NONE;
6673 break;
6674
6675 case SP4_MACH_CRED:
6676 args.state_protect = nfs4_sp4_mach_cred_request;
6677 break;
6678
6679 default:
6680 /* unsupported! */
6681 WARN_ON_ONCE(1);
6682 status = -EINVAL;
6683 goto out_server_scope;
6684 }
6685
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006686 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006687 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006688 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006689 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006690
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006691 if (status == 0)
6692 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6693
Chuck Lever177313f2012-05-21 22:44:58 -04006694 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006695 clp->cl_clientid = res.clientid;
6696 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6697 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6698 clp->cl_seqid = res.seqid;
6699
Chuck Leveracdeb692012-05-21 22:46:16 -04006700 kfree(clp->cl_serverowner);
6701 clp->cl_serverowner = res.server_owner;
6702 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006703
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006704 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006705 kfree(clp->cl_implid);
6706 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006707
Chuck Lever177313f2012-05-21 22:44:58 -04006708 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006709 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006710 res.server_scope)) {
6711 dprintk("%s: server_scope mismatch detected\n",
6712 __func__);
6713 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006714 kfree(clp->cl_serverscope);
6715 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006716 }
6717
Chuck Lever177313f2012-05-21 22:44:58 -04006718 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006719 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006720 goto out;
6721 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006722 } else
6723 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006724
Chuck Leveracdeb692012-05-21 22:46:16 -04006725out_server_owner:
6726 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006727out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006728 kfree(res.server_scope);
6729out:
Chuck Lever177313f2012-05-21 22:44:58 -04006730 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006731 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006732 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006733 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006734 clp->cl_implid->date.seconds,
6735 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006736 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006737 return status;
6738}
6739
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006740/*
6741 * nfs4_proc_exchange_id()
6742 *
6743 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6744 *
6745 * Since the clientid has expired, all compounds using sessions
6746 * associated with the stale clientid will be returning
6747 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6748 * be in some phase of session reset.
6749 *
6750 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6751 */
6752int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6753{
6754 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6755 int status;
6756
6757 /* try SP4_MACH_CRED if krb5i/p */
6758 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6759 authflavor == RPC_AUTH_GSS_KRB5P) {
6760 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6761 if (!status)
6762 return 0;
6763 }
6764
6765 /* try SP4_NONE */
6766 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6767}
6768
Trond Myklebust66245532012-05-25 17:18:09 -04006769static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6770 struct rpc_cred *cred)
6771{
6772 struct rpc_message msg = {
6773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6774 .rpc_argp = clp,
6775 .rpc_cred = cred,
6776 };
6777 int status;
6778
6779 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006780 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006781 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006782 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006783 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6784 return status;
6785}
6786
6787static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6788 struct rpc_cred *cred)
6789{
6790 unsigned int loop;
6791 int ret;
6792
6793 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6794 ret = _nfs4_proc_destroy_clientid(clp, cred);
6795 switch (ret) {
6796 case -NFS4ERR_DELAY:
6797 case -NFS4ERR_CLIENTID_BUSY:
6798 ssleep(1);
6799 break;
6800 default:
6801 return ret;
6802 }
6803 }
6804 return 0;
6805}
6806
6807int nfs4_destroy_clientid(struct nfs_client *clp)
6808{
6809 struct rpc_cred *cred;
6810 int ret = 0;
6811
6812 if (clp->cl_mvops->minor_version < 1)
6813 goto out;
6814 if (clp->cl_exchange_flags == 0)
6815 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006816 if (clp->cl_preserve_clid)
6817 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006818 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006819 ret = nfs4_proc_destroy_clientid(clp, cred);
6820 if (cred)
6821 put_rpccred(cred);
6822 switch (ret) {
6823 case 0:
6824 case -NFS4ERR_STALE_CLIENTID:
6825 clp->cl_exchange_flags = 0;
6826 }
6827out:
6828 return ret;
6829}
6830
Andy Adamson2050f0c2009-04-01 09:22:30 -04006831struct nfs4_get_lease_time_data {
6832 struct nfs4_get_lease_time_args *args;
6833 struct nfs4_get_lease_time_res *res;
6834 struct nfs_client *clp;
6835};
6836
6837static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6838 void *calldata)
6839{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006840 struct nfs4_get_lease_time_data *data =
6841 (struct nfs4_get_lease_time_data *)calldata;
6842
6843 dprintk("--> %s\n", __func__);
6844 /* just setup sequence, do not trigger session recovery
6845 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006846 nfs41_setup_sequence(data->clp->cl_session,
6847 &data->args->la_seq_args,
6848 &data->res->lr_seq_res,
6849 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006850 dprintk("<-- %s\n", __func__);
6851}
6852
6853/*
6854 * Called from nfs4_state_manager thread for session setup, so don't recover
6855 * from sequence operation or clientid errors.
6856 */
6857static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6858{
6859 struct nfs4_get_lease_time_data *data =
6860 (struct nfs4_get_lease_time_data *)calldata;
6861
6862 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006863 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6864 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006865 switch (task->tk_status) {
6866 case -NFS4ERR_DELAY:
6867 case -NFS4ERR_GRACE:
6868 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6869 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6870 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006871 /* fall through */
6872 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006873 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006874 return;
6875 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006876 dprintk("<-- %s\n", __func__);
6877}
6878
Trond Myklebust17280172012-03-11 13:11:00 -04006879static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006880 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6881 .rpc_call_done = nfs4_get_lease_time_done,
6882};
6883
6884int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6885{
6886 struct rpc_task *task;
6887 struct nfs4_get_lease_time_args args;
6888 struct nfs4_get_lease_time_res res = {
6889 .lr_fsinfo = fsinfo,
6890 };
6891 struct nfs4_get_lease_time_data data = {
6892 .args = &args,
6893 .res = &res,
6894 .clp = clp,
6895 };
6896 struct rpc_message msg = {
6897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6898 .rpc_argp = &args,
6899 .rpc_resp = &res,
6900 };
6901 struct rpc_task_setup task_setup = {
6902 .rpc_client = clp->cl_rpcclient,
6903 .rpc_message = &msg,
6904 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006905 .callback_data = &data,
6906 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006907 };
6908 int status;
6909
Chuck Levera9c92d62013-08-09 12:48:18 -04006910 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006911 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006912 dprintk("--> %s\n", __func__);
6913 task = rpc_run_task(&task_setup);
6914
6915 if (IS_ERR(task))
6916 status = PTR_ERR(task);
6917 else {
6918 status = task->tk_status;
6919 rpc_put_task(task);
6920 }
6921 dprintk("<-- %s return %d\n", __func__, status);
6922
6923 return status;
6924}
6925
Andy Adamsonfc931582009-04-01 09:22:31 -04006926/*
6927 * Initialize the values to be used by the client in CREATE_SESSION
6928 * If nfs4_init_session set the fore channel request and response sizes,
6929 * use them.
6930 *
6931 * Set the back channel max_resp_sz_cached to zero to force the client to
6932 * always set csa_cachethis to FALSE because the current implementation
6933 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6934 */
6935static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6936{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006937 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006938
Andy Adamson18aad3d2013-06-26 12:21:49 -04006939 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6940 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6941
Andy Adamsonfc931582009-04-01 09:22:31 -04006942 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006943 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6944 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006945 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006946 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006947
6948 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006949 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006950 __func__,
6951 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006952 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006953
6954 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006955 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6956 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6957 args->bc_attrs.max_resp_sz_cached = 0;
6958 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6959 args->bc_attrs.max_reqs = 1;
6960
6961 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6962 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6963 __func__,
6964 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6965 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6966 args->bc_attrs.max_reqs);
6967}
6968
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006969static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006970{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006971 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6972 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6973
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006974 if (rcvd->max_resp_sz > sent->max_resp_sz)
6975 return -EINVAL;
6976 /*
6977 * Our requested max_ops is the minimum we need; we're not
6978 * prepared to break up compounds into smaller pieces than that.
6979 * So, no point even trying to continue if the server won't
6980 * cooperate:
6981 */
6982 if (rcvd->max_ops < sent->max_ops)
6983 return -EINVAL;
6984 if (rcvd->max_reqs == 0)
6985 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006986 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6987 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006988 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006989}
6990
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006991static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6992{
6993 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6994 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006995
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006996 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6997 return -EINVAL;
6998 if (rcvd->max_resp_sz < sent->max_resp_sz)
6999 return -EINVAL;
7000 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7001 return -EINVAL;
7002 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007003 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007004 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007005 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007006 return -EINVAL;
7007 return 0;
7008}
Andy Adamson8d353012009-04-01 09:22:32 -04007009
Andy Adamson8d353012009-04-01 09:22:32 -04007010static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7011 struct nfs4_session *session)
7012{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007013 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007014
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007015 ret = nfs4_verify_fore_channel_attrs(args, session);
7016 if (ret)
7017 return ret;
7018 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007019}
7020
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007021static int _nfs4_proc_create_session(struct nfs_client *clp,
7022 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007023{
7024 struct nfs4_session *session = clp->cl_session;
7025 struct nfs41_create_session_args args = {
7026 .client = clp,
7027 .cb_program = NFS4_CALLBACK,
7028 };
7029 struct nfs41_create_session_res res = {
7030 .client = clp,
7031 };
7032 struct rpc_message msg = {
7033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7034 .rpc_argp = &args,
7035 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007036 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007037 };
7038 int status;
7039
7040 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007041 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007042
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007043 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007044 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007045
Trond Myklebust43095d32012-11-20 11:13:12 -05007046 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007047 /* Verify the session's negotiated channel_attrs values */
7048 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007049 /* Increment the clientid slot sequence id */
7050 clp->cl_seqid++;
7051 }
7052
7053 return status;
7054}
7055
7056/*
7057 * Issues a CREATE_SESSION operation to the server.
7058 * It is the responsibility of the caller to verify the session is
7059 * expired before calling this routine.
7060 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007061int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007062{
7063 int status;
7064 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007065 struct nfs4_session *session = clp->cl_session;
7066
7067 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7068
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007069 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007070 if (status)
7071 goto out;
7072
Andy Adamsonaacd5532011-11-09 13:58:21 -05007073 /* Init or reset the session slot tables */
7074 status = nfs4_setup_session_slot_tables(session);
7075 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007076 if (status)
7077 goto out;
7078
7079 ptr = (unsigned *)&session->sess_id.data[0];
7080 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7081 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007082out:
7083 dprintk("<-- %s\n", __func__);
7084 return status;
7085}
7086
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007087/*
7088 * Issue the over-the-wire RPC DESTROY_SESSION.
7089 * The caller must serialize access to this routine.
7090 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007091int nfs4_proc_destroy_session(struct nfs4_session *session,
7092 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007093{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007094 struct rpc_message msg = {
7095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7096 .rpc_argp = session,
7097 .rpc_cred = cred,
7098 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007099 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007100
7101 dprintk("--> nfs4_proc_destroy_session\n");
7102
7103 /* session is still being setup */
7104 if (session->clp->cl_cons_state != NFS_CS_READY)
7105 return status;
7106
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007107 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007108 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007109
7110 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007111 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007112 "Session has been destroyed regardless...\n", status);
7113
7114 dprintk("<-- nfs4_proc_destroy_session\n");
7115 return status;
7116}
7117
Trond Myklebust7b38c362012-05-23 13:23:31 -04007118/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007119 * Renew the cl_session lease.
7120 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007121struct nfs4_sequence_data {
7122 struct nfs_client *clp;
7123 struct nfs4_sequence_args args;
7124 struct nfs4_sequence_res res;
7125};
7126
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007127static void nfs41_sequence_release(void *data)
7128{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007129 struct nfs4_sequence_data *calldata = data;
7130 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007131
Alexandros Batsakis71358402010-02-05 03:45:05 -08007132 if (atomic_read(&clp->cl_count) > 1)
7133 nfs4_schedule_state_renewal(clp);
7134 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007135 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007136}
7137
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007138static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7139{
7140 switch(task->tk_status) {
7141 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007142 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7143 return -EAGAIN;
7144 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007145 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007146 }
7147 return 0;
7148}
7149
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007150static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007151{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007152 struct nfs4_sequence_data *calldata = data;
7153 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007154
Trond Myklebust14516c32010-07-31 14:29:06 -04007155 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7156 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007157
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007158 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007159 if (task->tk_status < 0) {
7160 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007161 if (atomic_read(&clp->cl_count) == 1)
7162 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007163
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007164 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7165 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007166 return;
7167 }
7168 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007169 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007170out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007171 dprintk("<-- %s\n", __func__);
7172}
7173
7174static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7175{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007176 struct nfs4_sequence_data *calldata = data;
7177 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007178 struct nfs4_sequence_args *args;
7179 struct nfs4_sequence_res *res;
7180
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007181 args = task->tk_msg.rpc_argp;
7182 res = task->tk_msg.rpc_resp;
7183
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007184 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007185}
7186
7187static const struct rpc_call_ops nfs41_sequence_ops = {
7188 .rpc_call_done = nfs41_sequence_call_done,
7189 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007190 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007191};
7192
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007193static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7194 struct rpc_cred *cred,
7195 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007196{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007197 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007198 struct rpc_message msg = {
7199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7200 .rpc_cred = cred,
7201 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007202 struct rpc_task_setup task_setup_data = {
7203 .rpc_client = clp->cl_rpcclient,
7204 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007205 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007206 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007207 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007208
Alexandros Batsakis71358402010-02-05 03:45:05 -08007209 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007210 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007211 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007212 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007213 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007214 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007215 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007216 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007217 if (is_privileged)
7218 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007219 msg.rpc_argp = &calldata->args;
7220 msg.rpc_resp = &calldata->res;
7221 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007222 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007223
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007224 return rpc_run_task(&task_setup_data);
7225}
7226
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007227static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007228{
7229 struct rpc_task *task;
7230 int ret = 0;
7231
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007232 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7233 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007234 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007235 if (IS_ERR(task))
7236 ret = PTR_ERR(task);
7237 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007238 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007239 dprintk("<-- %s status=%d\n", __func__, ret);
7240 return ret;
7241}
7242
7243static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7244{
7245 struct rpc_task *task;
7246 int ret;
7247
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007248 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007249 if (IS_ERR(task)) {
7250 ret = PTR_ERR(task);
7251 goto out;
7252 }
7253 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007254 if (!ret) {
7255 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7256
7257 if (task->tk_status == 0)
7258 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007259 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007260 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007261 rpc_put_task(task);
7262out:
7263 dprintk("<-- %s status=%d\n", __func__, ret);
7264 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007265}
7266
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007267struct nfs4_reclaim_complete_data {
7268 struct nfs_client *clp;
7269 struct nfs41_reclaim_complete_args arg;
7270 struct nfs41_reclaim_complete_res res;
7271};
7272
7273static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7274{
7275 struct nfs4_reclaim_complete_data *calldata = data;
7276
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007277 nfs41_setup_sequence(calldata->clp->cl_session,
7278 &calldata->arg.seq_args,
7279 &calldata->res.seq_res,
7280 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007281}
7282
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007283static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7284{
7285 switch(task->tk_status) {
7286 case 0:
7287 case -NFS4ERR_COMPLETE_ALREADY:
7288 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7289 break;
7290 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007291 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007292 /* fall through */
7293 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007294 return -EAGAIN;
7295 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007296 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007297 }
7298 return 0;
7299}
7300
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007301static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7302{
7303 struct nfs4_reclaim_complete_data *calldata = data;
7304 struct nfs_client *clp = calldata->clp;
7305 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7306
7307 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007308 if (!nfs41_sequence_done(task, res))
7309 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007310
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007311 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007312 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7313 rpc_restart_call_prepare(task);
7314 return;
7315 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007316 dprintk("<-- %s\n", __func__);
7317}
7318
7319static void nfs4_free_reclaim_complete_data(void *data)
7320{
7321 struct nfs4_reclaim_complete_data *calldata = data;
7322
7323 kfree(calldata);
7324}
7325
7326static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7327 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7328 .rpc_call_done = nfs4_reclaim_complete_done,
7329 .rpc_release = nfs4_free_reclaim_complete_data,
7330};
7331
7332/*
7333 * Issue a global reclaim complete.
7334 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007335static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7336 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007337{
7338 struct nfs4_reclaim_complete_data *calldata;
7339 struct rpc_task *task;
7340 struct rpc_message msg = {
7341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007342 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007343 };
7344 struct rpc_task_setup task_setup_data = {
7345 .rpc_client = clp->cl_rpcclient,
7346 .rpc_message = &msg,
7347 .callback_ops = &nfs4_reclaim_complete_call_ops,
7348 .flags = RPC_TASK_ASYNC,
7349 };
7350 int status = -ENOMEM;
7351
7352 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007353 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007354 if (calldata == NULL)
7355 goto out;
7356 calldata->clp = clp;
7357 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007358
Chuck Levera9c92d62013-08-09 12:48:18 -04007359 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007360 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007361 msg.rpc_argp = &calldata->arg;
7362 msg.rpc_resp = &calldata->res;
7363 task_setup_data.callback_data = calldata;
7364 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007365 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007366 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007367 goto out;
7368 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007369 status = nfs4_wait_for_completion_rpc_task(task);
7370 if (status == 0)
7371 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007372 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007373 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007374out:
7375 dprintk("<-- %s status=%d\n", __func__, status);
7376 return status;
7377}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007378
7379static void
7380nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7381{
7382 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007383 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007384 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007385
7386 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007387 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7388 * right now covering the LAYOUTGET we are about to send.
7389 * However, that is not so catastrophic, and there seems
7390 * to be no way to prevent it completely.
7391 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007392 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007393 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007394 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007395 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7396 NFS_I(lgp->args.inode)->layout,
7397 lgp->args.ctx->state)) {
7398 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007399 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007400}
7401
7402static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7403{
7404 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007405 struct inode *inode = lgp->args.inode;
7406 struct nfs_server *server = NFS_SERVER(inode);
7407 struct pnfs_layout_hdr *lo;
7408 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007409 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007410
Boaz Harroshed7e5422014-01-22 20:34:54 +02007411 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007412
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007413 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007414 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007415
7416 switch (task->tk_status) {
7417 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007418 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007419 /*
7420 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7421 * (or clients) writing to the same RAID stripe
7422 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007423 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007424 /*
7425 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7426 * existing layout before getting a new one).
7427 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007428 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007429 timeo = rpc_get_timeout(task->tk_client);
7430 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007431 now = jiffies;
7432 if (time_after(giveup, now)) {
7433 unsigned long delay;
7434
7435 /* Delay for:
7436 * - Not less then NFS4_POLL_RETRY_MIN.
7437 * - One last time a jiffie before we give up
7438 * - exponential backoff (time_now minus start_attempt)
7439 */
7440 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7441 min((giveup - now - 1),
7442 now - lgp->args.timestamp));
7443
7444 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7445 __func__, delay);
7446 rpc_delay(task, delay);
7447 task->tk_status = 0;
7448 rpc_restart_call_prepare(task);
7449 goto out; /* Do not call nfs4_async_handle_error() */
7450 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007451 break;
7452 case -NFS4ERR_EXPIRED:
7453 case -NFS4ERR_BAD_STATEID:
7454 spin_lock(&inode->i_lock);
7455 lo = NFS_I(inode)->layout;
7456 if (!lo || list_empty(&lo->plh_segs)) {
7457 spin_unlock(&inode->i_lock);
7458 /* If the open stateid was bad, then recover it. */
7459 state = lgp->args.ctx->state;
7460 } else {
7461 LIST_HEAD(head);
7462
7463 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7464 spin_unlock(&inode->i_lock);
7465 /* Mark the bad layout state as invalid, then
7466 * retry using the open stateid. */
7467 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007468 }
7469 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007470 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7471 rpc_restart_call_prepare(task);
7472out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007473 dprintk("<-- %s\n", __func__);
7474}
7475
Idan Kedar85541162012-08-02 11:47:10 +03007476static size_t max_response_pages(struct nfs_server *server)
7477{
7478 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7479 return nfs_page_array_len(0, max_resp_sz);
7480}
7481
7482static void nfs4_free_pages(struct page **pages, size_t size)
7483{
7484 int i;
7485
7486 if (!pages)
7487 return;
7488
7489 for (i = 0; i < size; i++) {
7490 if (!pages[i])
7491 break;
7492 __free_page(pages[i]);
7493 }
7494 kfree(pages);
7495}
7496
7497static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7498{
7499 struct page **pages;
7500 int i;
7501
7502 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7503 if (!pages) {
7504 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7505 return NULL;
7506 }
7507
7508 for (i = 0; i < size; i++) {
7509 pages[i] = alloc_page(gfp_flags);
7510 if (!pages[i]) {
7511 dprintk("%s: failed to allocate page\n", __func__);
7512 nfs4_free_pages(pages, size);
7513 return NULL;
7514 }
7515 }
7516
7517 return pages;
7518}
7519
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007520static void nfs4_layoutget_release(void *calldata)
7521{
7522 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007523 struct inode *inode = lgp->args.inode;
7524 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007525 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007526
7527 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007528 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007529 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007530 put_nfs_open_context(lgp->args.ctx);
7531 kfree(calldata);
7532 dprintk("<-- %s\n", __func__);
7533}
7534
7535static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7536 .rpc_call_prepare = nfs4_layoutget_prepare,
7537 .rpc_call_done = nfs4_layoutget_done,
7538 .rpc_release = nfs4_layoutget_release,
7539};
7540
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007541struct pnfs_layout_segment *
7542nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007543{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007544 struct inode *inode = lgp->args.inode;
7545 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007546 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007547 struct rpc_task *task;
7548 struct rpc_message msg = {
7549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7550 .rpc_argp = &lgp->args,
7551 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007552 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007553 };
7554 struct rpc_task_setup task_setup_data = {
7555 .rpc_client = server->client,
7556 .rpc_message = &msg,
7557 .callback_ops = &nfs4_layoutget_call_ops,
7558 .callback_data = lgp,
7559 .flags = RPC_TASK_ASYNC,
7560 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007561 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007562 int status = 0;
7563
7564 dprintk("--> %s\n", __func__);
7565
Idan Kedar85541162012-08-02 11:47:10 +03007566 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7567 if (!lgp->args.layout.pages) {
7568 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007569 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007570 }
7571 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007572 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007573
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007574 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007575 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007576 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007577
7578 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7579 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7580
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007581 task = rpc_run_task(&task_setup_data);
7582 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007583 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007584 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007585 if (status == 0)
7586 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007587 trace_nfs4_layoutget(lgp->args.ctx,
7588 &lgp->args.range,
7589 &lgp->res.range,
7590 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007591 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7592 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007593 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007594 rpc_put_task(task);
7595 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007596 if (status)
7597 return ERR_PTR(status);
7598 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007599}
7600
Benny Halevycbe82602011-05-22 19:52:37 +03007601static void
7602nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7603{
7604 struct nfs4_layoutreturn *lrp = calldata;
7605
7606 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007607 nfs41_setup_sequence(lrp->clp->cl_session,
7608 &lrp->args.seq_args,
7609 &lrp->res.seq_res,
7610 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007611}
7612
7613static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7614{
7615 struct nfs4_layoutreturn *lrp = calldata;
7616 struct nfs_server *server;
7617
7618 dprintk("--> %s\n", __func__);
7619
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007620 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007621 return;
7622
7623 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007624 switch (task->tk_status) {
7625 default:
7626 task->tk_status = 0;
7627 case 0:
7628 break;
7629 case -NFS4ERR_DELAY:
7630 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7631 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007632 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007633 return;
7634 }
Benny Halevycbe82602011-05-22 19:52:37 +03007635 dprintk("<-- %s\n", __func__);
7636}
7637
7638static void nfs4_layoutreturn_release(void *calldata)
7639{
7640 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007641 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007642
7643 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007644 spin_lock(&lo->plh_inode->i_lock);
7645 if (lrp->res.lrs_present)
7646 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7647 lo->plh_block_lgets--;
7648 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007649 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007650 kfree(calldata);
7651 dprintk("<-- %s\n", __func__);
7652}
7653
7654static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7655 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7656 .rpc_call_done = nfs4_layoutreturn_done,
7657 .rpc_release = nfs4_layoutreturn_release,
7658};
7659
7660int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7661{
7662 struct rpc_task *task;
7663 struct rpc_message msg = {
7664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7665 .rpc_argp = &lrp->args,
7666 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007667 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007668 };
7669 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007670 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007671 .rpc_message = &msg,
7672 .callback_ops = &nfs4_layoutreturn_call_ops,
7673 .callback_data = lrp,
7674 };
7675 int status;
7676
7677 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007678 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007679 task = rpc_run_task(&task_setup_data);
7680 if (IS_ERR(task))
7681 return PTR_ERR(task);
7682 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007683 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007684 dprintk("<-- %s status=%d\n", __func__, status);
7685 rpc_put_task(task);
7686 return status;
7687}
7688
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007689/*
7690 * Retrieve the list of Data Server devices from the MDS.
7691 */
7692static int _nfs4_getdevicelist(struct nfs_server *server,
7693 const struct nfs_fh *fh,
7694 struct pnfs_devicelist *devlist)
7695{
7696 struct nfs4_getdevicelist_args args = {
7697 .fh = fh,
7698 .layoutclass = server->pnfs_curr_ld->id,
7699 };
7700 struct nfs4_getdevicelist_res res = {
7701 .devlist = devlist,
7702 };
7703 struct rpc_message msg = {
7704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7705 .rpc_argp = &args,
7706 .rpc_resp = &res,
7707 };
7708 int status;
7709
7710 dprintk("--> %s\n", __func__);
7711 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7712 &res.seq_res, 0);
7713 dprintk("<-- %s status=%d\n", __func__, status);
7714 return status;
7715}
7716
7717int nfs4_proc_getdevicelist(struct nfs_server *server,
7718 const struct nfs_fh *fh,
7719 struct pnfs_devicelist *devlist)
7720{
7721 struct nfs4_exception exception = { };
7722 int err;
7723
7724 do {
7725 err = nfs4_handle_exception(server,
7726 _nfs4_getdevicelist(server, fh, devlist),
7727 &exception);
7728 } while (exception.retry);
7729
7730 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7731 err, devlist->num_devs);
7732
7733 return err;
7734}
7735EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7736
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007737static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007738_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7739 struct pnfs_device *pdev,
7740 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007741{
7742 struct nfs4_getdeviceinfo_args args = {
7743 .pdev = pdev,
7744 };
7745 struct nfs4_getdeviceinfo_res res = {
7746 .pdev = pdev,
7747 };
7748 struct rpc_message msg = {
7749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7750 .rpc_argp = &args,
7751 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007752 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007753 };
7754 int status;
7755
7756 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007757 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007758 dprintk("<-- %s status=%d\n", __func__, status);
7759
7760 return status;
7761}
7762
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007763int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7764 struct pnfs_device *pdev,
7765 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007766{
7767 struct nfs4_exception exception = { };
7768 int err;
7769
7770 do {
7771 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007772 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007773 &exception);
7774 } while (exception.retry);
7775 return err;
7776}
7777EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7778
Andy Adamson863a3c62011-03-23 13:27:54 +00007779static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7780{
7781 struct nfs4_layoutcommit_data *data = calldata;
7782 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007783 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007784
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007785 nfs41_setup_sequence(session,
7786 &data->args.seq_args,
7787 &data->res.seq_res,
7788 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007789}
7790
7791static void
7792nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7793{
7794 struct nfs4_layoutcommit_data *data = calldata;
7795 struct nfs_server *server = NFS_SERVER(data->args.inode);
7796
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007797 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007798 return;
7799
7800 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007801 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7802 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7803 case -NFS4ERR_BADLAYOUT: /* no layout */
7804 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007805 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007806 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007807 break;
7808 default:
7809 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7810 rpc_restart_call_prepare(task);
7811 return;
7812 }
7813 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007814}
7815
7816static void nfs4_layoutcommit_release(void *calldata)
7817{
7818 struct nfs4_layoutcommit_data *data = calldata;
7819
Andy Adamsondb29c082011-07-30 20:52:38 -04007820 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007821 nfs_post_op_update_inode_force_wcc(data->args.inode,
7822 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007823 put_rpccred(data->cred);
7824 kfree(data);
7825}
7826
7827static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7828 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7829 .rpc_call_done = nfs4_layoutcommit_done,
7830 .rpc_release = nfs4_layoutcommit_release,
7831};
7832
7833int
Andy Adamsonef311532011-03-12 02:58:10 -05007834nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007835{
7836 struct rpc_message msg = {
7837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7838 .rpc_argp = &data->args,
7839 .rpc_resp = &data->res,
7840 .rpc_cred = data->cred,
7841 };
7842 struct rpc_task_setup task_setup_data = {
7843 .task = &data->task,
7844 .rpc_client = NFS_CLIENT(data->args.inode),
7845 .rpc_message = &msg,
7846 .callback_ops = &nfs4_layoutcommit_ops,
7847 .callback_data = data,
7848 .flags = RPC_TASK_ASYNC,
7849 };
7850 struct rpc_task *task;
7851 int status = 0;
7852
7853 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7854 "lbw: %llu inode %lu\n",
7855 data->task.tk_pid, sync,
7856 data->args.lastbytewritten,
7857 data->args.inode->i_ino);
7858
Chuck Levera9c92d62013-08-09 12:48:18 -04007859 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007860 task = rpc_run_task(&task_setup_data);
7861 if (IS_ERR(task))
7862 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007863 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007864 goto out;
7865 status = nfs4_wait_for_completion_rpc_task(task);
7866 if (status != 0)
7867 goto out;
7868 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007869 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007870out:
7871 dprintk("%s: status %d\n", __func__, status);
7872 rpc_put_task(task);
7873 return status;
7874}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007875
Andy Adamson97431202013-08-08 10:57:56 -04007876/**
7877 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7878 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7879 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007880static int
7881_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007882 struct nfs_fsinfo *info,
7883 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007884{
7885 struct nfs41_secinfo_no_name_args args = {
7886 .style = SECINFO_STYLE_CURRENT_FH,
7887 };
7888 struct nfs4_secinfo_res res = {
7889 .flavors = flavors,
7890 };
7891 struct rpc_message msg = {
7892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7893 .rpc_argp = &args,
7894 .rpc_resp = &res,
7895 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007896 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007897 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007898 int status;
7899
7900 if (use_integrity) {
7901 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007902 cred = nfs4_get_clid_cred(server->nfs_client);
7903 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007904 }
7905
7906 dprintk("--> %s\n", __func__);
7907 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7908 &res.seq_res, 0);
7909 dprintk("<-- %s status=%d\n", __func__, status);
7910
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007911 if (cred)
7912 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007913
7914 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007915}
7916
7917static int
7918nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7919 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7920{
7921 struct nfs4_exception exception = { };
7922 int err;
7923 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007924 /* first try using integrity protection */
7925 err = -NFS4ERR_WRONGSEC;
7926
7927 /* try to use integrity protection with machine cred */
7928 if (_nfs4_is_integrity_protected(server->nfs_client))
7929 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7930 flavors, true);
7931
7932 /*
7933 * if unable to use integrity protection, or SECINFO with
7934 * integrity protection returns NFS4ERR_WRONGSEC (which is
7935 * disallowed by spec, but exists in deployed servers) use
7936 * the current filesystem's rpc_client and the user cred.
7937 */
7938 if (err == -NFS4ERR_WRONGSEC)
7939 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7940 flavors, false);
7941
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007942 switch (err) {
7943 case 0:
7944 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007945 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007946 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007947 default:
7948 err = nfs4_handle_exception(server, err, &exception);
7949 }
7950 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007951out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007952 return err;
7953}
7954
7955static int
7956nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7957 struct nfs_fsinfo *info)
7958{
7959 int err;
7960 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007961 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007962 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007963 struct nfs4_secinfo4 *secinfo;
7964 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007965
7966 page = alloc_page(GFP_KERNEL);
7967 if (!page) {
7968 err = -ENOMEM;
7969 goto out;
7970 }
7971
7972 flavors = page_address(page);
7973 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7974
7975 /*
7976 * Fall back on "guess and check" method if
7977 * the server doesn't support SECINFO_NO_NAME
7978 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007979 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007980 err = nfs4_find_root_sec(server, fhandle, info);
7981 goto out_freepage;
7982 }
7983 if (err)
7984 goto out_freepage;
7985
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007986 for (i = 0; i < flavors->num_flavors; i++) {
7987 secinfo = &flavors->flavors[i];
7988
7989 switch (secinfo->flavor) {
7990 case RPC_AUTH_NULL:
7991 case RPC_AUTH_UNIX:
7992 case RPC_AUTH_GSS:
7993 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
7994 &secinfo->flavor_info);
7995 break;
7996 default:
7997 flavor = RPC_AUTH_MAXFLAVOR;
7998 break;
7999 }
8000
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008001 if (!nfs_auth_info_match(&server->auth_info, flavor))
8002 flavor = RPC_AUTH_MAXFLAVOR;
8003
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008004 if (flavor != RPC_AUTH_MAXFLAVOR) {
8005 err = nfs4_lookup_root_sec(server, fhandle,
8006 info, flavor);
8007 if (!err)
8008 break;
8009 }
8010 }
8011
8012 if (flavor == RPC_AUTH_MAXFLAVOR)
8013 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008014
8015out_freepage:
8016 put_page(page);
8017 if (err == -EACCES)
8018 return -EPERM;
8019out:
8020 return err;
8021}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008022
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008023static int _nfs41_test_stateid(struct nfs_server *server,
8024 nfs4_stateid *stateid,
8025 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008026{
8027 int status;
8028 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008029 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008030 };
8031 struct nfs41_test_stateid_res res;
8032 struct rpc_message msg = {
8033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8034 .rpc_argp = &args,
8035 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008036 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008037 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008038 struct rpc_clnt *rpc_client = server->client;
8039
8040 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8041 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008042
Chuck Lever38527b12012-07-11 16:30:23 -04008043 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008044 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008045 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008046 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008047 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008048 if (status != NFS_OK) {
8049 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008050 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008051 }
8052 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008053 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008054}
8055
Chuck Lever38527b12012-07-11 16:30:23 -04008056/**
8057 * nfs41_test_stateid - perform a TEST_STATEID operation
8058 *
8059 * @server: server / transport on which to perform the operation
8060 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008061 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008062 *
8063 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8064 * Otherwise a negative NFS4ERR value is returned if the operation
8065 * failed or the state ID is not currently valid.
8066 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008067static int nfs41_test_stateid(struct nfs_server *server,
8068 nfs4_stateid *stateid,
8069 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008070{
8071 struct nfs4_exception exception = { };
8072 int err;
8073 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008074 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008075 if (err != -NFS4ERR_DELAY)
8076 break;
8077 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008078 } while (exception.retry);
8079 return err;
8080}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008081
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008082struct nfs_free_stateid_data {
8083 struct nfs_server *server;
8084 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008085 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008086};
8087
8088static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8089{
8090 struct nfs_free_stateid_data *data = calldata;
8091 nfs41_setup_sequence(nfs4_get_session(data->server),
8092 &data->args.seq_args,
8093 &data->res.seq_res,
8094 task);
8095}
8096
8097static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8098{
8099 struct nfs_free_stateid_data *data = calldata;
8100
8101 nfs41_sequence_done(task, &data->res.seq_res);
8102
8103 switch (task->tk_status) {
8104 case -NFS4ERR_DELAY:
8105 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8106 rpc_restart_call_prepare(task);
8107 }
8108}
8109
8110static void nfs41_free_stateid_release(void *calldata)
8111{
8112 kfree(calldata);
8113}
8114
Trond Myklebust17f26b12013-08-21 15:48:42 -04008115static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008116 .rpc_call_prepare = nfs41_free_stateid_prepare,
8117 .rpc_call_done = nfs41_free_stateid_done,
8118 .rpc_release = nfs41_free_stateid_release,
8119};
8120
8121static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8122 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008123 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008124 bool privileged)
8125{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008126 struct rpc_message msg = {
8127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008128 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008129 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008130 struct rpc_task_setup task_setup = {
8131 .rpc_client = server->client,
8132 .rpc_message = &msg,
8133 .callback_ops = &nfs41_free_stateid_ops,
8134 .flags = RPC_TASK_ASYNC,
8135 };
8136 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008137
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008138 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8139 &task_setup.rpc_client, &msg);
8140
Chuck Lever38527b12012-07-11 16:30:23 -04008141 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008142 data = kmalloc(sizeof(*data), GFP_NOFS);
8143 if (!data)
8144 return ERR_PTR(-ENOMEM);
8145 data->server = server;
8146 nfs4_stateid_copy(&data->args.stateid, stateid);
8147
8148 task_setup.callback_data = data;
8149
8150 msg.rpc_argp = &data->args;
8151 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008152 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008153 if (privileged)
8154 nfs4_set_sequence_privileged(&data->args.seq_args);
8155
8156 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008157}
8158
Chuck Lever38527b12012-07-11 16:30:23 -04008159/**
8160 * nfs41_free_stateid - perform a FREE_STATEID operation
8161 *
8162 * @server: server / transport on which to perform the operation
8163 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008164 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008165 *
8166 * Returns NFS_OK if the server freed "stateid". Otherwise a
8167 * negative NFS4ERR value is returned.
8168 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008169static int nfs41_free_stateid(struct nfs_server *server,
8170 nfs4_stateid *stateid,
8171 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008172{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008173 struct rpc_task *task;
8174 int ret;
8175
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008176 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008177 if (IS_ERR(task))
8178 return PTR_ERR(task);
8179 ret = rpc_wait_for_completion_task(task);
8180 if (!ret)
8181 ret = task->tk_status;
8182 rpc_put_task(task);
8183 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008184}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008185
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008186static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8187{
8188 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008189 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008190
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008191 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008192 nfs4_free_lock_state(server, lsp);
8193 if (IS_ERR(task))
8194 return PTR_ERR(task);
8195 rpc_put_task(task);
8196 return 0;
8197}
8198
Trond Myklebust36281ca2012-03-04 18:13:56 -05008199static bool nfs41_match_stateid(const nfs4_stateid *s1,
8200 const nfs4_stateid *s2)
8201{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008202 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008203 return false;
8204
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008205 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008206 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008207 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008208 return true;
8209
8210 return false;
8211}
8212
Andy Adamson557134a2009-04-01 09:21:53 -04008213#endif /* CONFIG_NFS_V4_1 */
8214
Trond Myklebust36281ca2012-03-04 18:13:56 -05008215static bool nfs4_match_stateid(const nfs4_stateid *s1,
8216 const nfs4_stateid *s2)
8217{
Trond Myklebustf597c532012-03-04 18:13:56 -05008218 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008219}
8220
8221
Trond Myklebust17280172012-03-11 13:11:00 -04008222static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008223 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008224 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008225 .recover_open = nfs4_open_reclaim,
8226 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008227 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008228 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008229};
8230
Andy Adamson591d71c2009-04-01 09:22:47 -04008231#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008232static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008233 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8234 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8235 .recover_open = nfs4_open_reclaim,
8236 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008237 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008239 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008240};
8241#endif /* CONFIG_NFS_V4_1 */
8242
Trond Myklebust17280172012-03-11 13:11:00 -04008243static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008244 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008245 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246 .recover_open = nfs4_open_expired,
8247 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008248 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008249};
8250
Andy Adamson591d71c2009-04-01 09:22:47 -04008251#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008252static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008253 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8254 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008255 .recover_open = nfs41_open_expired,
8256 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008257 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008258};
8259#endif /* CONFIG_NFS_V4_1 */
8260
Trond Myklebust17280172012-03-11 13:11:00 -04008261static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008262 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008263 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008264 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008265};
8266
8267#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008268static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008269 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008270 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008271 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008272};
8273#endif
8274
Chuck Leverec011fe2013-10-17 14:12:39 -04008275static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008276 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008277 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008278};
8279
8280#if defined(CONFIG_NFS_V4_1)
8281static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008282 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008283 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008284};
8285#endif /* CONFIG_NFS_V4_1 */
8286
Trond Myklebust97dc1352010-06-16 09:52:26 -04008287static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8288 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008289 .init_caps = NFS_CAP_READDIRPLUS
8290 | NFS_CAP_ATOMIC_OPEN
8291 | NFS_CAP_CHANGE_ATTR
8292 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008293 .init_client = nfs40_init_client,
8294 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008295 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008296 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008297 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008298 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008299 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8300 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8301 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008302 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008303};
8304
8305#if defined(CONFIG_NFS_V4_1)
8306static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8307 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008308 .init_caps = NFS_CAP_READDIRPLUS
8309 | NFS_CAP_ATOMIC_OPEN
8310 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008311 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008312 | NFS_CAP_STATEID_NFSV41
8313 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008314 .init_client = nfs41_init_client,
8315 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008316 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008317 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008318 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008319 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008320 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8321 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8322 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008323 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008324};
8325#endif
8326
Steve Dickson42c2c422013-05-22 12:50:38 -04008327#if defined(CONFIG_NFS_V4_2)
8328static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8329 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008330 .init_caps = NFS_CAP_READDIRPLUS
8331 | NFS_CAP_ATOMIC_OPEN
8332 | NFS_CAP_CHANGE_ATTR
8333 | NFS_CAP_POSIX_LOCK
8334 | NFS_CAP_STATEID_NFSV41
8335 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008336 .init_client = nfs41_init_client,
8337 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008338 .match_stateid = nfs41_match_stateid,
8339 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008340 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008341 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008342 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8343 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8344 .state_renewal_ops = &nfs41_state_renewal_ops,
8345};
8346#endif
8347
Trond Myklebust97dc1352010-06-16 09:52:26 -04008348const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8349 [0] = &nfs_v4_0_minor_ops,
8350#if defined(CONFIG_NFS_V4_1)
8351 [1] = &nfs_v4_1_minor_ops,
8352#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008353#if defined(CONFIG_NFS_V4_2)
8354 [2] = &nfs_v4_2_minor_ops,
8355#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008356};
8357
Trond Myklebust17f26b12013-08-21 15:48:42 -04008358static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008359 .create = nfs_create,
8360 .lookup = nfs_lookup,
8361 .atomic_open = nfs_atomic_open,
8362 .link = nfs_link,
8363 .unlink = nfs_unlink,
8364 .symlink = nfs_symlink,
8365 .mkdir = nfs_mkdir,
8366 .rmdir = nfs_rmdir,
8367 .mknod = nfs_mknod,
8368 .rename = nfs_rename,
8369 .permission = nfs_permission,
8370 .getattr = nfs_getattr,
8371 .setattr = nfs_setattr,
8372 .getxattr = generic_getxattr,
8373 .setxattr = generic_setxattr,
8374 .listxattr = generic_listxattr,
8375 .removexattr = generic_removexattr,
8376};
8377
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008378static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008379 .permission = nfs_permission,
8380 .getattr = nfs_getattr,
8381 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008382 .getxattr = generic_getxattr,
8383 .setxattr = generic_setxattr,
8384 .listxattr = generic_listxattr,
8385 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008386};
8387
David Howells509de812006-08-22 20:06:11 -04008388const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008389 .version = 4, /* protocol version */
8390 .dentry_ops = &nfs4_dentry_operations,
8391 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008392 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008393 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008394 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008395 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008396 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008397 .getattr = nfs4_proc_getattr,
8398 .setattr = nfs4_proc_setattr,
8399 .lookup = nfs4_proc_lookup,
8400 .access = nfs4_proc_access,
8401 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 .create = nfs4_proc_create,
8403 .remove = nfs4_proc_remove,
8404 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008405 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008406 .unlink_done = nfs4_proc_unlink_done,
8407 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008408 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008409 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008410 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008411 .link = nfs4_proc_link,
8412 .symlink = nfs4_proc_symlink,
8413 .mkdir = nfs4_proc_mkdir,
8414 .rmdir = nfs4_proc_remove,
8415 .readdir = nfs4_proc_readdir,
8416 .mknod = nfs4_proc_mknod,
8417 .statfs = nfs4_proc_statfs,
8418 .fsinfo = nfs4_proc_fsinfo,
8419 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008420 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008421 .decode_dirent = nfs4_decode_dirent,
8422 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008423 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008424 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008425 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008426 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008427 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008428 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008429 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008430 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008431 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008432 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008434 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008435 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008436 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008437 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008438 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008439 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008440 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008441 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008442 .create_server = nfs4_create_server,
8443 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008444};
8445
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008446static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8447 .prefix = XATTR_NAME_NFSV4_ACL,
8448 .list = nfs4_xattr_list_nfs4_acl,
8449 .get = nfs4_xattr_get_nfs4_acl,
8450 .set = nfs4_xattr_set_nfs4_acl,
8451};
8452
8453const struct xattr_handler *nfs4_xattr_handlers[] = {
8454 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008455#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8456 &nfs4_xattr_nfs4_label_handler,
8457#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008458 NULL
8459};
8460
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461/*
8462 * Local variables:
8463 * c-basic-offset: 8
8464 * End:
8465 */