blob: 2427ef4c4d639a5b682ca085f3ff7877fe2e1d40 [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 Myklebust4f14c192014-02-12 19:15:06 -05001140static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
1141{
1142 struct nfs_client *clp = state->owner->so_server->nfs_client;
1143 bool need_recover = false;
1144
1145 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1146 need_recover = true;
1147 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1148 need_recover = true;
1149 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1150 need_recover = true;
1151 if (need_recover)
1152 nfs4_state_mark_reclaim_nograce(clp, state);
1153}
1154
Trond Myklebuste999e802014-02-10 18:20:47 -05001155static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1156 nfs4_stateid *stateid)
1157{
1158 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1159 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001160 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1161 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001162 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001163 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001164 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1165 return true;
1166 return false;
1167}
1168
Trond Myklebust226056c2014-02-11 10:41:07 -05001169static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1170 nfs4_stateid *stateid, fmode_t fmode)
1171{
1172 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1173 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1174 case FMODE_WRITE:
1175 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1176 break;
1177 case FMODE_READ:
1178 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1179 break;
1180 case 0:
1181 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1183 clear_bit(NFS_OPEN_STATE, &state->flags);
1184 }
1185 if (stateid == NULL)
1186 return;
1187 if (!nfs_need_update_open_stateid(state, stateid))
1188 return;
1189 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1190 nfs4_stateid_copy(&state->stateid, stateid);
1191 nfs4_stateid_copy(&state->open_stateid, stateid);
1192}
1193
1194static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1195{
1196 write_seqlock(&state->seqlock);
1197 nfs_clear_open_stateid_locked(state, stateid, fmode);
1198 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001199 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1200 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001201}
1202
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001204{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001206 case FMODE_READ:
1207 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1208 break;
1209 case FMODE_WRITE:
1210 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1211 break;
1212 case FMODE_READ|FMODE_WRITE:
1213 set_bit(NFS_O_RDWR_STATE, &state->flags);
1214 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001215 if (!nfs_need_update_open_stateid(state, stateid))
1216 return;
1217 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1218 nfs4_stateid_copy(&state->stateid, stateid);
1219 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001220}
1221
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222static 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 -07001223{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 /*
1225 * Protect the call to nfs4_state_set_mode_locked and
1226 * serialise the stateid update
1227 */
1228 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001229 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001231 set_bit(NFS_DELEGATED_STATE, &state->flags);
1232 }
1233 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001234 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001235 write_sequnlock(&state->seqlock);
1236 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001238 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001241static 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 -05001242{
1243 struct nfs_inode *nfsi = NFS_I(state->inode);
1244 struct nfs_delegation *deleg_cur;
1245 int ret = 0;
1246
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001248
1249 rcu_read_lock();
1250 deleg_cur = rcu_dereference(nfsi->delegation);
1251 if (deleg_cur == NULL)
1252 goto no_delegation;
1253
1254 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001255 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001256 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001258 goto no_delegation_unlock;
1259
1260 if (delegation == NULL)
1261 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001262 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001263 goto no_delegation_unlock;
1264
Trond Myklebustb7391f42008-12-23 15:21:52 -05001265 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001267 ret = 1;
1268no_delegation_unlock:
1269 spin_unlock(&deleg_cur->lock);
1270no_delegation:
1271 rcu_read_unlock();
1272
1273 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001275 ret = 1;
1276 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001277 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1278 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001279
1280 return ret;
1281}
1282
1283
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001284static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285{
1286 struct nfs_delegation *delegation;
1287
1288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001291 rcu_read_unlock();
1292 return;
1293 }
1294 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001295 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296}
1297
Trond Myklebust6ee41262007-07-08 14:11:36 -04001298static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001299{
1300 struct nfs4_state *state = opendata->state;
1301 struct nfs_inode *nfsi = NFS_I(state->inode);
1302 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001303 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001305 nfs4_stateid stateid;
1306 int ret = -EAGAIN;
1307
Trond Myklebustaac00a82007-07-05 19:02:21 -04001308 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 if (can_open_cached(state, fmode, open_mode)) {
1312 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001314 goto out_return_state;
1315 }
1316 spin_unlock(&state->owner->so_lock);
1317 }
Trond Myklebust34310432008-12-23 15:21:38 -05001318 rcu_read_lock();
1319 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001320 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001321 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001323 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001324 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001325 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001326 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001327 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001328 if (!opendata->is_recover) {
1329 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1330 if (ret != 0)
1331 goto out;
1332 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001333 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001334
1335 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001337 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001339out:
1340 return ERR_PTR(ret);
1341out_return_state:
1342 atomic_inc(&state->count);
1343 return state;
1344}
1345
Andy Adamsone23008e2012-10-02 21:07:32 -04001346static void
1347nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1348{
1349 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1350 struct nfs_delegation *delegation;
1351 int delegation_flags = 0;
1352
1353 rcu_read_lock();
1354 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1355 if (delegation)
1356 delegation_flags = delegation->flags;
1357 rcu_read_unlock();
1358 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1359 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1360 "returning a delegation for "
1361 "OPEN(CLAIM_DELEGATE_CUR)\n",
1362 clp->cl_hostname);
1363 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1364 nfs_inode_set_delegation(state->inode,
1365 data->owner->so_cred,
1366 &data->o_res);
1367 else
1368 nfs_inode_reclaim_delegation(state->inode,
1369 data->owner->so_cred,
1370 &data->o_res);
1371}
1372
1373/*
1374 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1375 * and update the nfs4_state.
1376 */
1377static struct nfs4_state *
1378_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1379{
1380 struct inode *inode = data->state->inode;
1381 struct nfs4_state *state = data->state;
1382 int ret;
1383
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001384 if (!data->rpc_done) {
1385 if (data->rpc_status) {
1386 ret = data->rpc_status;
1387 goto err;
1388 }
1389 /* cached opens have already been processed */
1390 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001391 }
1392
Andy Adamsone23008e2012-10-02 21:07:32 -04001393 ret = nfs_refresh_inode(inode, &data->f_attr);
1394 if (ret)
1395 goto err;
1396
1397 if (data->o_res.delegation_type != 0)
1398 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001399update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001400 update_open_stateid(state, &data->o_res.stateid, NULL,
1401 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001402 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001403
1404 return state;
1405err:
1406 return ERR_PTR(ret);
1407
1408}
1409
1410static struct nfs4_state *
1411_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412{
1413 struct inode *inode;
1414 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001415 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416
Trond Myklebustaac00a82007-07-05 19:02:21 -04001417 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001418 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 goto out;
1420 }
1421
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001422 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001425 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001426 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001427 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001428 goto err;
1429 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 state = nfs4_get_open_state(inode, data->owner);
1431 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001432 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001433 if (data->o_res.delegation_type != 0)
1434 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001435 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001436 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001439 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441err_put_inode:
1442 iput(inode);
1443err:
1444 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445}
1446
Andy Adamsone23008e2012-10-02 21:07:32 -04001447static struct nfs4_state *
1448nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1449{
1450 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1451 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1452 return _nfs4_opendata_to_nfs4_state(data);
1453}
1454
Trond Myklebust864472e2006-01-03 09:55:15 +01001455static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1456{
1457 struct nfs_inode *nfsi = NFS_I(state->inode);
1458 struct nfs_open_context *ctx;
1459
1460 spin_lock(&state->inode->i_lock);
1461 list_for_each_entry(ctx, &nfsi->open_files, list) {
1462 if (ctx->state != state)
1463 continue;
1464 get_nfs_open_context(ctx);
1465 spin_unlock(&state->inode->i_lock);
1466 return ctx;
1467 }
1468 spin_unlock(&state->inode->i_lock);
1469 return ERR_PTR(-ENOENT);
1470}
1471
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001472static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1473 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474{
1475 struct nfs4_opendata *opendata;
1476
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001477 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001478 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001479 if (opendata == NULL)
1480 return ERR_PTR(-ENOMEM);
1481 opendata->state = state;
1482 atomic_inc(&state->count);
1483 return opendata;
1484}
1485
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001486static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001487{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001488 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001489 int ret;
1490
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001491 opendata->o_arg.open_flags = 0;
1492 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001493 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1494 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1495 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001496 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 if (ret != 0)
1498 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001499 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001500 if (IS_ERR(newstate))
1501 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001502 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001503 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001504 return 0;
1505}
1506
1507static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1508{
Trond Myklebust864472e2006-01-03 09:55:15 +01001509 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 int ret;
1511
Trond Myklebust4f14c192014-02-12 19:15:06 -05001512 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1513 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1514 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001516 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001517 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001518 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001519 smp_rmb();
1520 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001521 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001522 if (ret != 0)
1523 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001524 if (newstate != state)
1525 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001526 }
1527 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001528 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 if (ret != 0)
1530 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001531 if (newstate != state)
1532 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001533 }
1534 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001535 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001536 if (ret != 0)
1537 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001538 if (newstate != state)
1539 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001540 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001541 /*
1542 * We may have performed cached opens for all three recoveries.
1543 * Check if we need to update the current stateid.
1544 */
1545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001546 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001547 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001549 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001550 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001551 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001552 return 0;
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * OPEN_RECLAIM:
1557 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001559static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001561 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001562 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int status;
1565
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001566 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1567 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001568 if (IS_ERR(opendata))
1569 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001570 rcu_read_lock();
1571 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001572 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001573 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001574 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001575 opendata->o_arg.u.delegation_type = delegation_type;
1576 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return status;
1579}
1580
Trond Myklebust539cd032007-06-05 11:46:42 -04001581static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct nfs_server *server = NFS_SERVER(state->inode);
1584 struct nfs4_exception exception = { };
1585 int err;
1586 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001587 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001588 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001589 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1590 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001591 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001592 break;
1593 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 } while (exception.retry);
1595 return err;
1596}
1597
Trond Myklebust864472e2006-01-03 09:55:15 +01001598static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1599{
1600 struct nfs_open_context *ctx;
1601 int ret;
1602
1603 ctx = nfs4_state_find_open_context(state);
1604 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001605 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001606 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 put_nfs_open_context(ctx);
1608 return ret;
1609}
1610
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001611static 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 -07001612{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001613 switch (err) {
1614 default:
1615 printk(KERN_ERR "NFS: %s: unhandled error "
1616 "%d.\n", __func__, err);
1617 case 0:
1618 case -ENOENT:
1619 case -ESTALE:
1620 break;
1621 case -NFS4ERR_BADSESSION:
1622 case -NFS4ERR_BADSLOT:
1623 case -NFS4ERR_BAD_HIGH_SLOT:
1624 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1625 case -NFS4ERR_DEADSESSION:
1626 set_bit(NFS_DELEGATED_STATE, &state->flags);
1627 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1628 return -EAGAIN;
1629 case -NFS4ERR_STALE_CLIENTID:
1630 case -NFS4ERR_STALE_STATEID:
1631 set_bit(NFS_DELEGATED_STATE, &state->flags);
1632 case -NFS4ERR_EXPIRED:
1633 /* Don't recall a delegation if it was lost */
1634 nfs4_schedule_lease_recovery(server->nfs_client);
1635 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001636 case -NFS4ERR_MOVED:
1637 nfs4_schedule_migration_recovery(server);
1638 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001639 case -NFS4ERR_LEASE_MOVED:
1640 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1641 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001642 case -NFS4ERR_DELEG_REVOKED:
1643 case -NFS4ERR_ADMIN_REVOKED:
1644 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001645 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001646 nfs_inode_find_state_and_recover(state->inode,
1647 stateid);
1648 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001649 return 0;
1650 case -NFS4ERR_DELAY:
1651 case -NFS4ERR_GRACE:
1652 set_bit(NFS_DELEGATED_STATE, &state->flags);
1653 ssleep(1);
1654 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001655 case -ENOMEM:
1656 case -NFS4ERR_DENIED:
1657 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1658 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return err;
1661}
1662
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001663int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1664{
1665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs4_opendata *opendata;
1667 int err;
1668
1669 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1670 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1671 if (IS_ERR(opendata))
1672 return PTR_ERR(opendata);
1673 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1674 err = nfs4_open_recover(opendata, state);
1675 nfs4_opendata_put(opendata);
1676 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1677}
1678
Chuck Leverbe05c862013-08-09 12:49:47 -04001679static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1680{
1681 struct nfs4_opendata *data = calldata;
1682
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001683 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1684 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001685}
1686
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001687static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1688{
1689 struct nfs4_opendata *data = calldata;
1690
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001691 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001692
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001693 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001694 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001695 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001696 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001697 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001698 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001699 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001700}
1701
1702static void nfs4_open_confirm_release(void *calldata)
1703{
1704 struct nfs4_opendata *data = calldata;
1705 struct nfs4_state *state = NULL;
1706
1707 /* If this request hasn't been cancelled, do nothing */
1708 if (data->cancelled == 0)
1709 goto out_free;
1710 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001711 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001712 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001713 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001714 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001715 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001716out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001717 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001718}
1719
1720static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001721 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001722 .rpc_call_done = nfs4_open_confirm_done,
1723 .rpc_release = nfs4_open_confirm_release,
1724};
1725
1726/*
1727 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1728 */
1729static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1730{
1731 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1732 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001733 struct rpc_message msg = {
1734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1735 .rpc_argp = &data->c_arg,
1736 .rpc_resp = &data->c_res,
1737 .rpc_cred = data->owner->so_cred,
1738 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001739 struct rpc_task_setup task_setup_data = {
1740 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001741 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001742 .callback_ops = &nfs4_open_confirm_ops,
1743 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001744 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001745 .flags = RPC_TASK_ASYNC,
1746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int status;
1748
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001749 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001750 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001751 data->rpc_done = 0;
1752 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001753 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001754 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001755 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001756 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001757 status = nfs4_wait_for_completion_rpc_task(task);
1758 if (status != 0) {
1759 data->cancelled = 1;
1760 smp_wmb();
1761 } else
1762 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001763 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return status;
1765}
1766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769 struct nfs4_opendata *data = calldata;
1770 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001771 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001774 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001775 /*
1776 * Check if we still need to send an OPEN call, or if we can use
1777 * a delegation instead.
1778 */
1779 if (data->state != NULL) {
1780 struct nfs_delegation *delegation;
1781
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001782 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001783 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784 rcu_read_lock();
1785 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001786 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001787 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001788 can_open_delegated(delegation, data->o_arg.fmode))
1789 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001790 rcu_read_unlock();
1791 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001792 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001793 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001794 switch (data->o_arg.claim) {
1795 case NFS4_OPEN_CLAIM_PREVIOUS:
1796 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001798 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001799 case NFS4_OPEN_CLAIM_FH:
1800 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001801 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1802 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001803 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001804 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001805 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001806 &data->o_res.seq_res,
1807 task) != 0)
1808 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001809
1810 /* Set the create mode (note dependency on the session type) */
1811 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1812 if (data->o_arg.open_flags & O_EXCL) {
1813 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1814 if (nfs4_has_persistent_session(clp))
1815 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1816 else if (clp->cl_mvops->minor_version > 0)
1817 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1818 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001819 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001820unlock_no_action:
1821 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001822out_no_action:
1823 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001824out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001825 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001826}
1827
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828static void nfs4_open_done(struct rpc_task *task, void *calldata)
1829{
1830 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001832 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001833
Trond Myklebust14516c32010-07-31 14:29:06 -04001834 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1835 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001836
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001838 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1839 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001840 case S_IFREG:
1841 break;
1842 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001844 break;
1845 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001847 break;
1848 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001850 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001851 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001852 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001853 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1854 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001855 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001856 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001857}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001858
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859static void nfs4_open_release(void *calldata)
1860{
1861 struct nfs4_opendata *data = calldata;
1862 struct nfs4_state *state = NULL;
1863
1864 /* If this request hasn't been cancelled, do nothing */
1865 if (data->cancelled == 0)
1866 goto out_free;
1867 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001868 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001869 goto out_free;
1870 /* In case we need an open_confirm, no cleanup! */
1871 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1872 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001873 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001874 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001875 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001876out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001877 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001878}
1879
1880static const struct rpc_call_ops nfs4_open_ops = {
1881 .rpc_call_prepare = nfs4_open_prepare,
1882 .rpc_call_done = nfs4_open_done,
1883 .rpc_release = nfs4_open_release,
1884};
1885
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001886static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887{
1888 struct inode *dir = data->dir->d_inode;
1889 struct nfs_server *server = NFS_SERVER(dir);
1890 struct nfs_openargs *o_arg = &data->o_arg;
1891 struct nfs_openres *o_res = &data->o_res;
1892 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001893 struct rpc_message msg = {
1894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1895 .rpc_argp = o_arg,
1896 .rpc_resp = o_res,
1897 .rpc_cred = data->owner->so_cred,
1898 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001899 struct rpc_task_setup task_setup_data = {
1900 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001901 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001902 .callback_ops = &nfs4_open_ops,
1903 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001904 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001905 .flags = RPC_TASK_ASYNC,
1906 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001907 int status;
1908
Chuck Levera9c92d62013-08-09 12:48:18 -04001909 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001911 data->rpc_done = 0;
1912 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001913 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001914 data->is_recover = 0;
1915 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001916 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001917 data->is_recover = 1;
1918 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001919 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001920 if (IS_ERR(task))
1921 return PTR_ERR(task);
1922 status = nfs4_wait_for_completion_rpc_task(task);
1923 if (status != 0) {
1924 data->cancelled = 1;
1925 smp_wmb();
1926 } else
1927 status = data->rpc_status;
1928 rpc_put_task(task);
1929
1930 return status;
1931}
1932
1933static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1934{
1935 struct inode *dir = data->dir->d_inode;
1936 struct nfs_openres *o_res = &data->o_res;
1937 int status;
1938
1939 status = nfs4_run_open_task(data, 1);
1940 if (status != 0 || !data->rpc_done)
1941 return status;
1942
Trond Myklebust6926afd2012-01-07 13:22:46 -05001943 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1944
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001945 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1946 status = _nfs4_proc_open_confirm(data);
1947 if (status != 0)
1948 return status;
1949 }
1950
1951 return status;
1952}
1953
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001954static int nfs4_opendata_access(struct rpc_cred *cred,
1955 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001956 struct nfs4_state *state, fmode_t fmode,
1957 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001958{
1959 struct nfs_access_entry cache;
1960 u32 mask;
1961
1962 /* access call failed or for some reason the server doesn't
1963 * support any access modes -- defer access call until later */
1964 if (opendata->o_res.access_supported == 0)
1965 return 0;
1966
1967 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001968 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001969 * write mode bits, but POSIX allows the creating process to write.
1970 * use openflags to check for exec, because fmode won't
1971 * always have FMODE_EXEC set when file open for exec. */
1972 if (openflags & __FMODE_EXEC) {
1973 /* ONLY check for exec rights */
1974 mask = MAY_EXEC;
1975 } else if (fmode & FMODE_READ)
1976 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001977
1978 cache.cred = cred;
1979 cache.jiffies = jiffies;
1980 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1981 nfs_access_add_cache(state->inode, &cache);
1982
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001983 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001984 return 0;
1985
1986 /* even though OPEN succeeded, access is denied. Close the file */
1987 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001988 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001989}
1990
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001991/*
1992 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1993 */
1994static int _nfs4_proc_open(struct nfs4_opendata *data)
1995{
1996 struct inode *dir = data->dir->d_inode;
1997 struct nfs_server *server = NFS_SERVER(dir);
1998 struct nfs_openargs *o_arg = &data->o_arg;
1999 struct nfs_openres *o_res = &data->o_res;
2000 int status;
2001
2002 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002003 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002005 if (status != 0) {
2006 if (status == -NFS4ERR_BADNAME &&
2007 !(o_arg->open_flags & O_CREAT))
2008 return -ENOENT;
2009 return status;
2010 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002011
Trond Myklebust6926afd2012-01-07 13:22:46 -05002012 nfs_fattr_map_and_free_names(server, &data->f_attr);
2013
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002014 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002015 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002016 if (o_arg->open_flags & O_EXCL)
2017 data->file_created = 1;
2018 else if (o_res->cinfo.before != o_res->cinfo.after)
2019 data->file_created = 1;
2020 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002021 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2022 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002026 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04002029 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Andy Adamsond83217c2011-03-01 01:34:17 +00002033static int nfs4_recover_expired_lease(struct nfs_server *server)
2034{
2035 return nfs4_client_recover_expired_lease(server->nfs_client);
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/*
2039 * OPEN_EXPIRED:
2040 * reclaim state on the server after a network partition.
2041 * Assumes caller holds the appropriate lock
2042 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002043static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002045 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002046 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002048 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002049 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002050 if (IS_ERR(opendata))
2051 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002052 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002053 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002054 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002055 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002056 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002059static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002060{
Trond Myklebust539cd032007-06-05 11:46:42 -04002061 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002062 struct nfs4_exception exception = { };
2063 int err;
2064
2065 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002066 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002067 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002068 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2069 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002070 switch (err) {
2071 default:
2072 goto out;
2073 case -NFS4ERR_GRACE:
2074 case -NFS4ERR_DELAY:
2075 nfs4_handle_exception(server, err, &exception);
2076 err = 0;
2077 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002078 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002079out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002080 return err;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Trond Myklebust864472e2006-01-03 09:55:15 +01002088 ctx = nfs4_state_find_open_context(state);
2089 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002090 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002091 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002092 put_nfs_open_context(ctx);
2093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002096#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002097static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002098{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002099 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002100 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002101 struct nfs_delegation *delegation;
2102 struct rpc_cred *cred = NULL;
2103 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002104
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002105 /* If a state reset has been done, test_stateid is unneeded */
2106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2107 return;
2108
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002109 /* Get the delegation credential for use by test/free_stateid */
2110 rcu_read_lock();
2111 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2112 if (delegation != NULL &&
2113 nfs4_stateid_match(&delegation->stateid, stateid)) {
2114 cred = get_rpccred(delegation->cred);
2115 rcu_read_unlock();
2116 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002117 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002118 } else
2119 rcu_read_unlock();
2120
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002121 if (status != NFS_OK) {
2122 /* Free the stateid unless the server explicitly
2123 * informs us the stateid is unrecognized. */
2124 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002125 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002126 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002127
Bryan Schumaker69388672012-09-26 15:25:52 -04002128 write_seqlock(&state->seqlock);
2129 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2130 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002131 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2132 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002133
2134 if (cred != NULL)
2135 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002136}
2137
2138/**
2139 * nfs41_check_open_stateid - possibly free an open stateid
2140 *
2141 * @state: NFSv4 state for an inode
2142 *
2143 * Returns NFS_OK if recovery for this stateid is now finished.
2144 * Otherwise a negative NFS4ERR value is returned.
2145 */
2146static int nfs41_check_open_stateid(struct nfs4_state *state)
2147{
2148 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002149 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002150 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002151 int status;
2152
2153 /* If a state reset has been done, test_stateid is unneeded */
2154 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2155 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2156 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2157 return -NFS4ERR_BAD_STATEID;
2158
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002159 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002160 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002161 if (status != NFS_OK) {
2162 /* Free the stateid unless the server explicitly
2163 * informs us the stateid is unrecognized. */
2164 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002165 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002166
2167 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2168 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2169 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002170 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002171 }
2172 return status;
2173}
2174
2175static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2176{
Chuck Levereb64cf92012-07-11 16:30:05 -04002177 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002178
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002179 nfs41_clear_delegation_stateid(state);
2180 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002181 if (status != NFS_OK)
2182 status = nfs4_open_expired(sp, state);
2183 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002184}
2185#endif
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002188 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2189 * fields corresponding to attributes that were used to store the verifier.
2190 * Make sure we clobber those fields in the later setattr call
2191 */
2192static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2193{
2194 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2195 !(sattr->ia_valid & ATTR_ATIME_SET))
2196 sattr->ia_valid |= ATTR_ATIME;
2197
2198 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2199 !(sattr->ia_valid & ATTR_MTIME_SET))
2200 sattr->ia_valid |= ATTR_MTIME;
2201}
2202
Trond Myklebustc21443c2013-02-07 14:26:21 -05002203static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2204 fmode_t fmode,
2205 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002206 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002207{
2208 struct nfs4_state_owner *sp = opendata->owner;
2209 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002210 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002211 struct nfs4_state *state;
2212 unsigned int seq;
2213 int ret;
2214
2215 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2216
2217 ret = _nfs4_proc_open(opendata);
2218 if (ret != 0)
2219 goto out;
2220
2221 state = nfs4_opendata_to_nfs4_state(opendata);
2222 ret = PTR_ERR(state);
2223 if (IS_ERR(state))
2224 goto out;
2225 if (server->caps & NFS_CAP_POSIX_LOCK)
2226 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2227
Trond Myklebust275bb302013-05-29 13:11:28 -04002228 dentry = opendata->dentry;
2229 if (dentry->d_inode == NULL) {
2230 /* FIXME: Is this d_drop() ever needed? */
2231 d_drop(dentry);
2232 dentry = d_add_unique(dentry, igrab(state->inode));
2233 if (dentry == NULL) {
2234 dentry = opendata->dentry;
2235 } else if (dentry != ctx->dentry) {
2236 dput(ctx->dentry);
2237 ctx->dentry = dget(dentry);
2238 }
2239 nfs_set_verifier(dentry,
2240 nfs_save_change_attribute(opendata->dir->d_inode));
2241 }
2242
Trond Myklebustc21443c2013-02-07 14:26:21 -05002243 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2244 if (ret != 0)
2245 goto out;
2246
Trond Myklebust3efb9722013-05-29 13:17:04 -04002247 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002248 if (dentry->d_inode == state->inode) {
2249 nfs_inode_attach_open_context(ctx);
2250 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2251 nfs4_schedule_stateid_recovery(server, state);
2252 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002253out:
2254 return ret;
2255}
2256
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002257/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 */
Andy Adamson82be4172012-05-23 05:02:35 -04002260static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002261 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002262 int flags,
2263 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002264 struct nfs4_label *label,
2265 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 struct nfs4_state_owner *sp;
2268 struct nfs4_state *state = NULL;
2269 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002270 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002271 struct dentry *dentry = ctx->dentry;
2272 struct rpc_cred *cred = ctx->cred;
2273 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2274 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002275 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002276 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002277 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002281 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2282 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2284 goto out_err;
2285 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002286 status = nfs4_recover_expired_lease(server);
2287 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002288 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002289 if (dentry->d_inode != NULL)
2290 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002291 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002292 if (dentry->d_inode)
2293 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002294 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002295 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002296 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002297 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
David Quigley14c43f72013-05-22 12:50:43 -04002299 if (label) {
2300 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2301 if (IS_ERR(olabel)) {
2302 status = PTR_ERR(olabel);
2303 goto err_opendata_put;
2304 }
2305 }
2306
Andy Adamson82be4172012-05-23 05:02:35 -04002307 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2308 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2309 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002310 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002311 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002312 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002313 if (dentry->d_inode != NULL)
2314 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002315
Trond Myklebust3efb9722013-05-29 13:17:04 -04002316 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002317 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002318 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002319 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002320
Trond Myklebust549b19c2013-04-16 18:42:34 -04002321 if ((opendata->o_arg.open_flags & O_EXCL) &&
2322 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002323 nfs4_exclusive_attrset(opendata, sattr);
2324
2325 nfs_fattr_init(opendata->o_res.f_attr);
2326 status = nfs4_do_setattr(state->inode, cred,
2327 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002328 state, label, olabel);
2329 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002330 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002331 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002332 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002333 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002334 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002335 if (opendata->file_created)
2336 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002337
2338 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2339 *ctx_th = opendata->f_attr.mdsthreshold;
2340 else
2341 kfree(opendata->f_attr.mdsthreshold);
2342 opendata->f_attr.mdsthreshold = NULL;
2343
David Quigley14c43f72013-05-22 12:50:43 -04002344 nfs4_label_free(olabel);
2345
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002346 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002349err_free_label:
2350 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002351err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002352 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002353 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002354err_put_state_owner:
2355 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return status;
2358}
2359
2360
Andy Adamson82be4172012-05-23 05:02:35 -04002361static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002362 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002363 int flags,
2364 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002365 struct nfs4_label *label,
2366 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002368 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct nfs4_exception exception = { };
2370 struct nfs4_state *res;
2371 int status;
2372
2373 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002374 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002375 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002376 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (status == 0)
2378 break;
2379 /* NOTE: BAD_SEQID means the server and client disagree about the
2380 * book-keeping w.r.t. state-changing operations
2381 * (OPEN/CLOSE/LOCK/LOCKU...)
2382 * It is actually a sign of a bug on the client or on the server.
2383 *
2384 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002385 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * have unhashed the old state_owner for us, and that we can
2387 * therefore safely retry using a new one. We should still warn
2388 * the user though...
2389 */
2390 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002391 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002392 " returned a bad sequence-id error!\n",
2393 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 exception.retry = 1;
2395 continue;
2396 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002397 /*
2398 * BAD_STATEID on OPEN means that the server cancelled our
2399 * state before it received the OPEN_CONFIRM.
2400 * Recover by retrying the request as per the discussion
2401 * on Page 181 of RFC3530.
2402 */
2403 if (status == -NFS4ERR_BAD_STATEID) {
2404 exception.retry = 1;
2405 continue;
2406 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002407 if (status == -EAGAIN) {
2408 /* We must have found a delegation */
2409 exception.retry = 1;
2410 continue;
2411 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002412 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2413 continue;
2414 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 status, &exception));
2416 } while (exception.retry);
2417 return res;
2418}
2419
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002420static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2421 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002422 struct nfs4_state *state, struct nfs4_label *ilabel,
2423 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002425 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002427 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 .iap = sattr,
2429 .server = server,
2430 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002431 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 };
2433 struct nfs_setattrres res = {
2434 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002435 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 .server = server,
2437 };
2438 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2440 .rpc_argp = &arg,
2441 .rpc_resp = &res,
2442 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002444 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002445 fmode_t fmode;
2446 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
David Quigleyaa9c2662013-05-22 12:50:44 -04002449 arg.bitmask = nfs4_bitmask(server, ilabel);
2450 if (ilabel)
2451 arg.bitmask = nfs4_bitmask(server, olabel);
2452
Trond Myklebust0e574af2005-10-27 22:12:38 -04002453 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Trond Myklebustee3ae842013-04-29 10:35:36 -04002455 /* Servers should only apply open mode checks for file size changes */
2456 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2457 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2458
2459 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2460 /* Use that stateid */
2461 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002462 struct nfs_lockowner lockowner = {
2463 .l_owner = current->files,
2464 .l_pid = current->tgid,
2465 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002466 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002467 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002468 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002469 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Bryan Schumaker7c513052011-03-24 17:12:24 +00002471 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002472 if (status == 0 && state != NULL)
2473 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002474 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002477static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2478 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002479 struct nfs4_state *state, struct nfs4_label *ilabel,
2480 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002482 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002483 struct nfs4_exception exception = {
2484 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002485 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002486 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 int err;
2488 do {
David Quigley1775fd32013-05-22 12:50:42 -04002489 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002490 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002491 switch (err) {
2492 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002493 if (!(sattr->ia_valid & ATTR_SIZE)) {
2494 pr_warn_once("NFSv4: server %s is incorrectly "
2495 "applying open mode checks to "
2496 "a SETATTR that is not "
2497 "changing file size.\n",
2498 server->nfs_client->cl_hostname);
2499 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002500 if (state && !(state->state & FMODE_WRITE)) {
2501 err = -EBADF;
2502 if (sattr->ia_valid & ATTR_OPEN)
2503 err = -EACCES;
2504 goto out;
2505 }
2506 }
2507 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002509out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 return err;
2511}
2512
2513struct nfs4_closedata {
2514 struct inode *inode;
2515 struct nfs4_state *state;
2516 struct nfs_closeargs arg;
2517 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002518 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002519 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002520 bool roc;
2521 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522};
2523
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002524static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002525{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002526 struct nfs4_closedata *calldata = data;
2527 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002528 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002529
Fred Isamanf7e89172011-01-06 11:36:32 +00002530 if (calldata->roc)
2531 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002532 nfs4_put_open_state(calldata->state);
2533 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002534 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002535 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002536 kfree(calldata);
2537}
2538
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002539static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002541 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct nfs_server *server = NFS_SERVER(calldata->inode);
2544
Chuck Levera3ca5652012-03-01 17:00:40 -05002545 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002546 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2547 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002548 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 /* hmm. we are done with the inode, and in the process of freeing
2550 * the state_owner. we keep this around to process errors
2551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 switch (task->tk_status) {
2553 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002554 if (calldata->roc)
2555 pnfs_roc_set_barrier(state->inode,
2556 calldata->roc_barrier);
Trond Myklebust226056c2014-02-11 10:41:07 -05002557 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002558 renew_lease(server, calldata->timestamp);
Trond Myklebust226056c2014-02-11 10:41:07 -05002559 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002560 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002562 case -NFS4ERR_OLD_STATEID:
2563 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002565 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 default:
Trond Myklebust69794ad2013-11-20 12:57:19 -05002568 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002569 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002570 goto out_release;
2571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Trond Myklebust226056c2014-02-11 10:41:07 -05002573 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002574out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002575 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002576 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002577 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002580static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002582 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002583 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002584 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002585 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002586
Chuck Levera3ca5652012-03-01 17:00:40 -05002587 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002588 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002589 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002590
Trond Myklebust88069f72009-12-08 08:33:16 -05002591 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2592 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002593 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002594 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002595 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002596 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002597 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2598 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2599 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002600 }
2601 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002602 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2603 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2604 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002605 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002606 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002607 if (!nfs4_valid_open_stateid(state))
2608 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002609 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002610
2611 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002612 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002613 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002614 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002615
Fred Isamanf7e89172011-01-06 11:36:32 +00002616 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002617 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002618 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002619 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2620 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002621 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002622 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002623 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002624
Trond Myklebust516a6af2005-10-27 22:12:41 -04002625 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002626 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002627 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002628 &calldata->arg.seq_args,
2629 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002630 task) != 0)
2631 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002632 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002633 return;
2634out_no_action:
2635 task->tk_action = NULL;
2636out_wait:
2637 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002640static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002641 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002642 .rpc_call_done = nfs4_close_done,
2643 .rpc_release = nfs4_free_closedata,
2644};
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646/*
2647 * It is possible for data to be read/written from a mem-mapped file
2648 * after the sys_close call (which hits the vfs layer as a flush).
2649 * This means that we can't safely call nfsv4 close on a file until
2650 * the inode is cleared. This in turn means that we are not good
2651 * NFSv4 citizens - we do not indicate to the server to update the file's
2652 * share state even when we are done with one of the three share
2653 * stateid's in the inode.
2654 *
2655 * NOTE: Caller must be holding the sp->so_owner semaphore!
2656 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002657int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002659 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002661 struct nfs4_state_owner *sp = state->owner;
2662 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2665 .rpc_cred = state->owner->so_cred,
2666 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002667 struct rpc_task_setup task_setup_data = {
2668 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002669 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002670 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002671 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002672 .flags = RPC_TASK_ASYNC,
2673 };
Trond Myklebust95121352005-10-18 14:20:12 -07002674 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002676 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2677 &task_setup_data.rpc_client, &msg);
2678
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002679 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002681 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002682 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002683 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002685 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002686 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002688 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002689 if (calldata->arg.seqid == NULL)
2690 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002691 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002692 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002693 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002694 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002695 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002696 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002697 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002698
Trond Myklebust1174dd12010-12-21 10:52:24 -05002699 msg.rpc_argp = &calldata->arg;
2700 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002701 task_setup_data.callback_data = calldata;
2702 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002703 if (IS_ERR(task))
2704 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002705 status = 0;
2706 if (wait)
2707 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002708 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002709 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002710out_free_calldata:
2711 kfree(calldata);
2712out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002713 nfs4_put_open_state(state);
2714 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002715 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Trond Myklebust2b484292010-09-17 10:56:51 -04002718static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002719nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2720 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002723 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2724
2725 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Trond Myklebust565277f2007-10-15 18:17:53 -04002727 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002728 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002729
2730 nfs4_label_release_security(label);
2731
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002732 if (IS_ERR(state))
2733 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002734 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Trond Myklebust1185a552009-12-03 15:54:02 -05002737static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002738{
2739 if (ctx->state == NULL)
2740 return;
2741 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002742 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002743 else
Al Viro643168c2011-06-22 18:20:23 -04002744 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002745}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Trond Myklebustb944dba2013-11-04 15:20:20 -05002747#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2748#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2749#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2752{
Benny Halevy43652ad2009-04-01 09:21:54 -04002753 struct nfs4_server_caps_arg args = {
2754 .fhandle = fhandle,
2755 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 struct nfs4_server_caps_res res = {};
2757 struct rpc_message msg = {
2758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002759 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 .rpc_resp = &res,
2761 };
2762 int status;
2763
Bryan Schumaker7c513052011-03-24 17:12:24 +00002764 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002766 /* Sanity check the server answers */
2767 switch (server->nfs_client->cl_minorversion) {
2768 case 0:
2769 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2770 res.attr_bitmask[2] = 0;
2771 break;
2772 case 1:
2773 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2774 break;
2775 case 2:
2776 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002779 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2780 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2781 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2782 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002783 NFS_CAP_CTIME|NFS_CAP_MTIME|
2784 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002785 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2786 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 server->caps |= NFS_CAP_ACLS;
2788 if (res.has_links != 0)
2789 server->caps |= NFS_CAP_HARDLINKS;
2790 if (res.has_symlinks != 0)
2791 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002792 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2793 server->caps |= NFS_CAP_FILEID;
2794 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2795 server->caps |= NFS_CAP_MODE;
2796 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2797 server->caps |= NFS_CAP_NLINK;
2798 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2799 server->caps |= NFS_CAP_OWNER;
2800 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2801 server->caps |= NFS_CAP_OWNER_GROUP;
2802 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2803 server->caps |= NFS_CAP_ATIME;
2804 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2805 server->caps |= NFS_CAP_CTIME;
2806 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2807 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002808#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2809 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2810 server->caps |= NFS_CAP_SECURITY_LABEL;
2811#endif
2812 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2813 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002814 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002815
Trond Myklebusta65318b2009-03-11 14:10:28 -04002816 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2817 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2818 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002819 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002821 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return status;
2825}
2826
Trond Myklebust55a97592006-06-09 09:34:19 -04002827int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
2829 struct nfs4_exception exception = { };
2830 int err;
2831 do {
2832 err = nfs4_handle_exception(server,
2833 _nfs4_server_capabilities(server, fhandle),
2834 &exception);
2835 } while (exception.retry);
2836 return err;
2837}
2838
2839static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2840 struct nfs_fsinfo *info)
2841{
David Quigleyaa9c2662013-05-22 12:50:44 -04002842 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002844 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 };
2846 struct nfs4_lookup_res res = {
2847 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002848 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 .fh = fhandle,
2850 };
2851 struct rpc_message msg = {
2852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2853 .rpc_argp = &args,
2854 .rpc_resp = &res,
2855 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002856
David Quigleyaa9c2662013-05-22 12:50:44 -04002857 bitmask[0] = nfs4_fattr_bitmap[0];
2858 bitmask[1] = nfs4_fattr_bitmap[1];
2859 /*
2860 * Process the label in the upcoming getfattr
2861 */
2862 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2863
Trond Myklebust0e574af2005-10-27 22:12:38 -04002864 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002865 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
2868static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2869 struct nfs_fsinfo *info)
2870{
2871 struct nfs4_exception exception = { };
2872 int err;
2873 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002874 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002875 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002876 switch (err) {
2877 case 0:
2878 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002879 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002880 default:
2881 err = nfs4_handle_exception(server, err, &exception);
2882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002884out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return err;
2886}
2887
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002888static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2889 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2890{
Trond Myklebustc2190662013-08-26 19:23:04 -04002891 struct rpc_auth_create_args auth_args = {
2892 .pseudoflavor = flavor,
2893 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002894 struct rpc_auth *auth;
2895 int ret;
2896
Trond Myklebustc2190662013-08-26 19:23:04 -04002897 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002898 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002899 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002900 goto out;
2901 }
2902 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002903out:
2904 return ret;
2905}
2906
Chuck Lever9a744ba2013-03-16 15:56:02 -04002907/*
2908 * Retry pseudoroot lookup with various security flavors. We do this when:
2909 *
2910 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2911 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2912 *
2913 * Returns zero on success, or a negative NFS4ERR value, or a
2914 * negative errno value.
2915 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002916static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002917 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002919 /* Per 3530bis 15.33.5 */
2920 static const rpc_authflavor_t flav_array[] = {
2921 RPC_AUTH_GSS_KRB5P,
2922 RPC_AUTH_GSS_KRB5I,
2923 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002924 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002925 RPC_AUTH_NULL,
2926 };
2927 int status = -EPERM;
2928 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04002930 if (server->auth_info.flavor_len > 0) {
2931 /* try each flavor specified by user */
2932 for (i = 0; i < server->auth_info.flavor_len; i++) {
2933 status = nfs4_lookup_root_sec(server, fhandle, info,
2934 server->auth_info.flavors[i]);
2935 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2936 continue;
2937 break;
2938 }
2939 } else {
2940 /* no flavors specified by user, try default list */
2941 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2942 status = nfs4_lookup_root_sec(server, fhandle, info,
2943 flav_array[i]);
2944 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2945 continue;
2946 break;
2947 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002948 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002949
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002950 /*
2951 * -EACCESS could mean that the user doesn't have correct permissions
2952 * to access the mount. It could also mean that we tried to mount
2953 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2954 * existing mount programs don't handle -EACCES very well so it should
2955 * be mapped to -EPERM instead.
2956 */
2957 if (status == -EACCES)
2958 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002959 return status;
2960}
2961
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002962static int nfs4_do_find_root_sec(struct nfs_server *server,
2963 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2964{
2965 int mv = server->nfs_client->cl_minorversion;
2966 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2967}
2968
2969/**
2970 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2971 * @server: initialized nfs_server handle
2972 * @fhandle: we fill in the pseudo-fs root file handle
2973 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002974 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002975 *
2976 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002977 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002978int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002979 struct nfs_fsinfo *info,
2980 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002981{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002982 int status;
2983
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002984 switch (auth_probe) {
2985 case false:
2986 status = nfs4_lookup_root(server, fhandle, info);
2987 if (status != -NFS4ERR_WRONGSEC)
2988 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002989 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002990 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002991 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (status == 0)
2994 status = nfs4_server_capabilities(server, fhandle);
2995 if (status == 0)
2996 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002997
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002998 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
Bryan Schumakerbae36242012-05-10 15:07:31 -04003001static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3002 struct nfs_fsinfo *info)
3003{
3004 int error;
3005 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003006 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003007
3008 error = nfs4_server_capabilities(server, mntfh);
3009 if (error < 0) {
3010 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3011 return error;
3012 }
3013
David Quigley14c43f72013-05-22 12:50:43 -04003014 label = nfs4_label_alloc(server, GFP_KERNEL);
3015 if (IS_ERR(label))
3016 return PTR_ERR(label);
3017
David Quigley1775fd32013-05-22 12:50:42 -04003018 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003019 if (error < 0) {
3020 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003021 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003022 }
3023
3024 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3025 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3026 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3027
David Quigley14c43f72013-05-22 12:50:43 -04003028err_free_label:
3029 nfs4_label_free(label);
3030
Bryan Schumakerbae36242012-05-10 15:07:31 -04003031 return error;
3032}
3033
Manoj Naik6b97fd32006-06-09 09:34:29 -04003034/*
3035 * Get locations and (maybe) other attributes of a referral.
3036 * Note that we'll actually follow the referral later when
3037 * we detect fsid mismatch in inode revalidation
3038 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003039static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3040 const struct qstr *name, struct nfs_fattr *fattr,
3041 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003042{
3043 int status = -ENOMEM;
3044 struct page *page = NULL;
3045 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003046
3047 page = alloc_page(GFP_KERNEL);
3048 if (page == NULL)
3049 goto out;
3050 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3051 if (locations == NULL)
3052 goto out;
3053
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003054 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003055 if (status != 0)
3056 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003057
3058 /*
3059 * If the fsid didn't change, this is a migration event, not a
3060 * referral. Cause us to drop into the exception handler, which
3061 * will kick off migration recovery.
3062 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003063 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003064 dprintk("%s: server did not return a different fsid for"
3065 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003066 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003067 goto out;
3068 }
Andy Adamson533eb462011-06-13 18:25:56 -04003069 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3070 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003071
Andy Adamson533eb462011-06-13 18:25:56 -04003072 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003073 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003074 memset(fhandle, 0, sizeof(struct nfs_fh));
3075out:
3076 if (page)
3077 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003078 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003079 return status;
3080}
3081
David Quigley1775fd32013-05-22 12:50:42 -04003082static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3083 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
3085 struct nfs4_getattr_arg args = {
3086 .fh = fhandle,
3087 .bitmask = server->attr_bitmask,
3088 };
3089 struct nfs4_getattr_res res = {
3090 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003091 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 .server = server,
3093 };
3094 struct rpc_message msg = {
3095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3096 .rpc_argp = &args,
3097 .rpc_resp = &res,
3098 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003099
3100 args.bitmask = nfs4_bitmask(server, label);
3101
Trond Myklebust0e574af2005-10-27 22:12:38 -04003102 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003103 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
David Quigley1775fd32013-05-22 12:50:42 -04003106static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3107 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108{
3109 struct nfs4_exception exception = { };
3110 int err;
3111 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003112 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3113 trace_nfs4_getattr(server, fhandle, fattr, err);
3114 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 &exception);
3116 } while (exception.retry);
3117 return err;
3118}
3119
3120/*
3121 * The file is not closed if it is opened due to the a request to change
3122 * the size of the file. The open call will not be needed once the
3123 * VFS layer lookup-intents are implemented.
3124 *
3125 * Close is called when the inode is destroyed.
3126 * If we haven't opened the file for O_WRONLY, we
3127 * need to in the size_change case to obtain a stateid.
3128 *
3129 * Got race?
3130 * Because OPEN is always done by name in nfsv4, it is
3131 * possible that we opened a different file by the same
3132 * name. We can recognize this race condition, but we
3133 * can't do anything about it besides returning an error.
3134 *
3135 * This will be fixed with VFS changes (lookup-intent).
3136 */
3137static int
3138nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3139 struct iattr *sattr)
3140{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003141 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003142 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003143 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003144 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 int status;
3146
Benny Halevy8a1636c2010-07-14 15:43:57 -04003147 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003148 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003149
Trond Myklebust0e574af2005-10-27 22:12:38 -04003150 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Andy Adamson26699402012-05-30 16:12:24 -04003152 /* Deal with open(O_TRUNC) */
3153 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003154 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003155
3156 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003157 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003158 return 0;
3159
Trond Myklebustd5308382005-11-04 15:33:38 -05003160 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003161 if (sattr->ia_valid & ATTR_FILE) {
3162 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003163
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003164 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003165 if (ctx) {
3166 cred = ctx->cred;
3167 state = ctx->state;
3168 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003169 }
3170
David Quigley14c43f72013-05-22 12:50:43 -04003171 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3172 if (IS_ERR(label))
3173 return PTR_ERR(label);
3174
3175 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003176 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003177 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003178 nfs_setsecurity(inode, fattr, label);
3179 }
David Quigley14c43f72013-05-22 12:50:43 -04003180 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return status;
3182}
3183
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003184static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3185 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003186 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003187{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003188 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003189 int status;
3190 struct nfs4_lookup_arg args = {
3191 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003192 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003193 .name = name,
3194 };
3195 struct nfs4_lookup_res res = {
3196 .server = server,
3197 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003198 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003199 .fh = fhandle,
3200 };
3201 struct rpc_message msg = {
3202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3203 .rpc_argp = &args,
3204 .rpc_resp = &res,
3205 };
3206
David Quigleyaa9c2662013-05-22 12:50:44 -04003207 args.bitmask = nfs4_bitmask(server, label);
3208
David Howells2b3de442006-08-22 20:06:09 -04003209 nfs_fattr_init(fattr);
3210
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003212 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 dprintk("NFS reply lookup: %d\n", status);
3214 return status;
3215}
3216
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003217static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003218{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003219 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003220 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003221 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3222 fattr->nlink = 2;
3223}
3224
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003225static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3226 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003227 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003228{
3229 struct nfs4_exception exception = { };
3230 struct rpc_clnt *client = *clnt;
3231 int err;
3232 do {
David Quigley1775fd32013-05-22 12:50:42 -04003233 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003234 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003235 switch (err) {
3236 case -NFS4ERR_BADNAME:
3237 err = -ENOENT;
3238 goto out;
3239 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003240 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003241 goto out;
3242 case -NFS4ERR_WRONGSEC:
3243 err = -EPERM;
3244 if (client != *clnt)
3245 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003246 client = nfs4_create_sec_client(client, dir, name);
3247 if (IS_ERR(client))
3248 return PTR_ERR(client);
3249
3250 exception.retry = 1;
3251 break;
3252 default:
3253 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3254 }
3255 } while (exception.retry);
3256
3257out:
3258 if (err == 0)
3259 *clnt = client;
3260 else if (client != *clnt)
3261 rpc_shutdown_client(client);
3262
3263 return err;
3264}
3265
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003266static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003267 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3268 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003270 int status;
3271 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003272
David Quigley1775fd32013-05-22 12:50:42 -04003273 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003274 if (client != NFS_CLIENT(dir)) {
3275 rpc_shutdown_client(client);
3276 nfs_fixup_secinfo_attributes(fattr);
3277 }
3278 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279}
3280
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003281struct rpc_clnt *
3282nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3283 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3284{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003285 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003286 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003287
David Quigley1775fd32013-05-22 12:50:42 -04003288 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003289 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003290 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003291 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003292}
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3295{
Trond Myklebust76b32992007-08-10 17:45:11 -04003296 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 struct nfs4_accessargs args = {
3298 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003299 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003301 struct nfs4_accessres res = {
3302 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003303 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3306 .rpc_argp = &args,
3307 .rpc_resp = &res,
3308 .rpc_cred = entry->cred,
3309 };
3310 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003311 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313 /*
3314 * Determine which access bits we want to ask for...
3315 */
3316 if (mode & MAY_READ)
3317 args.access |= NFS4_ACCESS_READ;
3318 if (S_ISDIR(inode->i_mode)) {
3319 if (mode & MAY_WRITE)
3320 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3321 if (mode & MAY_EXEC)
3322 args.access |= NFS4_ACCESS_LOOKUP;
3323 } else {
3324 if (mode & MAY_WRITE)
3325 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3326 if (mode & MAY_EXEC)
3327 args.access |= NFS4_ACCESS_EXECUTE;
3328 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003329
3330 res.fattr = nfs_alloc_fattr();
3331 if (res.fattr == NULL)
3332 return -ENOMEM;
3333
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003336 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003337 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003339 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return status;
3341}
3342
3343static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3344{
3345 struct nfs4_exception exception = { };
3346 int err;
3347 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003348 err = _nfs4_proc_access(inode, entry);
3349 trace_nfs4_access(inode, err);
3350 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 &exception);
3352 } while (exception.retry);
3353 return err;
3354}
3355
3356/*
3357 * TODO: For the time being, we don't try to get any attributes
3358 * along with any of the zero-copy operations READ, READDIR,
3359 * READLINK, WRITE.
3360 *
3361 * In the case of the first three, we want to put the GETATTR
3362 * after the read-type operation -- this is because it is hard
3363 * to predict the length of a GETATTR response in v4, and thus
3364 * align the READ data correctly. This means that the GETATTR
3365 * may end up partially falling into the page cache, and we should
3366 * shift it into the 'tail' of the xdr_buf before processing.
3367 * To do this efficiently, we need to know the total length
3368 * of data received, which doesn't seem to be available outside
3369 * of the RPC layer.
3370 *
3371 * In the case of WRITE, we also want to put the GETATTR after
3372 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003373 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 *
3375 * Both of these changes to the XDR layer would in fact be quite
3376 * minor, but I decided to leave them for a subsequent patch.
3377 */
3378static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3379 unsigned int pgbase, unsigned int pglen)
3380{
3381 struct nfs4_readlink args = {
3382 .fh = NFS_FH(inode),
3383 .pgbase = pgbase,
3384 .pglen = pglen,
3385 .pages = &page,
3386 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003387 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 struct rpc_message msg = {
3389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3390 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003391 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 };
3393
Bryan Schumaker7c513052011-03-24 17:12:24 +00003394 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 -07003395}
3396
3397static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3398 unsigned int pgbase, unsigned int pglen)
3399{
3400 struct nfs4_exception exception = { };
3401 int err;
3402 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003403 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3404 trace_nfs4_readlink(inode, err);
3405 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 &exception);
3407 } while (exception.retry);
3408 return err;
3409}
3410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003412 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414static int
3415nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003416 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417{
David Quigleyaa9c2662013-05-22 12:50:44 -04003418 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003419 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003421 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 int status = 0;
3423
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003424 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3425 if (IS_ERR(ctx))
3426 return PTR_ERR(ctx);
3427
David Quigleyaa9c2662013-05-22 12:50:44 -04003428 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3429
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003430 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003431 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (IS_ERR(state)) {
3433 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003437 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003438 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return status;
3440}
3441
3442static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3443{
Trond Myklebust16e42952005-10-27 22:12:44 -04003444 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003445 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003447 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003449 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003450 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003451 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3454 .rpc_argp = &args,
3455 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003457 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003458
Bryan Schumaker7c513052011-03-24 17:12:24 +00003459 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003460 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003461 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 return status;
3463}
3464
3465static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3466{
3467 struct nfs4_exception exception = { };
3468 int err;
3469 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003470 err = _nfs4_proc_remove(dir, name);
3471 trace_nfs4_remove(dir, name, err);
3472 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 &exception);
3474 } while (exception.retry);
3475 return err;
3476}
3477
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003478static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003480 struct nfs_server *server = NFS_SERVER(dir);
3481 struct nfs_removeargs *args = msg->rpc_argp;
3482 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003484 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003486 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003487
3488 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003491static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3492{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003493 nfs4_setup_sequence(NFS_SERVER(data->dir),
3494 &data->args.seq_args,
3495 &data->res.seq_res,
3496 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497}
3498
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003499static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003501 struct nfs_unlinkdata *data = task->tk_calldata;
3502 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003503
Trond Myklebust14516c32010-07-31 14:29:06 -04003504 if (!nfs4_sequence_done(task, &res->seq_res))
3505 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003506 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003507 return 0;
3508 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003509 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Jeff Laytond3d41522010-09-17 17:31:57 -04003512static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3513{
3514 struct nfs_server *server = NFS_SERVER(dir);
3515 struct nfs_renameargs *arg = msg->rpc_argp;
3516 struct nfs_renameres *res = msg->rpc_resp;
3517
3518 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003519 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003520 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003521}
3522
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003523static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3524{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003525 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3526 &data->args.seq_args,
3527 &data->res.seq_res,
3528 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003529}
3530
3531static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3532 struct inode *new_dir)
3533{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003534 struct nfs_renamedata *data = task->tk_calldata;
3535 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003536
3537 if (!nfs4_sequence_done(task, &res->seq_res))
3538 return 0;
3539 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3540 return 0;
3541
3542 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003543 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003544 return 1;
3545}
3546
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3548 struct inode *new_dir, struct qstr *new_name)
3549{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003550 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003551 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 .old_dir = NFS_FH(old_dir),
3553 .new_dir = NFS_FH(new_dir),
3554 .old_name = old_name,
3555 .new_name = new_name,
3556 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003557 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003558 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003559 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 struct rpc_message msg = {
3561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3562 .rpc_argp = &arg,
3563 .rpc_resp = &res,
3564 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003565 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003566
Bryan Schumaker7c513052011-03-24 17:12:24 +00003567 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if (!status) {
3569 update_changeattr(old_dir, &res.old_cinfo);
3570 update_changeattr(new_dir, &res.new_cinfo);
3571 }
3572 return status;
3573}
3574
3575static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3576 struct inode *new_dir, struct qstr *new_name)
3577{
3578 struct nfs4_exception exception = { };
3579 int err;
3580 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003581 err = _nfs4_proc_rename(old_dir, old_name,
3582 new_dir, new_name);
3583 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3584 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 &exception);
3586 } while (exception.retry);
3587 return err;
3588}
3589
3590static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3591{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003592 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 struct nfs4_link_arg arg = {
3594 .fh = NFS_FH(inode),
3595 .dir_fh = NFS_FH(dir),
3596 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003597 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003599 struct nfs4_link_res res = {
3600 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003601 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003602 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 struct rpc_message msg = {
3604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3605 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003606 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003608 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609
Trond Myklebust136f2622010-04-16 16:22:49 -04003610 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003611 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003612 goto out;
3613
David Quigley14c43f72013-05-22 12:50:43 -04003614 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3615 if (IS_ERR(res.label)) {
3616 status = PTR_ERR(res.label);
3617 goto out;
3618 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003619 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003620
Bryan Schumaker7c513052011-03-24 17:12:24 +00003621 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003622 if (!status) {
3623 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003624 status = nfs_post_op_update_inode(inode, res.fattr);
3625 if (!status)
3626 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003627 }
David Quigley14c43f72013-05-22 12:50:43 -04003628
3629
3630 nfs4_label_free(res.label);
3631
Trond Myklebust136f2622010-04-16 16:22:49 -04003632out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003633 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 return status;
3635}
3636
3637static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3638{
3639 struct nfs4_exception exception = { };
3640 int err;
3641 do {
3642 err = nfs4_handle_exception(NFS_SERVER(inode),
3643 _nfs4_proc_link(inode, dir, name),
3644 &exception);
3645 } while (exception.retry);
3646 return err;
3647}
3648
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003649struct nfs4_createdata {
3650 struct rpc_message msg;
3651 struct nfs4_create_arg arg;
3652 struct nfs4_create_res res;
3653 struct nfs_fh fh;
3654 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003655 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656};
3657
3658static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3659 struct qstr *name, struct iattr *sattr, u32 ftype)
3660{
3661 struct nfs4_createdata *data;
3662
3663 data = kzalloc(sizeof(*data), GFP_KERNEL);
3664 if (data != NULL) {
3665 struct nfs_server *server = NFS_SERVER(dir);
3666
David Quigley14c43f72013-05-22 12:50:43 -04003667 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3668 if (IS_ERR(data->label))
3669 goto out_free;
3670
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003671 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3672 data->msg.rpc_argp = &data->arg;
3673 data->msg.rpc_resp = &data->res;
3674 data->arg.dir_fh = NFS_FH(dir);
3675 data->arg.server = server;
3676 data->arg.name = name;
3677 data->arg.attrs = sattr;
3678 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003679 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003680 data->res.server = server;
3681 data->res.fh = &data->fh;
3682 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003683 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003684 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003685 }
3686 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003687out_free:
3688 kfree(data);
3689 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003690}
3691
3692static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3693{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003694 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003695 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003696 if (status == 0) {
3697 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003698 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003699 }
3700 return status;
3701}
3702
3703static void nfs4_free_createdata(struct nfs4_createdata *data)
3704{
David Quigley14c43f72013-05-22 12:50:43 -04003705 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003706 kfree(data);
3707}
3708
Chuck Lever4f390c12006-08-22 20:06:22 -04003709static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003710 struct page *page, unsigned int len, struct iattr *sattr,
3711 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003713 struct nfs4_createdata *data;
3714 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
Chuck Lever94a6d752006-08-22 20:06:23 -04003716 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003717 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003718
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003719 status = -ENOMEM;
3720 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3721 if (data == NULL)
3722 goto out;
3723
3724 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3725 data->arg.u.symlink.pages = &page;
3726 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003727 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003729 status = nfs4_do_create(dir, dentry, data);
3730
3731 nfs4_free_createdata(data);
3732out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return status;
3734}
3735
Chuck Lever4f390c12006-08-22 20:06:22 -04003736static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003737 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
3739 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003740 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003742
3743 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003746 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3747 trace_nfs4_symlink(dir, &dentry->d_name, err);
3748 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 &exception);
3750 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003751
3752 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 return err;
3754}
3755
3756static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003757 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003759 struct nfs4_createdata *data;
3760 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003762 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3763 if (data == NULL)
3764 goto out;
3765
David Quigley1775fd32013-05-22 12:50:42 -04003766 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003767 status = nfs4_do_create(dir, dentry, data);
3768
3769 nfs4_free_createdata(data);
3770out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return status;
3772}
3773
3774static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3775 struct iattr *sattr)
3776{
3777 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003778 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003780
David Quigleyaa9c2662013-05-22 12:50:44 -04003781 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3782
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003783 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003785 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3786 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3787 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 &exception);
3789 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003790 nfs4_label_release_security(label);
3791
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 return err;
3793}
3794
3795static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003796 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
3798 struct inode *dir = dentry->d_inode;
3799 struct nfs4_readdir_arg args = {
3800 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003801 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 .pgbase = 0,
3803 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003804 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003805 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
3807 struct nfs4_readdir_res res;
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3810 .rpc_argp = &args,
3811 .rpc_resp = &res,
3812 .rpc_cred = cred,
3813 };
3814 int status;
3815
Al Viro6de14722013-09-16 10:53:17 -04003816 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3817 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003818 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003819 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003821 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 -05003822 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003823 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003824 status += args.pgbase;
3825 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003826
3827 nfs_invalidate_atime(dir);
3828
Harvey Harrison3110ff82008-05-02 13:42:44 -07003829 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 return status;
3831}
3832
3833static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003834 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
3836 struct nfs4_exception exception = { };
3837 int err;
3838 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003839 err = _nfs4_proc_readdir(dentry, cred, cookie,
3840 pages, count, plus);
3841 trace_nfs4_readdir(dentry->d_inode, err);
3842 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 &exception);
3844 } while (exception.retry);
3845 return err;
3846}
3847
3848static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003849 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003851 struct nfs4_createdata *data;
3852 int mode = sattr->ia_mode;
3853 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3856 if (data == NULL)
3857 goto out;
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003860 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003862 data->arg.ftype = NF4BLK;
3863 data->arg.u.device.specdata1 = MAJOR(rdev);
3864 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 }
3866 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003867 data->arg.ftype = NF4CHR;
3868 data->arg.u.device.specdata1 = MAJOR(rdev);
3869 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003870 } else if (!S_ISSOCK(mode)) {
3871 status = -EINVAL;
3872 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 }
David Quigley1775fd32013-05-22 12:50:42 -04003874
David Quigleyaa9c2662013-05-22 12:50:44 -04003875 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003876 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003877out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003878 nfs4_free_createdata(data);
3879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 return status;
3881}
3882
3883static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3884 struct iattr *sattr, dev_t rdev)
3885{
3886 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003889
David Quigleyaa9c2662013-05-22 12:50:44 -04003890 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3891
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003892 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003894 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3895 trace_nfs4_mknod(dir, &dentry->d_name, err);
3896 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 &exception);
3898 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003899
3900 nfs4_label_release_security(label);
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return err;
3903}
3904
3905static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3906 struct nfs_fsstat *fsstat)
3907{
3908 struct nfs4_statfs_arg args = {
3909 .fh = fhandle,
3910 .bitmask = server->attr_bitmask,
3911 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003912 struct nfs4_statfs_res res = {
3913 .fsstat = fsstat,
3914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 struct rpc_message msg = {
3916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3917 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003918 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 };
3920
Trond Myklebust0e574af2005-10-27 22:12:38 -04003921 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003922 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
3925static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3926{
3927 struct nfs4_exception exception = { };
3928 int err;
3929 do {
3930 err = nfs4_handle_exception(server,
3931 _nfs4_proc_statfs(server, fhandle, fsstat),
3932 &exception);
3933 } while (exception.retry);
3934 return err;
3935}
3936
3937static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3938 struct nfs_fsinfo *fsinfo)
3939{
3940 struct nfs4_fsinfo_arg args = {
3941 .fh = fhandle,
3942 .bitmask = server->attr_bitmask,
3943 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003944 struct nfs4_fsinfo_res res = {
3945 .fsinfo = fsinfo,
3946 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 struct rpc_message msg = {
3948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3949 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003950 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 };
3952
Bryan Schumaker7c513052011-03-24 17:12:24 +00003953 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
3956static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3957{
3958 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003959 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 int err;
3961
3962 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003963 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003964 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003965 if (err == 0) {
3966 struct nfs_client *clp = server->nfs_client;
3967
3968 spin_lock(&clp->cl_lock);
3969 clp->cl_lease_time = fsinfo->lease_time * HZ;
3970 clp->cl_last_renewal = now;
3971 spin_unlock(&clp->cl_lock);
3972 break;
3973 }
3974 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 } while (exception.retry);
3976 return err;
3977}
3978
3979static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3980{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003981 int error;
3982
Trond Myklebust0e574af2005-10-27 22:12:38 -04003983 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003984 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003985 if (error == 0) {
3986 /* block layout checks this! */
3987 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003988 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003989 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003990
3991 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
3994static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3995 struct nfs_pathconf *pathconf)
3996{
3997 struct nfs4_pathconf_arg args = {
3998 .fh = fhandle,
3999 .bitmask = server->attr_bitmask,
4000 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004001 struct nfs4_pathconf_res res = {
4002 .pathconf = pathconf,
4003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 struct rpc_message msg = {
4005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4006 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004007 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 };
4009
4010 /* None of the pathconf attributes are mandatory to implement */
4011 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4012 memset(pathconf, 0, sizeof(*pathconf));
4013 return 0;
4014 }
4015
Trond Myklebust0e574af2005-10-27 22:12:38 -04004016 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004017 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
4020static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4021 struct nfs_pathconf *pathconf)
4022{
4023 struct nfs4_exception exception = { };
4024 int err;
4025
4026 do {
4027 err = nfs4_handle_exception(server,
4028 _nfs4_proc_pathconf(server, fhandle, pathconf),
4029 &exception);
4030 } while (exception.retry);
4031 return err;
4032}
4033
Trond Myklebust5521abf2013-03-16 20:54:34 -04004034int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004035 const struct nfs_open_context *ctx,
4036 const struct nfs_lock_context *l_ctx,
4037 fmode_t fmode)
4038{
4039 const struct nfs_lockowner *lockowner = NULL;
4040
4041 if (l_ctx != NULL)
4042 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004043 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004044}
4045EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4046
Trond Myklebust5521abf2013-03-16 20:54:34 -04004047static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4048 const struct nfs_open_context *ctx,
4049 const struct nfs_lock_context *l_ctx,
4050 fmode_t fmode)
4051{
4052 nfs4_stateid current_stateid;
4053
4054 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
4055 return false;
4056 return nfs4_stateid_match(stateid, &current_stateid);
4057}
4058
4059static bool nfs4_error_stateid_expired(int err)
4060{
4061 switch (err) {
4062 case -NFS4ERR_DELEG_REVOKED:
4063 case -NFS4ERR_ADMIN_REVOKED:
4064 case -NFS4ERR_BAD_STATEID:
4065 case -NFS4ERR_STALE_STATEID:
4066 case -NFS4ERR_OLD_STATEID:
4067 case -NFS4ERR_OPENMODE:
4068 case -NFS4ERR_EXPIRED:
4069 return true;
4070 }
4071 return false;
4072}
4073
Benny Halevyd20581a2011-05-22 19:52:03 +03004074void __nfs4_read_done_cb(struct nfs_read_data *data)
4075{
Fred Isamancd841602012-04-20 14:47:44 -04004076 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004077}
4078
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004079static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080{
Fred Isamancd841602012-04-20 14:47:44 -04004081 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004083 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004084 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004085 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004086 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004088
Benny Halevyd20581a2011-05-22 19:52:03 +03004089 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004091 renew_lease(server, data->timestamp);
4092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
Trond Myklebust5521abf2013-03-16 20:54:34 -04004095static bool nfs4_read_stateid_changed(struct rpc_task *task,
4096 struct nfs_readargs *args)
4097{
4098
4099 if (!nfs4_error_stateid_expired(task->tk_status) ||
4100 nfs4_stateid_is_current(&args->stateid,
4101 args->context,
4102 args->lock_context,
4103 FMODE_READ))
4104 return false;
4105 rpc_restart_call_prepare(task);
4106 return true;
4107}
4108
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004109static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4110{
4111
4112 dprintk("--> %s\n", __func__);
4113
4114 if (!nfs4_sequence_done(task, &data->res.seq_res))
4115 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004116 if (nfs4_read_stateid_changed(task, &data->args))
4117 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004118 return data->read_done_cb ? data->read_done_cb(task, data) :
4119 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004120}
4121
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004122static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004125 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004126 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004127 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128}
4129
NeilBrownef1820f2013-09-04 17:04:49 +10004130static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004131{
Trond Myklebust9b206142013-03-17 15:52:00 -04004132 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004133 &data->args.seq_args,
4134 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004135 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004136 return 0;
4137 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4138 data->args.lock_context, FMODE_READ) == -EIO)
4139 return -EIO;
4140 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4141 return -EIO;
4142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143}
4144
Fred Isamanb029bc92011-03-03 15:13:42 +00004145static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Fred Isamancd841602012-04-20 14:47:44 -04004147 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004149 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004150 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004151 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004152 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004154 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004156 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004157 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159}
4160
Trond Myklebust5521abf2013-03-16 20:54:34 -04004161static bool nfs4_write_stateid_changed(struct rpc_task *task,
4162 struct nfs_writeargs *args)
4163{
4164
4165 if (!nfs4_error_stateid_expired(task->tk_status) ||
4166 nfs4_stateid_is_current(&args->stateid,
4167 args->context,
4168 args->lock_context,
4169 FMODE_WRITE))
4170 return false;
4171 rpc_restart_call_prepare(task);
4172 return true;
4173}
4174
Fred Isamanb029bc92011-03-03 15:13:42 +00004175static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4176{
4177 if (!nfs4_sequence_done(task, &data->res.seq_res))
4178 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004179 if (nfs4_write_stateid_changed(task, &data->args))
4180 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004181 return data->write_done_cb ? data->write_done_cb(task, data) :
4182 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004183}
4184
Trond Myklebust5a37f852012-04-28 14:55:16 -04004185static
4186bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004187{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004188 const struct nfs_pgio_header *hdr = data->header;
4189
4190 /* Don't request attributes for pNFS or O_DIRECT writes */
4191 if (data->ds_clp != NULL || hdr->dreq != NULL)
4192 return false;
4193 /* Otherwise, request attributes if and only if we don't hold
4194 * a delegation
4195 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004196 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004197}
Fred Isamana69aef12011-03-03 15:13:47 +00004198
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004199static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
Fred Isamancd841602012-04-20 14:47:44 -04004201 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004202
Trond Myklebust5a37f852012-04-28 14:55:16 -04004203 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004204 data->args.bitmask = NULL;
4205 data->res.fattr = NULL;
4206 } else
4207 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004208
Fred Isamanb029bc92011-03-03 15:13:42 +00004209 if (!data->write_done_cb)
4210 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004211 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 data->timestamp = jiffies;
4213
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004214 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004215 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
NeilBrownef1820f2013-09-04 17:04:49 +10004218static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004219{
Trond Myklebust9b206142013-03-17 15:52:00 -04004220 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004221 &data->args.seq_args,
4222 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004223 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004224 return 0;
4225 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4226 data->args.lock_context, FMODE_WRITE) == -EIO)
4227 return -EIO;
4228 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4229 return -EIO;
4230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Fred Isaman0b7c0152012-04-20 14:47:39 -04004233static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4234{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004235 nfs4_setup_sequence(NFS_SERVER(data->inode),
4236 &data->args.seq_args,
4237 &data->res.seq_res,
4238 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
Fred Isaman0b7c0152012-04-20 14:47:39 -04004241static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004244
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004245 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004246 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004247 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004248 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251}
4252
Fred Isaman0b7c0152012-04-20 14:47:39 -04004253static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004254{
4255 if (!nfs4_sequence_done(task, &data->res.seq_res))
4256 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004257 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004258}
4259
Fred Isaman0b7c0152012-04-20 14:47:39 -04004260static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004262 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004263
Fred Isaman0b7c0152012-04-20 14:47:39 -04004264 if (data->commit_done_cb == NULL)
4265 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004266 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004267 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004268 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269}
4270
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004271struct nfs4_renewdata {
4272 struct nfs_client *client;
4273 unsigned long timestamp;
4274};
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276/*
4277 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4278 * standalone procedure for queueing an asynchronous RENEW.
4279 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004280static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004281{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004282 struct nfs4_renewdata *data = calldata;
4283 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004284
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004285 if (atomic_read(&clp->cl_count) > 1)
4286 nfs4_schedule_state_renewal(clp);
4287 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004288 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004289}
4290
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004291static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004293 struct nfs4_renewdata *data = calldata;
4294 struct nfs_client *clp = data->client;
4295 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004297 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004298 switch (task->tk_status) {
4299 case 0:
4300 break;
4301 case -NFS4ERR_LEASE_MOVED:
4302 nfs4_schedule_lease_moved_recovery(clp);
4303 break;
4304 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004305 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004306 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4307 return;
4308 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004309 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004310 return;
4311 }
4312 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004314 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004317static const struct rpc_call_ops nfs4_renew_ops = {
4318 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004319 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004320};
4321
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004322static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
4324 struct rpc_message msg = {
4325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4326 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004327 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004329 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004331 if (renew_flags == 0)
4332 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004333 if (!atomic_inc_not_zero(&clp->cl_count))
4334 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004335 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004336 if (data == NULL)
4337 return -ENOMEM;
4338 data->client = clp;
4339 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004340 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004341 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342}
4343
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004344static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
4346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4348 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004349 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 };
4351 unsigned long now = jiffies;
4352 int status;
4353
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004354 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (status < 0)
4356 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004357 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return 0;
4359}
4360
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004361static inline int nfs4_server_supports_acls(struct nfs_server *server)
4362{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004363 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004364}
4365
Trond Myklebust21f498c2012-08-24 10:59:25 -04004366/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4367 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004368 * the stack.
4369 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004370#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004371
Neil Hormane9e3d722011-03-04 19:26:03 -05004372static int buf_to_pages_noslab(const void *buf, size_t buflen,
4373 struct page **pages, unsigned int *pgbase)
4374{
4375 struct page *newpage, **spages;
4376 int rc = 0;
4377 size_t len;
4378 spages = pages;
4379
4380 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004381 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004382 newpage = alloc_page(GFP_KERNEL);
4383
4384 if (newpage == NULL)
4385 goto unwind;
4386 memcpy(page_address(newpage), buf, len);
4387 buf += len;
4388 buflen -= len;
4389 *pages++ = newpage;
4390 rc++;
4391 } while (buflen != 0);
4392
4393 return rc;
4394
4395unwind:
4396 for(; rc > 0; rc--)
4397 __free_page(spages[rc-1]);
4398 return -ENOMEM;
4399}
4400
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004401struct nfs4_cached_acl {
4402 int cached;
4403 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004404 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004405};
4406
4407static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004408{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004409 struct nfs_inode *nfsi = NFS_I(inode);
4410
4411 spin_lock(&inode->i_lock);
4412 kfree(nfsi->nfs4_acl);
4413 nfsi->nfs4_acl = acl;
4414 spin_unlock(&inode->i_lock);
4415}
4416
4417static void nfs4_zap_acl_attr(struct inode *inode)
4418{
4419 nfs4_set_cached_acl(inode, NULL);
4420}
4421
4422static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4423{
4424 struct nfs_inode *nfsi = NFS_I(inode);
4425 struct nfs4_cached_acl *acl;
4426 int ret = -ENOENT;
4427
4428 spin_lock(&inode->i_lock);
4429 acl = nfsi->nfs4_acl;
4430 if (acl == NULL)
4431 goto out;
4432 if (buf == NULL) /* user is just asking for length */
4433 goto out_len;
4434 if (acl->cached == 0)
4435 goto out;
4436 ret = -ERANGE; /* see getxattr(2) man page */
4437 if (acl->len > buflen)
4438 goto out;
4439 memcpy(buf, acl->data, acl->len);
4440out_len:
4441 ret = acl->len;
4442out:
4443 spin_unlock(&inode->i_lock);
4444 return ret;
4445}
4446
Sachin Prabhu5794d212012-04-17 14:36:40 +01004447static 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 +00004448{
4449 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004450 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004451
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004452 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004453 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004454 if (acl == NULL)
4455 goto out;
4456 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004457 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004458 } else {
4459 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4460 if (acl == NULL)
4461 goto out;
4462 acl->cached = 0;
4463 }
4464 acl->len = acl_len;
4465out:
4466 nfs4_set_cached_acl(inode, acl);
4467}
4468
Andy Adamsonbf118a32011-12-07 11:55:27 -05004469/*
4470 * The getxattr API returns the required buffer length when called with a
4471 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4472 * the required buf. On a NULL buf, we send a page of data to the server
4473 * guessing that the ACL request can be serviced by a page. If so, we cache
4474 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4475 * the cache. If not so, we throw away the page, and cache the required
4476 * length. The next getxattr call will then produce another round trip to
4477 * the server, this time with the input buf of the required size.
4478 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004479static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004480{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004481 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004482 struct nfs_getaclargs args = {
4483 .fh = NFS_FH(inode),
4484 .acl_pages = pages,
4485 .acl_len = buflen,
4486 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004487 struct nfs_getaclres res = {
4488 .acl_len = buflen,
4489 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004490 struct rpc_message msg = {
4491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4492 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004493 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004494 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004495 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4496 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004497
Andy Adamsonbf118a32011-12-07 11:55:27 -05004498 /* As long as we're doing a round trip to the server anyway,
4499 * let's be prepared for a page of acl data. */
4500 if (npages == 0)
4501 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004502 if (npages > ARRAY_SIZE(pages))
4503 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004504
Andy Adamsonbf118a32011-12-07 11:55:27 -05004505 for (i = 0; i < npages; i++) {
4506 pages[i] = alloc_page(GFP_KERNEL);
4507 if (!pages[i])
4508 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004509 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004510
4511 /* for decoding across pages */
4512 res.acl_scratch = alloc_page(GFP_KERNEL);
4513 if (!res.acl_scratch)
4514 goto out_free;
4515
Andy Adamsonbf118a32011-12-07 11:55:27 -05004516 args.acl_len = npages * PAGE_SIZE;
4517 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004518
Peng Taode040be2012-01-10 22:42:47 +08004519 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004520 __func__, buf, buflen, npages, args.acl_len);
4521 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4522 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004523 if (ret)
4524 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004525
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004526 /* Handle the case where the passed-in buffer is too short */
4527 if (res.acl_flags & NFS4_ACL_TRUNC) {
4528 /* Did the user only issue a request for the acl length? */
4529 if (buf == NULL)
4530 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004531 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004532 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004533 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004534 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004535 if (buf) {
4536 if (res.acl_len > buflen) {
4537 ret = -ERANGE;
4538 goto out_free;
4539 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004540 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004541 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004542out_ok:
4543 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004544out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004545 for (i = 0; i < npages; i++)
4546 if (pages[i])
4547 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004548 if (res.acl_scratch)
4549 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004550 return ret;
4551}
4552
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004553static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4554{
4555 struct nfs4_exception exception = { };
4556 ssize_t ret;
4557 do {
4558 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004559 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004560 if (ret >= 0)
4561 break;
4562 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4563 } while (exception.retry);
4564 return ret;
4565}
4566
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004567static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4568{
4569 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004570 int ret;
4571
4572 if (!nfs4_server_supports_acls(server))
4573 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004574 ret = nfs_revalidate_inode(server, inode);
4575 if (ret < 0)
4576 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004577 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4578 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004579 ret = nfs4_read_cached_acl(inode, buf, buflen);
4580 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004581 /* -ENOENT is returned if there is no ACL or if there is an ACL
4582 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004583 return ret;
4584 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004585}
4586
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004587static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004588{
4589 struct nfs_server *server = NFS_SERVER(inode);
4590 struct page *pages[NFS4ACL_MAXPAGES];
4591 struct nfs_setaclargs arg = {
4592 .fh = NFS_FH(inode),
4593 .acl_pages = pages,
4594 .acl_len = buflen,
4595 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004596 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004597 struct rpc_message msg = {
4598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4599 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004600 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004601 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004602 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004603 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004604
4605 if (!nfs4_server_supports_acls(server))
4606 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004607 if (npages > ARRAY_SIZE(pages))
4608 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004609 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4610 if (i < 0)
4611 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004612 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004613 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004614
4615 /*
4616 * Free each page after tx, so the only ref left is
4617 * held by the network stack
4618 */
4619 for (; i > 0; i--)
4620 put_page(pages[i-1]);
4621
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004622 /*
4623 * Acl update can result in inode attribute update.
4624 * so mark the attribute cache invalid.
4625 */
4626 spin_lock(&inode->i_lock);
4627 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4628 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004629 nfs_access_zap_cache(inode);
4630 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004631 return ret;
4632}
4633
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004634static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4635{
4636 struct nfs4_exception exception = { };
4637 int err;
4638 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004639 err = __nfs4_proc_set_acl(inode, buf, buflen);
4640 trace_nfs4_set_acl(inode, err);
4641 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004642 &exception);
4643 } while (exception.retry);
4644 return err;
4645}
4646
David Quigleyaa9c2662013-05-22 12:50:44 -04004647#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4648static int _nfs4_get_security_label(struct inode *inode, void *buf,
4649 size_t buflen)
4650{
4651 struct nfs_server *server = NFS_SERVER(inode);
4652 struct nfs_fattr fattr;
4653 struct nfs4_label label = {0, 0, buflen, buf};
4654
4655 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004656 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004657 .fh = NFS_FH(inode),
4658 .bitmask = bitmask,
4659 };
4660 struct nfs4_getattr_res res = {
4661 .fattr = &fattr,
4662 .label = &label,
4663 .server = server,
4664 };
4665 struct rpc_message msg = {
4666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004667 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004668 .rpc_resp = &res,
4669 };
4670 int ret;
4671
4672 nfs_fattr_init(&fattr);
4673
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004674 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004675 if (ret)
4676 return ret;
4677 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4678 return -ENOENT;
4679 if (buflen < label.len)
4680 return -ERANGE;
4681 return 0;
4682}
4683
4684static int nfs4_get_security_label(struct inode *inode, void *buf,
4685 size_t buflen)
4686{
4687 struct nfs4_exception exception = { };
4688 int err;
4689
4690 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4691 return -EOPNOTSUPP;
4692
4693 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004694 err = _nfs4_get_security_label(inode, buf, buflen);
4695 trace_nfs4_get_security_label(inode, err);
4696 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004697 &exception);
4698 } while (exception.retry);
4699 return err;
4700}
4701
4702static int _nfs4_do_set_security_label(struct inode *inode,
4703 struct nfs4_label *ilabel,
4704 struct nfs_fattr *fattr,
4705 struct nfs4_label *olabel)
4706{
4707
4708 struct iattr sattr = {0};
4709 struct nfs_server *server = NFS_SERVER(inode);
4710 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004711 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004712 .fh = NFS_FH(inode),
4713 .iap = &sattr,
4714 .server = server,
4715 .bitmask = bitmask,
4716 .label = ilabel,
4717 };
4718 struct nfs_setattrres res = {
4719 .fattr = fattr,
4720 .label = olabel,
4721 .server = server,
4722 };
4723 struct rpc_message msg = {
4724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004725 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004726 .rpc_resp = &res,
4727 };
4728 int status;
4729
Jeff Layton12207f62013-11-01 10:49:32 -04004730 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004731
Jeff Layton12207f62013-11-01 10:49:32 -04004732 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004733 if (status)
4734 dprintk("%s failed: %d\n", __func__, status);
4735
4736 return status;
4737}
4738
4739static int nfs4_do_set_security_label(struct inode *inode,
4740 struct nfs4_label *ilabel,
4741 struct nfs_fattr *fattr,
4742 struct nfs4_label *olabel)
4743{
4744 struct nfs4_exception exception = { };
4745 int err;
4746
4747 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004748 err = _nfs4_do_set_security_label(inode, ilabel,
4749 fattr, olabel);
4750 trace_nfs4_set_security_label(inode, err);
4751 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004752 &exception);
4753 } while (exception.retry);
4754 return err;
4755}
4756
4757static int
4758nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4759{
4760 struct nfs4_label ilabel, *olabel = NULL;
4761 struct nfs_fattr fattr;
4762 struct rpc_cred *cred;
4763 struct inode *inode = dentry->d_inode;
4764 int status;
4765
4766 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4767 return -EOPNOTSUPP;
4768
4769 nfs_fattr_init(&fattr);
4770
4771 ilabel.pi = 0;
4772 ilabel.lfs = 0;
4773 ilabel.label = (char *)buf;
4774 ilabel.len = buflen;
4775
4776 cred = rpc_lookup_cred();
4777 if (IS_ERR(cred))
4778 return PTR_ERR(cred);
4779
4780 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4781 if (IS_ERR(olabel)) {
4782 status = -PTR_ERR(olabel);
4783 goto out;
4784 }
4785
4786 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4787 if (status == 0)
4788 nfs_setsecurity(inode, &fattr, olabel);
4789
4790 nfs4_label_free(olabel);
4791out:
4792 put_rpccred(cred);
4793 return status;
4794}
4795#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4796
4797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004799nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004801 struct nfs_client *clp = server->nfs_client;
4802
4803 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 return 0;
4805 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004806 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004807 case -NFS4ERR_ADMIN_REVOKED:
4808 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004809 if (state == NULL)
4810 break;
4811 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004812 case -NFS4ERR_OPENMODE:
4813 if (state == NULL)
4814 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004815 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004816 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004817 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004818 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004819 if (state != NULL) {
4820 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004821 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004824 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004825 nfs4_schedule_lease_recovery(clp);
4826 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004827 case -NFS4ERR_MOVED:
4828 if (nfs4_schedule_migration_recovery(server) < 0)
4829 goto recovery_failed;
4830 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004831 case -NFS4ERR_LEASE_MOVED:
4832 nfs4_schedule_lease_moved_recovery(clp);
4833 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004834#if defined(CONFIG_NFS_V4_1)
4835 case -NFS4ERR_BADSESSION:
4836 case -NFS4ERR_BADSLOT:
4837 case -NFS4ERR_BAD_HIGH_SLOT:
4838 case -NFS4ERR_DEADSESSION:
4839 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4840 case -NFS4ERR_SEQ_FALSE_RETRY:
4841 case -NFS4ERR_SEQ_MISORDERED:
4842 dprintk("%s ERROR %d, Reset session\n", __func__,
4843 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004844 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004845 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004846#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004848 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004849 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004851 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004853 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 }
4855 task->tk_status = nfs4_map_errors(task->tk_status);
4856 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004857recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004858 task->tk_status = -EIO;
4859 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004860wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004861 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004862 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4863 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004864 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4865 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004866restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004867 task->tk_status = 0;
4868 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
Chuck Leverf0920752012-05-21 22:45:41 -04004871static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4872 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004873{
4874 __be32 verf[2];
4875
Chuck Lever2c820d92012-05-21 22:45:33 -04004876 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4877 /* An impossible timestamp guarantees this value
4878 * will never match a generated boot time. */
4879 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004880 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004881 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004882 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004883 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4884 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004885 }
Chuck Levercd937102012-03-02 17:14:31 -05004886 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4887}
4888
Chuck Levere984a552012-09-14 17:24:21 -04004889static unsigned int
4890nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4891 char *buf, size_t len)
4892{
4893 unsigned int result;
4894
4895 rcu_read_lock();
4896 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4897 clp->cl_ipaddr,
4898 rpc_peeraddr2str(clp->cl_rpcclient,
4899 RPC_DISPLAY_ADDR),
4900 rpc_peeraddr2str(clp->cl_rpcclient,
4901 RPC_DISPLAY_PROTO));
4902 rcu_read_unlock();
4903 return result;
4904}
4905
4906static unsigned int
4907nfs4_init_uniform_client_string(const struct nfs_client *clp,
4908 char *buf, size_t len)
4909{
Trond Myklebust55b59292013-07-24 16:36:35 -04004910 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004911
4912 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004913 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4914 clp->rpc_ops->version,
4915 clp->cl_minorversion,
4916 nfs4_client_id_uniquifier,
4917 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004918 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4919 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004920 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004921}
4922
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004923/**
4924 * nfs4_proc_setclientid - Negotiate client ID
4925 * @clp: state data structure
4926 * @program: RPC program for NFSv4 callback service
4927 * @port: IP port number for NFS4 callback service
4928 * @cred: RPC credential to use for this call
4929 * @res: where to place the result
4930 *
4931 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4932 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004933int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4934 unsigned short port, struct rpc_cred *cred,
4935 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936{
4937 nfs4_verifier sc_verifier;
4938 struct nfs4_setclientid setclientid = {
4939 .sc_verifier = &sc_verifier,
4940 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004941 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 };
4943 struct rpc_message msg = {
4944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4945 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004946 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004947 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004949 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Chuck Leverde734832012-07-11 16:30:50 -04004951 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004952 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004953 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4954 setclientid.sc_name_len =
4955 nfs4_init_uniform_client_string(clp,
4956 setclientid.sc_name,
4957 sizeof(setclientid.sc_name));
4958 else
4959 setclientid.sc_name_len =
4960 nfs4_init_nonuniform_client_string(clp,
4961 setclientid.sc_name,
4962 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004963 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004964 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004965 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004966 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004967 rpc_peeraddr2str(clp->cl_rpcclient,
4968 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004969 rcu_read_unlock();
4970 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004971 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 clp->cl_ipaddr, port >> 8, port & 255);
4973
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004974 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4975 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4976 setclientid.sc_name_len, setclientid.sc_name);
4977 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004978 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004979 dprintk("NFS reply setclientid: %d\n", status);
4980 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981}
4982
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004983/**
4984 * nfs4_proc_setclientid_confirm - Confirm client ID
4985 * @clp: state data structure
4986 * @res: result of a previous SETCLIENTID
4987 * @cred: RPC credential to use for this call
4988 *
4989 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4990 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004991int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004992 struct nfs4_setclientid_res *arg,
4993 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 struct rpc_message msg = {
4996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004997 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004998 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 int status;
5001
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005002 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5003 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5004 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005005 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005006 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005007 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 return status;
5009}
5010
Trond Myklebustfe650402006-01-03 09:55:18 +01005011struct nfs4_delegreturndata {
5012 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005013 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005014 struct nfs_fh fh;
5015 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005016 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005017 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005018 int rpc_status;
5019};
5020
Trond Myklebustfe650402006-01-03 09:55:18 +01005021static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5022{
5023 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005024
Trond Myklebust14516c32010-07-31 14:29:06 -04005025 if (!nfs4_sequence_done(task, &data->res.seq_res))
5026 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005027
Trond Myklebustca8acf82013-08-13 10:36:56 -04005028 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005029 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005030 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005031 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005032 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005033 case -NFS4ERR_ADMIN_REVOKED:
5034 case -NFS4ERR_DELEG_REVOKED:
5035 case -NFS4ERR_BAD_STATEID:
5036 case -NFS4ERR_OLD_STATEID:
5037 case -NFS4ERR_STALE_STATEID:
5038 case -NFS4ERR_EXPIRED:
5039 task->tk_status = 0;
5040 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005041 default:
5042 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5043 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005044 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005045 return;
5046 }
5047 }
5048 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005049}
5050
5051static void nfs4_delegreturn_release(void *calldata)
5052{
Trond Myklebustfe650402006-01-03 09:55:18 +01005053 kfree(calldata);
5054}
5055
Andy Adamson938e1012009-04-01 09:22:28 -04005056static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5057{
5058 struct nfs4_delegreturndata *d_data;
5059
5060 d_data = (struct nfs4_delegreturndata *)data;
5061
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005062 nfs4_setup_sequence(d_data->res.server,
5063 &d_data->args.seq_args,
5064 &d_data->res.seq_res,
5065 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005066}
Andy Adamson938e1012009-04-01 09:22:28 -04005067
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005068static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005069 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005070 .rpc_call_done = nfs4_delegreturn_done,
5071 .rpc_release = nfs4_delegreturn_release,
5072};
5073
Trond Myklebuste6f81072008-01-24 18:14:34 -05005074static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005075{
5076 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005077 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005078 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005079 struct rpc_message msg = {
5080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5081 .rpc_cred = cred,
5082 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005083 struct rpc_task_setup task_setup_data = {
5084 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005085 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005086 .callback_ops = &nfs4_delegreturn_ops,
5087 .flags = RPC_TASK_ASYNC,
5088 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005089 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005090
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005091 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005092 if (data == NULL)
5093 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005094 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005095 data->args.fhandle = &data->fh;
5096 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005097 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005098 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005099 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005100 data->res.fattr = &data->fattr;
5101 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005102 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005103 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005104 data->rpc_status = 0;
5105
Trond Myklebustc970aa82007-07-14 15:39:59 -04005106 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005107 msg.rpc_argp = &data->args;
5108 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005109 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005110 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005111 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005112 if (!issync)
5113 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005114 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005115 if (status != 0)
5116 goto out;
5117 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005118 if (status == 0)
5119 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5120 else
5121 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005122out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005123 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005124 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125}
5126
Trond Myklebuste6f81072008-01-24 18:14:34 -05005127int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128{
5129 struct nfs_server *server = NFS_SERVER(inode);
5130 struct nfs4_exception exception = { };
5131 int err;
5132 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005133 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005134 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 switch (err) {
5136 case -NFS4ERR_STALE_STATEID:
5137 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 case 0:
5139 return 0;
5140 }
5141 err = nfs4_handle_exception(server, err, &exception);
5142 } while (exception.retry);
5143 return err;
5144}
5145
5146#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5147#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5148
5149/*
5150 * sleep, with exponential backoff, and retry the LOCK operation.
5151 */
5152static unsigned long
5153nfs4_set_lock_task_retry(unsigned long timeout)
5154{
Colin Cross416ad3c2013-05-06 23:50:06 +00005155 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 timeout <<= 1;
5157 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5158 return NFS4_LOCK_MAXTIMEOUT;
5159 return timeout;
5160}
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5163{
5164 struct inode *inode = state->inode;
5165 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005166 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005167 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005169 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005171 struct nfs_lockt_res res = {
5172 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 };
5174 struct rpc_message msg = {
5175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5176 .rpc_argp = &arg,
5177 .rpc_resp = &res,
5178 .rpc_cred = state->owner->so_cred,
5179 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 struct nfs4_lock_state *lsp;
5181 int status;
5182
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005183 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005184 status = nfs4_set_lock_state(state, request);
5185 if (status != 0)
5186 goto out;
5187 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005188 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005189 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005190 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005191 switch (status) {
5192 case 0:
5193 request->fl_type = F_UNLCK;
5194 break;
5195 case -NFS4ERR_DENIED:
5196 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005198 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005199 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005200out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 return status;
5202}
5203
5204static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5205{
5206 struct nfs4_exception exception = { };
5207 int err;
5208
5209 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005210 err = _nfs4_proc_getlk(state, cmd, request);
5211 trace_nfs4_get_lock(request, state, cmd, err);
5212 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 &exception);
5214 } while (exception.retry);
5215 return err;
5216}
5217
5218static int do_vfs_lock(struct file *file, struct file_lock *fl)
5219{
5220 int res = 0;
5221 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5222 case FL_POSIX:
5223 res = posix_lock_file_wait(file, fl);
5224 break;
5225 case FL_FLOCK:
5226 res = flock_lock_file_wait(file, fl);
5227 break;
5228 default:
5229 BUG();
5230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 return res;
5232}
5233
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005234struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005235 struct nfs_locku_args arg;
5236 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005237 struct nfs4_lock_state *lsp;
5238 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005239 struct file_lock fl;
5240 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005241 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005242};
5243
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005244static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5245 struct nfs_open_context *ctx,
5246 struct nfs4_lock_state *lsp,
5247 struct nfs_seqid *seqid)
5248{
5249 struct nfs4_unlockdata *p;
5250 struct inode *inode = lsp->ls_state->inode;
5251
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005252 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005253 if (p == NULL)
5254 return NULL;
5255 p->arg.fh = NFS_FH(inode);
5256 p->arg.fl = &p->fl;
5257 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005258 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005259 p->arg.stateid = &lsp->ls_stateid;
5260 p->lsp = lsp;
5261 atomic_inc(&lsp->ls_count);
5262 /* Ensure we don't close file until we're done freeing locks! */
5263 p->ctx = get_nfs_open_context(ctx);
5264 memcpy(&p->fl, fl, sizeof(p->fl));
5265 p->server = NFS_SERVER(inode);
5266 return p;
5267}
5268
Trond Myklebust06f814a2006-01-03 09:55:07 +01005269static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005270{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005271 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005272 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005273 nfs4_put_lock_state(calldata->lsp);
5274 put_nfs_open_context(calldata->ctx);
5275 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005276}
5277
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005278static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005279{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005280 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005281
Trond Myklebust14516c32010-07-31 14:29:06 -04005282 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5283 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005284 switch (task->tk_status) {
5285 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005286 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5287 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005288 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005289 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005290 case -NFS4ERR_BAD_STATEID:
5291 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005292 case -NFS4ERR_STALE_STATEID:
5293 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005294 break;
5295 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005296 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005297 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005298 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005299 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005300}
5301
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005302static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005303{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005304 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005306 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005307 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005308 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005309 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005310 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005311 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005312 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005313 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005314 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005315 &calldata->res.seq_res,
5316 task) != 0)
5317 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005318 return;
5319out_no_action:
5320 task->tk_action = NULL;
5321out_wait:
5322 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323}
5324
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005325static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005326 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005327 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005328 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005329};
5330
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005331static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5332 struct nfs_open_context *ctx,
5333 struct nfs4_lock_state *lsp,
5334 struct nfs_seqid *seqid)
5335{
5336 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005337 struct rpc_message msg = {
5338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5339 .rpc_cred = ctx->cred,
5340 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005341 struct rpc_task_setup task_setup_data = {
5342 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005343 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005344 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005345 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005346 .flags = RPC_TASK_ASYNC,
5347 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005348
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005349 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5350 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5351
Frank Filz137d6ac2007-07-09 15:32:29 -07005352 /* Ensure this is an unlock - when canceling a lock, the
5353 * canceled lock is passed in, and it won't be an unlock.
5354 */
5355 fl->fl_type = F_UNLCK;
5356
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005357 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5358 if (data == NULL) {
5359 nfs_free_seqid(seqid);
5360 return ERR_PTR(-ENOMEM);
5361 }
5362
Chuck Levera9c92d62013-08-09 12:48:18 -04005363 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005364 msg.rpc_argp = &data->arg;
5365 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005366 task_setup_data.callback_data = data;
5367 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005368}
5369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5371{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005372 struct inode *inode = state->inode;
5373 struct nfs4_state_owner *sp = state->owner;
5374 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005375 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005376 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005377 struct rpc_task *task;
5378 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005379 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Trond Myklebust9b073572006-06-29 16:38:34 -04005381 status = nfs4_set_lock_state(state, request);
5382 /* Unlock _before_ we do the RPC call */
5383 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005384 /* Exclude nfs_delegation_claim_locks() */
5385 mutex_lock(&sp->so_delegreturn_mutex);
5386 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005387 down_read(&nfsi->rwsem);
5388 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5389 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005390 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005391 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005392 }
5393 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005394 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005395 if (status != 0)
5396 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005397 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005398 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005399 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5400 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005401 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005402 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005403 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005404 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005405 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005406 status = PTR_ERR(task);
5407 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005408 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005409 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005410 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005411out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005412 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005413 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415}
5416
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005417struct nfs4_lockdata {
5418 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005419 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005420 struct nfs4_lock_state *lsp;
5421 struct nfs_open_context *ctx;
5422 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005423 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005424 int rpc_status;
5425 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005426 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427};
5428
5429static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005430 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5431 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005432{
5433 struct nfs4_lockdata *p;
5434 struct inode *inode = lsp->ls_state->inode;
5435 struct nfs_server *server = NFS_SERVER(inode);
5436
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005437 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005438 if (p == NULL)
5439 return NULL;
5440
5441 p->arg.fh = NFS_FH(inode);
5442 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005443 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005444 if (p->arg.open_seqid == NULL)
5445 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005446 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005447 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005448 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005449 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005450 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005451 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005452 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005453 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005454 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005455 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005456 atomic_inc(&lsp->ls_count);
5457 p->ctx = get_nfs_open_context(ctx);
5458 memcpy(&p->fl, fl, sizeof(p->fl));
5459 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005460out_free_seqid:
5461 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005462out_free:
5463 kfree(p);
5464 return NULL;
5465}
5466
5467static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5468{
5469 struct nfs4_lockdata *data = calldata;
5470 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471
Harvey Harrison3110ff82008-05-02 13:42:44 -07005472 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005473 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005474 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005475 /* Do we need to do an open_to_lock_owner? */
5476 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005477 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005478 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005479 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005480 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005481 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005482 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005483 } else
5484 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005485 if (!nfs4_valid_open_stateid(state)) {
5486 data->rpc_status = -EBADF;
5487 task->tk_action = NULL;
5488 goto out_release_open_seqid;
5489 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005490 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005491 if (nfs4_setup_sequence(data->server,
5492 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005493 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005494 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005495 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005496out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005497 nfs_release_seqid(data->arg.open_seqid);
5498out_release_lock_seqid:
5499 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005500out_wait:
5501 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005502 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005503}
5504
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005505static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5506{
5507 struct nfs4_lockdata *data = calldata;
5508
Harvey Harrison3110ff82008-05-02 13:42:44 -07005509 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005510
Trond Myklebust14516c32010-07-31 14:29:06 -04005511 if (!nfs4_sequence_done(task, &data->res.seq_res))
5512 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005513
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005514 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005515 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 if (data->rpc_status == 0)
5517 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5518 else
5519 goto out;
5520 }
5521 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005522 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005523 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005524 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005525 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005526out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005527 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005528}
5529
5530static void nfs4_lock_release(void *calldata)
5531{
5532 struct nfs4_lockdata *data = calldata;
5533
Harvey Harrison3110ff82008-05-02 13:42:44 -07005534 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005535 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005536 if (data->cancelled != 0) {
5537 struct rpc_task *task;
5538 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5539 data->arg.lock_seqid);
5540 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005541 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005542 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005543 } else
5544 nfs_free_seqid(data->arg.lock_seqid);
5545 nfs4_put_lock_state(data->lsp);
5546 put_nfs_open_context(data->ctx);
5547 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005548 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005549}
5550
5551static const struct rpc_call_ops nfs4_lock_ops = {
5552 .rpc_call_prepare = nfs4_lock_prepare,
5553 .rpc_call_done = nfs4_lock_done,
5554 .rpc_release = nfs4_lock_release,
5555};
5556
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005557static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5558{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005559 switch (error) {
5560 case -NFS4ERR_ADMIN_REVOKED:
5561 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005562 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005563 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005564 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005565 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005566 break;
5567 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005568 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005569 case -NFS4ERR_EXPIRED:
5570 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005571 };
5572}
5573
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005574static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005575{
5576 struct nfs4_lockdata *data;
5577 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005578 struct rpc_message msg = {
5579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5580 .rpc_cred = state->owner->so_cred,
5581 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005582 struct rpc_task_setup task_setup_data = {
5583 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005584 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005585 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005587 .flags = RPC_TASK_ASYNC,
5588 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005589 int ret;
5590
Harvey Harrison3110ff82008-05-02 13:42:44 -07005591 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005592 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005593 fl->fl_u.nfs4_fl.owner,
5594 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005595 if (data == NULL)
5596 return -ENOMEM;
5597 if (IS_SETLKW(cmd))
5598 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005599 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005600 msg.rpc_argp = &data->arg;
5601 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005602 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005603 if (recovery_type > NFS_LOCK_NEW) {
5604 if (recovery_type == NFS_LOCK_RECLAIM)
5605 data->arg.reclaim = NFS_LOCK_RECLAIM;
5606 nfs4_set_sequence_privileged(&data->arg.seq_args);
5607 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005608 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005609 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005610 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005611 ret = nfs4_wait_for_completion_rpc_task(task);
5612 if (ret == 0) {
5613 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005614 if (ret)
5615 nfs4_handle_setlk_error(data->server, data->lsp,
5616 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005617 } else
5618 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005619 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005620 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005621 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622}
5623
5624static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5625{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005626 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005627 struct nfs4_exception exception = {
5628 .inode = state->inode,
5629 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005630 int err;
5631
5632 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005633 /* Cache the lock if possible... */
5634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5635 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005637 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005638 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005639 break;
5640 nfs4_handle_exception(server, err, &exception);
5641 } while (exception.retry);
5642 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643}
5644
5645static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5646{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005647 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005648 struct nfs4_exception exception = {
5649 .inode = state->inode,
5650 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005651 int err;
5652
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005653 err = nfs4_set_lock_state(state, request);
5654 if (err != 0)
5655 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005656 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005657 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5658 return 0;
5659 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005660 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005661 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5662 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005663 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005664 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005665 switch (err) {
5666 default:
5667 goto out;
5668 case -NFS4ERR_GRACE:
5669 case -NFS4ERR_DELAY:
5670 nfs4_handle_exception(server, err, &exception);
5671 err = 0;
5672 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005673 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005674out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676}
5677
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005678#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005679/**
5680 * nfs41_check_expired_locks - possibly free a lock stateid
5681 *
5682 * @state: NFSv4 state for an inode
5683 *
5684 * Returns NFS_OK if recovery for this stateid is now finished.
5685 * Otherwise a negative NFS4ERR value is returned.
5686 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005687static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005688{
Chuck Levereb64cf92012-07-11 16:30:05 -04005689 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005690 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005691 struct nfs_server *server = NFS_SERVER(state->inode);
5692
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005693 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005694 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005695 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5696
5697 status = nfs41_test_stateid(server,
5698 &lsp->ls_stateid,
5699 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005700 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005701 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005702 /* Free the stateid unless the server
5703 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005704 if (status != -NFS4ERR_BAD_STATEID)
5705 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005706 &lsp->ls_stateid,
5707 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005708 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005709 ret = status;
5710 }
5711 }
5712 };
5713
5714 return ret;
5715}
5716
5717static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5718{
5719 int status = NFS_OK;
5720
5721 if (test_bit(LK_STATE_IN_USE, &state->flags))
5722 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005723 if (status != NFS_OK)
5724 status = nfs4_lock_expired(state, request);
5725 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005726}
5727#endif
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5730{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005731 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005732 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005733 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005734 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005735 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005737 if ((fl_flags & FL_POSIX) &&
5738 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5739 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005740 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005741 status = nfs4_set_lock_state(state, request);
5742 if (status != 0)
5743 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005744 request->fl_flags |= FL_ACCESS;
5745 status = do_vfs_lock(request->fl_file, request);
5746 if (status < 0)
5747 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005748 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005749 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005750 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005751 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005752 request->fl_flags = fl_flags & ~FL_SLEEP;
5753 status = do_vfs_lock(request->fl_file, request);
5754 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005755 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005756 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5757 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005758 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005759 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005760 goto out;
5761 down_read(&nfsi->rwsem);
5762 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5763 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005764 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005765 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005766 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005767 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005768 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005769 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5770 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005771out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005772 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005773out:
5774 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 return status;
5776}
5777
5778static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5779{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005780 struct nfs4_exception exception = {
5781 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005782 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005783 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 int err;
5785
5786 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005787 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005788 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005789 if (err == -NFS4ERR_DENIED)
5790 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005792 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 } while (exception.retry);
5794 return err;
5795}
5796
5797static int
5798nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5799{
5800 struct nfs_open_context *ctx;
5801 struct nfs4_state *state;
5802 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5803 int status;
5804
5805 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005806 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 state = ctx->state;
5808
5809 if (request->fl_start < 0 || request->fl_end < 0)
5810 return -EINVAL;
5811
Trond Myklebustd9531262009-07-21 19:22:38 -04005812 if (IS_GETLK(cmd)) {
5813 if (state != NULL)
5814 return nfs4_proc_getlk(state, F_GETLK, request);
5815 return 0;
5816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
5818 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5819 return -EINVAL;
5820
Trond Myklebustd9531262009-07-21 19:22:38 -04005821 if (request->fl_type == F_UNLCK) {
5822 if (state != NULL)
5823 return nfs4_proc_unlck(state, cmd, request);
5824 return 0;
5825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Trond Myklebustd9531262009-07-21 19:22:38 -04005827 if (state == NULL)
5828 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005829 /*
5830 * Don't rely on the VFS having checked the file open mode,
5831 * since it won't do this for flock() locks.
5832 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005833 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005834 case F_RDLCK:
5835 if (!(filp->f_mode & FMODE_READ))
5836 return -EBADF;
5837 break;
5838 case F_WRLCK:
5839 if (!(filp->f_mode & FMODE_WRITE))
5840 return -EBADF;
5841 }
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 do {
5844 status = nfs4_proc_setlk(state, cmd, request);
5845 if ((status != -EAGAIN) || IS_SETLK(cmd))
5846 break;
5847 timeout = nfs4_set_lock_task_retry(timeout);
5848 status = -ERESTARTSYS;
5849 if (signalled())
5850 break;
5851 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 return status;
5853}
5854
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005855int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005856{
5857 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005858 int err;
5859
5860 err = nfs4_set_lock_state(state, fl);
5861 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005862 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005863 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005864 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005865}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005866
Trond Myklebustcf470c32012-03-07 13:49:12 -05005867struct nfs_release_lockowner_data {
5868 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005869 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005870 struct nfs_release_lockowner_args args;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005871 struct nfs4_sequence_args seq_args;
5872 struct nfs4_sequence_res seq_res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005873 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005874};
5875
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005876static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5877{
5878 struct nfs_release_lockowner_data *data = calldata;
5879 nfs40_setup_sequence(data->server,
5880 &data->seq_args, &data->seq_res, task);
Chuck Lever60ea6812013-10-17 14:13:47 -04005881 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005882}
5883
5884static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5885{
5886 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005887 struct nfs_server *server = data->server;
5888
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005889 nfs40_sequence_done(task, &data->seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005890
5891 switch (task->tk_status) {
5892 case 0:
5893 renew_lease(server, data->timestamp);
5894 break;
5895 case -NFS4ERR_STALE_CLIENTID:
5896 case -NFS4ERR_EXPIRED:
5897 case -NFS4ERR_LEASE_MOVED:
5898 case -NFS4ERR_DELAY:
5899 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5900 rpc_restart_call_prepare(task);
5901 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005902}
5903
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005904static void nfs4_release_lockowner_release(void *calldata)
5905{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005906 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005907 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005908 kfree(calldata);
5909}
5910
Trond Myklebust17280172012-03-11 13:11:00 -04005911static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005912 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5913 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005914 .rpc_release = nfs4_release_lockowner_release,
5915};
5916
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005917static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005918{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005919 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005920 struct rpc_message msg = {
5921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5922 };
5923
5924 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005925 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005926
Trond Myklebustcf470c32012-03-07 13:49:12 -05005927 data = kmalloc(sizeof(*data), GFP_NOFS);
5928 if (!data)
5929 return -ENOMEM;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005930 nfs4_init_sequence(&data->seq_args, &data->seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005931 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005932 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005933 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5934 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5935 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005936
Trond Myklebustcf470c32012-03-07 13:49:12 -05005937 msg.rpc_argp = &data->args;
5938 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5939 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005940}
5941
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005942#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5943
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005944static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5945 const void *buf, size_t buflen,
5946 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005947{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005948 if (strcmp(key, "") != 0)
5949 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005950
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005951 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005952}
5953
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005954static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5955 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005956{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005957 if (strcmp(key, "") != 0)
5958 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005959
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005960 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005961}
5962
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005963static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5964 size_t list_len, const char *name,
5965 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005966{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005967 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005968
J. Bruce Fields096455a2006-03-20 23:23:42 -05005969 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5970 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005971
5972 if (list && len <= list_len)
5973 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005974 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005975}
5976
David Quigleyc9bccef2013-05-22 12:50:45 -04005977#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5978static inline int nfs4_server_supports_labels(struct nfs_server *server)
5979{
5980 return server->caps & NFS_CAP_SECURITY_LABEL;
5981}
5982
5983static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5984 const void *buf, size_t buflen,
5985 int flags, int type)
5986{
5987 if (security_ismaclabel(key))
5988 return nfs4_set_security_label(dentry, buf, buflen);
5989
5990 return -EOPNOTSUPP;
5991}
5992
5993static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5994 void *buf, size_t buflen, int type)
5995{
5996 if (security_ismaclabel(key))
5997 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5998 return -EOPNOTSUPP;
5999}
6000
6001static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
6002 size_t list_len, const char *name,
6003 size_t name_len, int type)
6004{
6005 size_t len = 0;
6006
6007 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
6008 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
6009 if (list && len <= list_len)
6010 security_inode_listsecurity(dentry->d_inode, list, len);
6011 }
6012 return len;
6013}
6014
6015static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6016 .prefix = XATTR_SECURITY_PREFIX,
6017 .list = nfs4_xattr_list_nfs4_label,
6018 .get = nfs4_xattr_get_nfs4_label,
6019 .set = nfs4_xattr_set_nfs4_label,
6020};
6021#endif
6022
6023
Andy Adamson533eb462011-06-13 18:25:56 -04006024/*
6025 * nfs_fhget will use either the mounted_on_fileid or the fileid
6026 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006027static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6028{
Andy Adamson533eb462011-06-13 18:25:56 -04006029 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6030 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6031 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006032 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006033 return;
6034
6035 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006036 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006037 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6038 fattr->nlink = 2;
6039}
6040
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006041static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6042 const struct qstr *name,
6043 struct nfs4_fs_locations *fs_locations,
6044 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006045{
6046 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006047 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006048 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006049 };
6050 struct nfs4_fs_locations_arg args = {
6051 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006052 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006053 .page = page,
6054 .bitmask = bitmask,
6055 };
Benny Halevy22958462009-04-01 09:22:02 -04006056 struct nfs4_fs_locations_res res = {
6057 .fs_locations = fs_locations,
6058 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006059 struct rpc_message msg = {
6060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6061 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006062 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006063 };
6064 int status;
6065
Harvey Harrison3110ff82008-05-02 13:42:44 -07006066 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006067
6068 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6069 * is not supported */
6070 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6071 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6072 else
6073 bitmask[0] |= FATTR4_WORD0_FILEID;
6074
Trond Myklebustc228fd32007-01-13 02:28:11 -05006075 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006076 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006077 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006078 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006079 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006080 return status;
6081}
6082
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006083int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6084 const struct qstr *name,
6085 struct nfs4_fs_locations *fs_locations,
6086 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006087{
6088 struct nfs4_exception exception = { };
6089 int err;
6090 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006091 err = _nfs4_proc_fs_locations(client, dir, name,
6092 fs_locations, page);
6093 trace_nfs4_get_fs_locations(dir, name, err);
6094 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006095 &exception);
6096 } while (exception.retry);
6097 return err;
6098}
6099
Chuck Leverb03d7352013-10-17 14:12:50 -04006100/*
6101 * This operation also signals the server that this client is
6102 * performing migration recovery. The server can stop returning
6103 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6104 * appended to this compound to identify the client ID which is
6105 * performing recovery.
6106 */
6107static int _nfs40_proc_get_locations(struct inode *inode,
6108 struct nfs4_fs_locations *locations,
6109 struct page *page, struct rpc_cred *cred)
6110{
6111 struct nfs_server *server = NFS_SERVER(inode);
6112 struct rpc_clnt *clnt = server->client;
6113 u32 bitmask[2] = {
6114 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6115 };
6116 struct nfs4_fs_locations_arg args = {
6117 .clientid = server->nfs_client->cl_clientid,
6118 .fh = NFS_FH(inode),
6119 .page = page,
6120 .bitmask = bitmask,
6121 .migration = 1, /* skip LOOKUP */
6122 .renew = 1, /* append RENEW */
6123 };
6124 struct nfs4_fs_locations_res res = {
6125 .fs_locations = locations,
6126 .migration = 1,
6127 .renew = 1,
6128 };
6129 struct rpc_message msg = {
6130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6131 .rpc_argp = &args,
6132 .rpc_resp = &res,
6133 .rpc_cred = cred,
6134 };
6135 unsigned long now = jiffies;
6136 int status;
6137
6138 nfs_fattr_init(&locations->fattr);
6139 locations->server = server;
6140 locations->nlocations = 0;
6141
6142 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6143 nfs4_set_sequence_privileged(&args.seq_args);
6144 status = nfs4_call_sync_sequence(clnt, server, &msg,
6145 &args.seq_args, &res.seq_res);
6146 if (status)
6147 return status;
6148
6149 renew_lease(server, now);
6150 return 0;
6151}
6152
6153#ifdef CONFIG_NFS_V4_1
6154
6155/*
6156 * This operation also signals the server that this client is
6157 * performing migration recovery. The server can stop asserting
6158 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6159 * performing this operation is identified in the SEQUENCE
6160 * operation in this compound.
6161 *
6162 * When the client supports GETATTR(fs_locations_info), it can
6163 * be plumbed in here.
6164 */
6165static int _nfs41_proc_get_locations(struct inode *inode,
6166 struct nfs4_fs_locations *locations,
6167 struct page *page, struct rpc_cred *cred)
6168{
6169 struct nfs_server *server = NFS_SERVER(inode);
6170 struct rpc_clnt *clnt = server->client;
6171 u32 bitmask[2] = {
6172 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6173 };
6174 struct nfs4_fs_locations_arg args = {
6175 .fh = NFS_FH(inode),
6176 .page = page,
6177 .bitmask = bitmask,
6178 .migration = 1, /* skip LOOKUP */
6179 };
6180 struct nfs4_fs_locations_res res = {
6181 .fs_locations = locations,
6182 .migration = 1,
6183 };
6184 struct rpc_message msg = {
6185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6186 .rpc_argp = &args,
6187 .rpc_resp = &res,
6188 .rpc_cred = cred,
6189 };
6190 int status;
6191
6192 nfs_fattr_init(&locations->fattr);
6193 locations->server = server;
6194 locations->nlocations = 0;
6195
6196 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6197 nfs4_set_sequence_privileged(&args.seq_args);
6198 status = nfs4_call_sync_sequence(clnt, server, &msg,
6199 &args.seq_args, &res.seq_res);
6200 if (status == NFS4_OK &&
6201 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6202 status = -NFS4ERR_LEASE_MOVED;
6203 return status;
6204}
6205
6206#endif /* CONFIG_NFS_V4_1 */
6207
6208/**
6209 * nfs4_proc_get_locations - discover locations for a migrated FSID
6210 * @inode: inode on FSID that is migrating
6211 * @locations: result of query
6212 * @page: buffer
6213 * @cred: credential to use for this operation
6214 *
6215 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6216 * operation failed, or a negative errno if a local error occurred.
6217 *
6218 * On success, "locations" is filled in, but if the server has
6219 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6220 * asserted.
6221 *
6222 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6223 * from this client that require migration recovery.
6224 */
6225int nfs4_proc_get_locations(struct inode *inode,
6226 struct nfs4_fs_locations *locations,
6227 struct page *page, struct rpc_cred *cred)
6228{
6229 struct nfs_server *server = NFS_SERVER(inode);
6230 struct nfs_client *clp = server->nfs_client;
6231 const struct nfs4_mig_recovery_ops *ops =
6232 clp->cl_mvops->mig_recovery_ops;
6233 struct nfs4_exception exception = { };
6234 int status;
6235
6236 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6237 (unsigned long long)server->fsid.major,
6238 (unsigned long long)server->fsid.minor,
6239 clp->cl_hostname);
6240 nfs_display_fhandle(NFS_FH(inode), __func__);
6241
6242 do {
6243 status = ops->get_locations(inode, locations, page, cred);
6244 if (status != -NFS4ERR_DELAY)
6245 break;
6246 nfs4_handle_exception(server, status, &exception);
6247 } while (exception.retry);
6248 return status;
6249}
6250
Chuck Lever44c99932013-10-17 14:13:30 -04006251/*
6252 * This operation also signals the server that this client is
6253 * performing "lease moved" recovery. The server can stop
6254 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6255 * is appended to this compound to identify the client ID which is
6256 * performing recovery.
6257 */
6258static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6259{
6260 struct nfs_server *server = NFS_SERVER(inode);
6261 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6262 struct rpc_clnt *clnt = server->client;
6263 struct nfs4_fsid_present_arg args = {
6264 .fh = NFS_FH(inode),
6265 .clientid = clp->cl_clientid,
6266 .renew = 1, /* append RENEW */
6267 };
6268 struct nfs4_fsid_present_res res = {
6269 .renew = 1,
6270 };
6271 struct rpc_message msg = {
6272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6273 .rpc_argp = &args,
6274 .rpc_resp = &res,
6275 .rpc_cred = cred,
6276 };
6277 unsigned long now = jiffies;
6278 int status;
6279
6280 res.fh = nfs_alloc_fhandle();
6281 if (res.fh == NULL)
6282 return -ENOMEM;
6283
6284 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6285 nfs4_set_sequence_privileged(&args.seq_args);
6286 status = nfs4_call_sync_sequence(clnt, server, &msg,
6287 &args.seq_args, &res.seq_res);
6288 nfs_free_fhandle(res.fh);
6289 if (status)
6290 return status;
6291
6292 do_renew_lease(clp, now);
6293 return 0;
6294}
6295
6296#ifdef CONFIG_NFS_V4_1
6297
6298/*
6299 * This operation also signals the server that this client is
6300 * performing "lease moved" recovery. The server can stop asserting
6301 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6302 * this operation is identified in the SEQUENCE operation in this
6303 * compound.
6304 */
6305static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6306{
6307 struct nfs_server *server = NFS_SERVER(inode);
6308 struct rpc_clnt *clnt = server->client;
6309 struct nfs4_fsid_present_arg args = {
6310 .fh = NFS_FH(inode),
6311 };
6312 struct nfs4_fsid_present_res res = {
6313 };
6314 struct rpc_message msg = {
6315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6316 .rpc_argp = &args,
6317 .rpc_resp = &res,
6318 .rpc_cred = cred,
6319 };
6320 int status;
6321
6322 res.fh = nfs_alloc_fhandle();
6323 if (res.fh == NULL)
6324 return -ENOMEM;
6325
6326 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6327 nfs4_set_sequence_privileged(&args.seq_args);
6328 status = nfs4_call_sync_sequence(clnt, server, &msg,
6329 &args.seq_args, &res.seq_res);
6330 nfs_free_fhandle(res.fh);
6331 if (status == NFS4_OK &&
6332 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6333 status = -NFS4ERR_LEASE_MOVED;
6334 return status;
6335}
6336
6337#endif /* CONFIG_NFS_V4_1 */
6338
6339/**
6340 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6341 * @inode: inode on FSID to check
6342 * @cred: credential to use for this operation
6343 *
6344 * Server indicates whether the FSID is present, moved, or not
6345 * recognized. This operation is necessary to clear a LEASE_MOVED
6346 * condition for this client ID.
6347 *
6348 * Returns NFS4_OK if the FSID is present on this server,
6349 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6350 * NFS4ERR code if some error occurred on the server, or a
6351 * negative errno if a local failure occurred.
6352 */
6353int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6354{
6355 struct nfs_server *server = NFS_SERVER(inode);
6356 struct nfs_client *clp = server->nfs_client;
6357 const struct nfs4_mig_recovery_ops *ops =
6358 clp->cl_mvops->mig_recovery_ops;
6359 struct nfs4_exception exception = { };
6360 int status;
6361
6362 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6363 (unsigned long long)server->fsid.major,
6364 (unsigned long long)server->fsid.minor,
6365 clp->cl_hostname);
6366 nfs_display_fhandle(NFS_FH(inode), __func__);
6367
6368 do {
6369 status = ops->fsid_present(inode, cred);
6370 if (status != -NFS4ERR_DELAY)
6371 break;
6372 nfs4_handle_exception(server, status, &exception);
6373 } while (exception.retry);
6374 return status;
6375}
6376
Andy Adamson5ec16a82013-08-08 10:57:55 -04006377/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006378 * If 'use_integrity' is true and the state managment nfs_client
6379 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6380 * and the machine credential as per RFC3530bis and RFC5661 Security
6381 * Considerations sections. Otherwise, just use the user cred with the
6382 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006383 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006384static 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 +00006385{
6386 int status;
6387 struct nfs4_secinfo_arg args = {
6388 .dir_fh = NFS_FH(dir),
6389 .name = name,
6390 };
6391 struct nfs4_secinfo_res res = {
6392 .flavors = flavors,
6393 };
6394 struct rpc_message msg = {
6395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6396 .rpc_argp = &args,
6397 .rpc_resp = &res,
6398 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006399 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006400 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006401
6402 if (use_integrity) {
6403 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006404 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6405 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006406 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006407
6408 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006409
6410 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6411 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6412
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006413 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6414 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006415 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006416
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006417 if (cred)
6418 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006419
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006420 return status;
6421}
6422
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006423int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6424 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006425{
6426 struct nfs4_exception exception = { };
6427 int err;
6428 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006429 err = -NFS4ERR_WRONGSEC;
6430
6431 /* try to use integrity protection with machine cred */
6432 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6433 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6434
6435 /*
6436 * if unable to use integrity protection, or SECINFO with
6437 * integrity protection returns NFS4ERR_WRONGSEC (which is
6438 * disallowed by spec, but exists in deployed servers) use
6439 * the current filesystem's rpc_client and the user cred.
6440 */
6441 if (err == -NFS4ERR_WRONGSEC)
6442 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6443
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006444 trace_nfs4_secinfo(dir, name, err);
6445 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006446 &exception);
6447 } while (exception.retry);
6448 return err;
6449}
6450
Andy Adamson557134a2009-04-01 09:21:53 -04006451#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006452/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006453 * Check the exchange flags returned by the server for invalid flags, having
6454 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6455 * DS flags set.
6456 */
6457static int nfs4_check_cl_exchange_flags(u32 flags)
6458{
6459 if (flags & ~EXCHGID4_FLAG_MASK_R)
6460 goto out_inval;
6461 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6462 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6463 goto out_inval;
6464 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6465 goto out_inval;
6466 return NFS_OK;
6467out_inval:
6468 return -NFS4ERR_INVAL;
6469}
6470
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006471static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006472nfs41_same_server_scope(struct nfs41_server_scope *a,
6473 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006474{
6475 if (a->server_scope_sz == b->server_scope_sz &&
6476 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6477 return true;
6478
6479 return false;
6480}
6481
Andy Adamson357f54d2010-12-14 10:11:57 -05006482/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006483 * nfs4_proc_bind_conn_to_session()
6484 *
6485 * The 4.1 client currently uses the same TCP connection for the
6486 * fore and backchannel.
6487 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006488int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006489{
6490 int status;
6491 struct nfs41_bind_conn_to_session_res res;
6492 struct rpc_message msg = {
6493 .rpc_proc =
6494 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6495 .rpc_argp = clp,
6496 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006497 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006498 };
6499
6500 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006501
6502 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6503 if (unlikely(res.session == NULL)) {
6504 status = -ENOMEM;
6505 goto out;
6506 }
6507
6508 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006509 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006510 if (status == 0) {
6511 if (memcmp(res.session->sess_id.data,
6512 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6513 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6514 status = -EIO;
6515 goto out_session;
6516 }
6517 if (res.dir != NFS4_CDFS4_BOTH) {
6518 dprintk("NFS: %s: Unexpected direction from server\n",
6519 __func__);
6520 status = -EIO;
6521 goto out_session;
6522 }
6523 if (res.use_conn_in_rdma_mode) {
6524 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6525 __func__);
6526 status = -EIO;
6527 goto out_session;
6528 }
6529 }
6530out_session:
6531 kfree(res.session);
6532out:
6533 dprintk("<-- %s status= %d\n", __func__, status);
6534 return status;
6535}
6536
6537/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006538 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6539 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006540 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006541static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6542 .how = SP4_MACH_CRED,
6543 .enforce.u.words = {
6544 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6545 1 << (OP_EXCHANGE_ID - 32) |
6546 1 << (OP_CREATE_SESSION - 32) |
6547 1 << (OP_DESTROY_SESSION - 32) |
6548 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006549 },
6550 .allow.u.words = {
6551 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006552 1 << (OP_LOCKU) |
6553 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006554 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006555 1 << (OP_SECINFO_NO_NAME - 32) |
6556 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006557 1 << (OP_FREE_STATEID - 32) |
6558 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006559 }
6560};
6561
6562/*
6563 * Select the state protection mode for client `clp' given the server results
6564 * from exchange_id in `sp'.
6565 *
6566 * Returns 0 on success, negative errno otherwise.
6567 */
6568static int nfs4_sp4_select_mode(struct nfs_client *clp,
6569 struct nfs41_state_protection *sp)
6570{
6571 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6572 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6573 1 << (OP_EXCHANGE_ID - 32) |
6574 1 << (OP_CREATE_SESSION - 32) |
6575 1 << (OP_DESTROY_SESSION - 32) |
6576 1 << (OP_DESTROY_CLIENTID - 32)
6577 };
6578 unsigned int i;
6579
6580 if (sp->how == SP4_MACH_CRED) {
6581 /* Print state protect result */
6582 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6583 for (i = 0; i <= LAST_NFS4_OP; i++) {
6584 if (test_bit(i, sp->enforce.u.longs))
6585 dfprintk(MOUNT, " enforce op %d\n", i);
6586 if (test_bit(i, sp->allow.u.longs))
6587 dfprintk(MOUNT, " allow op %d\n", i);
6588 }
6589
6590 /* make sure nothing is on enforce list that isn't supported */
6591 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6592 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6593 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6594 return -EINVAL;
6595 }
6596 }
6597
6598 /*
6599 * Minimal mode - state operations are allowed to use machine
6600 * credential. Note this already happens by default, so the
6601 * client doesn't have to do anything more than the negotiation.
6602 *
6603 * NOTE: we don't care if EXCHANGE_ID is in the list -
6604 * we're already using the machine cred for exchange_id
6605 * and will never use a different cred.
6606 */
6607 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6608 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6609 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6610 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6611 dfprintk(MOUNT, "sp4_mach_cred:\n");
6612 dfprintk(MOUNT, " minimal mode enabled\n");
6613 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6614 } else {
6615 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6616 return -EINVAL;
6617 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006618
6619 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6620 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6621 dfprintk(MOUNT, " cleanup mode enabled\n");
6622 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6623 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006624
6625 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6626 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6627 dfprintk(MOUNT, " secinfo mode enabled\n");
6628 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6629 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006630
6631 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6632 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6633 dfprintk(MOUNT, " stateid mode enabled\n");
6634 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6635 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006636
6637 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6638 dfprintk(MOUNT, " write mode enabled\n");
6639 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6640 }
6641
6642 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6643 dfprintk(MOUNT, " commit mode enabled\n");
6644 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6645 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006646 }
6647
6648 return 0;
6649}
6650
6651/*
6652 * _nfs4_proc_exchange_id()
6653 *
6654 * Wrapper for EXCHANGE_ID operation.
6655 */
6656static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6657 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006658{
6659 nfs4_verifier verifier;
6660 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006661 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006662 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006663#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006664 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006665 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6666 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6667#else
6668 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6669 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6670#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006671 };
6672 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006673 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006674 };
6675 int status;
6676 struct rpc_message msg = {
6677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6678 .rpc_argp = &args,
6679 .rpc_resp = &res,
6680 .rpc_cred = cred,
6681 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006682
Chuck Leverf0920752012-05-21 22:45:41 -04006683 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006684 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6685 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006686 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6687 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6688 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006689
Chuck Leveracdeb692012-05-21 22:46:16 -04006690 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006691 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006692 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006693 status = -ENOMEM;
6694 goto out;
6695 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006696
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006697 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006698 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006699 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006700 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006701 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006702 }
6703
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006704 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006705 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006706 status = -ENOMEM;
6707 goto out_server_scope;
6708 }
6709
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006710 switch (sp4_how) {
6711 case SP4_NONE:
6712 args.state_protect.how = SP4_NONE;
6713 break;
6714
6715 case SP4_MACH_CRED:
6716 args.state_protect = nfs4_sp4_mach_cred_request;
6717 break;
6718
6719 default:
6720 /* unsupported! */
6721 WARN_ON_ONCE(1);
6722 status = -EINVAL;
6723 goto out_server_scope;
6724 }
6725
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006726 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006727 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006728 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006729 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006730
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006731 if (status == 0)
6732 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6733
Chuck Lever177313f2012-05-21 22:44:58 -04006734 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006735 clp->cl_clientid = res.clientid;
6736 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6737 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6738 clp->cl_seqid = res.seqid;
6739
Chuck Leveracdeb692012-05-21 22:46:16 -04006740 kfree(clp->cl_serverowner);
6741 clp->cl_serverowner = res.server_owner;
6742 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006743
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006744 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006745 kfree(clp->cl_implid);
6746 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006747
Chuck Lever177313f2012-05-21 22:44:58 -04006748 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006749 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006750 res.server_scope)) {
6751 dprintk("%s: server_scope mismatch detected\n",
6752 __func__);
6753 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006754 kfree(clp->cl_serverscope);
6755 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006756 }
6757
Chuck Lever177313f2012-05-21 22:44:58 -04006758 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006759 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006760 goto out;
6761 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006762 } else
6763 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006764
Chuck Leveracdeb692012-05-21 22:46:16 -04006765out_server_owner:
6766 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006767out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006768 kfree(res.server_scope);
6769out:
Chuck Lever177313f2012-05-21 22:44:58 -04006770 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006771 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006772 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006773 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006774 clp->cl_implid->date.seconds,
6775 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006776 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006777 return status;
6778}
6779
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006780/*
6781 * nfs4_proc_exchange_id()
6782 *
6783 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6784 *
6785 * Since the clientid has expired, all compounds using sessions
6786 * associated with the stale clientid will be returning
6787 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6788 * be in some phase of session reset.
6789 *
6790 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6791 */
6792int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6793{
6794 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6795 int status;
6796
6797 /* try SP4_MACH_CRED if krb5i/p */
6798 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6799 authflavor == RPC_AUTH_GSS_KRB5P) {
6800 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6801 if (!status)
6802 return 0;
6803 }
6804
6805 /* try SP4_NONE */
6806 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6807}
6808
Trond Myklebust66245532012-05-25 17:18:09 -04006809static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6810 struct rpc_cred *cred)
6811{
6812 struct rpc_message msg = {
6813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6814 .rpc_argp = clp,
6815 .rpc_cred = cred,
6816 };
6817 int status;
6818
6819 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006820 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006821 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006822 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006823 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6824 return status;
6825}
6826
6827static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6828 struct rpc_cred *cred)
6829{
6830 unsigned int loop;
6831 int ret;
6832
6833 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6834 ret = _nfs4_proc_destroy_clientid(clp, cred);
6835 switch (ret) {
6836 case -NFS4ERR_DELAY:
6837 case -NFS4ERR_CLIENTID_BUSY:
6838 ssleep(1);
6839 break;
6840 default:
6841 return ret;
6842 }
6843 }
6844 return 0;
6845}
6846
6847int nfs4_destroy_clientid(struct nfs_client *clp)
6848{
6849 struct rpc_cred *cred;
6850 int ret = 0;
6851
6852 if (clp->cl_mvops->minor_version < 1)
6853 goto out;
6854 if (clp->cl_exchange_flags == 0)
6855 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006856 if (clp->cl_preserve_clid)
6857 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006858 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006859 ret = nfs4_proc_destroy_clientid(clp, cred);
6860 if (cred)
6861 put_rpccred(cred);
6862 switch (ret) {
6863 case 0:
6864 case -NFS4ERR_STALE_CLIENTID:
6865 clp->cl_exchange_flags = 0;
6866 }
6867out:
6868 return ret;
6869}
6870
Andy Adamson2050f0c2009-04-01 09:22:30 -04006871struct nfs4_get_lease_time_data {
6872 struct nfs4_get_lease_time_args *args;
6873 struct nfs4_get_lease_time_res *res;
6874 struct nfs_client *clp;
6875};
6876
6877static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6878 void *calldata)
6879{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006880 struct nfs4_get_lease_time_data *data =
6881 (struct nfs4_get_lease_time_data *)calldata;
6882
6883 dprintk("--> %s\n", __func__);
6884 /* just setup sequence, do not trigger session recovery
6885 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006886 nfs41_setup_sequence(data->clp->cl_session,
6887 &data->args->la_seq_args,
6888 &data->res->lr_seq_res,
6889 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006890 dprintk("<-- %s\n", __func__);
6891}
6892
6893/*
6894 * Called from nfs4_state_manager thread for session setup, so don't recover
6895 * from sequence operation or clientid errors.
6896 */
6897static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6898{
6899 struct nfs4_get_lease_time_data *data =
6900 (struct nfs4_get_lease_time_data *)calldata;
6901
6902 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006903 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6904 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006905 switch (task->tk_status) {
6906 case -NFS4ERR_DELAY:
6907 case -NFS4ERR_GRACE:
6908 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6909 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6910 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006911 /* fall through */
6912 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006913 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006914 return;
6915 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006916 dprintk("<-- %s\n", __func__);
6917}
6918
Trond Myklebust17280172012-03-11 13:11:00 -04006919static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006920 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6921 .rpc_call_done = nfs4_get_lease_time_done,
6922};
6923
6924int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6925{
6926 struct rpc_task *task;
6927 struct nfs4_get_lease_time_args args;
6928 struct nfs4_get_lease_time_res res = {
6929 .lr_fsinfo = fsinfo,
6930 };
6931 struct nfs4_get_lease_time_data data = {
6932 .args = &args,
6933 .res = &res,
6934 .clp = clp,
6935 };
6936 struct rpc_message msg = {
6937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6938 .rpc_argp = &args,
6939 .rpc_resp = &res,
6940 };
6941 struct rpc_task_setup task_setup = {
6942 .rpc_client = clp->cl_rpcclient,
6943 .rpc_message = &msg,
6944 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006945 .callback_data = &data,
6946 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006947 };
6948 int status;
6949
Chuck Levera9c92d62013-08-09 12:48:18 -04006950 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006951 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006952 dprintk("--> %s\n", __func__);
6953 task = rpc_run_task(&task_setup);
6954
6955 if (IS_ERR(task))
6956 status = PTR_ERR(task);
6957 else {
6958 status = task->tk_status;
6959 rpc_put_task(task);
6960 }
6961 dprintk("<-- %s return %d\n", __func__, status);
6962
6963 return status;
6964}
6965
Andy Adamsonfc931582009-04-01 09:22:31 -04006966/*
6967 * Initialize the values to be used by the client in CREATE_SESSION
6968 * If nfs4_init_session set the fore channel request and response sizes,
6969 * use them.
6970 *
6971 * Set the back channel max_resp_sz_cached to zero to force the client to
6972 * always set csa_cachethis to FALSE because the current implementation
6973 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6974 */
6975static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6976{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006977 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006978
Andy Adamson18aad3d2013-06-26 12:21:49 -04006979 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6980 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6981
Andy Adamsonfc931582009-04-01 09:22:31 -04006982 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006983 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6984 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006985 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006986 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006987
6988 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006989 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006990 __func__,
6991 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006992 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006993
6994 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006995 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6996 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6997 args->bc_attrs.max_resp_sz_cached = 0;
6998 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6999 args->bc_attrs.max_reqs = 1;
7000
7001 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7002 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7003 __func__,
7004 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7005 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7006 args->bc_attrs.max_reqs);
7007}
7008
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007009static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04007010{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007011 struct nfs4_channel_attrs *sent = &args->fc_attrs;
7012 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
7013
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007014 if (rcvd->max_resp_sz > sent->max_resp_sz)
7015 return -EINVAL;
7016 /*
7017 * Our requested max_ops is the minimum we need; we're not
7018 * prepared to break up compounds into smaller pieces than that.
7019 * So, no point even trying to continue if the server won't
7020 * cooperate:
7021 */
7022 if (rcvd->max_ops < sent->max_ops)
7023 return -EINVAL;
7024 if (rcvd->max_reqs == 0)
7025 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007026 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7027 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007028 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007029}
7030
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007031static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7032{
7033 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7034 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007035
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007036 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7037 return -EINVAL;
7038 if (rcvd->max_resp_sz < sent->max_resp_sz)
7039 return -EINVAL;
7040 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7041 return -EINVAL;
7042 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007043 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007044 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007045 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007046 return -EINVAL;
7047 return 0;
7048}
Andy Adamson8d353012009-04-01 09:22:32 -04007049
Andy Adamson8d353012009-04-01 09:22:32 -04007050static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7051 struct nfs4_session *session)
7052{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007053 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007054
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007055 ret = nfs4_verify_fore_channel_attrs(args, session);
7056 if (ret)
7057 return ret;
7058 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007059}
7060
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007061static int _nfs4_proc_create_session(struct nfs_client *clp,
7062 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007063{
7064 struct nfs4_session *session = clp->cl_session;
7065 struct nfs41_create_session_args args = {
7066 .client = clp,
7067 .cb_program = NFS4_CALLBACK,
7068 };
7069 struct nfs41_create_session_res res = {
7070 .client = clp,
7071 };
7072 struct rpc_message msg = {
7073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7074 .rpc_argp = &args,
7075 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007076 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007077 };
7078 int status;
7079
7080 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007081 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007082
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007083 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007084 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007085
Trond Myklebust43095d32012-11-20 11:13:12 -05007086 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007087 /* Verify the session's negotiated channel_attrs values */
7088 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007089 /* Increment the clientid slot sequence id */
7090 clp->cl_seqid++;
7091 }
7092
7093 return status;
7094}
7095
7096/*
7097 * Issues a CREATE_SESSION operation to the server.
7098 * It is the responsibility of the caller to verify the session is
7099 * expired before calling this routine.
7100 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007101int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007102{
7103 int status;
7104 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007105 struct nfs4_session *session = clp->cl_session;
7106
7107 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7108
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007109 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007110 if (status)
7111 goto out;
7112
Andy Adamsonaacd5532011-11-09 13:58:21 -05007113 /* Init or reset the session slot tables */
7114 status = nfs4_setup_session_slot_tables(session);
7115 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007116 if (status)
7117 goto out;
7118
7119 ptr = (unsigned *)&session->sess_id.data[0];
7120 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7121 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007122out:
7123 dprintk("<-- %s\n", __func__);
7124 return status;
7125}
7126
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007127/*
7128 * Issue the over-the-wire RPC DESTROY_SESSION.
7129 * The caller must serialize access to this routine.
7130 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007131int nfs4_proc_destroy_session(struct nfs4_session *session,
7132 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007133{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007134 struct rpc_message msg = {
7135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7136 .rpc_argp = session,
7137 .rpc_cred = cred,
7138 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007139 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007140
7141 dprintk("--> nfs4_proc_destroy_session\n");
7142
7143 /* session is still being setup */
7144 if (session->clp->cl_cons_state != NFS_CS_READY)
7145 return status;
7146
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007147 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007148 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007149
7150 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007151 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007152 "Session has been destroyed regardless...\n", status);
7153
7154 dprintk("<-- nfs4_proc_destroy_session\n");
7155 return status;
7156}
7157
Trond Myklebust7b38c362012-05-23 13:23:31 -04007158/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007159 * Renew the cl_session lease.
7160 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007161struct nfs4_sequence_data {
7162 struct nfs_client *clp;
7163 struct nfs4_sequence_args args;
7164 struct nfs4_sequence_res res;
7165};
7166
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007167static void nfs41_sequence_release(void *data)
7168{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007169 struct nfs4_sequence_data *calldata = data;
7170 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007171
Alexandros Batsakis71358402010-02-05 03:45:05 -08007172 if (atomic_read(&clp->cl_count) > 1)
7173 nfs4_schedule_state_renewal(clp);
7174 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007175 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007176}
7177
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007178static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7179{
7180 switch(task->tk_status) {
7181 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007182 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7183 return -EAGAIN;
7184 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007185 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007186 }
7187 return 0;
7188}
7189
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007190static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007191{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007192 struct nfs4_sequence_data *calldata = data;
7193 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007194
Trond Myklebust14516c32010-07-31 14:29:06 -04007195 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7196 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007197
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007198 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007199 if (task->tk_status < 0) {
7200 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007201 if (atomic_read(&clp->cl_count) == 1)
7202 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007203
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007204 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7205 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007206 return;
7207 }
7208 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007209 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007210out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007211 dprintk("<-- %s\n", __func__);
7212}
7213
7214static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7215{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007216 struct nfs4_sequence_data *calldata = data;
7217 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007218 struct nfs4_sequence_args *args;
7219 struct nfs4_sequence_res *res;
7220
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007221 args = task->tk_msg.rpc_argp;
7222 res = task->tk_msg.rpc_resp;
7223
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007224 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007225}
7226
7227static const struct rpc_call_ops nfs41_sequence_ops = {
7228 .rpc_call_done = nfs41_sequence_call_done,
7229 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007230 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007231};
7232
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007233static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7234 struct rpc_cred *cred,
7235 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007236{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007237 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007238 struct rpc_message msg = {
7239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7240 .rpc_cred = cred,
7241 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007242 struct rpc_task_setup task_setup_data = {
7243 .rpc_client = clp->cl_rpcclient,
7244 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007245 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007246 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007247 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007248
Alexandros Batsakis71358402010-02-05 03:45:05 -08007249 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007250 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007251 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007252 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007253 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007254 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007255 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007256 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007257 if (is_privileged)
7258 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007259 msg.rpc_argp = &calldata->args;
7260 msg.rpc_resp = &calldata->res;
7261 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007262 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007263
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007264 return rpc_run_task(&task_setup_data);
7265}
7266
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007267static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007268{
7269 struct rpc_task *task;
7270 int ret = 0;
7271
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007272 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7273 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007274 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007275 if (IS_ERR(task))
7276 ret = PTR_ERR(task);
7277 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007278 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007279 dprintk("<-- %s status=%d\n", __func__, ret);
7280 return ret;
7281}
7282
7283static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7284{
7285 struct rpc_task *task;
7286 int ret;
7287
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007288 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007289 if (IS_ERR(task)) {
7290 ret = PTR_ERR(task);
7291 goto out;
7292 }
7293 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007294 if (!ret) {
7295 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7296
7297 if (task->tk_status == 0)
7298 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007299 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007300 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007301 rpc_put_task(task);
7302out:
7303 dprintk("<-- %s status=%d\n", __func__, ret);
7304 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007305}
7306
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007307struct nfs4_reclaim_complete_data {
7308 struct nfs_client *clp;
7309 struct nfs41_reclaim_complete_args arg;
7310 struct nfs41_reclaim_complete_res res;
7311};
7312
7313static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7314{
7315 struct nfs4_reclaim_complete_data *calldata = data;
7316
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007317 nfs41_setup_sequence(calldata->clp->cl_session,
7318 &calldata->arg.seq_args,
7319 &calldata->res.seq_res,
7320 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007321}
7322
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007323static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7324{
7325 switch(task->tk_status) {
7326 case 0:
7327 case -NFS4ERR_COMPLETE_ALREADY:
7328 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7329 break;
7330 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007331 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007332 /* fall through */
7333 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007334 return -EAGAIN;
7335 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007336 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007337 }
7338 return 0;
7339}
7340
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007341static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7342{
7343 struct nfs4_reclaim_complete_data *calldata = data;
7344 struct nfs_client *clp = calldata->clp;
7345 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7346
7347 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007348 if (!nfs41_sequence_done(task, res))
7349 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007350
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007351 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007352 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7353 rpc_restart_call_prepare(task);
7354 return;
7355 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007356 dprintk("<-- %s\n", __func__);
7357}
7358
7359static void nfs4_free_reclaim_complete_data(void *data)
7360{
7361 struct nfs4_reclaim_complete_data *calldata = data;
7362
7363 kfree(calldata);
7364}
7365
7366static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7367 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7368 .rpc_call_done = nfs4_reclaim_complete_done,
7369 .rpc_release = nfs4_free_reclaim_complete_data,
7370};
7371
7372/*
7373 * Issue a global reclaim complete.
7374 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007375static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7376 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007377{
7378 struct nfs4_reclaim_complete_data *calldata;
7379 struct rpc_task *task;
7380 struct rpc_message msg = {
7381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007382 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007383 };
7384 struct rpc_task_setup task_setup_data = {
7385 .rpc_client = clp->cl_rpcclient,
7386 .rpc_message = &msg,
7387 .callback_ops = &nfs4_reclaim_complete_call_ops,
7388 .flags = RPC_TASK_ASYNC,
7389 };
7390 int status = -ENOMEM;
7391
7392 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007393 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007394 if (calldata == NULL)
7395 goto out;
7396 calldata->clp = clp;
7397 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007398
Chuck Levera9c92d62013-08-09 12:48:18 -04007399 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007400 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007401 msg.rpc_argp = &calldata->arg;
7402 msg.rpc_resp = &calldata->res;
7403 task_setup_data.callback_data = calldata;
7404 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007405 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007406 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007407 goto out;
7408 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007409 status = nfs4_wait_for_completion_rpc_task(task);
7410 if (status == 0)
7411 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007412 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007413 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007414out:
7415 dprintk("<-- %s status=%d\n", __func__, status);
7416 return status;
7417}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007418
7419static void
7420nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7421{
7422 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007423 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007424 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007425
7426 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007427 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7428 * right now covering the LAYOUTGET we are about to send.
7429 * However, that is not so catastrophic, and there seems
7430 * to be no way to prevent it completely.
7431 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007432 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007433 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007434 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007435 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7436 NFS_I(lgp->args.inode)->layout,
7437 lgp->args.ctx->state)) {
7438 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007439 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007440}
7441
7442static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7443{
7444 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007445 struct inode *inode = lgp->args.inode;
7446 struct nfs_server *server = NFS_SERVER(inode);
7447 struct pnfs_layout_hdr *lo;
7448 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007449 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007450
Boaz Harroshed7e5422014-01-22 20:34:54 +02007451 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007452
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007453 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007454 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007455
7456 switch (task->tk_status) {
7457 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007458 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007459 /*
7460 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7461 * (or clients) writing to the same RAID stripe
7462 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007463 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007464 /*
7465 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7466 * existing layout before getting a new one).
7467 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007468 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007469 timeo = rpc_get_timeout(task->tk_client);
7470 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007471 now = jiffies;
7472 if (time_after(giveup, now)) {
7473 unsigned long delay;
7474
7475 /* Delay for:
7476 * - Not less then NFS4_POLL_RETRY_MIN.
7477 * - One last time a jiffie before we give up
7478 * - exponential backoff (time_now minus start_attempt)
7479 */
7480 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7481 min((giveup - now - 1),
7482 now - lgp->args.timestamp));
7483
7484 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7485 __func__, delay);
7486 rpc_delay(task, delay);
7487 task->tk_status = 0;
7488 rpc_restart_call_prepare(task);
7489 goto out; /* Do not call nfs4_async_handle_error() */
7490 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007491 break;
7492 case -NFS4ERR_EXPIRED:
7493 case -NFS4ERR_BAD_STATEID:
7494 spin_lock(&inode->i_lock);
7495 lo = NFS_I(inode)->layout;
7496 if (!lo || list_empty(&lo->plh_segs)) {
7497 spin_unlock(&inode->i_lock);
7498 /* If the open stateid was bad, then recover it. */
7499 state = lgp->args.ctx->state;
7500 } else {
7501 LIST_HEAD(head);
7502
7503 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7504 spin_unlock(&inode->i_lock);
7505 /* Mark the bad layout state as invalid, then
7506 * retry using the open stateid. */
7507 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007508 }
7509 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007510 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7511 rpc_restart_call_prepare(task);
7512out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007513 dprintk("<-- %s\n", __func__);
7514}
7515
Idan Kedar85541162012-08-02 11:47:10 +03007516static size_t max_response_pages(struct nfs_server *server)
7517{
7518 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7519 return nfs_page_array_len(0, max_resp_sz);
7520}
7521
7522static void nfs4_free_pages(struct page **pages, size_t size)
7523{
7524 int i;
7525
7526 if (!pages)
7527 return;
7528
7529 for (i = 0; i < size; i++) {
7530 if (!pages[i])
7531 break;
7532 __free_page(pages[i]);
7533 }
7534 kfree(pages);
7535}
7536
7537static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7538{
7539 struct page **pages;
7540 int i;
7541
7542 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7543 if (!pages) {
7544 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7545 return NULL;
7546 }
7547
7548 for (i = 0; i < size; i++) {
7549 pages[i] = alloc_page(gfp_flags);
7550 if (!pages[i]) {
7551 dprintk("%s: failed to allocate page\n", __func__);
7552 nfs4_free_pages(pages, size);
7553 return NULL;
7554 }
7555 }
7556
7557 return pages;
7558}
7559
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007560static void nfs4_layoutget_release(void *calldata)
7561{
7562 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007563 struct inode *inode = lgp->args.inode;
7564 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007565 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007566
7567 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007568 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007569 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007570 put_nfs_open_context(lgp->args.ctx);
7571 kfree(calldata);
7572 dprintk("<-- %s\n", __func__);
7573}
7574
7575static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7576 .rpc_call_prepare = nfs4_layoutget_prepare,
7577 .rpc_call_done = nfs4_layoutget_done,
7578 .rpc_release = nfs4_layoutget_release,
7579};
7580
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007581struct pnfs_layout_segment *
7582nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007583{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007584 struct inode *inode = lgp->args.inode;
7585 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007586 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007587 struct rpc_task *task;
7588 struct rpc_message msg = {
7589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7590 .rpc_argp = &lgp->args,
7591 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007592 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007593 };
7594 struct rpc_task_setup task_setup_data = {
7595 .rpc_client = server->client,
7596 .rpc_message = &msg,
7597 .callback_ops = &nfs4_layoutget_call_ops,
7598 .callback_data = lgp,
7599 .flags = RPC_TASK_ASYNC,
7600 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007601 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007602 int status = 0;
7603
7604 dprintk("--> %s\n", __func__);
7605
Idan Kedar85541162012-08-02 11:47:10 +03007606 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7607 if (!lgp->args.layout.pages) {
7608 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007609 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007610 }
7611 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007612 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007613
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007614 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007615 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007616 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007617
7618 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7619 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7620
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007621 task = rpc_run_task(&task_setup_data);
7622 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007623 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007624 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007625 if (status == 0)
7626 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007627 trace_nfs4_layoutget(lgp->args.ctx,
7628 &lgp->args.range,
7629 &lgp->res.range,
7630 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007631 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7632 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007633 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007634 rpc_put_task(task);
7635 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007636 if (status)
7637 return ERR_PTR(status);
7638 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007639}
7640
Benny Halevycbe82602011-05-22 19:52:37 +03007641static void
7642nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7643{
7644 struct nfs4_layoutreturn *lrp = calldata;
7645
7646 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007647 nfs41_setup_sequence(lrp->clp->cl_session,
7648 &lrp->args.seq_args,
7649 &lrp->res.seq_res,
7650 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007651}
7652
7653static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7654{
7655 struct nfs4_layoutreturn *lrp = calldata;
7656 struct nfs_server *server;
7657
7658 dprintk("--> %s\n", __func__);
7659
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007660 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007661 return;
7662
7663 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007664 switch (task->tk_status) {
7665 default:
7666 task->tk_status = 0;
7667 case 0:
7668 break;
7669 case -NFS4ERR_DELAY:
7670 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7671 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007672 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007673 return;
7674 }
Benny Halevycbe82602011-05-22 19:52:37 +03007675 dprintk("<-- %s\n", __func__);
7676}
7677
7678static void nfs4_layoutreturn_release(void *calldata)
7679{
7680 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007681 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007682
7683 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007684 spin_lock(&lo->plh_inode->i_lock);
7685 if (lrp->res.lrs_present)
7686 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7687 lo->plh_block_lgets--;
7688 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007689 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007690 kfree(calldata);
7691 dprintk("<-- %s\n", __func__);
7692}
7693
7694static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7695 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7696 .rpc_call_done = nfs4_layoutreturn_done,
7697 .rpc_release = nfs4_layoutreturn_release,
7698};
7699
7700int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7701{
7702 struct rpc_task *task;
7703 struct rpc_message msg = {
7704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7705 .rpc_argp = &lrp->args,
7706 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007707 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007708 };
7709 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007710 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007711 .rpc_message = &msg,
7712 .callback_ops = &nfs4_layoutreturn_call_ops,
7713 .callback_data = lrp,
7714 };
7715 int status;
7716
7717 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007718 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007719 task = rpc_run_task(&task_setup_data);
7720 if (IS_ERR(task))
7721 return PTR_ERR(task);
7722 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007723 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007724 dprintk("<-- %s status=%d\n", __func__, status);
7725 rpc_put_task(task);
7726 return status;
7727}
7728
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007729/*
7730 * Retrieve the list of Data Server devices from the MDS.
7731 */
7732static int _nfs4_getdevicelist(struct nfs_server *server,
7733 const struct nfs_fh *fh,
7734 struct pnfs_devicelist *devlist)
7735{
7736 struct nfs4_getdevicelist_args args = {
7737 .fh = fh,
7738 .layoutclass = server->pnfs_curr_ld->id,
7739 };
7740 struct nfs4_getdevicelist_res res = {
7741 .devlist = devlist,
7742 };
7743 struct rpc_message msg = {
7744 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7745 .rpc_argp = &args,
7746 .rpc_resp = &res,
7747 };
7748 int status;
7749
7750 dprintk("--> %s\n", __func__);
7751 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7752 &res.seq_res, 0);
7753 dprintk("<-- %s status=%d\n", __func__, status);
7754 return status;
7755}
7756
7757int nfs4_proc_getdevicelist(struct nfs_server *server,
7758 const struct nfs_fh *fh,
7759 struct pnfs_devicelist *devlist)
7760{
7761 struct nfs4_exception exception = { };
7762 int err;
7763
7764 do {
7765 err = nfs4_handle_exception(server,
7766 _nfs4_getdevicelist(server, fh, devlist),
7767 &exception);
7768 } while (exception.retry);
7769
7770 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7771 err, devlist->num_devs);
7772
7773 return err;
7774}
7775EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7776
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007777static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007778_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7779 struct pnfs_device *pdev,
7780 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007781{
7782 struct nfs4_getdeviceinfo_args args = {
7783 .pdev = pdev,
7784 };
7785 struct nfs4_getdeviceinfo_res res = {
7786 .pdev = pdev,
7787 };
7788 struct rpc_message msg = {
7789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7790 .rpc_argp = &args,
7791 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007792 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007793 };
7794 int status;
7795
7796 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007797 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007798 dprintk("<-- %s status=%d\n", __func__, status);
7799
7800 return status;
7801}
7802
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007803int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7804 struct pnfs_device *pdev,
7805 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007806{
7807 struct nfs4_exception exception = { };
7808 int err;
7809
7810 do {
7811 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007812 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007813 &exception);
7814 } while (exception.retry);
7815 return err;
7816}
7817EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7818
Andy Adamson863a3c62011-03-23 13:27:54 +00007819static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7820{
7821 struct nfs4_layoutcommit_data *data = calldata;
7822 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007823 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007824
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007825 nfs41_setup_sequence(session,
7826 &data->args.seq_args,
7827 &data->res.seq_res,
7828 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007829}
7830
7831static void
7832nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7833{
7834 struct nfs4_layoutcommit_data *data = calldata;
7835 struct nfs_server *server = NFS_SERVER(data->args.inode);
7836
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007837 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007838 return;
7839
7840 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007841 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7842 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7843 case -NFS4ERR_BADLAYOUT: /* no layout */
7844 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007845 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007846 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007847 break;
7848 default:
7849 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7850 rpc_restart_call_prepare(task);
7851 return;
7852 }
7853 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007854}
7855
7856static void nfs4_layoutcommit_release(void *calldata)
7857{
7858 struct nfs4_layoutcommit_data *data = calldata;
7859
Andy Adamsondb29c082011-07-30 20:52:38 -04007860 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007861 nfs_post_op_update_inode_force_wcc(data->args.inode,
7862 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007863 put_rpccred(data->cred);
7864 kfree(data);
7865}
7866
7867static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7868 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7869 .rpc_call_done = nfs4_layoutcommit_done,
7870 .rpc_release = nfs4_layoutcommit_release,
7871};
7872
7873int
Andy Adamsonef311532011-03-12 02:58:10 -05007874nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007875{
7876 struct rpc_message msg = {
7877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7878 .rpc_argp = &data->args,
7879 .rpc_resp = &data->res,
7880 .rpc_cred = data->cred,
7881 };
7882 struct rpc_task_setup task_setup_data = {
7883 .task = &data->task,
7884 .rpc_client = NFS_CLIENT(data->args.inode),
7885 .rpc_message = &msg,
7886 .callback_ops = &nfs4_layoutcommit_ops,
7887 .callback_data = data,
7888 .flags = RPC_TASK_ASYNC,
7889 };
7890 struct rpc_task *task;
7891 int status = 0;
7892
7893 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7894 "lbw: %llu inode %lu\n",
7895 data->task.tk_pid, sync,
7896 data->args.lastbytewritten,
7897 data->args.inode->i_ino);
7898
Chuck Levera9c92d62013-08-09 12:48:18 -04007899 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007900 task = rpc_run_task(&task_setup_data);
7901 if (IS_ERR(task))
7902 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007903 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007904 goto out;
7905 status = nfs4_wait_for_completion_rpc_task(task);
7906 if (status != 0)
7907 goto out;
7908 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007909 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007910out:
7911 dprintk("%s: status %d\n", __func__, status);
7912 rpc_put_task(task);
7913 return status;
7914}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007915
Andy Adamson97431202013-08-08 10:57:56 -04007916/**
7917 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7918 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7919 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007920static int
7921_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007922 struct nfs_fsinfo *info,
7923 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007924{
7925 struct nfs41_secinfo_no_name_args args = {
7926 .style = SECINFO_STYLE_CURRENT_FH,
7927 };
7928 struct nfs4_secinfo_res res = {
7929 .flavors = flavors,
7930 };
7931 struct rpc_message msg = {
7932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7933 .rpc_argp = &args,
7934 .rpc_resp = &res,
7935 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007936 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007937 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007938 int status;
7939
7940 if (use_integrity) {
7941 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007942 cred = nfs4_get_clid_cred(server->nfs_client);
7943 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007944 }
7945
7946 dprintk("--> %s\n", __func__);
7947 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7948 &res.seq_res, 0);
7949 dprintk("<-- %s status=%d\n", __func__, status);
7950
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007951 if (cred)
7952 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007953
7954 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007955}
7956
7957static int
7958nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7959 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7960{
7961 struct nfs4_exception exception = { };
7962 int err;
7963 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007964 /* first try using integrity protection */
7965 err = -NFS4ERR_WRONGSEC;
7966
7967 /* try to use integrity protection with machine cred */
7968 if (_nfs4_is_integrity_protected(server->nfs_client))
7969 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7970 flavors, true);
7971
7972 /*
7973 * if unable to use integrity protection, or SECINFO with
7974 * integrity protection returns NFS4ERR_WRONGSEC (which is
7975 * disallowed by spec, but exists in deployed servers) use
7976 * the current filesystem's rpc_client and the user cred.
7977 */
7978 if (err == -NFS4ERR_WRONGSEC)
7979 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7980 flavors, false);
7981
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007982 switch (err) {
7983 case 0:
7984 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007985 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007986 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007987 default:
7988 err = nfs4_handle_exception(server, err, &exception);
7989 }
7990 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007991out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007992 return err;
7993}
7994
7995static int
7996nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7997 struct nfs_fsinfo *info)
7998{
7999 int err;
8000 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008001 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008002 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008003 struct nfs4_secinfo4 *secinfo;
8004 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008005
8006 page = alloc_page(GFP_KERNEL);
8007 if (!page) {
8008 err = -ENOMEM;
8009 goto out;
8010 }
8011
8012 flavors = page_address(page);
8013 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8014
8015 /*
8016 * Fall back on "guess and check" method if
8017 * the server doesn't support SECINFO_NO_NAME
8018 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008019 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008020 err = nfs4_find_root_sec(server, fhandle, info);
8021 goto out_freepage;
8022 }
8023 if (err)
8024 goto out_freepage;
8025
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008026 for (i = 0; i < flavors->num_flavors; i++) {
8027 secinfo = &flavors->flavors[i];
8028
8029 switch (secinfo->flavor) {
8030 case RPC_AUTH_NULL:
8031 case RPC_AUTH_UNIX:
8032 case RPC_AUTH_GSS:
8033 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8034 &secinfo->flavor_info);
8035 break;
8036 default:
8037 flavor = RPC_AUTH_MAXFLAVOR;
8038 break;
8039 }
8040
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008041 if (!nfs_auth_info_match(&server->auth_info, flavor))
8042 flavor = RPC_AUTH_MAXFLAVOR;
8043
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008044 if (flavor != RPC_AUTH_MAXFLAVOR) {
8045 err = nfs4_lookup_root_sec(server, fhandle,
8046 info, flavor);
8047 if (!err)
8048 break;
8049 }
8050 }
8051
8052 if (flavor == RPC_AUTH_MAXFLAVOR)
8053 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008054
8055out_freepage:
8056 put_page(page);
8057 if (err == -EACCES)
8058 return -EPERM;
8059out:
8060 return err;
8061}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008062
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008063static int _nfs41_test_stateid(struct nfs_server *server,
8064 nfs4_stateid *stateid,
8065 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008066{
8067 int status;
8068 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008069 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008070 };
8071 struct nfs41_test_stateid_res res;
8072 struct rpc_message msg = {
8073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8074 .rpc_argp = &args,
8075 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008076 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008077 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008078 struct rpc_clnt *rpc_client = server->client;
8079
8080 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8081 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008082
Chuck Lever38527b12012-07-11 16:30:23 -04008083 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008084 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008085 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008086 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008087 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008088 if (status != NFS_OK) {
8089 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008090 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008091 }
8092 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008093 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008094}
8095
Chuck Lever38527b12012-07-11 16:30:23 -04008096/**
8097 * nfs41_test_stateid - perform a TEST_STATEID operation
8098 *
8099 * @server: server / transport on which to perform the operation
8100 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008101 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008102 *
8103 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8104 * Otherwise a negative NFS4ERR value is returned if the operation
8105 * failed or the state ID is not currently valid.
8106 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008107static int nfs41_test_stateid(struct nfs_server *server,
8108 nfs4_stateid *stateid,
8109 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008110{
8111 struct nfs4_exception exception = { };
8112 int err;
8113 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008114 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008115 if (err != -NFS4ERR_DELAY)
8116 break;
8117 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008118 } while (exception.retry);
8119 return err;
8120}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008121
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008122struct nfs_free_stateid_data {
8123 struct nfs_server *server;
8124 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008125 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008126};
8127
8128static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8129{
8130 struct nfs_free_stateid_data *data = calldata;
8131 nfs41_setup_sequence(nfs4_get_session(data->server),
8132 &data->args.seq_args,
8133 &data->res.seq_res,
8134 task);
8135}
8136
8137static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8138{
8139 struct nfs_free_stateid_data *data = calldata;
8140
8141 nfs41_sequence_done(task, &data->res.seq_res);
8142
8143 switch (task->tk_status) {
8144 case -NFS4ERR_DELAY:
8145 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8146 rpc_restart_call_prepare(task);
8147 }
8148}
8149
8150static void nfs41_free_stateid_release(void *calldata)
8151{
8152 kfree(calldata);
8153}
8154
Trond Myklebust17f26b12013-08-21 15:48:42 -04008155static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008156 .rpc_call_prepare = nfs41_free_stateid_prepare,
8157 .rpc_call_done = nfs41_free_stateid_done,
8158 .rpc_release = nfs41_free_stateid_release,
8159};
8160
8161static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8162 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008163 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008164 bool privileged)
8165{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008166 struct rpc_message msg = {
8167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008168 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008169 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008170 struct rpc_task_setup task_setup = {
8171 .rpc_client = server->client,
8172 .rpc_message = &msg,
8173 .callback_ops = &nfs41_free_stateid_ops,
8174 .flags = RPC_TASK_ASYNC,
8175 };
8176 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008177
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008178 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8179 &task_setup.rpc_client, &msg);
8180
Chuck Lever38527b12012-07-11 16:30:23 -04008181 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008182 data = kmalloc(sizeof(*data), GFP_NOFS);
8183 if (!data)
8184 return ERR_PTR(-ENOMEM);
8185 data->server = server;
8186 nfs4_stateid_copy(&data->args.stateid, stateid);
8187
8188 task_setup.callback_data = data;
8189
8190 msg.rpc_argp = &data->args;
8191 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008192 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008193 if (privileged)
8194 nfs4_set_sequence_privileged(&data->args.seq_args);
8195
8196 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008197}
8198
Chuck Lever38527b12012-07-11 16:30:23 -04008199/**
8200 * nfs41_free_stateid - perform a FREE_STATEID operation
8201 *
8202 * @server: server / transport on which to perform the operation
8203 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008204 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008205 *
8206 * Returns NFS_OK if the server freed "stateid". Otherwise a
8207 * negative NFS4ERR value is returned.
8208 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008209static int nfs41_free_stateid(struct nfs_server *server,
8210 nfs4_stateid *stateid,
8211 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008212{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008213 struct rpc_task *task;
8214 int ret;
8215
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008216 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008217 if (IS_ERR(task))
8218 return PTR_ERR(task);
8219 ret = rpc_wait_for_completion_task(task);
8220 if (!ret)
8221 ret = task->tk_status;
8222 rpc_put_task(task);
8223 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008224}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008225
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008226static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8227{
8228 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008229 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008230
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008231 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008232 nfs4_free_lock_state(server, lsp);
8233 if (IS_ERR(task))
8234 return PTR_ERR(task);
8235 rpc_put_task(task);
8236 return 0;
8237}
8238
Trond Myklebust36281ca2012-03-04 18:13:56 -05008239static bool nfs41_match_stateid(const nfs4_stateid *s1,
8240 const nfs4_stateid *s2)
8241{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008242 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008243 return false;
8244
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008245 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008246 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008247 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008248 return true;
8249
8250 return false;
8251}
8252
Andy Adamson557134a2009-04-01 09:21:53 -04008253#endif /* CONFIG_NFS_V4_1 */
8254
Trond Myklebust36281ca2012-03-04 18:13:56 -05008255static bool nfs4_match_stateid(const nfs4_stateid *s1,
8256 const nfs4_stateid *s2)
8257{
Trond Myklebustf597c532012-03-04 18:13:56 -05008258 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008259}
8260
8261
Trond Myklebust17280172012-03-11 13:11:00 -04008262static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008263 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008264 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008265 .recover_open = nfs4_open_reclaim,
8266 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008267 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008268 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269};
8270
Andy Adamson591d71c2009-04-01 09:22:47 -04008271#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008272static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008273 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8274 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8275 .recover_open = nfs4_open_reclaim,
8276 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008277 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008278 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008279 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008280};
8281#endif /* CONFIG_NFS_V4_1 */
8282
Trond Myklebust17280172012-03-11 13:11:00 -04008283static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008284 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008285 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008286 .recover_open = nfs4_open_expired,
8287 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008288 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008289};
8290
Andy Adamson591d71c2009-04-01 09:22:47 -04008291#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008292static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008293 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8294 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008295 .recover_open = nfs41_open_expired,
8296 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008297 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008298};
8299#endif /* CONFIG_NFS_V4_1 */
8300
Trond Myklebust17280172012-03-11 13:11:00 -04008301static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008302 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008303 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008304 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008305};
8306
8307#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008308static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008309 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008310 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008311 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008312};
8313#endif
8314
Chuck Leverec011fe2013-10-17 14:12:39 -04008315static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008316 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008317 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008318};
8319
8320#if defined(CONFIG_NFS_V4_1)
8321static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008322 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008323 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008324};
8325#endif /* CONFIG_NFS_V4_1 */
8326
Trond Myklebust97dc1352010-06-16 09:52:26 -04008327static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8328 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008329 .init_caps = NFS_CAP_READDIRPLUS
8330 | NFS_CAP_ATOMIC_OPEN
8331 | NFS_CAP_CHANGE_ATTR
8332 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008333 .init_client = nfs40_init_client,
8334 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008335 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008336 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008337 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008338 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008339 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8340 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8341 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008342 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008343};
8344
8345#if defined(CONFIG_NFS_V4_1)
8346static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8347 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008348 .init_caps = NFS_CAP_READDIRPLUS
8349 | NFS_CAP_ATOMIC_OPEN
8350 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008351 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008352 | NFS_CAP_STATEID_NFSV41
8353 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008354 .init_client = nfs41_init_client,
8355 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008356 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008357 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008358 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008359 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008360 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8361 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8362 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008363 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008364};
8365#endif
8366
Steve Dickson42c2c422013-05-22 12:50:38 -04008367#if defined(CONFIG_NFS_V4_2)
8368static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8369 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008370 .init_caps = NFS_CAP_READDIRPLUS
8371 | NFS_CAP_ATOMIC_OPEN
8372 | NFS_CAP_CHANGE_ATTR
8373 | NFS_CAP_POSIX_LOCK
8374 | NFS_CAP_STATEID_NFSV41
8375 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008376 .init_client = nfs41_init_client,
8377 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008378 .match_stateid = nfs41_match_stateid,
8379 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008380 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008381 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008382 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8383 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8384 .state_renewal_ops = &nfs41_state_renewal_ops,
8385};
8386#endif
8387
Trond Myklebust97dc1352010-06-16 09:52:26 -04008388const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8389 [0] = &nfs_v4_0_minor_ops,
8390#if defined(CONFIG_NFS_V4_1)
8391 [1] = &nfs_v4_1_minor_ops,
8392#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008393#if defined(CONFIG_NFS_V4_2)
8394 [2] = &nfs_v4_2_minor_ops,
8395#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008396};
8397
Trond Myklebust17f26b12013-08-21 15:48:42 -04008398static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008399 .create = nfs_create,
8400 .lookup = nfs_lookup,
8401 .atomic_open = nfs_atomic_open,
8402 .link = nfs_link,
8403 .unlink = nfs_unlink,
8404 .symlink = nfs_symlink,
8405 .mkdir = nfs_mkdir,
8406 .rmdir = nfs_rmdir,
8407 .mknod = nfs_mknod,
8408 .rename = nfs_rename,
8409 .permission = nfs_permission,
8410 .getattr = nfs_getattr,
8411 .setattr = nfs_setattr,
8412 .getxattr = generic_getxattr,
8413 .setxattr = generic_setxattr,
8414 .listxattr = generic_listxattr,
8415 .removexattr = generic_removexattr,
8416};
8417
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008418static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008419 .permission = nfs_permission,
8420 .getattr = nfs_getattr,
8421 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008422 .getxattr = generic_getxattr,
8423 .setxattr = generic_setxattr,
8424 .listxattr = generic_listxattr,
8425 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008426};
8427
David Howells509de812006-08-22 20:06:11 -04008428const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008429 .version = 4, /* protocol version */
8430 .dentry_ops = &nfs4_dentry_operations,
8431 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008432 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008433 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008434 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008435 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008436 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008437 .getattr = nfs4_proc_getattr,
8438 .setattr = nfs4_proc_setattr,
8439 .lookup = nfs4_proc_lookup,
8440 .access = nfs4_proc_access,
8441 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 .create = nfs4_proc_create,
8443 .remove = nfs4_proc_remove,
8444 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008445 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008446 .unlink_done = nfs4_proc_unlink_done,
8447 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04008448 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008449 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008450 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008451 .link = nfs4_proc_link,
8452 .symlink = nfs4_proc_symlink,
8453 .mkdir = nfs4_proc_mkdir,
8454 .rmdir = nfs4_proc_remove,
8455 .readdir = nfs4_proc_readdir,
8456 .mknod = nfs4_proc_mknod,
8457 .statfs = nfs4_proc_statfs,
8458 .fsinfo = nfs4_proc_fsinfo,
8459 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008460 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008461 .decode_dirent = nfs4_decode_dirent,
8462 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008463 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008464 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008465 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008466 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008467 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008468 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008469 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008470 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008471 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008472 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008474 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008475 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008476 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008477 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008478 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008479 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008480 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008481 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008482 .create_server = nfs4_create_server,
8483 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008484};
8485
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008486static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8487 .prefix = XATTR_NAME_NFSV4_ACL,
8488 .list = nfs4_xattr_list_nfs4_acl,
8489 .get = nfs4_xattr_get_nfs4_acl,
8490 .set = nfs4_xattr_set_nfs4_acl,
8491};
8492
8493const struct xattr_handler *nfs4_xattr_handlers[] = {
8494 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008495#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8496 &nfs4_xattr_nfs4_label_handler,
8497#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008498 NULL
8499};
8500
Linus Torvalds1da177e2005-04-16 15:20:36 -07008501/*
8502 * Local variables:
8503 * c-basic-offset: 8
8504 * End:
8505 */