blob: 1bf291f03c619dd023cd17d47faefabbf568e218 [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
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebustcdd4e682006-01-03 09:55:12 +010074struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010075static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080076static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050078static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050079static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040080static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
81static 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 -040082static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
83 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040084 struct nfs4_state *state, struct nfs4_label *ilabel,
85 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040086#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040087static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
88 struct rpc_cred *);
89static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040091#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040092
93#ifdef CONFIG_NFS_V4_SECURITY_LABEL
94static inline struct nfs4_label *
95nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
96 struct iattr *sattr, struct nfs4_label *label)
97{
98 int err;
99
100 if (label == NULL)
101 return NULL;
102
103 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
104 return NULL;
105
106 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
107 return NULL;
108
109 err = security_dentry_init_security(dentry, sattr->ia_mode,
110 &dentry->d_name, (void **)&label->label, &label->len);
111 if (err == 0)
112 return label;
113
114 return NULL;
115}
116static inline void
117nfs4_label_release_security(struct nfs4_label *label)
118{
119 if (label)
120 security_release_secctx(label->label, label->len);
121}
122static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
123{
124 if (label)
125 return server->attr_bitmask;
126
127 return server->attr_bitmask_nl;
128}
129#else
130static inline struct nfs4_label *
131nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
132 struct iattr *sattr, struct nfs4_label *l)
133{ return NULL; }
134static inline void
135nfs4_label_release_security(struct nfs4_label *label)
136{ return; }
137static inline u32 *
138nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
139{ return server->attr_bitmask; }
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500143static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Trond Myklebust52567b02009-10-23 14:46:42 -0400145 if (err >= -1000)
146 return err;
147 switch (err) {
148 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500149 case -NFS4ERR_LAYOUTTRYLATER:
150 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400151 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000152 case -NFS4ERR_WRONGSEC:
153 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;
Trond Myklebust03391692010-01-26 15:42:38 -0500384#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400385 case -NFS4ERR_BADSESSION:
386 case -NFS4ERR_BADSLOT:
387 case -NFS4ERR_BAD_HIGH_SLOT:
388 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
389 case -NFS4ERR_DEADSESSION:
390 case -NFS4ERR_SEQ_FALSE_RETRY:
391 case -NFS4ERR_SEQ_MISORDERED:
392 dprintk("%s ERROR: %d Reset session\n", __func__,
393 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400394 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400395 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500396#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500397 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500398 if (exception->timeout > HZ) {
399 /* We have retried a decent amount, time to
400 * fail
401 */
402 ret = -EBUSY;
403 break;
404 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_GRACE:
406 case -NFS4ERR_DELAY:
407 ret = nfs4_delay(server->client, &exception->timeout);
408 if (ret != 0)
409 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400410 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 case -NFS4ERR_OLD_STATEID:
412 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800413 break;
414 case -NFS4ERR_BADOWNER:
415 /* The following works around a Linux server bug! */
416 case -NFS4ERR_BADNAME:
417 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
418 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
419 exception->retry = 1;
420 printk(KERN_WARNING "NFS: v4 server %s "
421 "does not accept raw "
422 "uid/gids. "
423 "Reenabling the idmapper.\n",
424 server->nfs_client->cl_hostname);
425 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 }
427 /* We failed to handle the error */
428 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500429wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500430 ret = nfs4_wait_clnt_recover(clp);
431 if (ret == 0)
432 exception->retry = 1;
433 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500434}
435
436
Trond Myklebust452e9352010-07-31 14:29:06 -0400437static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 spin_lock(&clp->cl_lock);
440 if (time_before(clp->cl_last_renewal,timestamp))
441 clp->cl_last_renewal = timestamp;
442 spin_unlock(&clp->cl_lock);
443}
444
Trond Myklebust452e9352010-07-31 14:29:06 -0400445static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
446{
447 do_renew_lease(server->nfs_client, timestamp);
448}
449
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400450#if defined(CONFIG_NFS_V4_1)
451
Trond Myklebustd185a332010-06-16 09:52:25 -0400452static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400453{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500454 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400455 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500456 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400457
Benny Halevydfb4f3092010-09-24 09:17:01 -0400458 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400459 /* just wake up the next guy waiting since
460 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500461 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500462 return;
Andy Adamson13615872009-04-01 09:22:17 -0400463 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500464 tbl = res->sr_slot->table;
465 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500466
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500467 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500468 /* Be nice to the server: try to ensure that the last transmitted
469 * value for highest_user_slotid <= target_highest_slotid
470 */
471 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
472 send_new_highest_used_slotid = true;
473
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500474 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
475 send_new_highest_used_slotid = false;
476 goto out_unlock;
477 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500478 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500479
480 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
481 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500482out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500483 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400484 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500485 if (send_new_highest_used_slotid)
486 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400487}
488
Trond Myklebust14516c32010-07-31 14:29:06 -0400489static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500491 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500492 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400493 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500494 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500495 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400496
Bryan Schumaker468f8612011-04-18 15:57:32 -0400497 /* don't increment the sequence number if the task wasn't sent */
498 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400499 goto out;
500
Trond Myklebust933602e2012-11-16 12:12:38 -0500501 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500502 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500503
Trond Myklebustac20d162012-12-15 15:36:07 -0500504 if (slot->interrupted) {
505 slot->interrupted = 0;
506 interrupted = true;
507 }
508
Andy Adamson691daf32009-12-04 15:55:39 -0500509 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400510 switch (res->sr_status) {
511 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500513 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500514 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500515 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500516 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500517 if (res->sr_status_flags != 0)
518 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500519 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400520 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500521 case 1:
522 /*
523 * sr_status remains 1 if an RPC level error occurred.
524 * The server may or may not have processed the sequence
525 * operation..
526 * Mark the slot as having hosted an interrupted RPC call.
527 */
528 slot->interrupted = 1;
529 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400530 case -NFS4ERR_DELAY:
531 /* The server detected a resend of the RPC call and
532 * returned NFS4ERR_DELAY as per Section 2.10.6.2
533 * of RFC5661.
534 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500535 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400536 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500537 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500538 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400539 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500540 case -NFS4ERR_BADSLOT:
541 /*
542 * The slot id we used was probably retired. Try again
543 * using a different slot id.
544 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500545 goto retry_nowait;
546 case -NFS4ERR_SEQ_MISORDERED:
547 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500548 * Was the last operation on this sequence interrupted?
549 * If so, retry after bumping the sequence number.
550 */
551 if (interrupted) {
552 ++slot->seq_nr;
553 goto retry_nowait;
554 }
555 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500556 * Could this slot have been previously retired?
557 * If so, then the server may be expecting seq_nr = 1!
558 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500559 if (slot->seq_nr != 1) {
560 slot->seq_nr = 1;
561 goto retry_nowait;
562 }
563 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500564 case -NFS4ERR_SEQ_FALSE_RETRY:
565 ++slot->seq_nr;
566 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400567 default:
568 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500569 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400570 }
571out:
572 /* The session may be reset by one of the error handlers. */
573 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400574 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500575 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500576retry_nowait:
577 if (rpc_restart_call_prepare(task)) {
578 task->tk_status = 0;
579 ret = 0;
580 }
581 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400582out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400583 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400584 goto out;
585 rpc_delay(task, NFS4_POLL_RETRY_MAX);
586 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400587}
588
Trond Myklebust14516c32010-07-31 14:29:06 -0400589static int nfs4_sequence_done(struct rpc_task *task,
590 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400591{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500592 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400593 return 1;
594 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400595}
596
Trond Myklebust9d12b212012-01-17 22:04:25 -0500597static void nfs41_init_sequence(struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res, int cache_reply)
599{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500600 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500601 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400602 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500603 if (cache_reply)
604 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500605 res->sr_slot = NULL;
606}
607
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400608static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
609{
610 args->sa_privileged = 1;
611}
612
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000613int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 struct rpc_task *task)
617{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400618 struct nfs4_slot *slot;
619 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400620
621 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400623 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400624 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400626 tbl = &session->fc_slot_table;
627
Trond Myklebust69d206b2012-11-22 13:21:02 -0500628 task->tk_timeout = 0;
629
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400630 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400631 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400632 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500633 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500634 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400635 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400636 }
637
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500638 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500639 if (IS_ERR(slot)) {
640 /* If out of memory, try again in 1/4 second */
641 if (slot == ERR_PTR(-ENOMEM))
642 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400643 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400644 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400645 }
646 spin_unlock(&tbl->slot_tbl_lock);
647
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500648 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400649
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500650 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
651 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652
Benny Halevydfb4f3092010-09-24 09:17:01 -0400653 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500654 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400655 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400656 /*
657 * sr_status is only set in decode_sequence, and so will remain
658 * set to 1 if an rpc level failure occurs.
659 */
660 res->sr_status = 1;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400661out_success:
662 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400664out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400665 /* Privileged tasks are queued with top priority */
666 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400667 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
668 NULL, RPC_PRIORITY_PRIVILEGED);
669 else
670 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400671 spin_unlock(&tbl->slot_tbl_lock);
672 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000674EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675
Trond Myklebust035168a2010-06-16 09:52:26 -0400676int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 struct nfs4_sequence_args *args,
678 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679 struct rpc_task *task)
680{
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 int ret = 0;
683
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400684 if (session == NULL) {
685 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400687 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400688
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500689 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400690 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500691 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400692
Trond Myklebust9d12b212012-01-17 22:04:25 -0500693 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400694out:
695 dprintk("<-- %s status=%d\n", __func__, ret);
696 return ret;
697}
698
699struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400700 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 struct nfs4_sequence_args *seq_args;
702 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703};
704
705static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
706{
707 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400708 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400709
Trond Myklebust035168a2010-06-16 09:52:26 -0400710 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
711
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400712 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713}
714
Andy Adamson69ab40c2009-04-01 09:22:19 -0400715static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
716{
717 struct nfs41_call_sync_data *data = calldata;
718
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400720}
721
Trond Myklebust17280172012-03-11 13:11:00 -0400722static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400723 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400724 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400725};
726
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400731 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400732{
733 int ret;
734 struct rpc_task *task;
735 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400736 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400737 .seq_args = args,
738 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400739 };
740 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000741 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400742 .rpc_message = msg,
743 .callback_ops = &nfs41_call_sync_ops,
744 .callback_data = &data
745 };
746
Andy Adamsonce5039c2009-04-01 09:22:13 -0400747 task = rpc_run_task(&task_setup);
748 if (IS_ERR(task))
749 ret = PTR_ERR(task);
750 else {
751 ret = task->tk_status;
752 rpc_put_task(task);
753 }
754 return ret;
755}
756
Trond Myklebustdf896452010-06-16 09:52:26 -0400757#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400758static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500759void nfs41_init_sequence(struct nfs4_sequence_args *args,
760 struct nfs4_sequence_res *res, int cache_reply)
761{
762}
763
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400764static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
765{
766}
767
768
Trond Myklebust14516c32010-07-31 14:29:06 -0400769static int nfs4_sequence_done(struct rpc_task *task,
770 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400771{
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400773}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400774#endif /* CONFIG_NFS_V4_1 */
775
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400776static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000777int _nfs4_call_sync(struct rpc_clnt *clnt,
778 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400779 struct rpc_message *msg,
780 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400781 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400782{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000783 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400784}
785
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400786static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787int nfs4_call_sync(struct rpc_clnt *clnt,
788 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000789 struct rpc_message *msg,
790 struct nfs4_sequence_args *args,
791 struct nfs4_sequence_res *res,
792 int cache_reply)
793{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400794 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000795 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400796 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000797}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400798
Trond Myklebust38478b22006-05-25 01:40:57 -0400799static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Trond Myklebust38478b22006-05-25 01:40:57 -0400801 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Trond Myklebust38478b22006-05-25 01:40:57 -0400803 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400804 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700805 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400806 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700807 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000808 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400809 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 struct nfs_openargs o_arg;
815 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100816 struct nfs_open_confirmargs c_arg;
817 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500818 struct nfs4_string owner_name;
819 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400821 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400823 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400825 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100827 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400828 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400829 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100830 int rpc_status;
831 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832};
833
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400834static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
835 int err, struct nfs4_exception *exception)
836{
837 if (err != -EINVAL)
838 return false;
839 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
840 return false;
841 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
842 exception->retry = 1;
843 return true;
844}
845
846static enum open_claim_type4
847nfs4_map_atomic_open_claim(struct nfs_server *server,
848 enum open_claim_type4 claim)
849{
850 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
851 return claim;
852 switch (claim) {
853 default:
854 return claim;
855 case NFS4_OPEN_CLAIM_FH:
856 return NFS4_OPEN_CLAIM_NULL;
857 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
858 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
859 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
860 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
861 }
862}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400863
864static void nfs4_init_opendata_res(struct nfs4_opendata *p)
865{
866 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400867 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400868 p->o_res.seqid = p->o_arg.seqid;
869 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400870 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400871 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400872 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500873 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400874}
875
Al Viro82a2c1b2011-06-22 18:30:55 -0400876static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400878 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400879 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400880 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400881 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100882{
Al Viro82a2c1b2011-06-22 18:30:55 -0400883 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100884 struct inode *dir = parent->d_inode;
885 struct nfs_server *server = NFS_SERVER(dir);
886 struct nfs4_opendata *p;
887
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400888 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100889 if (p == NULL)
890 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400891
892 p->f_label = nfs4_label_alloc(server, gfp_mask);
893 if (IS_ERR(p->f_label))
894 goto err_free_p;
895
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400896 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100897 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400898 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400899 nfs_sb_active(dentry->d_sb);
900 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 p->dir = parent;
902 p->owner = sp;
903 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 p->o_arg.open_flags = flags;
905 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700906 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
907 * will return permission denied for all bits until close */
908 if (!(flags & O_EXCL)) {
909 /* ask server to check for all possible rights as results
910 * are cached */
911 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
912 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700913 }
David Howells7539bba2006-08-22 20:06:09 -0400914 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400915 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
916 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400917 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100918 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400919 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400920 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400921 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400922 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
923 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400924 case NFS4_OPEN_CLAIM_NULL:
925 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
926 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
927 p->o_arg.fh = NFS_FH(dir);
928 break;
929 case NFS4_OPEN_CLAIM_PREVIOUS:
930 case NFS4_OPEN_CLAIM_FH:
931 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
932 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
933 p->o_arg.fh = NFS_FH(dentry->d_inode);
934 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500935 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500936 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400937
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100938 p->o_arg.u.attrs = &p->attrs;
939 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500940
941 verf[0] = jiffies;
942 verf[1] = current->pid;
943 memcpy(p->o_arg.u.verifier.data, verf,
944 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100945 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100946 p->c_arg.fh = &p->o_res.fh;
947 p->c_arg.stateid = &p->o_res.stateid;
948 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400949 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400950 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100951 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400952
953err_free_label:
954 nfs4_label_free(p->f_label);
955err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100956 kfree(p);
957err:
958 dput(parent);
959 return NULL;
960}
961
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400962static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100963{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400964 struct nfs4_opendata *p = container_of(kref,
965 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400966 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400967
968 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969 if (p->state != NULL)
970 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400971 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400972
973 nfs4_label_free(p->f_label);
974
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400975 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400976 dput(p->dentry);
977 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500978 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400979 kfree(p);
980}
981
982static void nfs4_opendata_put(struct nfs4_opendata *p)
983{
984 if (p != NULL)
985 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100986}
987
Trond Myklebust06f814a2006-01-03 09:55:07 +0100988static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
989{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100990 int ret;
991
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100993 return ret;
994}
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997{
998 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999
Trond Myklebust536e43d2012-01-17 22:04:26 -05001000 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 goto out;
1002 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001003 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001004 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1005 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001006 break;
1007 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001008 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1009 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 break;
1011 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001012 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1013 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 return ret;
1017}
1018
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001021 if (delegation == NULL)
1022 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001025 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001027 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1028 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001029 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 return 1;
1031}
1032
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001034{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001035 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001036 case FMODE_WRITE:
1037 state->n_wronly++;
1038 break;
1039 case FMODE_READ:
1040 state->n_rdonly++;
1041 break;
1042 case FMODE_READ|FMODE_WRITE:
1043 state->n_rdwr++;
1044 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001046}
1047
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001048static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001049{
1050 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001051 nfs4_stateid_copy(&state->stateid, stateid);
1052 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001053 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001055 case FMODE_READ:
1056 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1057 break;
1058 case FMODE_WRITE:
1059 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1060 break;
1061 case FMODE_READ|FMODE_WRITE:
1062 set_bit(NFS_O_RDWR_STATE, &state->flags);
1063 }
1064}
1065
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001067{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001068 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001070 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001071}
1072
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073static 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 -07001074{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001075 /*
1076 * Protect the call to nfs4_state_set_mode_locked and
1077 * serialise the stateid update
1078 */
1079 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001080 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001081 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001082 set_bit(NFS_DELEGATED_STATE, &state->flags);
1083 }
1084 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001085 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001086 write_sequnlock(&state->seqlock);
1087 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001089 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092static 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 -05001093{
1094 struct nfs_inode *nfsi = NFS_I(state->inode);
1095 struct nfs_delegation *deleg_cur;
1096 int ret = 0;
1097
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001099
1100 rcu_read_lock();
1101 deleg_cur = rcu_dereference(nfsi->delegation);
1102 if (deleg_cur == NULL)
1103 goto no_delegation;
1104
1105 spin_lock(&deleg_cur->lock);
1106 if (nfsi->delegation != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001107 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001109 goto no_delegation_unlock;
1110
1111 if (delegation == NULL)
1112 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001113 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001114 goto no_delegation_unlock;
1115
Trond Myklebustb7391f42008-12-23 15:21:52 -05001116 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001117 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001118 ret = 1;
1119no_delegation_unlock:
1120 spin_unlock(&deleg_cur->lock);
1121no_delegation:
1122 rcu_read_unlock();
1123
1124 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001126 ret = 1;
1127 }
1128
1129 return ret;
1130}
1131
1132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001134{
1135 struct nfs_delegation *delegation;
1136
1137 rcu_read_lock();
1138 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001140 rcu_read_unlock();
1141 return;
1142 }
1143 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001144 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001145}
1146
Trond Myklebust6ee41262007-07-08 14:11:36 -04001147static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001148{
1149 struct nfs4_state *state = opendata->state;
1150 struct nfs_inode *nfsi = NFS_I(state->inode);
1151 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001152 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001154 nfs4_stateid stateid;
1155 int ret = -EAGAIN;
1156
Trond Myklebustaac00a82007-07-05 19:02:21 -04001157 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001158 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001159 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 if (can_open_cached(state, fmode, open_mode)) {
1161 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001162 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001163 goto out_return_state;
1164 }
1165 spin_unlock(&state->owner->so_lock);
1166 }
Trond Myklebust34310432008-12-23 15:21:38 -05001167 rcu_read_lock();
1168 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001169 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001170 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001171 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001172 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001173 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001174 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001175 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001176 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001177 if (!opendata->is_recover) {
1178 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1179 if (ret != 0)
1180 goto out;
1181 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001182 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001183
1184 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001186 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001187 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001188out:
1189 return ERR_PTR(ret);
1190out_return_state:
1191 atomic_inc(&state->count);
1192 return state;
1193}
1194
Andy Adamsone23008e2012-10-02 21:07:32 -04001195static void
1196nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1197{
1198 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1199 struct nfs_delegation *delegation;
1200 int delegation_flags = 0;
1201
1202 rcu_read_lock();
1203 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1204 if (delegation)
1205 delegation_flags = delegation->flags;
1206 rcu_read_unlock();
1207 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1208 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1209 "returning a delegation for "
1210 "OPEN(CLAIM_DELEGATE_CUR)\n",
1211 clp->cl_hostname);
1212 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1213 nfs_inode_set_delegation(state->inode,
1214 data->owner->so_cred,
1215 &data->o_res);
1216 else
1217 nfs_inode_reclaim_delegation(state->inode,
1218 data->owner->so_cred,
1219 &data->o_res);
1220}
1221
1222/*
1223 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1224 * and update the nfs4_state.
1225 */
1226static struct nfs4_state *
1227_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1228{
1229 struct inode *inode = data->state->inode;
1230 struct nfs4_state *state = data->state;
1231 int ret;
1232
1233 if (!data->rpc_done) {
1234 ret = data->rpc_status;
1235 goto err;
1236 }
1237
1238 ret = -ESTALE;
1239 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1240 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1241 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1242 goto err;
1243
1244 ret = -ENOMEM;
1245 state = nfs4_get_open_state(inode, data->owner);
1246 if (state == NULL)
1247 goto err;
1248
1249 ret = nfs_refresh_inode(inode, &data->f_attr);
1250 if (ret)
1251 goto err;
1252
David Quigleyaa9c2662013-05-22 12:50:44 -04001253 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1254
Andy Adamsone23008e2012-10-02 21:07:32 -04001255 if (data->o_res.delegation_type != 0)
1256 nfs4_opendata_check_deleg(data, state);
1257 update_open_stateid(state, &data->o_res.stateid, NULL,
1258 data->o_arg.fmode);
1259
1260 return state;
1261err:
1262 return ERR_PTR(ret);
1263
1264}
1265
1266static struct nfs4_state *
1267_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001268{
1269 struct inode *inode;
1270 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001271 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001272
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001274 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275 goto out;
1276 }
1277
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001278 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001279 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001280 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001281 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001283 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 goto err;
1285 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001286 state = nfs4_get_open_state(inode, data->owner);
1287 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001289 if (data->o_res.delegation_type != 0)
1290 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001291 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001292 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001293 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001294out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001295 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001296 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001297err_put_inode:
1298 iput(inode);
1299err:
1300 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001301}
1302
Andy Adamsone23008e2012-10-02 21:07:32 -04001303static struct nfs4_state *
1304nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1305{
1306 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1307 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1308 return _nfs4_opendata_to_nfs4_state(data);
1309}
1310
Trond Myklebust864472e2006-01-03 09:55:15 +01001311static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1312{
1313 struct nfs_inode *nfsi = NFS_I(state->inode);
1314 struct nfs_open_context *ctx;
1315
1316 spin_lock(&state->inode->i_lock);
1317 list_for_each_entry(ctx, &nfsi->open_files, list) {
1318 if (ctx->state != state)
1319 continue;
1320 get_nfs_open_context(ctx);
1321 spin_unlock(&state->inode->i_lock);
1322 return ctx;
1323 }
1324 spin_unlock(&state->inode->i_lock);
1325 return ERR_PTR(-ENOENT);
1326}
1327
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001328static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1329 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001330{
1331 struct nfs4_opendata *opendata;
1332
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001333 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001334 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001335 if (opendata == NULL)
1336 return ERR_PTR(-ENOMEM);
1337 opendata->state = state;
1338 atomic_inc(&state->count);
1339 return opendata;
1340}
1341
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001343{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001344 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001345 int ret;
1346
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347 opendata->o_arg.open_flags = 0;
1348 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001349 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1350 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1351 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001352 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001353 if (ret != 0)
1354 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001355 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 if (IS_ERR(newstate))
1357 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001358 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001360 return 0;
1361}
1362
1363static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1364{
Trond Myklebust864472e2006-01-03 09:55:15 +01001365 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001366 int ret;
1367
1368 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001369 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001370 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001371 smp_rmb();
1372 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001373 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001374 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 if (ret != 0)
1376 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001377 if (newstate != state)
1378 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001379 }
1380 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001381 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001382 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 if (ret != 0)
1384 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001385 if (newstate != state)
1386 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 }
1388 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001389 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001390 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 if (ret != 0)
1392 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001393 if (newstate != state)
1394 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001396 /*
1397 * We may have performed cached opens for all three recoveries.
1398 * Check if we need to update the current stateid.
1399 */
1400 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001401 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001402 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001403 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001404 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001405 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001406 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001407 return 0;
1408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/*
1411 * OPEN_RECLAIM:
1412 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001414static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001416 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001417 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001418 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int status;
1420
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001421 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1422 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001423 if (IS_ERR(opendata))
1424 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001425 rcu_read_lock();
1426 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001427 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001428 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001430 opendata->o_arg.u.delegation_type = delegation_type;
1431 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001432 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 return status;
1434}
1435
Trond Myklebust539cd032007-06-05 11:46:42 -04001436static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
1438 struct nfs_server *server = NFS_SERVER(state->inode);
1439 struct nfs4_exception exception = { };
1440 int err;
1441 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001442 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001443 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1444 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001445 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001446 break;
1447 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 } while (exception.retry);
1449 return err;
1450}
1451
Trond Myklebust864472e2006-01-03 09:55:15 +01001452static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1453{
1454 struct nfs_open_context *ctx;
1455 int ret;
1456
1457 ctx = nfs4_state_find_open_context(state);
1458 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001459 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001460 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001461 put_nfs_open_context(ctx);
1462 return ret;
1463}
1464
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001465static 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 -07001466{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001467 switch (err) {
1468 default:
1469 printk(KERN_ERR "NFS: %s: unhandled error "
1470 "%d.\n", __func__, err);
1471 case 0:
1472 case -ENOENT:
1473 case -ESTALE:
1474 break;
1475 case -NFS4ERR_BADSESSION:
1476 case -NFS4ERR_BADSLOT:
1477 case -NFS4ERR_BAD_HIGH_SLOT:
1478 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1479 case -NFS4ERR_DEADSESSION:
1480 set_bit(NFS_DELEGATED_STATE, &state->flags);
1481 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1482 return -EAGAIN;
1483 case -NFS4ERR_STALE_CLIENTID:
1484 case -NFS4ERR_STALE_STATEID:
1485 set_bit(NFS_DELEGATED_STATE, &state->flags);
1486 case -NFS4ERR_EXPIRED:
1487 /* Don't recall a delegation if it was lost */
1488 nfs4_schedule_lease_recovery(server->nfs_client);
1489 return -EAGAIN;
1490 case -NFS4ERR_DELEG_REVOKED:
1491 case -NFS4ERR_ADMIN_REVOKED:
1492 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001493 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001494 nfs_inode_find_state_and_recover(state->inode,
1495 stateid);
1496 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001497 return 0;
1498 case -NFS4ERR_DELAY:
1499 case -NFS4ERR_GRACE:
1500 set_bit(NFS_DELEGATED_STATE, &state->flags);
1501 ssleep(1);
1502 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001503 case -ENOMEM:
1504 case -NFS4ERR_DENIED:
1505 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1506 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 return err;
1509}
1510
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001511int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1512{
1513 struct nfs_server *server = NFS_SERVER(state->inode);
1514 struct nfs4_opendata *opendata;
1515 int err;
1516
1517 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1518 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1519 if (IS_ERR(opendata))
1520 return PTR_ERR(opendata);
1521 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1522 err = nfs4_open_recover(opendata, state);
1523 nfs4_opendata_put(opendata);
1524 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1525}
1526
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001527static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1528{
1529 struct nfs4_opendata *data = calldata;
1530
1531 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001532 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001533 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001534 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001535 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001536 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001537 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001538}
1539
1540static void nfs4_open_confirm_release(void *calldata)
1541{
1542 struct nfs4_opendata *data = calldata;
1543 struct nfs4_state *state = NULL;
1544
1545 /* If this request hasn't been cancelled, do nothing */
1546 if (data->cancelled == 0)
1547 goto out_free;
1548 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001549 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001550 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001551 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001552 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001553 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001555 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001556}
1557
1558static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559 .rpc_call_done = nfs4_open_confirm_done,
1560 .rpc_release = nfs4_open_confirm_release,
1561};
1562
1563/*
1564 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1565 */
1566static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1567{
1568 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1569 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001570 struct rpc_message msg = {
1571 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1572 .rpc_argp = &data->c_arg,
1573 .rpc_resp = &data->c_res,
1574 .rpc_cred = data->owner->so_cred,
1575 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001576 struct rpc_task_setup task_setup_data = {
1577 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001578 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001579 .callback_ops = &nfs4_open_confirm_ops,
1580 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001581 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001582 .flags = RPC_TASK_ASYNC,
1583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int status;
1585
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001587 data->rpc_done = 0;
1588 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001589 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001590 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001591 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001592 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001593 status = nfs4_wait_for_completion_rpc_task(task);
1594 if (status != 0) {
1595 data->cancelled = 1;
1596 smp_wmb();
1597 } else
1598 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001599 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return status;
1601}
1602
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 struct nfs4_opendata *data = calldata;
1606 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001607 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001608
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001610 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001611 /*
1612 * Check if we still need to send an OPEN call, or if we can use
1613 * a delegation instead.
1614 */
1615 if (data->state != NULL) {
1616 struct nfs_delegation *delegation;
1617
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001618 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001619 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001620 rcu_read_lock();
1621 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001622 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001623 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001624 can_open_delegated(delegation, data->o_arg.fmode))
1625 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001626 rcu_read_unlock();
1627 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001628 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001629 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001630 switch (data->o_arg.claim) {
1631 case NFS4_OPEN_CLAIM_PREVIOUS:
1632 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1633 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001634 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001635 case NFS4_OPEN_CLAIM_FH:
1636 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001637 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1638 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001639 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001640 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001641 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001642 &data->o_res.seq_res,
1643 task) != 0)
1644 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001645
1646 /* Set the create mode (note dependency on the session type) */
1647 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1648 if (data->o_arg.open_flags & O_EXCL) {
1649 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1650 if (nfs4_has_persistent_session(clp))
1651 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1652 else if (clp->cl_mvops->minor_version > 0)
1653 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1654 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001655 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001656unlock_no_action:
1657 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658out_no_action:
1659 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001660out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001661 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001662}
1663
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001664static void nfs4_open_done(struct rpc_task *task, void *calldata)
1665{
1666 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001668 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001669
Trond Myklebust14516c32010-07-31 14:29:06 -04001670 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1671 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001672
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001674 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1675 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001676 case S_IFREG:
1677 break;
1678 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001679 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001680 break;
1681 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001683 break;
1684 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001686 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001687 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001688 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001689 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1690 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001691 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001692 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001693}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001694
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695static void nfs4_open_release(void *calldata)
1696{
1697 struct nfs4_opendata *data = calldata;
1698 struct nfs4_state *state = NULL;
1699
1700 /* If this request hasn't been cancelled, do nothing */
1701 if (data->cancelled == 0)
1702 goto out_free;
1703 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001704 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001705 goto out_free;
1706 /* In case we need an open_confirm, no cleanup! */
1707 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1708 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001710 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001711 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001712out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001713 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714}
1715
1716static const struct rpc_call_ops nfs4_open_ops = {
1717 .rpc_call_prepare = nfs4_open_prepare,
1718 .rpc_call_done = nfs4_open_done,
1719 .rpc_release = nfs4_open_release,
1720};
1721
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001722static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001723{
1724 struct inode *dir = data->dir->d_inode;
1725 struct nfs_server *server = NFS_SERVER(dir);
1726 struct nfs_openargs *o_arg = &data->o_arg;
1727 struct nfs_openres *o_res = &data->o_res;
1728 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001729 struct rpc_message msg = {
1730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1731 .rpc_argp = o_arg,
1732 .rpc_resp = o_res,
1733 .rpc_cred = data->owner->so_cred,
1734 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001735 struct rpc_task_setup task_setup_data = {
1736 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001737 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001738 .callback_ops = &nfs4_open_ops,
1739 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001740 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001741 .flags = RPC_TASK_ASYNC,
1742 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001743 int status;
1744
Trond Myklebust9d12b212012-01-17 22:04:25 -05001745 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001746 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001747 data->rpc_done = 0;
1748 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001749 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001750 data->is_recover = 0;
1751 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001752 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001753 data->is_recover = 1;
1754 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001755 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001756 if (IS_ERR(task))
1757 return PTR_ERR(task);
1758 status = nfs4_wait_for_completion_rpc_task(task);
1759 if (status != 0) {
1760 data->cancelled = 1;
1761 smp_wmb();
1762 } else
1763 status = data->rpc_status;
1764 rpc_put_task(task);
1765
1766 return status;
1767}
1768
1769static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1770{
1771 struct inode *dir = data->dir->d_inode;
1772 struct nfs_openres *o_res = &data->o_res;
1773 int status;
1774
1775 status = nfs4_run_open_task(data, 1);
1776 if (status != 0 || !data->rpc_done)
1777 return status;
1778
Trond Myklebust6926afd2012-01-07 13:22:46 -05001779 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1780
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001781 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1782 status = _nfs4_proc_open_confirm(data);
1783 if (status != 0)
1784 return status;
1785 }
1786
1787 return status;
1788}
1789
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001790static int nfs4_opendata_access(struct rpc_cred *cred,
1791 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001792 struct nfs4_state *state, fmode_t fmode,
1793 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001794{
1795 struct nfs_access_entry cache;
1796 u32 mask;
1797
1798 /* access call failed or for some reason the server doesn't
1799 * support any access modes -- defer access call until later */
1800 if (opendata->o_res.access_supported == 0)
1801 return 0;
1802
1803 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001804 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001805 * write mode bits, but POSIX allows the creating process to write.
1806 * use openflags to check for exec, because fmode won't
1807 * always have FMODE_EXEC set when file open for exec. */
1808 if (openflags & __FMODE_EXEC) {
1809 /* ONLY check for exec rights */
1810 mask = MAY_EXEC;
1811 } else if (fmode & FMODE_READ)
1812 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001813
1814 cache.cred = cred;
1815 cache.jiffies = jiffies;
1816 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1817 nfs_access_add_cache(state->inode, &cache);
1818
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001819 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001820 return 0;
1821
1822 /* even though OPEN succeeded, access is denied. Close the file */
1823 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001824 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001825}
1826
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001827/*
1828 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1829 */
1830static int _nfs4_proc_open(struct nfs4_opendata *data)
1831{
1832 struct inode *dir = data->dir->d_inode;
1833 struct nfs_server *server = NFS_SERVER(dir);
1834 struct nfs_openargs *o_arg = &data->o_arg;
1835 struct nfs_openres *o_res = &data->o_res;
1836 int status;
1837
1838 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001839 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001840 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001841 if (status != 0) {
1842 if (status == -NFS4ERR_BADNAME &&
1843 !(o_arg->open_flags & O_CREAT))
1844 return -ENOENT;
1845 return status;
1846 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847
Trond Myklebust6926afd2012-01-07 13:22:46 -05001848 nfs_fattr_map_and_free_names(server, &data->f_attr);
1849
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001850 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001851 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001852 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1853 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001855 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001860 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001861 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862}
1863
Andy Adamsond83217c2011-03-01 01:34:17 +00001864static int nfs4_recover_expired_lease(struct nfs_server *server)
1865{
1866 return nfs4_client_recover_expired_lease(server->nfs_client);
1867}
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869/*
1870 * OPEN_EXPIRED:
1871 * reclaim state on the server after a network partition.
1872 * Assumes caller holds the appropriate lock
1873 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001874static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001876 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001877 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001879 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001880 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001881 if (IS_ERR(opendata))
1882 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001883 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001884 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001885 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001886 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001887 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001890static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001891{
Trond Myklebust539cd032007-06-05 11:46:42 -04001892 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001893 struct nfs4_exception exception = { };
1894 int err;
1895
1896 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 err = _nfs4_open_expired(ctx, state);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001898 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1899 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001900 switch (err) {
1901 default:
1902 goto out;
1903 case -NFS4ERR_GRACE:
1904 case -NFS4ERR_DELAY:
1905 nfs4_handle_exception(server, err, &exception);
1906 err = 0;
1907 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001908 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001909out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001910 return err;
1911}
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1914{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001916 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Trond Myklebust864472e2006-01-03 09:55:15 +01001918 ctx = nfs4_state_find_open_context(state);
1919 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001920 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001921 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 put_nfs_open_context(ctx);
1923 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001926#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001927static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001928{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001929 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001930 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001931 struct nfs_delegation *delegation;
1932 struct rpc_cred *cred = NULL;
1933 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001934
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001935 /* If a state reset has been done, test_stateid is unneeded */
1936 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1937 return;
1938
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001939 /* Get the delegation credential for use by test/free_stateid */
1940 rcu_read_lock();
1941 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1942 if (delegation != NULL &&
1943 nfs4_stateid_match(&delegation->stateid, stateid)) {
1944 cred = get_rpccred(delegation->cred);
1945 rcu_read_unlock();
1946 status = nfs41_test_stateid(server, stateid, cred);
1947 } else
1948 rcu_read_unlock();
1949
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001950 if (status != NFS_OK) {
1951 /* Free the stateid unless the server explicitly
1952 * informs us the stateid is unrecognized. */
1953 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001954 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001955 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001956
Bryan Schumaker69388672012-09-26 15:25:52 -04001957 write_seqlock(&state->seqlock);
1958 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1959 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001960 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1961 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001962
1963 if (cred != NULL)
1964 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001965}
1966
1967/**
1968 * nfs41_check_open_stateid - possibly free an open stateid
1969 *
1970 * @state: NFSv4 state for an inode
1971 *
1972 * Returns NFS_OK if recovery for this stateid is now finished.
1973 * Otherwise a negative NFS4ERR value is returned.
1974 */
1975static int nfs41_check_open_stateid(struct nfs4_state *state)
1976{
1977 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001978 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001979 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001980 int status;
1981
1982 /* If a state reset has been done, test_stateid is unneeded */
1983 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1984 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1985 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1986 return -NFS4ERR_BAD_STATEID;
1987
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001988 status = nfs41_test_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001989 if (status != NFS_OK) {
1990 /* Free the stateid unless the server explicitly
1991 * informs us the stateid is unrecognized. */
1992 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001993 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001994
1995 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1996 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1997 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001998 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001999 }
2000 return status;
2001}
2002
2003static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2004{
Chuck Levereb64cf92012-07-11 16:30:05 -04002005 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002006
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002007 nfs41_clear_delegation_stateid(state);
2008 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002009 if (status != NFS_OK)
2010 status = nfs4_open_expired(sp, state);
2011 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002012}
2013#endif
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002016 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2017 * fields corresponding to attributes that were used to store the verifier.
2018 * Make sure we clobber those fields in the later setattr call
2019 */
2020static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2021{
2022 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2023 !(sattr->ia_valid & ATTR_ATIME_SET))
2024 sattr->ia_valid |= ATTR_ATIME;
2025
2026 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2027 !(sattr->ia_valid & ATTR_MTIME_SET))
2028 sattr->ia_valid |= ATTR_MTIME;
2029}
2030
Trond Myklebustc21443c2013-02-07 14:26:21 -05002031static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2032 fmode_t fmode,
2033 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002034 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002035{
2036 struct nfs4_state_owner *sp = opendata->owner;
2037 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002038 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002039 struct nfs4_state *state;
2040 unsigned int seq;
2041 int ret;
2042
2043 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2044
2045 ret = _nfs4_proc_open(opendata);
2046 if (ret != 0)
2047 goto out;
2048
2049 state = nfs4_opendata_to_nfs4_state(opendata);
2050 ret = PTR_ERR(state);
2051 if (IS_ERR(state))
2052 goto out;
2053 if (server->caps & NFS_CAP_POSIX_LOCK)
2054 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2055
Trond Myklebust275bb302013-05-29 13:11:28 -04002056 dentry = opendata->dentry;
2057 if (dentry->d_inode == NULL) {
2058 /* FIXME: Is this d_drop() ever needed? */
2059 d_drop(dentry);
2060 dentry = d_add_unique(dentry, igrab(state->inode));
2061 if (dentry == NULL) {
2062 dentry = opendata->dentry;
2063 } else if (dentry != ctx->dentry) {
2064 dput(ctx->dentry);
2065 ctx->dentry = dget(dentry);
2066 }
2067 nfs_set_verifier(dentry,
2068 nfs_save_change_attribute(opendata->dir->d_inode));
2069 }
2070
Trond Myklebustc21443c2013-02-07 14:26:21 -05002071 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2072 if (ret != 0)
2073 goto out;
2074
Trond Myklebust3efb9722013-05-29 13:17:04 -04002075 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002076 if (dentry->d_inode == state->inode) {
2077 nfs_inode_attach_open_context(ctx);
2078 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2079 nfs4_schedule_stateid_recovery(server, state);
2080 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002081out:
2082 return ret;
2083}
2084
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002085/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002086 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 */
Andy Adamson82be4172012-05-23 05:02:35 -04002088static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002089 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002090 int flags,
2091 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002092 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 struct nfs4_state_owner *sp;
2095 struct nfs4_state *state = NULL;
2096 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002097 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002098 struct dentry *dentry = ctx->dentry;
2099 struct rpc_cred *cred = ctx->cred;
2100 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2101 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002102 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002103 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002104 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002108 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2109 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2111 goto out_err;
2112 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002113 status = nfs4_recover_expired_lease(server);
2114 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002115 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002116 if (dentry->d_inode != NULL)
2117 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002118 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002119 if (dentry->d_inode)
2120 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002121 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002122 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002123 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002124 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
David Quigley14c43f72013-05-22 12:50:43 -04002126 if (label) {
2127 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2128 if (IS_ERR(olabel)) {
2129 status = PTR_ERR(olabel);
2130 goto err_opendata_put;
2131 }
2132 }
2133
Andy Adamson82be4172012-05-23 05:02:35 -04002134 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2135 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2136 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002137 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002138 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002139 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002140 if (dentry->d_inode != NULL)
2141 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002142
Trond Myklebust3efb9722013-05-29 13:17:04 -04002143 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002144 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002145 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002146 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002147
Trond Myklebust549b19c2013-04-16 18:42:34 -04002148 if ((opendata->o_arg.open_flags & O_EXCL) &&
2149 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002150 nfs4_exclusive_attrset(opendata, sattr);
2151
2152 nfs_fattr_init(opendata->o_res.f_attr);
2153 status = nfs4_do_setattr(state->inode, cred,
2154 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002155 state, label, olabel);
2156 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002157 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002158 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002159 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002160 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002161 }
Andy Adamson82be4172012-05-23 05:02:35 -04002162
2163 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2164 *ctx_th = opendata->f_attr.mdsthreshold;
2165 else
2166 kfree(opendata->f_attr.mdsthreshold);
2167 opendata->f_attr.mdsthreshold = NULL;
2168
David Quigley14c43f72013-05-22 12:50:43 -04002169 nfs4_label_free(olabel);
2170
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002171 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002174err_free_label:
2175 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002176err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002177 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002178 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002179err_put_state_owner:
2180 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return status;
2183}
2184
2185
Andy Adamson82be4172012-05-23 05:02:35 -04002186static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002187 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002188 int flags,
2189 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002190 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002192 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct nfs4_exception exception = { };
2194 struct nfs4_state *res;
2195 int status;
2196
2197 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002198 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002199 res = ctx->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (status == 0)
2201 break;
2202 /* NOTE: BAD_SEQID means the server and client disagree about the
2203 * book-keeping w.r.t. state-changing operations
2204 * (OPEN/CLOSE/LOCK/LOCKU...)
2205 * It is actually a sign of a bug on the client or on the server.
2206 *
2207 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002208 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 * have unhashed the old state_owner for us, and that we can
2210 * therefore safely retry using a new one. We should still warn
2211 * the user though...
2212 */
2213 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002214 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002215 " returned a bad sequence-id error!\n",
2216 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 exception.retry = 1;
2218 continue;
2219 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002220 /*
2221 * BAD_STATEID on OPEN means that the server cancelled our
2222 * state before it received the OPEN_CONFIRM.
2223 * Recover by retrying the request as per the discussion
2224 * on Page 181 of RFC3530.
2225 */
2226 if (status == -NFS4ERR_BAD_STATEID) {
2227 exception.retry = 1;
2228 continue;
2229 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002230 if (status == -EAGAIN) {
2231 /* We must have found a delegation */
2232 exception.retry = 1;
2233 continue;
2234 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002235 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2236 continue;
2237 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 status, &exception));
2239 } while (exception.retry);
2240 return res;
2241}
2242
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002243static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2244 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002245 struct nfs4_state *state, struct nfs4_label *ilabel,
2246 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002248 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002250 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 .iap = sattr,
2252 .server = server,
2253 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002254 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 };
2256 struct nfs_setattrres res = {
2257 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002258 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .server = server,
2260 };
2261 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2263 .rpc_argp = &arg,
2264 .rpc_resp = &res,
2265 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002267 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002268 fmode_t fmode;
2269 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002270 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
David Quigleyaa9c2662013-05-22 12:50:44 -04002272 arg.bitmask = nfs4_bitmask(server, ilabel);
2273 if (ilabel)
2274 arg.bitmask = nfs4_bitmask(server, olabel);
2275
Trond Myklebust0e574af2005-10-27 22:12:38 -04002276 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Trond Myklebustee3ae842013-04-29 10:35:36 -04002278 /* Servers should only apply open mode checks for file size changes */
2279 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2280 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2281
2282 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2283 /* Use that stateid */
2284 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002285 struct nfs_lockowner lockowner = {
2286 .l_owner = current->files,
2287 .l_pid = current->tgid,
2288 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002289 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002290 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002291 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002292 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Bryan Schumaker7c513052011-03-24 17:12:24 +00002294 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002295 if (status == 0 && state != NULL)
2296 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002297 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002300static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2301 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002302 struct nfs4_state *state, struct nfs4_label *ilabel,
2303 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002305 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002306 struct nfs4_exception exception = {
2307 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002308 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002309 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 int err;
2311 do {
David Quigley1775fd32013-05-22 12:50:42 -04002312 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002313 switch (err) {
2314 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002315 if (!(sattr->ia_valid & ATTR_SIZE)) {
2316 pr_warn_once("NFSv4: server %s is incorrectly "
2317 "applying open mode checks to "
2318 "a SETATTR that is not "
2319 "changing file size.\n",
2320 server->nfs_client->cl_hostname);
2321 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002322 if (state && !(state->state & FMODE_WRITE)) {
2323 err = -EBADF;
2324 if (sattr->ia_valid & ATTR_OPEN)
2325 err = -EACCES;
2326 goto out;
2327 }
2328 }
2329 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002331out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return err;
2333}
2334
2335struct nfs4_closedata {
2336 struct inode *inode;
2337 struct nfs4_state *state;
2338 struct nfs_closeargs arg;
2339 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002340 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002341 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002342 bool roc;
2343 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344};
2345
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002346static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002347{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002348 struct nfs4_closedata *calldata = data;
2349 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002350 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002351
Fred Isamanf7e89172011-01-06 11:36:32 +00002352 if (calldata->roc)
2353 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002354 nfs4_put_open_state(calldata->state);
2355 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002356 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002357 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002358 kfree(calldata);
2359}
2360
Trond Myklebust88069f72009-12-08 08:33:16 -05002361static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2362 fmode_t fmode)
2363{
2364 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002365 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002366 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2367 case FMODE_WRITE:
2368 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2369 break;
2370 case FMODE_READ:
2371 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2372 break;
2373 case 0:
2374 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2375 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2376 clear_bit(NFS_OPEN_STATE, &state->flags);
2377 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002378 spin_unlock(&state->owner->so_lock);
2379}
2380
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002381static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002383 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct nfs_server *server = NFS_SERVER(calldata->inode);
2386
Chuck Levera3ca5652012-03-01 17:00:40 -05002387 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002388 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2389 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 /* hmm. we are done with the inode, and in the process of freeing
2391 * the state_owner. we keep this around to process errors
2392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 switch (task->tk_status) {
2394 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002395 if (calldata->roc)
2396 pnfs_roc_set_barrier(state->inode,
2397 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002398 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002399 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002400 nfs4_close_clear_stateid_flags(state,
2401 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 break;
2403 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002404 case -NFS4ERR_OLD_STATEID:
2405 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002407 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002410 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2411 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002413 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002414 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002415 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002418static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002420 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002421 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002422 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002423 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002424
Chuck Levera3ca5652012-03-01 17:00:40 -05002425 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002426 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002427 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002428
Trond Myklebust88069f72009-12-08 08:33:16 -05002429 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2430 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002431 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002432 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002433 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002434 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002435 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2436 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2437 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002438 }
2439 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002440 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2441 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2442 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002443 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002444 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002445 if (!nfs4_valid_open_stateid(state))
2446 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002447 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002448
2449 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002450 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002451 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002452 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002453
Fred Isamanf7e89172011-01-06 11:36:32 +00002454 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002455 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002456 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002457 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2458 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002459 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002460 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002461 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002462
Trond Myklebust516a6af2005-10-27 22:12:41 -04002463 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002464 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002465 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002466 &calldata->arg.seq_args,
2467 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002468 task) != 0)
2469 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002470 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002471 return;
2472out_no_action:
2473 task->tk_action = NULL;
2474out_wait:
2475 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002478static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002479 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002480 .rpc_call_done = nfs4_close_done,
2481 .rpc_release = nfs4_free_closedata,
2482};
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484/*
2485 * It is possible for data to be read/written from a mem-mapped file
2486 * after the sys_close call (which hits the vfs layer as a flush).
2487 * This means that we can't safely call nfsv4 close on a file until
2488 * the inode is cleared. This in turn means that we are not good
2489 * NFSv4 citizens - we do not indicate to the server to update the file's
2490 * share state even when we are done with one of the three share
2491 * stateid's in the inode.
2492 *
2493 * NOTE: Caller must be holding the sp->so_owner semaphore!
2494 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002495int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002497 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002499 struct nfs4_state_owner *sp = state->owner;
2500 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002501 struct rpc_message msg = {
2502 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2503 .rpc_cred = state->owner->so_cred,
2504 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002505 struct rpc_task_setup task_setup_data = {
2506 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002507 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002508 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002509 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002510 .flags = RPC_TASK_ASYNC,
2511 };
Trond Myklebust95121352005-10-18 14:20:12 -07002512 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002514 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002516 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002517 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002518 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002520 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002521 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002523 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002524 if (calldata->arg.seqid == NULL)
2525 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002526 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002527 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002528 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002529 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002530 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002531 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002532 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002533
Trond Myklebust1174dd12010-12-21 10:52:24 -05002534 msg.rpc_argp = &calldata->arg;
2535 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002536 task_setup_data.callback_data = calldata;
2537 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002538 if (IS_ERR(task))
2539 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002540 status = 0;
2541 if (wait)
2542 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002543 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002544 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002545out_free_calldata:
2546 kfree(calldata);
2547out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002548 nfs4_put_open_state(state);
2549 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002550 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Trond Myklebust2b484292010-09-17 10:56:51 -04002553static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002554nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002557 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2558
2559 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Trond Myklebust565277f2007-10-15 18:17:53 -04002561 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002562 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002563
2564 nfs4_label_release_security(label);
2565
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002566 if (IS_ERR(state))
2567 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002568 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Trond Myklebust1185a552009-12-03 15:54:02 -05002571static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002572{
2573 if (ctx->state == NULL)
2574 return;
2575 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002576 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002577 else
Al Viro643168c2011-06-22 18:20:23 -04002578 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002579}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2582{
Benny Halevy43652ad2009-04-01 09:21:54 -04002583 struct nfs4_server_caps_arg args = {
2584 .fhandle = fhandle,
2585 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 struct nfs4_server_caps_res res = {};
2587 struct rpc_message msg = {
2588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002589 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 .rpc_resp = &res,
2591 };
2592 int status;
2593
Bryan Schumaker7c513052011-03-24 17:12:24 +00002594 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (status == 0) {
2596 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002597 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2598 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2599 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2600 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2601 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2603 server->caps |= NFS_CAP_ACLS;
2604 if (res.has_links != 0)
2605 server->caps |= NFS_CAP_HARDLINKS;
2606 if (res.has_symlinks != 0)
2607 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002608 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2609 server->caps |= NFS_CAP_FILEID;
2610 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2611 server->caps |= NFS_CAP_MODE;
2612 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2613 server->caps |= NFS_CAP_NLINK;
2614 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2615 server->caps |= NFS_CAP_OWNER;
2616 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2617 server->caps |= NFS_CAP_OWNER_GROUP;
2618 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2619 server->caps |= NFS_CAP_ATIME;
2620 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2621 server->caps |= NFS_CAP_CTIME;
2622 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2623 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002624#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2625 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2626 server->caps |= NFS_CAP_SECURITY_LABEL;
2627#endif
2628 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2629 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002630
David Quigleyaa9c2662013-05-22 12:50:44 -04002631 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2632 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2633 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2634 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002635 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2636 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2637 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002639 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return status;
2643}
2644
Trond Myklebust55a97592006-06-09 09:34:19 -04002645int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
2647 struct nfs4_exception exception = { };
2648 int err;
2649 do {
2650 err = nfs4_handle_exception(server,
2651 _nfs4_server_capabilities(server, fhandle),
2652 &exception);
2653 } while (exception.retry);
2654 return err;
2655}
2656
2657static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2658 struct nfs_fsinfo *info)
2659{
David Quigleyaa9c2662013-05-22 12:50:44 -04002660 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002662 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
2664 struct nfs4_lookup_res res = {
2665 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002666 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 .fh = fhandle,
2668 };
2669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2671 .rpc_argp = &args,
2672 .rpc_resp = &res,
2673 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002674
David Quigleyaa9c2662013-05-22 12:50:44 -04002675 bitmask[0] = nfs4_fattr_bitmap[0];
2676 bitmask[1] = nfs4_fattr_bitmap[1];
2677 /*
2678 * Process the label in the upcoming getfattr
2679 */
2680 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2681
Trond Myklebust0e574af2005-10-27 22:12:38 -04002682 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002683 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2687 struct nfs_fsinfo *info)
2688{
2689 struct nfs4_exception exception = { };
2690 int err;
2691 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002692 err = _nfs4_lookup_root(server, fhandle, info);
2693 switch (err) {
2694 case 0:
2695 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002696 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002697 default:
2698 err = nfs4_handle_exception(server, err, &exception);
2699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002701out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 return err;
2703}
2704
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002705static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2706 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2707{
2708 struct rpc_auth *auth;
2709 int ret;
2710
2711 auth = rpcauth_create(flavor, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002712 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002713 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002714 goto out;
2715 }
2716 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002717out:
2718 return ret;
2719}
2720
Chuck Lever9a744ba2013-03-16 15:56:02 -04002721/*
2722 * Retry pseudoroot lookup with various security flavors. We do this when:
2723 *
2724 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2725 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2726 *
2727 * Returns zero on success, or a negative NFS4ERR value, or a
2728 * negative errno value.
2729 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002730static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002731 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002733 /* Per 3530bis 15.33.5 */
2734 static const rpc_authflavor_t flav_array[] = {
2735 RPC_AUTH_GSS_KRB5P,
2736 RPC_AUTH_GSS_KRB5I,
2737 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002738 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002739 RPC_AUTH_NULL,
2740 };
2741 int status = -EPERM;
2742 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Chuck Lever9a744ba2013-03-16 15:56:02 -04002744 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002745 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002746 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002747 continue;
2748 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002749 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002750
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002751 /*
2752 * -EACCESS could mean that the user doesn't have correct permissions
2753 * to access the mount. It could also mean that we tried to mount
2754 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2755 * existing mount programs don't handle -EACCES very well so it should
2756 * be mapped to -EPERM instead.
2757 */
2758 if (status == -EACCES)
2759 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002760 return status;
2761}
2762
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002763static int nfs4_do_find_root_sec(struct nfs_server *server,
2764 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2765{
2766 int mv = server->nfs_client->cl_minorversion;
2767 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2768}
2769
2770/**
2771 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2772 * @server: initialized nfs_server handle
2773 * @fhandle: we fill in the pseudo-fs root file handle
2774 * @info: we fill in an FSINFO struct
2775 *
2776 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002777 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002778int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2779 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002780{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002781 int status;
2782
2783 status = nfs4_lookup_root(server, fhandle, info);
2784 if ((status == -NFS4ERR_WRONGSEC) &&
2785 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2786 status = nfs4_do_find_root_sec(server, fhandle, info);
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (status == 0)
2789 status = nfs4_server_capabilities(server, fhandle);
2790 if (status == 0)
2791 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002792
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002793 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794}
2795
Bryan Schumakerbae36242012-05-10 15:07:31 -04002796static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2797 struct nfs_fsinfo *info)
2798{
2799 int error;
2800 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002801 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002802
2803 error = nfs4_server_capabilities(server, mntfh);
2804 if (error < 0) {
2805 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2806 return error;
2807 }
2808
David Quigley14c43f72013-05-22 12:50:43 -04002809 label = nfs4_label_alloc(server, GFP_KERNEL);
2810 if (IS_ERR(label))
2811 return PTR_ERR(label);
2812
David Quigley1775fd32013-05-22 12:50:42 -04002813 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002814 if (error < 0) {
2815 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002816 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002817 }
2818
2819 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2820 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2821 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2822
David Quigley14c43f72013-05-22 12:50:43 -04002823err_free_label:
2824 nfs4_label_free(label);
2825
Bryan Schumakerbae36242012-05-10 15:07:31 -04002826 return error;
2827}
2828
Manoj Naik6b97fd32006-06-09 09:34:29 -04002829/*
2830 * Get locations and (maybe) other attributes of a referral.
2831 * Note that we'll actually follow the referral later when
2832 * we detect fsid mismatch in inode revalidation
2833 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002834static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2835 const struct qstr *name, struct nfs_fattr *fattr,
2836 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002837{
2838 int status = -ENOMEM;
2839 struct page *page = NULL;
2840 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002841
2842 page = alloc_page(GFP_KERNEL);
2843 if (page == NULL)
2844 goto out;
2845 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2846 if (locations == NULL)
2847 goto out;
2848
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002849 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002850 if (status != 0)
2851 goto out;
2852 /* Make sure server returned a different fsid for the referral */
2853 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002854 dprintk("%s: server did not return a different fsid for"
2855 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002856 status = -EIO;
2857 goto out;
2858 }
Andy Adamson533eb462011-06-13 18:25:56 -04002859 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2860 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002861
Andy Adamson533eb462011-06-13 18:25:56 -04002862 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002863 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002864 memset(fhandle, 0, sizeof(struct nfs_fh));
2865out:
2866 if (page)
2867 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002868 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002869 return status;
2870}
2871
David Quigley1775fd32013-05-22 12:50:42 -04002872static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2873 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 struct nfs4_getattr_arg args = {
2876 .fh = fhandle,
2877 .bitmask = server->attr_bitmask,
2878 };
2879 struct nfs4_getattr_res res = {
2880 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002881 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 .server = server,
2883 };
2884 struct rpc_message msg = {
2885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2886 .rpc_argp = &args,
2887 .rpc_resp = &res,
2888 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002889
2890 args.bitmask = nfs4_bitmask(server, label);
2891
Trond Myklebust0e574af2005-10-27 22:12:38 -04002892 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002893 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
David Quigley1775fd32013-05-22 12:50:42 -04002896static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2897 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct nfs4_exception exception = { };
2900 int err;
2901 do {
2902 err = nfs4_handle_exception(server,
David Quigley1775fd32013-05-22 12:50:42 -04002903 _nfs4_proc_getattr(server, fhandle, fattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 &exception);
2905 } while (exception.retry);
2906 return err;
2907}
2908
2909/*
2910 * The file is not closed if it is opened due to the a request to change
2911 * the size of the file. The open call will not be needed once the
2912 * VFS layer lookup-intents are implemented.
2913 *
2914 * Close is called when the inode is destroyed.
2915 * If we haven't opened the file for O_WRONLY, we
2916 * need to in the size_change case to obtain a stateid.
2917 *
2918 * Got race?
2919 * Because OPEN is always done by name in nfsv4, it is
2920 * possible that we opened a different file by the same
2921 * name. We can recognize this race condition, but we
2922 * can't do anything about it besides returning an error.
2923 *
2924 * This will be fixed with VFS changes (lookup-intent).
2925 */
2926static int
2927nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2928 struct iattr *sattr)
2929{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002930 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002931 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002932 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002933 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 int status;
2935
Benny Halevy8a1636c2010-07-14 15:43:57 -04002936 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002937 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002938
Trond Myklebust0e574af2005-10-27 22:12:38 -04002939 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Andy Adamson26699402012-05-30 16:12:24 -04002941 /* Deal with open(O_TRUNC) */
2942 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002943 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002944
2945 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002946 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002947 return 0;
2948
Trond Myklebustd5308382005-11-04 15:33:38 -05002949 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002950 if (sattr->ia_valid & ATTR_FILE) {
2951 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002952
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002953 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002954 if (ctx) {
2955 cred = ctx->cred;
2956 state = ctx->state;
2957 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002958 }
2959
David Quigley14c43f72013-05-22 12:50:43 -04002960 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2961 if (IS_ERR(label))
2962 return PTR_ERR(label);
2963
2964 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002965 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002966 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002967 nfs_setsecurity(inode, fattr, label);
2968 }
David Quigley14c43f72013-05-22 12:50:43 -04002969 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 return status;
2971}
2972
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002973static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2974 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002975 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002976{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002977 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002978 int status;
2979 struct nfs4_lookup_arg args = {
2980 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002981 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002982 .name = name,
2983 };
2984 struct nfs4_lookup_res res = {
2985 .server = server,
2986 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04002987 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04002988 .fh = fhandle,
2989 };
2990 struct rpc_message msg = {
2991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2992 .rpc_argp = &args,
2993 .rpc_resp = &res,
2994 };
2995
David Quigleyaa9c2662013-05-22 12:50:44 -04002996 args.bitmask = nfs4_bitmask(server, label);
2997
David Howells2b3de442006-08-22 20:06:09 -04002998 nfs_fattr_init(fattr);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003001 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 dprintk("NFS reply lookup: %d\n", status);
3003 return status;
3004}
3005
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003006static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003007{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3011 fattr->nlink = 2;
3012}
3013
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003014static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3015 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003016 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003017{
3018 struct nfs4_exception exception = { };
3019 struct rpc_clnt *client = *clnt;
3020 int err;
3021 do {
David Quigley1775fd32013-05-22 12:50:42 -04003022 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003023 switch (err) {
3024 case -NFS4ERR_BADNAME:
3025 err = -ENOENT;
3026 goto out;
3027 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003028 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003029 goto out;
3030 case -NFS4ERR_WRONGSEC:
3031 err = -EPERM;
3032 if (client != *clnt)
3033 goto out;
3034
3035 client = nfs4_create_sec_client(client, dir, name);
3036 if (IS_ERR(client))
3037 return PTR_ERR(client);
3038
3039 exception.retry = 1;
3040 break;
3041 default:
3042 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3043 }
3044 } while (exception.retry);
3045
3046out:
3047 if (err == 0)
3048 *clnt = client;
3049 else if (client != *clnt)
3050 rpc_shutdown_client(client);
3051
3052 return err;
3053}
3054
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003055static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003056 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3057 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003059 int status;
3060 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003061
David Quigley1775fd32013-05-22 12:50:42 -04003062 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003063 if (client != NFS_CLIENT(dir)) {
3064 rpc_shutdown_client(client);
3065 nfs_fixup_secinfo_attributes(fattr);
3066 }
3067 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003070struct rpc_clnt *
3071nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3072 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3073{
3074 int status;
3075 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3076
David Quigley1775fd32013-05-22 12:50:42 -04003077 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003078 if (status < 0) {
3079 rpc_shutdown_client(client);
3080 return ERR_PTR(status);
3081 }
3082 return client;
3083}
3084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3086{
Trond Myklebust76b32992007-08-10 17:45:11 -04003087 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 struct nfs4_accessargs args = {
3089 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003090 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003092 struct nfs4_accessres res = {
3093 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003094 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 struct rpc_message msg = {
3096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3097 .rpc_argp = &args,
3098 .rpc_resp = &res,
3099 .rpc_cred = entry->cred,
3100 };
3101 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003102 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 /*
3105 * Determine which access bits we want to ask for...
3106 */
3107 if (mode & MAY_READ)
3108 args.access |= NFS4_ACCESS_READ;
3109 if (S_ISDIR(inode->i_mode)) {
3110 if (mode & MAY_WRITE)
3111 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3112 if (mode & MAY_EXEC)
3113 args.access |= NFS4_ACCESS_LOOKUP;
3114 } else {
3115 if (mode & MAY_WRITE)
3116 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3117 if (mode & MAY_EXEC)
3118 args.access |= NFS4_ACCESS_EXECUTE;
3119 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003120
3121 res.fattr = nfs_alloc_fattr();
3122 if (res.fattr == NULL)
3123 return -ENOMEM;
3124
Bryan Schumaker7c513052011-03-24 17:12:24 +00003125 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003127 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003128 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003130 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return status;
3132}
3133
3134static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3135{
3136 struct nfs4_exception exception = { };
3137 int err;
3138 do {
3139 err = nfs4_handle_exception(NFS_SERVER(inode),
3140 _nfs4_proc_access(inode, entry),
3141 &exception);
3142 } while (exception.retry);
3143 return err;
3144}
3145
3146/*
3147 * TODO: For the time being, we don't try to get any attributes
3148 * along with any of the zero-copy operations READ, READDIR,
3149 * READLINK, WRITE.
3150 *
3151 * In the case of the first three, we want to put the GETATTR
3152 * after the read-type operation -- this is because it is hard
3153 * to predict the length of a GETATTR response in v4, and thus
3154 * align the READ data correctly. This means that the GETATTR
3155 * may end up partially falling into the page cache, and we should
3156 * shift it into the 'tail' of the xdr_buf before processing.
3157 * To do this efficiently, we need to know the total length
3158 * of data received, which doesn't seem to be available outside
3159 * of the RPC layer.
3160 *
3161 * In the case of WRITE, we also want to put the GETATTR after
3162 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003163 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 *
3165 * Both of these changes to the XDR layer would in fact be quite
3166 * minor, but I decided to leave them for a subsequent patch.
3167 */
3168static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3169 unsigned int pgbase, unsigned int pglen)
3170{
3171 struct nfs4_readlink args = {
3172 .fh = NFS_FH(inode),
3173 .pgbase = pgbase,
3174 .pglen = pglen,
3175 .pages = &page,
3176 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003177 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct rpc_message msg = {
3179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3180 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003181 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 };
3183
Bryan Schumaker7c513052011-03-24 17:12:24 +00003184 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 -07003185}
3186
3187static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3188 unsigned int pgbase, unsigned int pglen)
3189{
3190 struct nfs4_exception exception = { };
3191 int err;
3192 do {
3193 err = nfs4_handle_exception(NFS_SERVER(inode),
3194 _nfs4_proc_readlink(inode, page, pgbase, pglen),
3195 &exception);
3196 } while (exception.retry);
3197 return err;
3198}
3199
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003201 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203static int
3204nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003205 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
David Quigleyaa9c2662013-05-22 12:50:44 -04003207 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003208 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 int status = 0;
3211
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003212 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3213 if (IS_ERR(ctx))
3214 return PTR_ERR(ctx);
3215
David Quigleyaa9c2662013-05-22 12:50:44 -04003216 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3217
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003218 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003219 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 if (IS_ERR(state)) {
3221 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003225 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003226 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return status;
3228}
3229
3230static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3231{
Trond Myklebust16e42952005-10-27 22:12:44 -04003232 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003233 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003235 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003237 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003238 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003239 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3242 .rpc_argp = &args,
3243 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003245 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003246
Bryan Schumaker7c513052011-03-24 17:12:24 +00003247 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003248 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003249 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 return status;
3251}
3252
3253static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3254{
3255 struct nfs4_exception exception = { };
3256 int err;
3257 do {
3258 err = nfs4_handle_exception(NFS_SERVER(dir),
3259 _nfs4_proc_remove(dir, name),
3260 &exception);
3261 } while (exception.retry);
3262 return err;
3263}
3264
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003265static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003267 struct nfs_server *server = NFS_SERVER(dir);
3268 struct nfs_removeargs *args = msg->rpc_argp;
3269 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003271 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003273 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003274
3275 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003278static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3279{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003280 nfs4_setup_sequence(NFS_SERVER(data->dir),
3281 &data->args.seq_args,
3282 &data->res.seq_res,
3283 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003286static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003288 struct nfs_removeres *res = task->tk_msg.rpc_resp;
3289
Trond Myklebust14516c32010-07-31 14:29:06 -04003290 if (!nfs4_sequence_done(task, &res->seq_res))
3291 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003292 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003293 return 0;
3294 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003295 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Jeff Laytond3d41522010-09-17 17:31:57 -04003298static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3299{
3300 struct nfs_server *server = NFS_SERVER(dir);
3301 struct nfs_renameargs *arg = msg->rpc_argp;
3302 struct nfs_renameres *res = msg->rpc_resp;
3303
3304 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003305 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003306 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003307}
3308
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003309static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3310{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003311 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3312 &data->args.seq_args,
3313 &data->res.seq_res,
3314 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003315}
3316
3317static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3318 struct inode *new_dir)
3319{
3320 struct nfs_renameres *res = task->tk_msg.rpc_resp;
3321
3322 if (!nfs4_sequence_done(task, &res->seq_res))
3323 return 0;
3324 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3325 return 0;
3326
3327 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003328 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003329 return 1;
3330}
3331
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3333 struct inode *new_dir, struct qstr *new_name)
3334{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003335 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003336 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 .old_dir = NFS_FH(old_dir),
3338 .new_dir = NFS_FH(new_dir),
3339 .old_name = old_name,
3340 .new_name = new_name,
3341 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003342 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003343 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003344 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 struct rpc_message msg = {
3346 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3347 .rpc_argp = &arg,
3348 .rpc_resp = &res,
3349 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003350 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003351
Bryan Schumaker7c513052011-03-24 17:12:24 +00003352 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 if (!status) {
3354 update_changeattr(old_dir, &res.old_cinfo);
3355 update_changeattr(new_dir, &res.new_cinfo);
3356 }
3357 return status;
3358}
3359
3360static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3361 struct inode *new_dir, struct qstr *new_name)
3362{
3363 struct nfs4_exception exception = { };
3364 int err;
3365 do {
3366 err = nfs4_handle_exception(NFS_SERVER(old_dir),
3367 _nfs4_proc_rename(old_dir, old_name,
3368 new_dir, new_name),
3369 &exception);
3370 } while (exception.retry);
3371 return err;
3372}
3373
3374static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3375{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003376 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 struct nfs4_link_arg arg = {
3378 .fh = NFS_FH(inode),
3379 .dir_fh = NFS_FH(dir),
3380 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003381 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003383 struct nfs4_link_res res = {
3384 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003385 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3389 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003390 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003392 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Trond Myklebust136f2622010-04-16 16:22:49 -04003394 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003395 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003396 goto out;
3397
David Quigley14c43f72013-05-22 12:50:43 -04003398 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3399 if (IS_ERR(res.label)) {
3400 status = PTR_ERR(res.label);
3401 goto out;
3402 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003403 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003404
Bryan Schumaker7c513052011-03-24 17:12:24 +00003405 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003406 if (!status) {
3407 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003408 status = nfs_post_op_update_inode(inode, res.fattr);
3409 if (!status)
3410 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003411 }
David Quigley14c43f72013-05-22 12:50:43 -04003412
3413
3414 nfs4_label_free(res.label);
3415
Trond Myklebust136f2622010-04-16 16:22:49 -04003416out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003417 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return status;
3419}
3420
3421static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3422{
3423 struct nfs4_exception exception = { };
3424 int err;
3425 do {
3426 err = nfs4_handle_exception(NFS_SERVER(inode),
3427 _nfs4_proc_link(inode, dir, name),
3428 &exception);
3429 } while (exception.retry);
3430 return err;
3431}
3432
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003433struct nfs4_createdata {
3434 struct rpc_message msg;
3435 struct nfs4_create_arg arg;
3436 struct nfs4_create_res res;
3437 struct nfs_fh fh;
3438 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003439 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003440};
3441
3442static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3443 struct qstr *name, struct iattr *sattr, u32 ftype)
3444{
3445 struct nfs4_createdata *data;
3446
3447 data = kzalloc(sizeof(*data), GFP_KERNEL);
3448 if (data != NULL) {
3449 struct nfs_server *server = NFS_SERVER(dir);
3450
David Quigley14c43f72013-05-22 12:50:43 -04003451 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3452 if (IS_ERR(data->label))
3453 goto out_free;
3454
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003455 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3456 data->msg.rpc_argp = &data->arg;
3457 data->msg.rpc_resp = &data->res;
3458 data->arg.dir_fh = NFS_FH(dir);
3459 data->arg.server = server;
3460 data->arg.name = name;
3461 data->arg.attrs = sattr;
3462 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003463 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003464 data->res.server = server;
3465 data->res.fh = &data->fh;
3466 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003467 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003468 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003469 }
3470 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003471out_free:
3472 kfree(data);
3473 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003474}
3475
3476static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3477{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003478 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003479 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003480 if (status == 0) {
3481 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003482 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003483 }
3484 return status;
3485}
3486
3487static void nfs4_free_createdata(struct nfs4_createdata *data)
3488{
David Quigley14c43f72013-05-22 12:50:43 -04003489 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003490 kfree(data);
3491}
3492
Chuck Lever4f390c12006-08-22 20:06:22 -04003493static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003494 struct page *page, unsigned int len, struct iattr *sattr,
3495 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003497 struct nfs4_createdata *data;
3498 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Chuck Lever94a6d752006-08-22 20:06:23 -04003500 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003501 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003502
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003503 status = -ENOMEM;
3504 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3505 if (data == NULL)
3506 goto out;
3507
3508 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3509 data->arg.u.symlink.pages = &page;
3510 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003511 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003513 status = nfs4_do_create(dir, dentry, data);
3514
3515 nfs4_free_createdata(data);
3516out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return status;
3518}
3519
Chuck Lever4f390c12006-08-22 20:06:22 -04003520static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003521 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
3523 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003524 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003526
3527 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 do {
3530 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003531 _nfs4_proc_symlink(dir, dentry, page,
David Quigley1775fd32013-05-22 12:50:42 -04003532 len, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 &exception);
3534 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003535
3536 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return err;
3538}
3539
3540static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003541 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003543 struct nfs4_createdata *data;
3544 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003546 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3547 if (data == NULL)
3548 goto out;
3549
David Quigley1775fd32013-05-22 12:50:42 -04003550 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003551 status = nfs4_do_create(dir, dentry, data);
3552
3553 nfs4_free_createdata(data);
3554out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 return status;
3556}
3557
3558static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3559 struct iattr *sattr)
3560{
3561 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003562 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003564
David Quigleyaa9c2662013-05-22 12:50:44 -04003565 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3566
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003567 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 do {
3569 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigley1775fd32013-05-22 12:50:42 -04003570 _nfs4_proc_mkdir(dir, dentry, sattr, label),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 &exception);
3572 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003573 nfs4_label_release_security(label);
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return err;
3576}
3577
3578static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003579 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
3581 struct inode *dir = dentry->d_inode;
3582 struct nfs4_readdir_arg args = {
3583 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003584 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 .pgbase = 0,
3586 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003587 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003588 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 };
3590 struct nfs4_readdir_res res;
3591 struct rpc_message msg = {
3592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3593 .rpc_argp = &args,
3594 .rpc_resp = &res,
3595 .rpc_cred = cred,
3596 };
3597 int status;
3598
Harvey Harrison3110ff82008-05-02 13:42:44 -07003599 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003600 dentry->d_parent->d_name.name,
3601 dentry->d_name.name,
3602 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003603 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003605 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 -05003606 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003607 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003608 status += args.pgbase;
3609 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003610
3611 nfs_invalidate_atime(dir);
3612
Harvey Harrison3110ff82008-05-02 13:42:44 -07003613 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return status;
3615}
3616
3617static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003618 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 struct nfs4_exception exception = { };
3621 int err;
3622 do {
3623 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3624 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003625 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 &exception);
3627 } while (exception.retry);
3628 return err;
3629}
3630
3631static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003632 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003634 struct nfs4_createdata *data;
3635 int mode = sattr->ia_mode;
3636 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003638 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3639 if (data == NULL)
3640 goto out;
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003643 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003645 data->arg.ftype = NF4BLK;
3646 data->arg.u.device.specdata1 = MAJOR(rdev);
3647 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
3649 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003650 data->arg.ftype = NF4CHR;
3651 data->arg.u.device.specdata1 = MAJOR(rdev);
3652 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003653 } else if (!S_ISSOCK(mode)) {
3654 status = -EINVAL;
3655 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
David Quigley1775fd32013-05-22 12:50:42 -04003657
David Quigleyaa9c2662013-05-22 12:50:44 -04003658 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003660out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003661 nfs4_free_createdata(data);
3662out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return status;
3664}
3665
3666static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3667 struct iattr *sattr, dev_t rdev)
3668{
3669 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003670 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003672
David Quigleyaa9c2662013-05-22 12:50:44 -04003673 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3674
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003675 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 do {
3677 err = nfs4_handle_exception(NFS_SERVER(dir),
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 _nfs4_proc_mknod(dir, dentry, sattr, label, rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 &exception);
3680 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003681
3682 nfs4_label_release_security(label);
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return err;
3685}
3686
3687static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3688 struct nfs_fsstat *fsstat)
3689{
3690 struct nfs4_statfs_arg args = {
3691 .fh = fhandle,
3692 .bitmask = server->attr_bitmask,
3693 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003694 struct nfs4_statfs_res res = {
3695 .fsstat = fsstat,
3696 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 struct rpc_message msg = {
3698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3699 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003700 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 };
3702
Trond Myklebust0e574af2005-10-27 22:12:38 -04003703 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003704 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
3707static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3708{
3709 struct nfs4_exception exception = { };
3710 int err;
3711 do {
3712 err = nfs4_handle_exception(server,
3713 _nfs4_proc_statfs(server, fhandle, fsstat),
3714 &exception);
3715 } while (exception.retry);
3716 return err;
3717}
3718
3719static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3720 struct nfs_fsinfo *fsinfo)
3721{
3722 struct nfs4_fsinfo_arg args = {
3723 .fh = fhandle,
3724 .bitmask = server->attr_bitmask,
3725 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003726 struct nfs4_fsinfo_res res = {
3727 .fsinfo = fsinfo,
3728 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 struct rpc_message msg = {
3730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3731 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003732 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 };
3734
Bryan Schumaker7c513052011-03-24 17:12:24 +00003735 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
3738static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3739{
3740 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003741 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 int err;
3743
3744 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003745 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
3746 if (err == 0) {
3747 struct nfs_client *clp = server->nfs_client;
3748
3749 spin_lock(&clp->cl_lock);
3750 clp->cl_lease_time = fsinfo->lease_time * HZ;
3751 clp->cl_last_renewal = now;
3752 spin_unlock(&clp->cl_lock);
3753 break;
3754 }
3755 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 } while (exception.retry);
3757 return err;
3758}
3759
3760static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3761{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003762 int error;
3763
Trond Myklebust0e574af2005-10-27 22:12:38 -04003764 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003765 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003766 if (error == 0) {
3767 /* block layout checks this! */
3768 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003769 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003770 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003771
3772 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774
3775static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3776 struct nfs_pathconf *pathconf)
3777{
3778 struct nfs4_pathconf_arg args = {
3779 .fh = fhandle,
3780 .bitmask = server->attr_bitmask,
3781 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003782 struct nfs4_pathconf_res res = {
3783 .pathconf = pathconf,
3784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 struct rpc_message msg = {
3786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3787 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003788 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 };
3790
3791 /* None of the pathconf attributes are mandatory to implement */
3792 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3793 memset(pathconf, 0, sizeof(*pathconf));
3794 return 0;
3795 }
3796
Trond Myklebust0e574af2005-10-27 22:12:38 -04003797 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003798 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799}
3800
3801static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3802 struct nfs_pathconf *pathconf)
3803{
3804 struct nfs4_exception exception = { };
3805 int err;
3806
3807 do {
3808 err = nfs4_handle_exception(server,
3809 _nfs4_proc_pathconf(server, fhandle, pathconf),
3810 &exception);
3811 } while (exception.retry);
3812 return err;
3813}
3814
Trond Myklebust5521abf2013-03-16 20:54:34 -04003815int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003816 const struct nfs_open_context *ctx,
3817 const struct nfs_lock_context *l_ctx,
3818 fmode_t fmode)
3819{
3820 const struct nfs_lockowner *lockowner = NULL;
3821
3822 if (l_ctx != NULL)
3823 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003824 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003825}
3826EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3827
Trond Myklebust5521abf2013-03-16 20:54:34 -04003828static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3829 const struct nfs_open_context *ctx,
3830 const struct nfs_lock_context *l_ctx,
3831 fmode_t fmode)
3832{
3833 nfs4_stateid current_stateid;
3834
3835 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3836 return false;
3837 return nfs4_stateid_match(stateid, &current_stateid);
3838}
3839
3840static bool nfs4_error_stateid_expired(int err)
3841{
3842 switch (err) {
3843 case -NFS4ERR_DELEG_REVOKED:
3844 case -NFS4ERR_ADMIN_REVOKED:
3845 case -NFS4ERR_BAD_STATEID:
3846 case -NFS4ERR_STALE_STATEID:
3847 case -NFS4ERR_OLD_STATEID:
3848 case -NFS4ERR_OPENMODE:
3849 case -NFS4ERR_EXPIRED:
3850 return true;
3851 }
3852 return false;
3853}
3854
Benny Halevyd20581a2011-05-22 19:52:03 +03003855void __nfs4_read_done_cb(struct nfs_read_data *data)
3856{
Fred Isamancd841602012-04-20 14:47:44 -04003857 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003858}
3859
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003860static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Fred Isamancd841602012-04-20 14:47:44 -04003862 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003864 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003865 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003866 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003868
Benny Halevyd20581a2011-05-22 19:52:03 +03003869 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003871 renew_lease(server, data->timestamp);
3872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Trond Myklebust5521abf2013-03-16 20:54:34 -04003875static bool nfs4_read_stateid_changed(struct rpc_task *task,
3876 struct nfs_readargs *args)
3877{
3878
3879 if (!nfs4_error_stateid_expired(task->tk_status) ||
3880 nfs4_stateid_is_current(&args->stateid,
3881 args->context,
3882 args->lock_context,
3883 FMODE_READ))
3884 return false;
3885 rpc_restart_call_prepare(task);
3886 return true;
3887}
3888
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003889static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3890{
3891
3892 dprintk("--> %s\n", __func__);
3893
3894 if (!nfs4_sequence_done(task, &data->res.seq_res))
3895 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003896 if (nfs4_read_stateid_changed(task, &data->args))
3897 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003898 return data->read_done_cb ? data->read_done_cb(task, data) :
3899 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003900}
3901
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003902static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003905 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003906 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003907 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003910static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3911{
Trond Myklebust9b206142013-03-17 15:52:00 -04003912 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003913 &data->args.seq_args,
3914 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003915 task))
3916 return;
3917 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3918 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Fred Isamanb029bc92011-03-03 15:13:42 +00003921static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Fred Isamancd841602012-04-20 14:47:44 -04003923 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003925 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003926 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003927 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003929 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003931 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003932 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
Trond Myklebust5521abf2013-03-16 20:54:34 -04003936static bool nfs4_write_stateid_changed(struct rpc_task *task,
3937 struct nfs_writeargs *args)
3938{
3939
3940 if (!nfs4_error_stateid_expired(task->tk_status) ||
3941 nfs4_stateid_is_current(&args->stateid,
3942 args->context,
3943 args->lock_context,
3944 FMODE_WRITE))
3945 return false;
3946 rpc_restart_call_prepare(task);
3947 return true;
3948}
3949
Fred Isamanb029bc92011-03-03 15:13:42 +00003950static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3951{
3952 if (!nfs4_sequence_done(task, &data->res.seq_res))
3953 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003954 if (nfs4_write_stateid_changed(task, &data->args))
3955 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003956 return data->write_done_cb ? data->write_done_cb(task, data) :
3957 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003958}
3959
Trond Myklebust5a37f852012-04-28 14:55:16 -04003960static
3961bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003962{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003963 const struct nfs_pgio_header *hdr = data->header;
3964
3965 /* Don't request attributes for pNFS or O_DIRECT writes */
3966 if (data->ds_clp != NULL || hdr->dreq != NULL)
3967 return false;
3968 /* Otherwise, request attributes if and only if we don't hold
3969 * a delegation
3970 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04003971 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00003972}
Fred Isamana69aef12011-03-03 15:13:47 +00003973
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003974static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975{
Fred Isamancd841602012-04-20 14:47:44 -04003976 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003977
Trond Myklebust5a37f852012-04-28 14:55:16 -04003978 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003979 data->args.bitmask = NULL;
3980 data->res.fattr = NULL;
3981 } else
3982 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003983
Fred Isamanb029bc92011-03-03 15:13:42 +00003984 if (!data->write_done_cb)
3985 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003986 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 data->timestamp = jiffies;
3988
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003989 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003990 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991}
3992
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003993static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3994{
Trond Myklebust9b206142013-03-17 15:52:00 -04003995 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003996 &data->args.seq_args,
3997 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003998 task))
3999 return;
4000 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4001 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Fred Isaman0b7c0152012-04-20 14:47:39 -04004004static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4005{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004006 nfs4_setup_sequence(NFS_SERVER(data->inode),
4007 &data->args.seq_args,
4008 &data->res.seq_res,
4009 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010}
4011
Fred Isaman0b7c0152012-04-20 14:47:39 -04004012static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004015
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004016 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004017 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004018 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004020 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
Fred Isaman0b7c0152012-04-20 14:47:39 -04004023static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004024{
4025 if (!nfs4_sequence_done(task, &data->res.seq_res))
4026 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004027 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004028}
4029
Fred Isaman0b7c0152012-04-20 14:47:39 -04004030static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004032 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004033
Fred Isaman0b7c0152012-04-20 14:47:39 -04004034 if (data->commit_done_cb == NULL)
4035 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004036 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004037 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004038 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004041struct nfs4_renewdata {
4042 struct nfs_client *client;
4043 unsigned long timestamp;
4044};
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046/*
4047 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4048 * standalone procedure for queueing an asynchronous RENEW.
4049 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004050static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004051{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004052 struct nfs4_renewdata *data = calldata;
4053 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004054
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004055 if (atomic_read(&clp->cl_count) > 1)
4056 nfs4_schedule_state_renewal(clp);
4057 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004058 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004059}
4060
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004061static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004063 struct nfs4_renewdata *data = calldata;
4064 struct nfs_client *clp = data->client;
4065 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004068 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004069 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4070 return;
4071 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004072 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004073 return;
4074 }
4075 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004077 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078}
4079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004080static const struct rpc_call_ops nfs4_renew_ops = {
4081 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004082 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004083};
4084
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004085static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086{
4087 struct rpc_message msg = {
4088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4089 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004090 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004092 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004094 if (renew_flags == 0)
4095 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004096 if (!atomic_inc_not_zero(&clp->cl_count))
4097 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004098 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004099 if (data == NULL)
4100 return -ENOMEM;
4101 data->client = clp;
4102 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004103 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004104 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004107static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108{
4109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4111 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004112 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 };
4114 unsigned long now = jiffies;
4115 int status;
4116
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004117 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (status < 0)
4119 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004120 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return 0;
4122}
4123
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004124static inline int nfs4_server_supports_acls(struct nfs_server *server)
4125{
4126 return (server->caps & NFS_CAP_ACLS)
4127 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4128 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4129}
4130
Trond Myklebust21f498c2012-08-24 10:59:25 -04004131/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4132 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004133 * the stack.
4134 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004135#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004136
Neil Hormane9e3d722011-03-04 19:26:03 -05004137static int buf_to_pages_noslab(const void *buf, size_t buflen,
4138 struct page **pages, unsigned int *pgbase)
4139{
4140 struct page *newpage, **spages;
4141 int rc = 0;
4142 size_t len;
4143 spages = pages;
4144
4145 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004146 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004147 newpage = alloc_page(GFP_KERNEL);
4148
4149 if (newpage == NULL)
4150 goto unwind;
4151 memcpy(page_address(newpage), buf, len);
4152 buf += len;
4153 buflen -= len;
4154 *pages++ = newpage;
4155 rc++;
4156 } while (buflen != 0);
4157
4158 return rc;
4159
4160unwind:
4161 for(; rc > 0; rc--)
4162 __free_page(spages[rc-1]);
4163 return -ENOMEM;
4164}
4165
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004166struct nfs4_cached_acl {
4167 int cached;
4168 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004169 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004170};
4171
4172static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004173{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004174 struct nfs_inode *nfsi = NFS_I(inode);
4175
4176 spin_lock(&inode->i_lock);
4177 kfree(nfsi->nfs4_acl);
4178 nfsi->nfs4_acl = acl;
4179 spin_unlock(&inode->i_lock);
4180}
4181
4182static void nfs4_zap_acl_attr(struct inode *inode)
4183{
4184 nfs4_set_cached_acl(inode, NULL);
4185}
4186
4187static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4188{
4189 struct nfs_inode *nfsi = NFS_I(inode);
4190 struct nfs4_cached_acl *acl;
4191 int ret = -ENOENT;
4192
4193 spin_lock(&inode->i_lock);
4194 acl = nfsi->nfs4_acl;
4195 if (acl == NULL)
4196 goto out;
4197 if (buf == NULL) /* user is just asking for length */
4198 goto out_len;
4199 if (acl->cached == 0)
4200 goto out;
4201 ret = -ERANGE; /* see getxattr(2) man page */
4202 if (acl->len > buflen)
4203 goto out;
4204 memcpy(buf, acl->data, acl->len);
4205out_len:
4206 ret = acl->len;
4207out:
4208 spin_unlock(&inode->i_lock);
4209 return ret;
4210}
4211
Sachin Prabhu5794d212012-04-17 14:36:40 +01004212static 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 +00004213{
4214 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004215 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004216
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004217 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004218 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004219 if (acl == NULL)
4220 goto out;
4221 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004222 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004223 } else {
4224 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4225 if (acl == NULL)
4226 goto out;
4227 acl->cached = 0;
4228 }
4229 acl->len = acl_len;
4230out:
4231 nfs4_set_cached_acl(inode, acl);
4232}
4233
Andy Adamsonbf118a32011-12-07 11:55:27 -05004234/*
4235 * The getxattr API returns the required buffer length when called with a
4236 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4237 * the required buf. On a NULL buf, we send a page of data to the server
4238 * guessing that the ACL request can be serviced by a page. If so, we cache
4239 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4240 * the cache. If not so, we throw away the page, and cache the required
4241 * length. The next getxattr call will then produce another round trip to
4242 * the server, this time with the input buf of the required size.
4243 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004244static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004245{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004246 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004247 struct nfs_getaclargs args = {
4248 .fh = NFS_FH(inode),
4249 .acl_pages = pages,
4250 .acl_len = buflen,
4251 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004252 struct nfs_getaclres res = {
4253 .acl_len = buflen,
4254 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004255 struct rpc_message msg = {
4256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4257 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004258 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004259 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004260 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4261 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004262
Andy Adamsonbf118a32011-12-07 11:55:27 -05004263 /* As long as we're doing a round trip to the server anyway,
4264 * let's be prepared for a page of acl data. */
4265 if (npages == 0)
4266 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004267 if (npages > ARRAY_SIZE(pages))
4268 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004269
Andy Adamsonbf118a32011-12-07 11:55:27 -05004270 for (i = 0; i < npages; i++) {
4271 pages[i] = alloc_page(GFP_KERNEL);
4272 if (!pages[i])
4273 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004274 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004275
4276 /* for decoding across pages */
4277 res.acl_scratch = alloc_page(GFP_KERNEL);
4278 if (!res.acl_scratch)
4279 goto out_free;
4280
Andy Adamsonbf118a32011-12-07 11:55:27 -05004281 args.acl_len = npages * PAGE_SIZE;
4282 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004283
Peng Taode040be2012-01-10 22:42:47 +08004284 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004285 __func__, buf, buflen, npages, args.acl_len);
4286 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4287 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004288 if (ret)
4289 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004290
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004291 /* Handle the case where the passed-in buffer is too short */
4292 if (res.acl_flags & NFS4_ACL_TRUNC) {
4293 /* Did the user only issue a request for the acl length? */
4294 if (buf == NULL)
4295 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004296 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004297 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004298 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004299 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004300 if (buf) {
4301 if (res.acl_len > buflen) {
4302 ret = -ERANGE;
4303 goto out_free;
4304 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004305 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004306 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004307out_ok:
4308 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004309out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004310 for (i = 0; i < npages; i++)
4311 if (pages[i])
4312 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004313 if (res.acl_scratch)
4314 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004315 return ret;
4316}
4317
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004318static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4319{
4320 struct nfs4_exception exception = { };
4321 ssize_t ret;
4322 do {
4323 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
4324 if (ret >= 0)
4325 break;
4326 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4327 } while (exception.retry);
4328 return ret;
4329}
4330
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004331static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4332{
4333 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004334 int ret;
4335
4336 if (!nfs4_server_supports_acls(server))
4337 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004338 ret = nfs_revalidate_inode(server, inode);
4339 if (ret < 0)
4340 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004341 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4342 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004343 ret = nfs4_read_cached_acl(inode, buf, buflen);
4344 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004345 /* -ENOENT is returned if there is no ACL or if there is an ACL
4346 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004347 return ret;
4348 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004349}
4350
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004351static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004352{
4353 struct nfs_server *server = NFS_SERVER(inode);
4354 struct page *pages[NFS4ACL_MAXPAGES];
4355 struct nfs_setaclargs arg = {
4356 .fh = NFS_FH(inode),
4357 .acl_pages = pages,
4358 .acl_len = buflen,
4359 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004360 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004361 struct rpc_message msg = {
4362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4363 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004364 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004365 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004366 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004367 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004368
4369 if (!nfs4_server_supports_acls(server))
4370 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004371 if (npages > ARRAY_SIZE(pages))
4372 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004373 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4374 if (i < 0)
4375 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004376 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004377 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004378
4379 /*
4380 * Free each page after tx, so the only ref left is
4381 * held by the network stack
4382 */
4383 for (; i > 0; i--)
4384 put_page(pages[i-1]);
4385
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004386 /*
4387 * Acl update can result in inode attribute update.
4388 * so mark the attribute cache invalid.
4389 */
4390 spin_lock(&inode->i_lock);
4391 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4392 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004393 nfs_access_zap_cache(inode);
4394 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004395 return ret;
4396}
4397
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004398static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4399{
4400 struct nfs4_exception exception = { };
4401 int err;
4402 do {
4403 err = nfs4_handle_exception(NFS_SERVER(inode),
4404 __nfs4_proc_set_acl(inode, buf, buflen),
4405 &exception);
4406 } while (exception.retry);
4407 return err;
4408}
4409
David Quigleyaa9c2662013-05-22 12:50:44 -04004410#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4411static int _nfs4_get_security_label(struct inode *inode, void *buf,
4412 size_t buflen)
4413{
4414 struct nfs_server *server = NFS_SERVER(inode);
4415 struct nfs_fattr fattr;
4416 struct nfs4_label label = {0, 0, buflen, buf};
4417
4418 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4419 struct nfs4_getattr_arg args = {
4420 .fh = NFS_FH(inode),
4421 .bitmask = bitmask,
4422 };
4423 struct nfs4_getattr_res res = {
4424 .fattr = &fattr,
4425 .label = &label,
4426 .server = server,
4427 };
4428 struct rpc_message msg = {
4429 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4430 .rpc_argp = &args,
4431 .rpc_resp = &res,
4432 };
4433 int ret;
4434
4435 nfs_fattr_init(&fattr);
4436
4437 ret = rpc_call_sync(server->client, &msg, 0);
4438 if (ret)
4439 return ret;
4440 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4441 return -ENOENT;
4442 if (buflen < label.len)
4443 return -ERANGE;
4444 return 0;
4445}
4446
4447static int nfs4_get_security_label(struct inode *inode, void *buf,
4448 size_t buflen)
4449{
4450 struct nfs4_exception exception = { };
4451 int err;
4452
4453 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4454 return -EOPNOTSUPP;
4455
4456 do {
4457 err = nfs4_handle_exception(NFS_SERVER(inode),
4458 _nfs4_get_security_label(inode, buf, buflen),
4459 &exception);
4460 } while (exception.retry);
4461 return err;
4462}
4463
4464static int _nfs4_do_set_security_label(struct inode *inode,
4465 struct nfs4_label *ilabel,
4466 struct nfs_fattr *fattr,
4467 struct nfs4_label *olabel)
4468{
4469
4470 struct iattr sattr = {0};
4471 struct nfs_server *server = NFS_SERVER(inode);
4472 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4473 struct nfs_setattrargs args = {
4474 .fh = NFS_FH(inode),
4475 .iap = &sattr,
4476 .server = server,
4477 .bitmask = bitmask,
4478 .label = ilabel,
4479 };
4480 struct nfs_setattrres res = {
4481 .fattr = fattr,
4482 .label = olabel,
4483 .server = server,
4484 };
4485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4487 .rpc_argp = &args,
4488 .rpc_resp = &res,
4489 };
4490 int status;
4491
4492 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4493
4494 status = rpc_call_sync(server->client, &msg, 0);
4495 if (status)
4496 dprintk("%s failed: %d\n", __func__, status);
4497
4498 return status;
4499}
4500
4501static int nfs4_do_set_security_label(struct inode *inode,
4502 struct nfs4_label *ilabel,
4503 struct nfs_fattr *fattr,
4504 struct nfs4_label *olabel)
4505{
4506 struct nfs4_exception exception = { };
4507 int err;
4508
4509 do {
4510 err = nfs4_handle_exception(NFS_SERVER(inode),
4511 _nfs4_do_set_security_label(inode, ilabel,
4512 fattr, olabel),
4513 &exception);
4514 } while (exception.retry);
4515 return err;
4516}
4517
4518static int
4519nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4520{
4521 struct nfs4_label ilabel, *olabel = NULL;
4522 struct nfs_fattr fattr;
4523 struct rpc_cred *cred;
4524 struct inode *inode = dentry->d_inode;
4525 int status;
4526
4527 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4528 return -EOPNOTSUPP;
4529
4530 nfs_fattr_init(&fattr);
4531
4532 ilabel.pi = 0;
4533 ilabel.lfs = 0;
4534 ilabel.label = (char *)buf;
4535 ilabel.len = buflen;
4536
4537 cred = rpc_lookup_cred();
4538 if (IS_ERR(cred))
4539 return PTR_ERR(cred);
4540
4541 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4542 if (IS_ERR(olabel)) {
4543 status = -PTR_ERR(olabel);
4544 goto out;
4545 }
4546
4547 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4548 if (status == 0)
4549 nfs_setsecurity(inode, &fattr, olabel);
4550
4551 nfs4_label_free(olabel);
4552out:
4553 put_rpccred(cred);
4554 return status;
4555}
4556#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4557
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004560nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004562 struct nfs_client *clp = server->nfs_client;
4563
4564 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return 0;
4566 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004567 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004568 case -NFS4ERR_ADMIN_REVOKED:
4569 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004570 if (state == NULL)
4571 break;
4572 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004573 case -NFS4ERR_OPENMODE:
4574 if (state == NULL)
4575 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004576 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4577 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004578 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004579 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004580 if (state != NULL) {
4581 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4582 goto stateid_invalid;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004585 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004586 nfs4_schedule_lease_recovery(clp);
4587 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004588#if defined(CONFIG_NFS_V4_1)
4589 case -NFS4ERR_BADSESSION:
4590 case -NFS4ERR_BADSLOT:
4591 case -NFS4ERR_BAD_HIGH_SLOT:
4592 case -NFS4ERR_DEADSESSION:
4593 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4594 case -NFS4ERR_SEQ_FALSE_RETRY:
4595 case -NFS4ERR_SEQ_MISORDERED:
4596 dprintk("%s ERROR %d, Reset session\n", __func__,
4597 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004598 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004599 task->tk_status = 0;
4600 return -EAGAIN;
4601#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004603 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004604 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4606 task->tk_status = 0;
4607 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004608 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 case -NFS4ERR_OLD_STATEID:
4610 task->tk_status = 0;
4611 return -EAGAIN;
4612 }
4613 task->tk_status = nfs4_map_errors(task->tk_status);
4614 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004615stateid_invalid:
4616 task->tk_status = -EIO;
4617 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004618wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004619 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004620 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4621 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4622 task->tk_status = 0;
4623 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624}
4625
Chuck Leverf0920752012-05-21 22:45:41 -04004626static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4627 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004628{
4629 __be32 verf[2];
4630
Chuck Lever2c820d92012-05-21 22:45:33 -04004631 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4632 /* An impossible timestamp guarantees this value
4633 * will never match a generated boot time. */
4634 verf[0] = 0;
4635 verf[1] = (__be32)(NSEC_PER_SEC + 1);
4636 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004637 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
4638 verf[0] = (__be32)nn->boot_time.tv_sec;
4639 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04004640 }
Chuck Levercd937102012-03-02 17:14:31 -05004641 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4642}
4643
Chuck Levere984a552012-09-14 17:24:21 -04004644static unsigned int
4645nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4646 char *buf, size_t len)
4647{
4648 unsigned int result;
4649
4650 rcu_read_lock();
4651 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4652 clp->cl_ipaddr,
4653 rpc_peeraddr2str(clp->cl_rpcclient,
4654 RPC_DISPLAY_ADDR),
4655 rpc_peeraddr2str(clp->cl_rpcclient,
4656 RPC_DISPLAY_PROTO));
4657 rcu_read_unlock();
4658 return result;
4659}
4660
4661static unsigned int
4662nfs4_init_uniform_client_string(const struct nfs_client *clp,
4663 char *buf, size_t len)
4664{
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004665 char *nodename = clp->cl_rpcclient->cl_nodename;
4666
4667 if (nfs4_client_id_uniquifier[0] != '\0')
4668 nodename = nfs4_client_id_uniquifier;
Chuck Levere984a552012-09-14 17:24:21 -04004669 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4670 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004671 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004672}
4673
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004674/**
4675 * nfs4_proc_setclientid - Negotiate client ID
4676 * @clp: state data structure
4677 * @program: RPC program for NFSv4 callback service
4678 * @port: IP port number for NFS4 callback service
4679 * @cred: RPC credential to use for this call
4680 * @res: where to place the result
4681 *
4682 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4683 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004684int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4685 unsigned short port, struct rpc_cred *cred,
4686 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
4688 nfs4_verifier sc_verifier;
4689 struct nfs4_setclientid setclientid = {
4690 .sc_verifier = &sc_verifier,
4691 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004692 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 };
4694 struct rpc_message msg = {
4695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4696 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004697 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004698 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004700 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Chuck Leverde734832012-07-11 16:30:50 -04004702 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004703 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004704 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4705 setclientid.sc_name_len =
4706 nfs4_init_uniform_client_string(clp,
4707 setclientid.sc_name,
4708 sizeof(setclientid.sc_name));
4709 else
4710 setclientid.sc_name_len =
4711 nfs4_init_nonuniform_client_string(clp,
4712 setclientid.sc_name,
4713 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004714 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004715 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004716 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004717 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004718 rpc_peeraddr2str(clp->cl_rpcclient,
4719 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004720 rcu_read_unlock();
4721 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004722 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 clp->cl_ipaddr, port >> 8, port & 255);
4724
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004725 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4726 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4727 setclientid.sc_name_len, setclientid.sc_name);
4728 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
4729 dprintk("NFS reply setclientid: %d\n", status);
4730 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731}
4732
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004733/**
4734 * nfs4_proc_setclientid_confirm - Confirm client ID
4735 * @clp: state data structure
4736 * @res: result of a previous SETCLIENTID
4737 * @cred: RPC credential to use for this call
4738 *
4739 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4740 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004741int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004742 struct nfs4_setclientid_res *arg,
4743 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 struct rpc_message msg = {
4746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004747 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004748 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 int status;
4751
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004752 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4753 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4754 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004755 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004756 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 return status;
4758}
4759
Trond Myklebustfe650402006-01-03 09:55:18 +01004760struct nfs4_delegreturndata {
4761 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004762 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004763 struct nfs_fh fh;
4764 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004765 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004766 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004767 int rpc_status;
4768};
4769
Trond Myklebustfe650402006-01-03 09:55:18 +01004770static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4771{
4772 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004773
Trond Myklebust14516c32010-07-31 14:29:06 -04004774 if (!nfs4_sequence_done(task, &data->res.seq_res))
4775 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004776
Ricardo Labiaga79708862009-12-07 09:23:21 -05004777 switch (task->tk_status) {
4778 case -NFS4ERR_STALE_STATEID:
4779 case -NFS4ERR_EXPIRED:
4780 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004781 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004782 break;
4783 default:
4784 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4785 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004786 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004787 return;
4788 }
4789 }
4790 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004791}
4792
4793static void nfs4_delegreturn_release(void *calldata)
4794{
Trond Myklebustfe650402006-01-03 09:55:18 +01004795 kfree(calldata);
4796}
4797
Andy Adamson938e1012009-04-01 09:22:28 -04004798#if defined(CONFIG_NFS_V4_1)
4799static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4800{
4801 struct nfs4_delegreturndata *d_data;
4802
4803 d_data = (struct nfs4_delegreturndata *)data;
4804
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004805 nfs4_setup_sequence(d_data->res.server,
4806 &d_data->args.seq_args,
4807 &d_data->res.seq_res,
4808 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004809}
4810#endif /* CONFIG_NFS_V4_1 */
4811
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004812static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004813#if defined(CONFIG_NFS_V4_1)
4814 .rpc_call_prepare = nfs4_delegreturn_prepare,
4815#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004816 .rpc_call_done = nfs4_delegreturn_done,
4817 .rpc_release = nfs4_delegreturn_release,
4818};
4819
Trond Myklebuste6f81072008-01-24 18:14:34 -05004820static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004821{
4822 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004823 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004824 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004825 struct rpc_message msg = {
4826 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4827 .rpc_cred = cred,
4828 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004829 struct rpc_task_setup task_setup_data = {
4830 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004831 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004832 .callback_ops = &nfs4_delegreturn_ops,
4833 .flags = RPC_TASK_ASYNC,
4834 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004835 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004836
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004837 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004838 if (data == NULL)
4839 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004840 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004841 data->args.fhandle = &data->fh;
4842 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004843 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004844 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004845 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004846 data->res.fattr = &data->fattr;
4847 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004848 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004849 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004850 data->rpc_status = 0;
4851
Trond Myklebustc970aa82007-07-14 15:39:59 -04004852 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004853 msg.rpc_argp = &data->args;
4854 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004855 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004856 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004857 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004858 if (!issync)
4859 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004860 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004861 if (status != 0)
4862 goto out;
4863 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004864 if (status == 0)
4865 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4866 else
4867 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004868out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004869 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004870 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
Trond Myklebuste6f81072008-01-24 18:14:34 -05004873int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
4875 struct nfs_server *server = NFS_SERVER(inode);
4876 struct nfs4_exception exception = { };
4877 int err;
4878 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004879 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 switch (err) {
4881 case -NFS4ERR_STALE_STATEID:
4882 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 case 0:
4884 return 0;
4885 }
4886 err = nfs4_handle_exception(server, err, &exception);
4887 } while (exception.retry);
4888 return err;
4889}
4890
4891#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4892#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4893
4894/*
4895 * sleep, with exponential backoff, and retry the LOCK operation.
4896 */
4897static unsigned long
4898nfs4_set_lock_task_retry(unsigned long timeout)
4899{
Colin Cross416ad3c2013-05-06 23:50:06 +00004900 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 timeout <<= 1;
4902 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4903 return NFS4_LOCK_MAXTIMEOUT;
4904 return timeout;
4905}
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4908{
4909 struct inode *inode = state->inode;
4910 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004911 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004912 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004914 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004916 struct nfs_lockt_res res = {
4917 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 };
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4921 .rpc_argp = &arg,
4922 .rpc_resp = &res,
4923 .rpc_cred = state->owner->so_cred,
4924 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 struct nfs4_lock_state *lsp;
4926 int status;
4927
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004928 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004929 status = nfs4_set_lock_state(state, request);
4930 if (status != 0)
4931 goto out;
4932 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004933 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004934 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004935 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004936 switch (status) {
4937 case 0:
4938 request->fl_type = F_UNLCK;
4939 break;
4940 case -NFS4ERR_DENIED:
4941 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004943 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004944out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 return status;
4946}
4947
4948static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4949{
4950 struct nfs4_exception exception = { };
4951 int err;
4952
4953 do {
4954 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4955 _nfs4_proc_getlk(state, cmd, request),
4956 &exception);
4957 } while (exception.retry);
4958 return err;
4959}
4960
4961static int do_vfs_lock(struct file *file, struct file_lock *fl)
4962{
4963 int res = 0;
4964 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4965 case FL_POSIX:
4966 res = posix_lock_file_wait(file, fl);
4967 break;
4968 case FL_FLOCK:
4969 res = flock_lock_file_wait(file, fl);
4970 break;
4971 default:
4972 BUG();
4973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return res;
4975}
4976
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004977struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004978 struct nfs_locku_args arg;
4979 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004980 struct nfs4_lock_state *lsp;
4981 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004982 struct file_lock fl;
4983 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004984 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004985};
4986
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004987static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4988 struct nfs_open_context *ctx,
4989 struct nfs4_lock_state *lsp,
4990 struct nfs_seqid *seqid)
4991{
4992 struct nfs4_unlockdata *p;
4993 struct inode *inode = lsp->ls_state->inode;
4994
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004995 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004996 if (p == NULL)
4997 return NULL;
4998 p->arg.fh = NFS_FH(inode);
4999 p->arg.fl = &p->fl;
5000 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005001 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005002 p->arg.stateid = &lsp->ls_stateid;
5003 p->lsp = lsp;
5004 atomic_inc(&lsp->ls_count);
5005 /* Ensure we don't close file until we're done freeing locks! */
5006 p->ctx = get_nfs_open_context(ctx);
5007 memcpy(&p->fl, fl, sizeof(p->fl));
5008 p->server = NFS_SERVER(inode);
5009 return p;
5010}
5011
Trond Myklebust06f814a2006-01-03 09:55:07 +01005012static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005013{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005014 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005015 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005016 nfs4_put_lock_state(calldata->lsp);
5017 put_nfs_open_context(calldata->ctx);
5018 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005019}
5020
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005021static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005022{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005023 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005024
Trond Myklebust14516c32010-07-31 14:29:06 -04005025 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5026 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005027 switch (task->tk_status) {
5028 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005029 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5030 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005031 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005032 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005033 case -NFS4ERR_BAD_STATEID:
5034 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005035 case -NFS4ERR_STALE_STATEID:
5036 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005037 break;
5038 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005039 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005040 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005041 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005042 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005043}
5044
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005045static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005046{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005047 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005049 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005050 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005051 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005052 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005053 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005054 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005055 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005056 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005057 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005058 &calldata->res.seq_res,
5059 task) != 0)
5060 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005061 return;
5062out_no_action:
5063 task->tk_action = NULL;
5064out_wait:
5065 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066}
5067
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005068static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005069 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005070 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005071 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005072};
5073
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005074static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5075 struct nfs_open_context *ctx,
5076 struct nfs4_lock_state *lsp,
5077 struct nfs_seqid *seqid)
5078{
5079 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005080 struct rpc_message msg = {
5081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5082 .rpc_cred = ctx->cred,
5083 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005084 struct rpc_task_setup task_setup_data = {
5085 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005086 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005087 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005088 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005089 .flags = RPC_TASK_ASYNC,
5090 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005091
Frank Filz137d6ac2007-07-09 15:32:29 -07005092 /* Ensure this is an unlock - when canceling a lock, the
5093 * canceled lock is passed in, and it won't be an unlock.
5094 */
5095 fl->fl_type = F_UNLCK;
5096
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005097 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5098 if (data == NULL) {
5099 nfs_free_seqid(seqid);
5100 return ERR_PTR(-ENOMEM);
5101 }
5102
Trond Myklebust9d12b212012-01-17 22:04:25 -05005103 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005104 msg.rpc_argp = &data->arg;
5105 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005106 task_setup_data.callback_data = data;
5107 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005108}
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5111{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005112 struct inode *inode = state->inode;
5113 struct nfs4_state_owner *sp = state->owner;
5114 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005115 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005116 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005117 struct rpc_task *task;
5118 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005119 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120
Trond Myklebust9b073572006-06-29 16:38:34 -04005121 status = nfs4_set_lock_state(state, request);
5122 /* Unlock _before_ we do the RPC call */
5123 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005124 /* Exclude nfs_delegation_claim_locks() */
5125 mutex_lock(&sp->so_delegreturn_mutex);
5126 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005127 down_read(&nfsi->rwsem);
5128 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5129 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005130 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005131 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005132 }
5133 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005134 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005135 if (status != 0)
5136 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005137 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005138 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005139 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5140 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005141 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005142 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005143 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005144 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005145 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005146 status = PTR_ERR(task);
5147 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005148 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005149 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005150 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005151out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005152 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005153 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154}
5155
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005156struct nfs4_lockdata {
5157 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005158 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005159 struct nfs4_lock_state *lsp;
5160 struct nfs_open_context *ctx;
5161 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005162 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005163 int rpc_status;
5164 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005165 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005166};
5167
5168static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005169 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5170 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005171{
5172 struct nfs4_lockdata *p;
5173 struct inode *inode = lsp->ls_state->inode;
5174 struct nfs_server *server = NFS_SERVER(inode);
5175
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005176 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005177 if (p == NULL)
5178 return NULL;
5179
5180 p->arg.fh = NFS_FH(inode);
5181 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005182 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005183 if (p->arg.open_seqid == NULL)
5184 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005185 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005186 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005187 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005188 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005189 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005190 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005191 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005192 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005193 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005194 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005195 atomic_inc(&lsp->ls_count);
5196 p->ctx = get_nfs_open_context(ctx);
5197 memcpy(&p->fl, fl, sizeof(p->fl));
5198 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005199out_free_seqid:
5200 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005201out_free:
5202 kfree(p);
5203 return NULL;
5204}
5205
5206static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5207{
5208 struct nfs4_lockdata *data = calldata;
5209 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
Harvey Harrison3110ff82008-05-02 13:42:44 -07005211 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005212 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005213 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005214 /* Do we need to do an open_to_lock_owner? */
5215 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005216 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005217 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005218 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005219 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005220 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005221 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005222 } else
5223 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005224 if (!nfs4_valid_open_stateid(state)) {
5225 data->rpc_status = -EBADF;
5226 task->tk_action = NULL;
5227 goto out_release_open_seqid;
5228 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005229 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005230 if (nfs4_setup_sequence(data->server,
5231 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005232 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005233 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005234 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005235out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005236 nfs_release_seqid(data->arg.open_seqid);
5237out_release_lock_seqid:
5238 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005239out_wait:
5240 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005241 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005242}
5243
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005244static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5245{
5246 struct nfs4_lockdata *data = calldata;
5247
Harvey Harrison3110ff82008-05-02 13:42:44 -07005248 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005249
Trond Myklebust14516c32010-07-31 14:29:06 -04005250 if (!nfs4_sequence_done(task, &data->res.seq_res))
5251 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005252
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005253 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005254 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005255 if (data->rpc_status == 0)
5256 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5257 else
5258 goto out;
5259 }
5260 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005261 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005262 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005263 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005264 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005265out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005266 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005267}
5268
5269static void nfs4_lock_release(void *calldata)
5270{
5271 struct nfs4_lockdata *data = calldata;
5272
Harvey Harrison3110ff82008-05-02 13:42:44 -07005273 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005274 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005275 if (data->cancelled != 0) {
5276 struct rpc_task *task;
5277 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5278 data->arg.lock_seqid);
5279 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005280 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005281 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005282 } else
5283 nfs_free_seqid(data->arg.lock_seqid);
5284 nfs4_put_lock_state(data->lsp);
5285 put_nfs_open_context(data->ctx);
5286 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005287 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005288}
5289
5290static const struct rpc_call_ops nfs4_lock_ops = {
5291 .rpc_call_prepare = nfs4_lock_prepare,
5292 .rpc_call_done = nfs4_lock_done,
5293 .rpc_release = nfs4_lock_release,
5294};
5295
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005296static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5297{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005298 switch (error) {
5299 case -NFS4ERR_ADMIN_REVOKED:
5300 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005301 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005302 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005303 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005304 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005305 break;
5306 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005307 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005308 case -NFS4ERR_EXPIRED:
5309 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005310 };
5311}
5312
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005313static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005314{
5315 struct nfs4_lockdata *data;
5316 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005317 struct rpc_message msg = {
5318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5319 .rpc_cred = state->owner->so_cred,
5320 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005321 struct rpc_task_setup task_setup_data = {
5322 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005323 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005324 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005325 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005326 .flags = RPC_TASK_ASYNC,
5327 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005328 int ret;
5329
Harvey Harrison3110ff82008-05-02 13:42:44 -07005330 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005331 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005332 fl->fl_u.nfs4_fl.owner,
5333 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005334 if (data == NULL)
5335 return -ENOMEM;
5336 if (IS_SETLKW(cmd))
5337 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005338 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005339 msg.rpc_argp = &data->arg;
5340 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005341 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005342 if (recovery_type > NFS_LOCK_NEW) {
5343 if (recovery_type == NFS_LOCK_RECLAIM)
5344 data->arg.reclaim = NFS_LOCK_RECLAIM;
5345 nfs4_set_sequence_privileged(&data->arg.seq_args);
5346 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005347 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005348 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005349 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005350 ret = nfs4_wait_for_completion_rpc_task(task);
5351 if (ret == 0) {
5352 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005353 if (ret)
5354 nfs4_handle_setlk_error(data->server, data->lsp,
5355 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005356 } else
5357 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005358 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005359 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005360 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361}
5362
5363static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5364{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005365 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005366 struct nfs4_exception exception = {
5367 .inode = state->inode,
5368 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005369 int err;
5370
5371 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005372 /* Cache the lock if possible... */
5373 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5374 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005375 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005376 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005377 break;
5378 nfs4_handle_exception(server, err, &exception);
5379 } while (exception.retry);
5380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381}
5382
5383static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5384{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005385 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005386 struct nfs4_exception exception = {
5387 .inode = state->inode,
5388 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005389 int err;
5390
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005391 err = nfs4_set_lock_state(state, request);
5392 if (err != 0)
5393 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005394 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005395 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5396 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005397 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005398 switch (err) {
5399 default:
5400 goto out;
5401 case -NFS4ERR_GRACE:
5402 case -NFS4ERR_DELAY:
5403 nfs4_handle_exception(server, err, &exception);
5404 err = 0;
5405 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005406 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005407out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005408 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409}
5410
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005411#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005412/**
5413 * nfs41_check_expired_locks - possibly free a lock stateid
5414 *
5415 * @state: NFSv4 state for an inode
5416 *
5417 * Returns NFS_OK if recovery for this stateid is now finished.
5418 * Otherwise a negative NFS4ERR value is returned.
5419 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005420static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005421{
Chuck Levereb64cf92012-07-11 16:30:05 -04005422 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005423 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005424 struct nfs_server *server = NFS_SERVER(state->inode);
5425
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005426 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005427 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005428 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5429
5430 status = nfs41_test_stateid(server,
5431 &lsp->ls_stateid,
5432 cred);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005433 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005434 /* Free the stateid unless the server
5435 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005436 if (status != -NFS4ERR_BAD_STATEID)
5437 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005438 &lsp->ls_stateid,
5439 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005440 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005441 ret = status;
5442 }
5443 }
5444 };
5445
5446 return ret;
5447}
5448
5449static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5450{
5451 int status = NFS_OK;
5452
5453 if (test_bit(LK_STATE_IN_USE, &state->flags))
5454 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005455 if (status != NFS_OK)
5456 status = nfs4_lock_expired(state, request);
5457 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005458}
5459#endif
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5462{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005463 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005464 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005465 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005466 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005467 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005469 if ((fl_flags & FL_POSIX) &&
5470 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5471 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005472 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005473 status = nfs4_set_lock_state(state, request);
5474 if (status != 0)
5475 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005476 request->fl_flags |= FL_ACCESS;
5477 status = do_vfs_lock(request->fl_file, request);
5478 if (status < 0)
5479 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005480 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005481 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005482 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005483 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005484 request->fl_flags = fl_flags & ~FL_SLEEP;
5485 status = do_vfs_lock(request->fl_file, request);
5486 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005487 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005488 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5489 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005490 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005491 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005492 goto out;
5493 down_read(&nfsi->rwsem);
5494 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5495 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005496 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005497 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005498 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005499 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005500 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005501 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5502 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005503out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005504 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005505out:
5506 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 return status;
5508}
5509
5510static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5511{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005512 struct nfs4_exception exception = {
5513 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005514 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 int err;
5517
5518 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005519 err = _nfs4_proc_setlk(state, cmd, request);
5520 if (err == -NFS4ERR_DENIED)
5521 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005523 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 } while (exception.retry);
5525 return err;
5526}
5527
5528static int
5529nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5530{
5531 struct nfs_open_context *ctx;
5532 struct nfs4_state *state;
5533 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5534 int status;
5535
5536 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005537 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 state = ctx->state;
5539
5540 if (request->fl_start < 0 || request->fl_end < 0)
5541 return -EINVAL;
5542
Trond Myklebustd9531262009-07-21 19:22:38 -04005543 if (IS_GETLK(cmd)) {
5544 if (state != NULL)
5545 return nfs4_proc_getlk(state, F_GETLK, request);
5546 return 0;
5547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5550 return -EINVAL;
5551
Trond Myklebustd9531262009-07-21 19:22:38 -04005552 if (request->fl_type == F_UNLCK) {
5553 if (state != NULL)
5554 return nfs4_proc_unlck(state, cmd, request);
5555 return 0;
5556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Trond Myklebustd9531262009-07-21 19:22:38 -04005558 if (state == NULL)
5559 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005560 /*
5561 * Don't rely on the VFS having checked the file open mode,
5562 * since it won't do this for flock() locks.
5563 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005564 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005565 case F_RDLCK:
5566 if (!(filp->f_mode & FMODE_READ))
5567 return -EBADF;
5568 break;
5569 case F_WRLCK:
5570 if (!(filp->f_mode & FMODE_WRITE))
5571 return -EBADF;
5572 }
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 do {
5575 status = nfs4_proc_setlk(state, cmd, request);
5576 if ((status != -EAGAIN) || IS_SETLK(cmd))
5577 break;
5578 timeout = nfs4_set_lock_task_retry(timeout);
5579 status = -ERESTARTSYS;
5580 if (signalled())
5581 break;
5582 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 return status;
5584}
5585
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005586int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005587{
5588 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005589 int err;
5590
5591 err = nfs4_set_lock_state(state, fl);
5592 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005593 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005594 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005595 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005596}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005597
Trond Myklebustcf470c32012-03-07 13:49:12 -05005598struct nfs_release_lockowner_data {
5599 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005600 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005601 struct nfs_release_lockowner_args args;
5602};
5603
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005604static void nfs4_release_lockowner_release(void *calldata)
5605{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005606 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005607 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005608 kfree(calldata);
5609}
5610
Trond Myklebust17280172012-03-11 13:11:00 -04005611static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005612 .rpc_release = nfs4_release_lockowner_release,
5613};
5614
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005615static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005616{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005617 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005618 struct rpc_message msg = {
5619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5620 };
5621
5622 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005623 return -EINVAL;
5624 data = kmalloc(sizeof(*data), GFP_NOFS);
5625 if (!data)
5626 return -ENOMEM;
5627 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005628 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005629 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5630 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5631 data->args.lock_owner.s_dev = server->s_dev;
5632 msg.rpc_argp = &data->args;
5633 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5634 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005635}
5636
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005637#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5638
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005639static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5640 const void *buf, size_t buflen,
5641 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005642{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005643 if (strcmp(key, "") != 0)
5644 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005645
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005646 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005647}
5648
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005649static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5650 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005651{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005652 if (strcmp(key, "") != 0)
5653 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005654
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005655 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005656}
5657
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005658static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5659 size_t list_len, const char *name,
5660 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005661{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005662 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005663
J. Bruce Fields096455a2006-03-20 23:23:42 -05005664 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5665 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005666
5667 if (list && len <= list_len)
5668 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005669 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005670}
5671
David Quigleyc9bccef2013-05-22 12:50:45 -04005672#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5673static inline int nfs4_server_supports_labels(struct nfs_server *server)
5674{
5675 return server->caps & NFS_CAP_SECURITY_LABEL;
5676}
5677
5678static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5679 const void *buf, size_t buflen,
5680 int flags, int type)
5681{
5682 if (security_ismaclabel(key))
5683 return nfs4_set_security_label(dentry, buf, buflen);
5684
5685 return -EOPNOTSUPP;
5686}
5687
5688static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5689 void *buf, size_t buflen, int type)
5690{
5691 if (security_ismaclabel(key))
5692 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5693 return -EOPNOTSUPP;
5694}
5695
5696static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5697 size_t list_len, const char *name,
5698 size_t name_len, int type)
5699{
5700 size_t len = 0;
5701
5702 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5703 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5704 if (list && len <= list_len)
5705 security_inode_listsecurity(dentry->d_inode, list, len);
5706 }
5707 return len;
5708}
5709
5710static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5711 .prefix = XATTR_SECURITY_PREFIX,
5712 .list = nfs4_xattr_list_nfs4_label,
5713 .get = nfs4_xattr_get_nfs4_label,
5714 .set = nfs4_xattr_set_nfs4_label,
5715};
5716#endif
5717
5718
Andy Adamson533eb462011-06-13 18:25:56 -04005719/*
5720 * nfs_fhget will use either the mounted_on_fileid or the fileid
5721 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005722static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5723{
Andy Adamson533eb462011-06-13 18:25:56 -04005724 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5725 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5726 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005727 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005728 return;
5729
5730 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005731 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005732 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5733 fattr->nlink = 2;
5734}
5735
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005736static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5737 const struct qstr *name,
5738 struct nfs4_fs_locations *fs_locations,
5739 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005740{
5741 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005742 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005743 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005744 };
5745 struct nfs4_fs_locations_arg args = {
5746 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005747 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005748 .page = page,
5749 .bitmask = bitmask,
5750 };
Benny Halevy22958462009-04-01 09:22:02 -04005751 struct nfs4_fs_locations_res res = {
5752 .fs_locations = fs_locations,
5753 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005754 struct rpc_message msg = {
5755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5756 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005757 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005758 };
5759 int status;
5760
Harvey Harrison3110ff82008-05-02 13:42:44 -07005761 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005762
5763 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5764 * is not supported */
5765 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5766 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5767 else
5768 bitmask[0] |= FATTR4_WORD0_FILEID;
5769
Trond Myklebustc228fd32007-01-13 02:28:11 -05005770 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005771 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005772 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005773 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005774 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005775 return status;
5776}
5777
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005778int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5779 const struct qstr *name,
5780 struct nfs4_fs_locations *fs_locations,
5781 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005782{
5783 struct nfs4_exception exception = { };
5784 int err;
5785 do {
5786 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005787 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005788 &exception);
5789 } while (exception.retry);
5790 return err;
5791}
5792
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005793static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5794{
5795 int status;
5796 struct nfs4_secinfo_arg args = {
5797 .dir_fh = NFS_FH(dir),
5798 .name = name,
5799 };
5800 struct nfs4_secinfo_res res = {
5801 .flavors = flavors,
5802 };
5803 struct rpc_message msg = {
5804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5805 .rpc_argp = &args,
5806 .rpc_resp = &res,
5807 };
5808
5809 dprintk("NFS call secinfo %s\n", name->name);
5810 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5811 dprintk("NFS reply secinfo: %d\n", status);
5812 return status;
5813}
5814
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005815int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5816 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005817{
5818 struct nfs4_exception exception = { };
5819 int err;
5820 do {
5821 err = nfs4_handle_exception(NFS_SERVER(dir),
5822 _nfs4_proc_secinfo(dir, name, flavors),
5823 &exception);
5824 } while (exception.retry);
5825 return err;
5826}
5827
Andy Adamson557134a2009-04-01 09:21:53 -04005828#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005829/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005830 * Check the exchange flags returned by the server for invalid flags, having
5831 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5832 * DS flags set.
5833 */
5834static int nfs4_check_cl_exchange_flags(u32 flags)
5835{
5836 if (flags & ~EXCHGID4_FLAG_MASK_R)
5837 goto out_inval;
5838 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5839 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5840 goto out_inval;
5841 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5842 goto out_inval;
5843 return NFS_OK;
5844out_inval:
5845 return -NFS4ERR_INVAL;
5846}
5847
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005848static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005849nfs41_same_server_scope(struct nfs41_server_scope *a,
5850 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005851{
5852 if (a->server_scope_sz == b->server_scope_sz &&
5853 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5854 return true;
5855
5856 return false;
5857}
5858
Andy Adamson357f54d2010-12-14 10:11:57 -05005859/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005860 * nfs4_proc_bind_conn_to_session()
5861 *
5862 * The 4.1 client currently uses the same TCP connection for the
5863 * fore and backchannel.
5864 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005865int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005866{
5867 int status;
5868 struct nfs41_bind_conn_to_session_res res;
5869 struct rpc_message msg = {
5870 .rpc_proc =
5871 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5872 .rpc_argp = clp,
5873 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005874 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005875 };
5876
5877 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005878
5879 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5880 if (unlikely(res.session == NULL)) {
5881 status = -ENOMEM;
5882 goto out;
5883 }
5884
5885 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5886 if (status == 0) {
5887 if (memcmp(res.session->sess_id.data,
5888 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5889 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5890 status = -EIO;
5891 goto out_session;
5892 }
5893 if (res.dir != NFS4_CDFS4_BOTH) {
5894 dprintk("NFS: %s: Unexpected direction from server\n",
5895 __func__);
5896 status = -EIO;
5897 goto out_session;
5898 }
5899 if (res.use_conn_in_rdma_mode) {
5900 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5901 __func__);
5902 status = -EIO;
5903 goto out_session;
5904 }
5905 }
5906out_session:
5907 kfree(res.session);
5908out:
5909 dprintk("<-- %s status= %d\n", __func__, status);
5910 return status;
5911}
5912
5913/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005914 * nfs4_proc_exchange_id()
5915 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005916 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5917 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005918 * Since the clientid has expired, all compounds using sessions
5919 * associated with the stale clientid will be returning
5920 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5921 * be in some phase of session reset.
5922 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005923int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005924{
5925 nfs4_verifier verifier;
5926 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005927 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005928 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005929 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5930 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005931 };
5932 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005933 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005934 };
5935 int status;
5936 struct rpc_message msg = {
5937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5938 .rpc_argp = &args,
5939 .rpc_resp = &res,
5940 .rpc_cred = cred,
5941 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005942
Chuck Leverf0920752012-05-21 22:45:41 -04005943 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04005944 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
5945 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005946 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
5947 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5948 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005949
Chuck Leveracdeb692012-05-21 22:46:16 -04005950 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005951 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005952 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005953 status = -ENOMEM;
5954 goto out;
5955 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005956
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005957 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005958 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005959 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04005960 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005961 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04005962 }
5963
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005964 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005965 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005966 status = -ENOMEM;
5967 goto out_server_scope;
5968 }
5969
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005970 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005971 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005972 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005973
Chuck Lever177313f2012-05-21 22:44:58 -04005974 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005975 clp->cl_clientid = res.clientid;
5976 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5977 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5978 clp->cl_seqid = res.seqid;
5979
Chuck Leveracdeb692012-05-21 22:46:16 -04005980 kfree(clp->cl_serverowner);
5981 clp->cl_serverowner = res.server_owner;
5982 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005983
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005984 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005985 kfree(clp->cl_implid);
5986 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005987
Chuck Lever177313f2012-05-21 22:44:58 -04005988 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005989 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005990 res.server_scope)) {
5991 dprintk("%s: server_scope mismatch detected\n",
5992 __func__);
5993 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005994 kfree(clp->cl_serverscope);
5995 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005996 }
5997
Chuck Lever177313f2012-05-21 22:44:58 -04005998 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005999 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006000 goto out;
6001 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006002 } else
6003 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006004
Chuck Leveracdeb692012-05-21 22:46:16 -04006005out_server_owner:
6006 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006007out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006008 kfree(res.server_scope);
6009out:
Chuck Lever177313f2012-05-21 22:44:58 -04006010 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006011 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006012 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006013 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006014 clp->cl_implid->date.seconds,
6015 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006016 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006017 return status;
6018}
6019
Trond Myklebust66245532012-05-25 17:18:09 -04006020static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6021 struct rpc_cred *cred)
6022{
6023 struct rpc_message msg = {
6024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6025 .rpc_argp = clp,
6026 .rpc_cred = cred,
6027 };
6028 int status;
6029
6030 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
6031 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006032 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006033 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6034 return status;
6035}
6036
6037static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6038 struct rpc_cred *cred)
6039{
6040 unsigned int loop;
6041 int ret;
6042
6043 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6044 ret = _nfs4_proc_destroy_clientid(clp, cred);
6045 switch (ret) {
6046 case -NFS4ERR_DELAY:
6047 case -NFS4ERR_CLIENTID_BUSY:
6048 ssleep(1);
6049 break;
6050 default:
6051 return ret;
6052 }
6053 }
6054 return 0;
6055}
6056
6057int nfs4_destroy_clientid(struct nfs_client *clp)
6058{
6059 struct rpc_cred *cred;
6060 int ret = 0;
6061
6062 if (clp->cl_mvops->minor_version < 1)
6063 goto out;
6064 if (clp->cl_exchange_flags == 0)
6065 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006066 if (clp->cl_preserve_clid)
6067 goto out;
Trond Myklebust66245532012-05-25 17:18:09 -04006068 cred = nfs4_get_exchange_id_cred(clp);
6069 ret = nfs4_proc_destroy_clientid(clp, cred);
6070 if (cred)
6071 put_rpccred(cred);
6072 switch (ret) {
6073 case 0:
6074 case -NFS4ERR_STALE_CLIENTID:
6075 clp->cl_exchange_flags = 0;
6076 }
6077out:
6078 return ret;
6079}
6080
Andy Adamson2050f0c2009-04-01 09:22:30 -04006081struct nfs4_get_lease_time_data {
6082 struct nfs4_get_lease_time_args *args;
6083 struct nfs4_get_lease_time_res *res;
6084 struct nfs_client *clp;
6085};
6086
6087static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6088 void *calldata)
6089{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006090 struct nfs4_get_lease_time_data *data =
6091 (struct nfs4_get_lease_time_data *)calldata;
6092
6093 dprintk("--> %s\n", __func__);
6094 /* just setup sequence, do not trigger session recovery
6095 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006096 nfs41_setup_sequence(data->clp->cl_session,
6097 &data->args->la_seq_args,
6098 &data->res->lr_seq_res,
6099 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006100 dprintk("<-- %s\n", __func__);
6101}
6102
6103/*
6104 * Called from nfs4_state_manager thread for session setup, so don't recover
6105 * from sequence operation or clientid errors.
6106 */
6107static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6108{
6109 struct nfs4_get_lease_time_data *data =
6110 (struct nfs4_get_lease_time_data *)calldata;
6111
6112 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006113 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6114 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006115 switch (task->tk_status) {
6116 case -NFS4ERR_DELAY:
6117 case -NFS4ERR_GRACE:
6118 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6119 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6120 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006121 /* fall through */
6122 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006123 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006124 return;
6125 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006126 dprintk("<-- %s\n", __func__);
6127}
6128
Trond Myklebust17280172012-03-11 13:11:00 -04006129static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006130 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6131 .rpc_call_done = nfs4_get_lease_time_done,
6132};
6133
6134int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6135{
6136 struct rpc_task *task;
6137 struct nfs4_get_lease_time_args args;
6138 struct nfs4_get_lease_time_res res = {
6139 .lr_fsinfo = fsinfo,
6140 };
6141 struct nfs4_get_lease_time_data data = {
6142 .args = &args,
6143 .res = &res,
6144 .clp = clp,
6145 };
6146 struct rpc_message msg = {
6147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6148 .rpc_argp = &args,
6149 .rpc_resp = &res,
6150 };
6151 struct rpc_task_setup task_setup = {
6152 .rpc_client = clp->cl_rpcclient,
6153 .rpc_message = &msg,
6154 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006155 .callback_data = &data,
6156 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006157 };
6158 int status;
6159
Trond Myklebust9d12b212012-01-17 22:04:25 -05006160 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006161 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006162 dprintk("--> %s\n", __func__);
6163 task = rpc_run_task(&task_setup);
6164
6165 if (IS_ERR(task))
6166 status = PTR_ERR(task);
6167 else {
6168 status = task->tk_status;
6169 rpc_put_task(task);
6170 }
6171 dprintk("<-- %s return %d\n", __func__, status);
6172
6173 return status;
6174}
6175
Andy Adamsonfc931582009-04-01 09:22:31 -04006176/*
6177 * Initialize the values to be used by the client in CREATE_SESSION
6178 * If nfs4_init_session set the fore channel request and response sizes,
6179 * use them.
6180 *
6181 * Set the back channel max_resp_sz_cached to zero to force the client to
6182 * always set csa_cachethis to FALSE because the current implementation
6183 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6184 */
6185static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6186{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006187 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006188
Andy Adamson18aad3d2013-06-26 12:21:49 -04006189 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6190 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6191
Andy Adamsonfc931582009-04-01 09:22:31 -04006192 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006193 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6194 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006195 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006196 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006197
6198 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006199 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006200 __func__,
6201 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006202 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006203
6204 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006205 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6206 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6207 args->bc_attrs.max_resp_sz_cached = 0;
6208 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6209 args->bc_attrs.max_reqs = 1;
6210
6211 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6212 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6213 __func__,
6214 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6215 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6216 args->bc_attrs.max_reqs);
6217}
6218
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006219static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006220{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006221 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6222 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6223
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006224 if (rcvd->max_resp_sz > sent->max_resp_sz)
6225 return -EINVAL;
6226 /*
6227 * Our requested max_ops is the minimum we need; we're not
6228 * prepared to break up compounds into smaller pieces than that.
6229 * So, no point even trying to continue if the server won't
6230 * cooperate:
6231 */
6232 if (rcvd->max_ops < sent->max_ops)
6233 return -EINVAL;
6234 if (rcvd->max_reqs == 0)
6235 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006236 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6237 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006238 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006239}
6240
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006241static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6242{
6243 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6244 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006245
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006246 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6247 return -EINVAL;
6248 if (rcvd->max_resp_sz < sent->max_resp_sz)
6249 return -EINVAL;
6250 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6251 return -EINVAL;
6252 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006253 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006254 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006255 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006256 return -EINVAL;
6257 return 0;
6258}
Andy Adamson8d353012009-04-01 09:22:32 -04006259
Andy Adamson8d353012009-04-01 09:22:32 -04006260static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6261 struct nfs4_session *session)
6262{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006263 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006264
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006265 ret = nfs4_verify_fore_channel_attrs(args, session);
6266 if (ret)
6267 return ret;
6268 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006269}
6270
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006271static int _nfs4_proc_create_session(struct nfs_client *clp,
6272 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006273{
6274 struct nfs4_session *session = clp->cl_session;
6275 struct nfs41_create_session_args args = {
6276 .client = clp,
6277 .cb_program = NFS4_CALLBACK,
6278 };
6279 struct nfs41_create_session_res res = {
6280 .client = clp,
6281 };
6282 struct rpc_message msg = {
6283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6284 .rpc_argp = &args,
6285 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006286 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006287 };
6288 int status;
6289
6290 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006291 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006292
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006293 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04006294
Trond Myklebust43095d32012-11-20 11:13:12 -05006295 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006296 /* Verify the session's negotiated channel_attrs values */
6297 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006298 /* Increment the clientid slot sequence id */
6299 clp->cl_seqid++;
6300 }
6301
6302 return status;
6303}
6304
6305/*
6306 * Issues a CREATE_SESSION operation to the server.
6307 * It is the responsibility of the caller to verify the session is
6308 * expired before calling this routine.
6309 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006310int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006311{
6312 int status;
6313 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006314 struct nfs4_session *session = clp->cl_session;
6315
6316 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6317
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006318 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006319 if (status)
6320 goto out;
6321
Andy Adamsonaacd5532011-11-09 13:58:21 -05006322 /* Init or reset the session slot tables */
6323 status = nfs4_setup_session_slot_tables(session);
6324 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006325 if (status)
6326 goto out;
6327
6328 ptr = (unsigned *)&session->sess_id.data[0];
6329 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6330 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006331out:
6332 dprintk("<-- %s\n", __func__);
6333 return status;
6334}
6335
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006336/*
6337 * Issue the over-the-wire RPC DESTROY_SESSION.
6338 * The caller must serialize access to this routine.
6339 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006340int nfs4_proc_destroy_session(struct nfs4_session *session,
6341 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006342{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006343 struct rpc_message msg = {
6344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6345 .rpc_argp = session,
6346 .rpc_cred = cred,
6347 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006348 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006349
6350 dprintk("--> nfs4_proc_destroy_session\n");
6351
6352 /* session is still being setup */
6353 if (session->clp->cl_cons_state != NFS_CS_READY)
6354 return status;
6355
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006356 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006357
6358 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006359 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006360 "Session has been destroyed regardless...\n", status);
6361
6362 dprintk("<-- nfs4_proc_destroy_session\n");
6363 return status;
6364}
6365
Trond Myklebust7b38c362012-05-23 13:23:31 -04006366/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006367 * Renew the cl_session lease.
6368 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006369struct nfs4_sequence_data {
6370 struct nfs_client *clp;
6371 struct nfs4_sequence_args args;
6372 struct nfs4_sequence_res res;
6373};
6374
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006375static void nfs41_sequence_release(void *data)
6376{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006377 struct nfs4_sequence_data *calldata = data;
6378 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006379
Alexandros Batsakis71358402010-02-05 03:45:05 -08006380 if (atomic_read(&clp->cl_count) > 1)
6381 nfs4_schedule_state_renewal(clp);
6382 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006383 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006384}
6385
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006386static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6387{
6388 switch(task->tk_status) {
6389 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006390 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6391 return -EAGAIN;
6392 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006393 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006394 }
6395 return 0;
6396}
6397
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006398static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006399{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006400 struct nfs4_sequence_data *calldata = data;
6401 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006402
Trond Myklebust14516c32010-07-31 14:29:06 -04006403 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6404 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006405
6406 if (task->tk_status < 0) {
6407 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006408 if (atomic_read(&clp->cl_count) == 1)
6409 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006410
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006411 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6412 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006413 return;
6414 }
6415 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006416 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006417out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006418 dprintk("<-- %s\n", __func__);
6419}
6420
6421static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6422{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006423 struct nfs4_sequence_data *calldata = data;
6424 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006425 struct nfs4_sequence_args *args;
6426 struct nfs4_sequence_res *res;
6427
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006428 args = task->tk_msg.rpc_argp;
6429 res = task->tk_msg.rpc_resp;
6430
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006431 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006432}
6433
6434static const struct rpc_call_ops nfs41_sequence_ops = {
6435 .rpc_call_done = nfs41_sequence_call_done,
6436 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006437 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006438};
6439
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006440static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6441 struct rpc_cred *cred,
6442 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006443{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006444 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006445 struct rpc_message msg = {
6446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6447 .rpc_cred = cred,
6448 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006449 struct rpc_task_setup task_setup_data = {
6450 .rpc_client = clp->cl_rpcclient,
6451 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006452 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006453 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006454 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006455
Alexandros Batsakis71358402010-02-05 03:45:05 -08006456 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006457 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006458 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006459 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006460 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006461 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006462 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006463 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006464 if (is_privileged)
6465 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006466 msg.rpc_argp = &calldata->args;
6467 msg.rpc_resp = &calldata->res;
6468 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006469 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006470
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006471 return rpc_run_task(&task_setup_data);
6472}
6473
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006474static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006475{
6476 struct rpc_task *task;
6477 int ret = 0;
6478
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006479 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6480 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006481 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006482 if (IS_ERR(task))
6483 ret = PTR_ERR(task);
6484 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006485 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006486 dprintk("<-- %s status=%d\n", __func__, ret);
6487 return ret;
6488}
6489
6490static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6491{
6492 struct rpc_task *task;
6493 int ret;
6494
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006495 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006496 if (IS_ERR(task)) {
6497 ret = PTR_ERR(task);
6498 goto out;
6499 }
6500 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006501 if (!ret) {
6502 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6503
6504 if (task->tk_status == 0)
6505 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006506 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006507 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006508 rpc_put_task(task);
6509out:
6510 dprintk("<-- %s status=%d\n", __func__, ret);
6511 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006512}
6513
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006514struct nfs4_reclaim_complete_data {
6515 struct nfs_client *clp;
6516 struct nfs41_reclaim_complete_args arg;
6517 struct nfs41_reclaim_complete_res res;
6518};
6519
6520static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6521{
6522 struct nfs4_reclaim_complete_data *calldata = data;
6523
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006524 nfs41_setup_sequence(calldata->clp->cl_session,
6525 &calldata->arg.seq_args,
6526 &calldata->res.seq_res,
6527 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006528}
6529
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006530static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6531{
6532 switch(task->tk_status) {
6533 case 0:
6534 case -NFS4ERR_COMPLETE_ALREADY:
6535 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6536 break;
6537 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006538 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006539 /* fall through */
6540 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006541 return -EAGAIN;
6542 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006543 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006544 }
6545 return 0;
6546}
6547
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006548static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6549{
6550 struct nfs4_reclaim_complete_data *calldata = data;
6551 struct nfs_client *clp = calldata->clp;
6552 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6553
6554 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006555 if (!nfs41_sequence_done(task, res))
6556 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006557
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006558 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6559 rpc_restart_call_prepare(task);
6560 return;
6561 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006562 dprintk("<-- %s\n", __func__);
6563}
6564
6565static void nfs4_free_reclaim_complete_data(void *data)
6566{
6567 struct nfs4_reclaim_complete_data *calldata = data;
6568
6569 kfree(calldata);
6570}
6571
6572static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6573 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6574 .rpc_call_done = nfs4_reclaim_complete_done,
6575 .rpc_release = nfs4_free_reclaim_complete_data,
6576};
6577
6578/*
6579 * Issue a global reclaim complete.
6580 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006581static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6582 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006583{
6584 struct nfs4_reclaim_complete_data *calldata;
6585 struct rpc_task *task;
6586 struct rpc_message msg = {
6587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006588 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006589 };
6590 struct rpc_task_setup task_setup_data = {
6591 .rpc_client = clp->cl_rpcclient,
6592 .rpc_message = &msg,
6593 .callback_ops = &nfs4_reclaim_complete_call_ops,
6594 .flags = RPC_TASK_ASYNC,
6595 };
6596 int status = -ENOMEM;
6597
6598 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006599 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006600 if (calldata == NULL)
6601 goto out;
6602 calldata->clp = clp;
6603 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006604
Trond Myklebust9d12b212012-01-17 22:04:25 -05006605 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006606 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006607 msg.rpc_argp = &calldata->arg;
6608 msg.rpc_resp = &calldata->res;
6609 task_setup_data.callback_data = calldata;
6610 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006611 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006612 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006613 goto out;
6614 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006615 status = nfs4_wait_for_completion_rpc_task(task);
6616 if (status == 0)
6617 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006618 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006619 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006620out:
6621 dprintk("<-- %s status=%d\n", __func__, status);
6622 return status;
6623}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006624
6625static void
6626nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6627{
6628 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006629 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006630 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006631
6632 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006633 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6634 * right now covering the LAYOUTGET we are about to send.
6635 * However, that is not so catastrophic, and there seems
6636 * to be no way to prevent it completely.
6637 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006638 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006639 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006640 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006641 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6642 NFS_I(lgp->args.inode)->layout,
6643 lgp->args.ctx->state)) {
6644 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006645 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006646}
6647
6648static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6649{
6650 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006651 struct inode *inode = lgp->args.inode;
6652 struct nfs_server *server = NFS_SERVER(inode);
6653 struct pnfs_layout_hdr *lo;
6654 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006655 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006656
6657 dprintk("--> %s\n", __func__);
6658
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006659 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006660 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006661
6662 switch (task->tk_status) {
6663 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006664 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006665 case -NFS4ERR_LAYOUTTRYLATER:
6666 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006667 timeo = rpc_get_timeout(task->tk_client);
6668 giveup = lgp->args.timestamp + timeo;
6669 if (time_after(giveup, jiffies))
6670 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006671 break;
6672 case -NFS4ERR_EXPIRED:
6673 case -NFS4ERR_BAD_STATEID:
6674 spin_lock(&inode->i_lock);
6675 lo = NFS_I(inode)->layout;
6676 if (!lo || list_empty(&lo->plh_segs)) {
6677 spin_unlock(&inode->i_lock);
6678 /* If the open stateid was bad, then recover it. */
6679 state = lgp->args.ctx->state;
6680 } else {
6681 LIST_HEAD(head);
6682
6683 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6684 spin_unlock(&inode->i_lock);
6685 /* Mark the bad layout state as invalid, then
6686 * retry using the open stateid. */
6687 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006688 }
6689 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006690 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6691 rpc_restart_call_prepare(task);
6692out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006693 dprintk("<-- %s\n", __func__);
6694}
6695
Idan Kedar85541162012-08-02 11:47:10 +03006696static size_t max_response_pages(struct nfs_server *server)
6697{
6698 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6699 return nfs_page_array_len(0, max_resp_sz);
6700}
6701
6702static void nfs4_free_pages(struct page **pages, size_t size)
6703{
6704 int i;
6705
6706 if (!pages)
6707 return;
6708
6709 for (i = 0; i < size; i++) {
6710 if (!pages[i])
6711 break;
6712 __free_page(pages[i]);
6713 }
6714 kfree(pages);
6715}
6716
6717static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6718{
6719 struct page **pages;
6720 int i;
6721
6722 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6723 if (!pages) {
6724 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6725 return NULL;
6726 }
6727
6728 for (i = 0; i < size; i++) {
6729 pages[i] = alloc_page(gfp_flags);
6730 if (!pages[i]) {
6731 dprintk("%s: failed to allocate page\n", __func__);
6732 nfs4_free_pages(pages, size);
6733 return NULL;
6734 }
6735 }
6736
6737 return pages;
6738}
6739
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006740static void nfs4_layoutget_release(void *calldata)
6741{
6742 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006743 struct inode *inode = lgp->args.inode;
6744 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006745 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006746
6747 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006748 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006749 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006750 put_nfs_open_context(lgp->args.ctx);
6751 kfree(calldata);
6752 dprintk("<-- %s\n", __func__);
6753}
6754
6755static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6756 .rpc_call_prepare = nfs4_layoutget_prepare,
6757 .rpc_call_done = nfs4_layoutget_done,
6758 .rpc_release = nfs4_layoutget_release,
6759};
6760
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006761struct pnfs_layout_segment *
6762nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006763{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006764 struct inode *inode = lgp->args.inode;
6765 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006766 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006767 struct rpc_task *task;
6768 struct rpc_message msg = {
6769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6770 .rpc_argp = &lgp->args,
6771 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006772 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006773 };
6774 struct rpc_task_setup task_setup_data = {
6775 .rpc_client = server->client,
6776 .rpc_message = &msg,
6777 .callback_ops = &nfs4_layoutget_call_ops,
6778 .callback_data = lgp,
6779 .flags = RPC_TASK_ASYNC,
6780 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006781 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006782 int status = 0;
6783
6784 dprintk("--> %s\n", __func__);
6785
Idan Kedar85541162012-08-02 11:47:10 +03006786 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6787 if (!lgp->args.layout.pages) {
6788 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006789 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006790 }
6791 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006792 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006793
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006794 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006795 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006796 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006797
6798 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6799 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6800
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006801 task = rpc_run_task(&task_setup_data);
6802 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006803 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006804 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006805 if (status == 0)
6806 status = task->tk_status;
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006807 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6808 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006809 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006810 rpc_put_task(task);
6811 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006812 if (status)
6813 return ERR_PTR(status);
6814 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006815}
6816
Benny Halevycbe82602011-05-22 19:52:37 +03006817static void
6818nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6819{
6820 struct nfs4_layoutreturn *lrp = calldata;
6821
6822 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006823 nfs41_setup_sequence(lrp->clp->cl_session,
6824 &lrp->args.seq_args,
6825 &lrp->res.seq_res,
6826 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006827}
6828
6829static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6830{
6831 struct nfs4_layoutreturn *lrp = calldata;
6832 struct nfs_server *server;
6833
6834 dprintk("--> %s\n", __func__);
6835
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006836 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006837 return;
6838
6839 server = NFS_SERVER(lrp->args.inode);
6840 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006841 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006842 return;
6843 }
Benny Halevycbe82602011-05-22 19:52:37 +03006844 dprintk("<-- %s\n", __func__);
6845}
6846
6847static void nfs4_layoutreturn_release(void *calldata)
6848{
6849 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006850 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006851
6852 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006853 spin_lock(&lo->plh_inode->i_lock);
6854 if (lrp->res.lrs_present)
6855 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6856 lo->plh_block_lgets--;
6857 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006858 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006859 kfree(calldata);
6860 dprintk("<-- %s\n", __func__);
6861}
6862
6863static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6864 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6865 .rpc_call_done = nfs4_layoutreturn_done,
6866 .rpc_release = nfs4_layoutreturn_release,
6867};
6868
6869int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6870{
6871 struct rpc_task *task;
6872 struct rpc_message msg = {
6873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6874 .rpc_argp = &lrp->args,
6875 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006876 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006877 };
6878 struct rpc_task_setup task_setup_data = {
6879 .rpc_client = lrp->clp->cl_rpcclient,
6880 .rpc_message = &msg,
6881 .callback_ops = &nfs4_layoutreturn_call_ops,
6882 .callback_data = lrp,
6883 };
6884 int status;
6885
6886 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006887 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006888 task = rpc_run_task(&task_setup_data);
6889 if (IS_ERR(task))
6890 return PTR_ERR(task);
6891 status = task->tk_status;
6892 dprintk("<-- %s status=%d\n", __func__, status);
6893 rpc_put_task(task);
6894 return status;
6895}
6896
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006897/*
6898 * Retrieve the list of Data Server devices from the MDS.
6899 */
6900static int _nfs4_getdevicelist(struct nfs_server *server,
6901 const struct nfs_fh *fh,
6902 struct pnfs_devicelist *devlist)
6903{
6904 struct nfs4_getdevicelist_args args = {
6905 .fh = fh,
6906 .layoutclass = server->pnfs_curr_ld->id,
6907 };
6908 struct nfs4_getdevicelist_res res = {
6909 .devlist = devlist,
6910 };
6911 struct rpc_message msg = {
6912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6913 .rpc_argp = &args,
6914 .rpc_resp = &res,
6915 };
6916 int status;
6917
6918 dprintk("--> %s\n", __func__);
6919 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6920 &res.seq_res, 0);
6921 dprintk("<-- %s status=%d\n", __func__, status);
6922 return status;
6923}
6924
6925int nfs4_proc_getdevicelist(struct nfs_server *server,
6926 const struct nfs_fh *fh,
6927 struct pnfs_devicelist *devlist)
6928{
6929 struct nfs4_exception exception = { };
6930 int err;
6931
6932 do {
6933 err = nfs4_handle_exception(server,
6934 _nfs4_getdevicelist(server, fh, devlist),
6935 &exception);
6936 } while (exception.retry);
6937
6938 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6939 err, devlist->num_devs);
6940
6941 return err;
6942}
6943EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6944
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006945static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006946_nfs4_proc_getdeviceinfo(struct nfs_server *server,
6947 struct pnfs_device *pdev,
6948 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006949{
6950 struct nfs4_getdeviceinfo_args args = {
6951 .pdev = pdev,
6952 };
6953 struct nfs4_getdeviceinfo_res res = {
6954 .pdev = pdev,
6955 };
6956 struct rpc_message msg = {
6957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6958 .rpc_argp = &args,
6959 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006960 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006961 };
6962 int status;
6963
6964 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006965 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006966 dprintk("<-- %s status=%d\n", __func__, status);
6967
6968 return status;
6969}
6970
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006971int nfs4_proc_getdeviceinfo(struct nfs_server *server,
6972 struct pnfs_device *pdev,
6973 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006974{
6975 struct nfs4_exception exception = { };
6976 int err;
6977
6978 do {
6979 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04006980 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006981 &exception);
6982 } while (exception.retry);
6983 return err;
6984}
6985EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6986
Andy Adamson863a3c62011-03-23 13:27:54 +00006987static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6988{
6989 struct nfs4_layoutcommit_data *data = calldata;
6990 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006991 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00006992
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006993 nfs41_setup_sequence(session,
6994 &data->args.seq_args,
6995 &data->res.seq_res,
6996 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00006997}
6998
6999static void
7000nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7001{
7002 struct nfs4_layoutcommit_data *data = calldata;
7003 struct nfs_server *server = NFS_SERVER(data->args.inode);
7004
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007005 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007006 return;
7007
7008 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007009 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7010 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7011 case -NFS4ERR_BADLAYOUT: /* no layout */
7012 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007013 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007014 break;
7015 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007016 nfs_post_op_update_inode_force_wcc(data->args.inode,
7017 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007018 break;
7019 default:
7020 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7021 rpc_restart_call_prepare(task);
7022 return;
7023 }
7024 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007025}
7026
7027static void nfs4_layoutcommit_release(void *calldata)
7028{
7029 struct nfs4_layoutcommit_data *data = calldata;
7030
Andy Adamsondb29c082011-07-30 20:52:38 -04007031 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007032 put_rpccred(data->cred);
7033 kfree(data);
7034}
7035
7036static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7037 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7038 .rpc_call_done = nfs4_layoutcommit_done,
7039 .rpc_release = nfs4_layoutcommit_release,
7040};
7041
7042int
Andy Adamsonef311532011-03-12 02:58:10 -05007043nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007044{
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7047 .rpc_argp = &data->args,
7048 .rpc_resp = &data->res,
7049 .rpc_cred = data->cred,
7050 };
7051 struct rpc_task_setup task_setup_data = {
7052 .task = &data->task,
7053 .rpc_client = NFS_CLIENT(data->args.inode),
7054 .rpc_message = &msg,
7055 .callback_ops = &nfs4_layoutcommit_ops,
7056 .callback_data = data,
7057 .flags = RPC_TASK_ASYNC,
7058 };
7059 struct rpc_task *task;
7060 int status = 0;
7061
7062 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7063 "lbw: %llu inode %lu\n",
7064 data->task.tk_pid, sync,
7065 data->args.lastbytewritten,
7066 data->args.inode->i_ino);
7067
Trond Myklebust9d12b212012-01-17 22:04:25 -05007068 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007069 task = rpc_run_task(&task_setup_data);
7070 if (IS_ERR(task))
7071 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007072 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007073 goto out;
7074 status = nfs4_wait_for_completion_rpc_task(task);
7075 if (status != 0)
7076 goto out;
7077 status = task->tk_status;
7078out:
7079 dprintk("%s: status %d\n", __func__, status);
7080 rpc_put_task(task);
7081 return status;
7082}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007083
7084static int
7085_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7086 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7087{
7088 struct nfs41_secinfo_no_name_args args = {
7089 .style = SECINFO_STYLE_CURRENT_FH,
7090 };
7091 struct nfs4_secinfo_res res = {
7092 .flavors = flavors,
7093 };
7094 struct rpc_message msg = {
7095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7096 .rpc_argp = &args,
7097 .rpc_resp = &res,
7098 };
7099 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
7100}
7101
7102static int
7103nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7104 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7105{
7106 struct nfs4_exception exception = { };
7107 int err;
7108 do {
7109 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7110 switch (err) {
7111 case 0:
7112 case -NFS4ERR_WRONGSEC:
7113 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007114 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007115 default:
7116 err = nfs4_handle_exception(server, err, &exception);
7117 }
7118 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007119out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007120 return err;
7121}
7122
7123static int
7124nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7125 struct nfs_fsinfo *info)
7126{
7127 int err;
7128 struct page *page;
7129 rpc_authflavor_t flavor;
7130 struct nfs4_secinfo_flavors *flavors;
7131
7132 page = alloc_page(GFP_KERNEL);
7133 if (!page) {
7134 err = -ENOMEM;
7135 goto out;
7136 }
7137
7138 flavors = page_address(page);
7139 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7140
7141 /*
7142 * Fall back on "guess and check" method if
7143 * the server doesn't support SECINFO_NO_NAME
7144 */
7145 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7146 err = nfs4_find_root_sec(server, fhandle, info);
7147 goto out_freepage;
7148 }
7149 if (err)
7150 goto out_freepage;
7151
7152 flavor = nfs_find_best_sec(flavors);
7153 if (err == 0)
7154 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7155
7156out_freepage:
7157 put_page(page);
7158 if (err == -EACCES)
7159 return -EPERM;
7160out:
7161 return err;
7162}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007163
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007164static int _nfs41_test_stateid(struct nfs_server *server,
7165 nfs4_stateid *stateid,
7166 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007167{
7168 int status;
7169 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007170 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007171 };
7172 struct nfs41_test_stateid_res res;
7173 struct rpc_message msg = {
7174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7175 .rpc_argp = &args,
7176 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007177 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007178 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007179
Chuck Lever38527b12012-07-11 16:30:23 -04007180 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007181 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007182 nfs4_set_sequence_privileged(&args.seq_args);
7183 status = nfs4_call_sync_sequence(server->client, server, &msg,
7184 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007185 if (status != NFS_OK) {
7186 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007187 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007188 }
7189 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007190 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007191}
7192
Chuck Lever38527b12012-07-11 16:30:23 -04007193/**
7194 * nfs41_test_stateid - perform a TEST_STATEID operation
7195 *
7196 * @server: server / transport on which to perform the operation
7197 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007198 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007199 *
7200 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7201 * Otherwise a negative NFS4ERR value is returned if the operation
7202 * failed or the state ID is not currently valid.
7203 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007204static int nfs41_test_stateid(struct nfs_server *server,
7205 nfs4_stateid *stateid,
7206 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007207{
7208 struct nfs4_exception exception = { };
7209 int err;
7210 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007211 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007212 if (err != -NFS4ERR_DELAY)
7213 break;
7214 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007215 } while (exception.retry);
7216 return err;
7217}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007218
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007219struct nfs_free_stateid_data {
7220 struct nfs_server *server;
7221 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007222 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007223};
7224
7225static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7226{
7227 struct nfs_free_stateid_data *data = calldata;
7228 nfs41_setup_sequence(nfs4_get_session(data->server),
7229 &data->args.seq_args,
7230 &data->res.seq_res,
7231 task);
7232}
7233
7234static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7235{
7236 struct nfs_free_stateid_data *data = calldata;
7237
7238 nfs41_sequence_done(task, &data->res.seq_res);
7239
7240 switch (task->tk_status) {
7241 case -NFS4ERR_DELAY:
7242 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7243 rpc_restart_call_prepare(task);
7244 }
7245}
7246
7247static void nfs41_free_stateid_release(void *calldata)
7248{
7249 kfree(calldata);
7250}
7251
7252const struct rpc_call_ops nfs41_free_stateid_ops = {
7253 .rpc_call_prepare = nfs41_free_stateid_prepare,
7254 .rpc_call_done = nfs41_free_stateid_done,
7255 .rpc_release = nfs41_free_stateid_release,
7256};
7257
7258static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7259 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007260 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007261 bool privileged)
7262{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007263 struct rpc_message msg = {
7264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007265 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007266 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007267 struct rpc_task_setup task_setup = {
7268 .rpc_client = server->client,
7269 .rpc_message = &msg,
7270 .callback_ops = &nfs41_free_stateid_ops,
7271 .flags = RPC_TASK_ASYNC,
7272 };
7273 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007274
Chuck Lever38527b12012-07-11 16:30:23 -04007275 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007276 data = kmalloc(sizeof(*data), GFP_NOFS);
7277 if (!data)
7278 return ERR_PTR(-ENOMEM);
7279 data->server = server;
7280 nfs4_stateid_copy(&data->args.stateid, stateid);
7281
7282 task_setup.callback_data = data;
7283
7284 msg.rpc_argp = &data->args;
7285 msg.rpc_resp = &data->res;
7286 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7287 if (privileged)
7288 nfs4_set_sequence_privileged(&data->args.seq_args);
7289
7290 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007291}
7292
Chuck Lever38527b12012-07-11 16:30:23 -04007293/**
7294 * nfs41_free_stateid - perform a FREE_STATEID operation
7295 *
7296 * @server: server / transport on which to perform the operation
7297 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007298 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007299 *
7300 * Returns NFS_OK if the server freed "stateid". Otherwise a
7301 * negative NFS4ERR value is returned.
7302 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007303static int nfs41_free_stateid(struct nfs_server *server,
7304 nfs4_stateid *stateid,
7305 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007306{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007307 struct rpc_task *task;
7308 int ret;
7309
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007310 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007311 if (IS_ERR(task))
7312 return PTR_ERR(task);
7313 ret = rpc_wait_for_completion_task(task);
7314 if (!ret)
7315 ret = task->tk_status;
7316 rpc_put_task(task);
7317 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007318}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007319
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007320static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7321{
7322 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007323 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007324
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007325 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007326 nfs4_free_lock_state(server, lsp);
7327 if (IS_ERR(task))
7328 return PTR_ERR(task);
7329 rpc_put_task(task);
7330 return 0;
7331}
7332
Trond Myklebust36281ca2012-03-04 18:13:56 -05007333static bool nfs41_match_stateid(const nfs4_stateid *s1,
7334 const nfs4_stateid *s2)
7335{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007336 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007337 return false;
7338
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007339 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007340 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007341 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007342 return true;
7343
7344 return false;
7345}
7346
Andy Adamson557134a2009-04-01 09:21:53 -04007347#endif /* CONFIG_NFS_V4_1 */
7348
Trond Myklebust36281ca2012-03-04 18:13:56 -05007349static bool nfs4_match_stateid(const nfs4_stateid *s1,
7350 const nfs4_stateid *s2)
7351{
Trond Myklebustf597c532012-03-04 18:13:56 -05007352 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007353}
7354
7355
Trond Myklebust17280172012-03-11 13:11:00 -04007356static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007357 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007358 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007359 .recover_open = nfs4_open_reclaim,
7360 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007361 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007362 .get_clid_cred = nfs4_get_setclientid_cred,
Chuck Lever05f4c352012-09-14 17:24:32 -04007363 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007364};
7365
Andy Adamson591d71c2009-04-01 09:22:47 -04007366#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007367static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007368 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7369 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7370 .recover_open = nfs4_open_reclaim,
7371 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007372 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007373 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007374 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007375 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007376};
7377#endif /* CONFIG_NFS_V4_1 */
7378
Trond Myklebust17280172012-03-11 13:11:00 -04007379static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007380 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007381 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 .recover_open = nfs4_open_expired,
7383 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007384 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04007385 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386};
7387
Andy Adamson591d71c2009-04-01 09:22:47 -04007388#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007389static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007390 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7391 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007392 .recover_open = nfs41_open_expired,
7393 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007394 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04007395 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04007396};
7397#endif /* CONFIG_NFS_V4_1 */
7398
Trond Myklebust17280172012-03-11 13:11:00 -04007399static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007400 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007401 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007402 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007403};
7404
7405#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007406static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007407 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007408 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007409 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007410};
7411#endif
7412
Trond Myklebust97dc1352010-06-16 09:52:26 -04007413static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7414 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007415 .init_caps = NFS_CAP_READDIRPLUS
7416 | NFS_CAP_ATOMIC_OPEN
7417 | NFS_CAP_CHANGE_ATTR
7418 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007419 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007420 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007421 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007422 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007423 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7424 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7425 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007426};
7427
7428#if defined(CONFIG_NFS_V4_1)
7429static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7430 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007431 .init_caps = NFS_CAP_READDIRPLUS
7432 | NFS_CAP_ATOMIC_OPEN
7433 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007434 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007435 | NFS_CAP_STATEID_NFSV41
7436 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007437 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007438 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007439 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007440 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007441 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7442 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7443 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007444};
7445#endif
7446
Steve Dickson42c2c422013-05-22 12:50:38 -04007447#if defined(CONFIG_NFS_V4_2)
7448static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7449 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007450 .init_caps = NFS_CAP_READDIRPLUS
7451 | NFS_CAP_ATOMIC_OPEN
7452 | NFS_CAP_CHANGE_ATTR
7453 | NFS_CAP_POSIX_LOCK
7454 | NFS_CAP_STATEID_NFSV41
7455 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007456 .call_sync = nfs4_call_sync_sequence,
7457 .match_stateid = nfs41_match_stateid,
7458 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007459 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007460 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7461 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7462 .state_renewal_ops = &nfs41_state_renewal_ops,
7463};
7464#endif
7465
Trond Myklebust97dc1352010-06-16 09:52:26 -04007466const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7467 [0] = &nfs_v4_0_minor_ops,
7468#if defined(CONFIG_NFS_V4_1)
7469 [1] = &nfs_v4_1_minor_ops,
7470#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007471#if defined(CONFIG_NFS_V4_2)
7472 [2] = &nfs_v4_2_minor_ops,
7473#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007474};
7475
Bryan Schumaker73a79702012-07-16 16:39:12 -04007476const struct inode_operations nfs4_dir_inode_operations = {
7477 .create = nfs_create,
7478 .lookup = nfs_lookup,
7479 .atomic_open = nfs_atomic_open,
7480 .link = nfs_link,
7481 .unlink = nfs_unlink,
7482 .symlink = nfs_symlink,
7483 .mkdir = nfs_mkdir,
7484 .rmdir = nfs_rmdir,
7485 .mknod = nfs_mknod,
7486 .rename = nfs_rename,
7487 .permission = nfs_permission,
7488 .getattr = nfs_getattr,
7489 .setattr = nfs_setattr,
7490 .getxattr = generic_getxattr,
7491 .setxattr = generic_setxattr,
7492 .listxattr = generic_listxattr,
7493 .removexattr = generic_removexattr,
7494};
7495
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007496static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007497 .permission = nfs_permission,
7498 .getattr = nfs_getattr,
7499 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007500 .getxattr = generic_getxattr,
7501 .setxattr = generic_setxattr,
7502 .listxattr = generic_listxattr,
7503 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007504};
7505
David Howells509de812006-08-22 20:06:11 -04007506const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007507 .version = 4, /* protocol version */
7508 .dentry_ops = &nfs4_dentry_operations,
7509 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007510 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007511 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007513 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007514 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 .getattr = nfs4_proc_getattr,
7516 .setattr = nfs4_proc_setattr,
7517 .lookup = nfs4_proc_lookup,
7518 .access = nfs4_proc_access,
7519 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007520 .create = nfs4_proc_create,
7521 .remove = nfs4_proc_remove,
7522 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007523 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007524 .unlink_done = nfs4_proc_unlink_done,
7525 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007526 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007527 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007528 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007529 .link = nfs4_proc_link,
7530 .symlink = nfs4_proc_symlink,
7531 .mkdir = nfs4_proc_mkdir,
7532 .rmdir = nfs4_proc_remove,
7533 .readdir = nfs4_proc_readdir,
7534 .mknod = nfs4_proc_mknod,
7535 .statfs = nfs4_proc_statfs,
7536 .fsinfo = nfs4_proc_fsinfo,
7537 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007538 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 .decode_dirent = nfs4_decode_dirent,
7540 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007541 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007542 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007543 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007544 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007545 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007546 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007547 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007549 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007550 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007551 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007552 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007553 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007554 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007555 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007556 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007557 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007558 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007559 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007560 .create_server = nfs4_create_server,
7561 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007562};
7563
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007564static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7565 .prefix = XATTR_NAME_NFSV4_ACL,
7566 .list = nfs4_xattr_list_nfs4_acl,
7567 .get = nfs4_xattr_get_nfs4_acl,
7568 .set = nfs4_xattr_set_nfs4_acl,
7569};
7570
7571const struct xattr_handler *nfs4_xattr_handlers[] = {
7572 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007573#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7574 &nfs4_xattr_nfs4_label_handler,
7575#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007576 NULL
7577};
7578
Linus Torvalds1da177e2005-04-16 15:20:36 -07007579/*
7580 * Local variables:
7581 * c-basic-offset: 8
7582 * End:
7583 */