blob: c866e325577fb3a3cc4c379cfc81d2a170f133ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
David Quigleyaa9c2662013-05-22 12:50:44 -0400108 err = security_dentry_init_security(dentry, sattr->ia_mode,
109 &dentry->d_name, (void **)&label->label, &label->len);
110 if (err == 0)
111 return label;
112
113 return NULL;
114}
115static inline void
116nfs4_label_release_security(struct nfs4_label *label)
117{
118 if (label)
119 security_release_secctx(label->label, label->len);
120}
121static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
122{
123 if (label)
124 return server->attr_bitmask;
125
126 return server->attr_bitmask_nl;
127}
128#else
129static inline struct nfs4_label *
130nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
131 struct iattr *sattr, struct nfs4_label *l)
132{ return NULL; }
133static inline void
134nfs4_label_release_security(struct nfs4_label *label)
135{ return; }
136static inline u32 *
137nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{ return server->attr_bitmask; }
139#endif
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500142static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Trond Myklebust52567b02009-10-23 14:46:42 -0400144 if (err >= -1000)
145 return err;
146 switch (err) {
147 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500148 case -NFS4ERR_LAYOUTTRYLATER:
149 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400150 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000151 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400152 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000153 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800154 case -NFS4ERR_BADOWNER:
155 case -NFS4ERR_BADNAME:
156 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400157 case -NFS4ERR_SHARE_DENIED:
158 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400159 case -NFS4ERR_MINOR_VERS_MISMATCH:
160 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400161 case -NFS4ERR_ACCESS:
162 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400163 case -NFS4ERR_FILE_OPEN:
164 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700167 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400168 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
173/*
174 * This is our standard bitmap for GETATTR requests.
175 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400176const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 FATTR4_WORD0_TYPE
178 | FATTR4_WORD0_CHANGE
179 | FATTR4_WORD0_SIZE
180 | FATTR4_WORD0_FSID
181 | FATTR4_WORD0_FILEID,
182 FATTR4_WORD1_MODE
183 | FATTR4_WORD1_NUMLINKS
184 | FATTR4_WORD1_OWNER
185 | FATTR4_WORD1_OWNER_GROUP
186 | FATTR4_WORD1_RAWDEV
187 | FATTR4_WORD1_SPACE_USED
188 | FATTR4_WORD1_TIME_ACCESS
189 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400190 | FATTR4_WORD1_TIME_MODIFY,
191#ifdef CONFIG_NFS_V4_SECURITY_LABEL
192 FATTR4_WORD2_SECURITY_LABEL
193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Trond Myklebust1549210f2012-06-05 09:16:47 -0400196static const u32 nfs4_pnfs_open_bitmap[3] = {
197 FATTR4_WORD0_TYPE
198 | FATTR4_WORD0_CHANGE
199 | FATTR4_WORD0_SIZE
200 | FATTR4_WORD0_FSID
201 | FATTR4_WORD0_FILEID,
202 FATTR4_WORD1_MODE
203 | FATTR4_WORD1_NUMLINKS
204 | FATTR4_WORD1_OWNER
205 | FATTR4_WORD1_OWNER_GROUP
206 | FATTR4_WORD1_RAWDEV
207 | FATTR4_WORD1_SPACE_USED
208 | FATTR4_WORD1_TIME_ACCESS
209 | FATTR4_WORD1_TIME_METADATA
210 | FATTR4_WORD1_TIME_MODIFY,
211 FATTR4_WORD2_MDSTHRESHOLD
212};
213
Andy Adamsone23008e2012-10-02 21:07:32 -0400214static const u32 nfs4_open_noattr_bitmap[3] = {
215 FATTR4_WORD0_TYPE
216 | FATTR4_WORD0_CHANGE
217 | FATTR4_WORD0_FILEID,
218};
219
David Quigleya09df2c2013-05-22 12:50:41 -0400220const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 FATTR4_WORD0_FILES_AVAIL
222 | FATTR4_WORD0_FILES_FREE
223 | FATTR4_WORD0_FILES_TOTAL,
224 FATTR4_WORD1_SPACE_AVAIL
225 | FATTR4_WORD1_SPACE_FREE
226 | FATTR4_WORD1_SPACE_TOTAL
227};
228
David Quigleya09df2c2013-05-22 12:50:41 -0400229const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 FATTR4_WORD0_MAXLINK
231 | FATTR4_WORD0_MAXNAME,
232 0
233};
234
Fred Isamandae100c2011-07-30 20:52:37 -0400235const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 | FATTR4_WORD0_MAXREAD
237 | FATTR4_WORD0_MAXWRITE
238 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700239 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400240 | FATTR4_WORD1_FS_LAYOUT_TYPES,
241 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242};
243
David Quigleya09df2c2013-05-22 12:50:41 -0400244const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400245 FATTR4_WORD0_TYPE
246 | FATTR4_WORD0_CHANGE
247 | FATTR4_WORD0_SIZE
248 | FATTR4_WORD0_FSID
249 | FATTR4_WORD0_FILEID
250 | FATTR4_WORD0_FS_LOCATIONS,
251 FATTR4_WORD1_MODE
252 | FATTR4_WORD1_NUMLINKS
253 | FATTR4_WORD1_OWNER
254 | FATTR4_WORD1_OWNER_GROUP
255 | FATTR4_WORD1_RAWDEV
256 | FATTR4_WORD1_SPACE_USED
257 | FATTR4_WORD1_TIME_ACCESS
258 | FATTR4_WORD1_TIME_METADATA
259 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400260 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400261};
262
Al Virobc4785c2006-10-19 23:28:51 -0700263static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 struct nfs4_readdir_arg *readdir)
265{
Al Viro0dbb4c62006-10-19 23:28:49 -0700266 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000269 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
271 return;
272 }
273
274 readdir->cookie = 0;
275 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
276 if (cookie == 2)
277 return;
278
279 /*
280 * NFSv4 servers do not return entries for '.' and '..'
281 * Therefore, we fake these entries here. We let '.'
282 * have cookie 0 and '..' have cookie 1. Note that
283 * when talking to the server, we always send cookie 0
284 * instead of 1 or 2.
285 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800286 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (cookie == 0) {
289 *p++ = xdr_one; /* next */
290 *p++ = xdr_zero; /* cookie, first word */
291 *p++ = xdr_one; /* cookie, second word */
292 *p++ = xdr_one; /* entry len */
293 memcpy(p, ".\0\0\0", 4); /* entry */
294 p++;
295 *p++ = xdr_one; /* bitmap length */
296 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
297 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400298 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 *p++ = xdr_one; /* next */
302 *p++ = xdr_zero; /* cookie, first word */
303 *p++ = xdr_two; /* cookie, second word */
304 *p++ = xdr_two; /* entry len */
305 memcpy(p, "..\0\0", 4); /* entry */
306 p++;
307 *p++ = xdr_one; /* bitmap length */
308 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
309 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400310 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 readdir->pgbase = (char *)p - (char *)start;
313 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800314 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Trond Myklebust65de8722008-12-23 15:21:44 -0500317static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
318{
319 int res = 0;
320
321 might_sleep();
322
323 if (*timeout <= 0)
324 *timeout = NFS4_POLL_RETRY_MIN;
325 if (*timeout > NFS4_POLL_RETRY_MAX)
326 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000327 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 if (fatal_signal_pending(current))
329 res = -ERESTARTSYS;
330 *timeout <<= 1;
331 return res;
332}
333
334/* This is the error handling routine for processes that are allowed
335 * to sleep.
336 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800337static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500338{
339 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500340 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500341 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 int ret = errorcode;
343
344 exception->retry = 0;
345 switch(errorcode) {
346 case 0:
347 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500348 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400349 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400350 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500351 exception->retry = 1;
352 return 0;
353 }
354 if (state == NULL)
355 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400356 ret = nfs4_schedule_stateid_recovery(server, state);
357 if (ret < 0)
358 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500359 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500360 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500361 case -NFS4ERR_ADMIN_REVOKED:
362 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400363 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
364 nfs_remove_bad_delegation(inode);
365 exception->retry = 1;
366 break;
367 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500368 if (state == NULL)
369 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400370 ret = nfs4_schedule_stateid_recovery(server, state);
371 if (ret < 0)
372 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500373 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400374 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400375 if (state != NULL) {
376 ret = nfs4_schedule_stateid_recovery(server, state);
377 if (ret < 0)
378 break;
379 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500380 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500381 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500382 nfs4_schedule_lease_recovery(clp);
383 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400384 case -NFS4ERR_MOVED:
385 ret = nfs4_schedule_migration_recovery(server);
386 if (ret < 0)
387 break;
388 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400389 case -NFS4ERR_LEASE_MOVED:
390 nfs4_schedule_lease_moved_recovery(clp);
391 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500392#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400393 case -NFS4ERR_BADSESSION:
394 case -NFS4ERR_BADSLOT:
395 case -NFS4ERR_BAD_HIGH_SLOT:
396 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
397 case -NFS4ERR_DEADSESSION:
398 case -NFS4ERR_SEQ_FALSE_RETRY:
399 case -NFS4ERR_SEQ_MISORDERED:
400 dprintk("%s ERROR: %d Reset session\n", __func__,
401 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400402 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400403 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500404#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500406 if (exception->timeout > HZ) {
407 /* We have retried a decent amount, time to
408 * fail
409 */
410 ret = -EBUSY;
411 break;
412 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_GRACE:
414 case -NFS4ERR_DELAY:
415 ret = nfs4_delay(server->client, &exception->timeout);
416 if (ret != 0)
417 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400418 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500419 case -NFS4ERR_OLD_STATEID:
420 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800421 break;
422 case -NFS4ERR_BADOWNER:
423 /* The following works around a Linux server bug! */
424 case -NFS4ERR_BADNAME:
425 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
426 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
427 exception->retry = 1;
428 printk(KERN_WARNING "NFS: v4 server %s "
429 "does not accept raw "
430 "uid/gids. "
431 "Reenabling the idmapper.\n",
432 server->nfs_client->cl_hostname);
433 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 }
435 /* We failed to handle the error */
436 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500437wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500438 ret = nfs4_wait_clnt_recover(clp);
Chuck Lever519ae252013-10-17 14:13:19 -0400439 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
440 return -EIO;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500441 if (ret == 0)
442 exception->retry = 1;
443 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500444}
445
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400446/*
447 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
448 * or 'false' otherwise.
449 */
450static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
451{
452 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
453
454 if (flavor == RPC_AUTH_GSS_KRB5I ||
455 flavor == RPC_AUTH_GSS_KRB5P)
456 return true;
457
458 return false;
459}
Trond Myklebust65de8722008-12-23 15:21:44 -0500460
Trond Myklebust452e9352010-07-31 14:29:06 -0400461static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 spin_lock(&clp->cl_lock);
464 if (time_before(clp->cl_last_renewal,timestamp))
465 clp->cl_last_renewal = timestamp;
466 spin_unlock(&clp->cl_lock);
467}
468
Trond Myklebust452e9352010-07-31 14:29:06 -0400469static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
470{
471 do_renew_lease(server->nfs_client, timestamp);
472}
473
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400474struct nfs4_call_sync_data {
475 const struct nfs_server *seq_server;
476 struct nfs4_sequence_args *seq_args;
477 struct nfs4_sequence_res *seq_res;
478};
479
Chuck Levera9c92d62013-08-09 12:48:18 -0400480static void nfs4_init_sequence(struct nfs4_sequence_args *args,
481 struct nfs4_sequence_res *res, int cache_reply)
482{
483 args->sa_slot = NULL;
484 args->sa_cache_this = cache_reply;
485 args->sa_privileged = 0;
486
487 res->sr_slot = NULL;
488}
489
490static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
491{
492 args->sa_privileged = 1;
493}
494
Chuck Lever3bd23842013-08-09 12:49:19 -0400495static int nfs40_setup_sequence(const struct nfs_server *server,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 struct rpc_task *task)
499{
500 struct nfs4_slot_table *tbl = server->nfs_client->cl_slot_tbl;
501 struct nfs4_slot *slot;
502
503 /* slot already allocated? */
504 if (res->sr_slot != NULL)
505 goto out_start;
506
507 spin_lock(&tbl->slot_tbl_lock);
508 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
509 goto out_sleep;
510
511 slot = nfs4_alloc_slot(tbl);
512 if (IS_ERR(slot)) {
513 if (slot == ERR_PTR(-ENOMEM))
514 task->tk_timeout = HZ >> 2;
515 goto out_sleep;
516 }
517 spin_unlock(&tbl->slot_tbl_lock);
518
519 args->sa_slot = slot;
520 res->sr_slot = slot;
521
522out_start:
523 rpc_call_start(task);
524 return 0;
525
526out_sleep:
527 if (args->sa_privileged)
528 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
529 NULL, RPC_PRIORITY_PRIVILEGED);
530 else
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 return -EAGAIN;
534}
535
536static int nfs40_sequence_done(struct rpc_task *task,
537 struct nfs4_sequence_res *res)
538{
539 struct nfs4_slot *slot = res->sr_slot;
540 struct nfs4_slot_table *tbl;
541
Trond Myklebustcab92c12014-01-29 12:12:15 -0500542 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400543 goto out;
544
545 tbl = slot->table;
546 spin_lock(&tbl->slot_tbl_lock);
547 if (!nfs41_wake_and_assign_slot(tbl, slot))
548 nfs4_free_slot(tbl, slot);
549 spin_unlock(&tbl->slot_tbl_lock);
550
551 res->sr_slot = NULL;
552out:
553 return 1;
554}
555
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400556#if defined(CONFIG_NFS_V4_1)
557
Trond Myklebustd185a332010-06-16 09:52:25 -0400558static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400559{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500560 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400561 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500562 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500563 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400564
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500565 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500566 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500567
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500568 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500569 /* Be nice to the server: try to ensure that the last transmitted
570 * value for highest_user_slotid <= target_highest_slotid
571 */
572 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
573 send_new_highest_used_slotid = true;
574
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500575 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500576 send_new_highest_used_slotid = false;
577 goto out_unlock;
578 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500579 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500580
581 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
582 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500583out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500584 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400585 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500586 if (send_new_highest_used_slotid)
587 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400588}
589
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500590int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400591{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500592 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500593 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400594 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500595 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500596 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400597
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500598 if (slot == NULL)
599 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400600 /* don't increment the sequence number if the task wasn't sent */
601 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400602 goto out;
603
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500604 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500605
Trond Myklebustac20d162012-12-15 15:36:07 -0500606 if (slot->interrupted) {
607 slot->interrupted = 0;
608 interrupted = true;
609 }
610
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400611 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500612 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400613 switch (res->sr_status) {
614 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400615 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500616 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500617 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500618 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500619 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500620 if (res->sr_status_flags != 0)
621 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500622 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400623 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500624 case 1:
625 /*
626 * sr_status remains 1 if an RPC level error occurred.
627 * The server may or may not have processed the sequence
628 * operation..
629 * Mark the slot as having hosted an interrupted RPC call.
630 */
631 slot->interrupted = 1;
632 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400633 case -NFS4ERR_DELAY:
634 /* The server detected a resend of the RPC call and
635 * returned NFS4ERR_DELAY as per Section 2.10.6.2
636 * of RFC5661.
637 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500638 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400639 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500640 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500641 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400642 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500643 case -NFS4ERR_BADSLOT:
644 /*
645 * The slot id we used was probably retired. Try again
646 * using a different slot id.
647 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500648 goto retry_nowait;
649 case -NFS4ERR_SEQ_MISORDERED:
650 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500651 * Was the last operation on this sequence interrupted?
652 * If so, retry after bumping the sequence number.
653 */
654 if (interrupted) {
655 ++slot->seq_nr;
656 goto retry_nowait;
657 }
658 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500659 * Could this slot have been previously retired?
660 * If so, then the server may be expecting seq_nr = 1!
661 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500662 if (slot->seq_nr != 1) {
663 slot->seq_nr = 1;
664 goto retry_nowait;
665 }
666 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500667 case -NFS4ERR_SEQ_FALSE_RETRY:
668 ++slot->seq_nr;
669 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400670 default:
671 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500672 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400673 }
674out:
675 /* The session may be reset by one of the error handlers. */
676 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400677 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500678out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500679 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500680retry_nowait:
681 if (rpc_restart_call_prepare(task)) {
682 task->tk_status = 0;
683 ret = 0;
684 }
685 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400686out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400687 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 goto out;
689 rpc_delay(task, NFS4_POLL_RETRY_MAX);
690 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500692EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400693
Trond Myklebust14516c32010-07-31 14:29:06 -0400694static int nfs4_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400698 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 if (!res->sr_slot->table->session)
700 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400701 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400702}
703
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000704int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 struct rpc_task *task)
708{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400709 struct nfs4_slot *slot;
710 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400711
712 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400714 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400715 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400716
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400717 tbl = &session->fc_slot_table;
718
Trond Myklebust69d206b2012-11-22 13:21:02 -0500719 task->tk_timeout = 0;
720
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400721 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400722 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400723 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500724 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500725 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400726 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400727 }
728
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500729 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500730 if (IS_ERR(slot)) {
731 /* If out of memory, try again in 1/4 second */
732 if (slot == ERR_PTR(-ENOMEM))
733 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400734 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400735 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400736 }
737 spin_unlock(&tbl->slot_tbl_lock);
738
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500739 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400740
Chuck Levere8d92382013-08-09 12:47:51 -0400741 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500742 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400743
Benny Halevydfb4f3092010-09-24 09:17:01 -0400744 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500745 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400746 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400747 /*
748 * sr_status is only set in decode_sequence, and so will remain
749 * set to 1 if an rpc level failure occurs.
750 */
751 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400752 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400753out_success:
754 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400755 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400756out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400757 /* Privileged tasks are queued with top priority */
758 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400759 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
760 NULL, RPC_PRIORITY_PRIVILEGED);
761 else
762 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400763 spin_unlock(&tbl->slot_tbl_lock);
764 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400765}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000766EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400767
Chuck Lever5a580e02013-08-09 12:48:09 -0400768static int nfs4_setup_sequence(const struct nfs_server *server,
769 struct nfs4_sequence_args *args,
770 struct nfs4_sequence_res *res,
771 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400772{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400773 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400774 int ret = 0;
775
Chuck Lever3bd23842013-08-09 12:49:19 -0400776 if (!session)
777 return nfs40_setup_sequence(server, args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400778
Chuck Levere8d92382013-08-09 12:47:51 -0400779 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400780 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400781 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400782
Trond Myklebust9d12b212012-01-17 22:04:25 -0500783 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400784
Andy Adamsonce5039c2009-04-01 09:22:13 -0400785 dprintk("<-- %s status=%d\n", __func__, ret);
786 return ret;
787}
788
Andy Adamsonce5039c2009-04-01 09:22:13 -0400789static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
790{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400791 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400792 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793
Trond Myklebust035168ab2010-06-16 09:52:26 -0400794 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
795
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400796 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400797}
798
Andy Adamson69ab40c2009-04-01 09:22:19 -0400799static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
800{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400801 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400802
Trond Myklebust14516c32010-07-31 14:29:06 -0400803 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400804}
805
Trond Myklebust17280172012-03-11 13:11:00 -0400806static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400807 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400808 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400809};
810
Chuck Lever3bd23842013-08-09 12:49:19 -0400811#else /* !CONFIG_NFS_V4_1 */
812
Chuck Lever5a580e02013-08-09 12:48:09 -0400813static int nfs4_setup_sequence(const struct nfs_server *server,
814 struct nfs4_sequence_args *args,
815 struct nfs4_sequence_res *res,
816 struct rpc_task *task)
817{
Chuck Lever3bd23842013-08-09 12:49:19 -0400818 return nfs40_setup_sequence(server, args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400819}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400820
Trond Myklebust14516c32010-07-31 14:29:06 -0400821static int nfs4_sequence_done(struct rpc_task *task,
822 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400823{
Chuck Lever3bd23842013-08-09 12:49:19 -0400824 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400825}
Chuck Lever3bd23842013-08-09 12:49:19 -0400826
827#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400828
Chuck Lever9915ea72013-08-09 12:48:27 -0400829static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
830{
831 struct nfs4_call_sync_data *data = calldata;
832 nfs4_setup_sequence(data->seq_server,
833 data->seq_args, data->seq_res, task);
834}
835
836static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
837{
838 struct nfs4_call_sync_data *data = calldata;
839 nfs4_sequence_done(task, data->seq_res);
840}
841
842static const struct rpc_call_ops nfs40_call_sync_ops = {
843 .rpc_call_prepare = nfs40_call_sync_prepare,
844 .rpc_call_done = nfs40_call_sync_done,
845};
846
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400847static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400848 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100850 struct nfs4_sequence_args *args,
851 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400852{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100853 int ret;
854 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400855 struct nfs_client *clp = server->nfs_client;
856 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100857 .seq_server = server,
858 .seq_args = args,
859 .seq_res = res,
860 };
861 struct rpc_task_setup task_setup = {
862 .rpc_client = clnt,
863 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400864 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865 .callback_data = &data
866 };
867
868 task = rpc_run_task(&task_setup);
869 if (IS_ERR(task))
870 ret = PTR_ERR(task);
871 else {
872 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400873 rpc_put_task(task);
874 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100875 return ret;
876}
877
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400878static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000879int nfs4_call_sync(struct rpc_clnt *clnt,
880 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000881 struct rpc_message *msg,
882 struct nfs4_sequence_args *args,
883 struct nfs4_sequence_res *res,
884 int cache_reply)
885{
Chuck Levera9c92d62013-08-09 12:48:18 -0400886 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400887 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000888}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
891{
892 struct nfs_inode *nfsi = NFS_I(dir);
893
894 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400895 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700896 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700898 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000899 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 spin_unlock(&dir->i_lock);
901}
902
903struct nfs4_opendata {
904 struct kref kref;
905 struct nfs_openargs o_arg;
906 struct nfs_openres o_res;
907 struct nfs_open_confirmargs c_arg;
908 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500909 struct nfs4_string owner_name;
910 struct nfs4_string group_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400912 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400914 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nfs4_state_owner *owner;
916 struct nfs4_state *state;
917 struct iattr attrs;
918 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400919 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -0400920 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400921 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -0400922 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int cancelled;
924};
Trond Myklebustdecf4912005-10-27 22:12:39 -0400925
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400926static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
927 int err, struct nfs4_exception *exception)
928{
929 if (err != -EINVAL)
930 return false;
931 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
932 return false;
933 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
934 exception->retry = 1;
935 return true;
936}
937
938static enum open_claim_type4
939nfs4_map_atomic_open_claim(struct nfs_server *server,
940 enum open_claim_type4 claim)
941{
942 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
943 return claim;
944 switch (claim) {
945 default:
946 return claim;
947 case NFS4_OPEN_CLAIM_FH:
948 return NFS4_OPEN_CLAIM_NULL;
949 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
950 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
951 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
952 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
953 }
954}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957{
958 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400959 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 p->o_res.seqid = p->o_arg.seqid;
961 p->c_res.seqid = p->c_arg.seqid;
962 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400963 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100964 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500965 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966}
967
Al Viro82a2c1b2011-06-22 18:30:55 -0400968static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100969 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400970 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400971 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400972 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400973 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100974{
Al Viro82a2c1b2011-06-22 18:30:55 -0400975 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100976 struct inode *dir = parent->d_inode;
977 struct nfs_server *server = NFS_SERVER(dir);
978 struct nfs4_opendata *p;
979
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400980 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100981 if (p == NULL)
982 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400983
984 p->f_label = nfs4_label_alloc(server, gfp_mask);
985 if (IS_ERR(p->f_label))
986 goto err_free_p;
987
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400988 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100989 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400990 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400991 nfs_sb_active(dentry->d_sb);
992 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100993 p->dir = parent;
994 p->owner = sp;
995 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996 p->o_arg.open_flags = flags;
997 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700998 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
999 * will return permission denied for all bits until close */
1000 if (!(flags & O_EXCL)) {
1001 /* ask server to check for all possible rights as results
1002 * are cached */
1003 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1004 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001005 }
David Howells7539bba2006-08-22 20:06:09 -04001006 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001007 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1008 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001009 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001011 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001012 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -04001013 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001014 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1015 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001016 case NFS4_OPEN_CLAIM_NULL:
1017 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1018 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1019 p->o_arg.fh = NFS_FH(dir);
1020 break;
1021 case NFS4_OPEN_CLAIM_PREVIOUS:
1022 case NFS4_OPEN_CLAIM_FH:
1023 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1024 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1025 p->o_arg.fh = NFS_FH(dentry->d_inode);
1026 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001027 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001028 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001029
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 p->o_arg.u.attrs = &p->attrs;
1031 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001032
1033 verf[0] = jiffies;
1034 verf[1] = current->pid;
1035 memcpy(p->o_arg.u.verifier.data, verf,
1036 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001037 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001038 p->c_arg.fh = &p->o_res.fh;
1039 p->c_arg.stateid = &p->o_res.stateid;
1040 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001041 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001042 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001044
1045err_free_label:
1046 nfs4_label_free(p->f_label);
1047err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 kfree(p);
1049err:
1050 dput(parent);
1051 return NULL;
1052}
1053
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001054static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001055{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001056 struct nfs4_opendata *p = container_of(kref,
1057 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001058 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001059
1060 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001061 if (p->state != NULL)
1062 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001063 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001064
1065 nfs4_label_free(p->f_label);
1066
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001067 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001068 dput(p->dentry);
1069 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001070 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001071 kfree(p);
1072}
1073
1074static void nfs4_opendata_put(struct nfs4_opendata *p)
1075{
1076 if (p != NULL)
1077 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001078}
1079
Trond Myklebust06f814a2006-01-03 09:55:07 +01001080static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1081{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001082 int ret;
1083
Trond Myklebust06f814a2006-01-03 09:55:07 +01001084 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001085 return ret;
1086}
1087
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001089{
1090 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091
Trond Myklebust536e43d2012-01-17 22:04:26 -05001092 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093 goto out;
1094 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001095 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001096 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1097 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001098 break;
1099 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001100 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1101 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001102 break;
1103 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001104 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1105 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001106 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001108 return ret;
1109}
1110
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001112{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001113 if (delegation == NULL)
1114 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001115 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001116 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001117 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001118 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001119 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1120 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001121 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001122 return 1;
1123}
1124
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001125static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001126{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001127 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001128 case FMODE_WRITE:
1129 state->n_wronly++;
1130 break;
1131 case FMODE_READ:
1132 state->n_rdonly++;
1133 break;
1134 case FMODE_READ|FMODE_WRITE:
1135 state->n_rdwr++;
1136 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001138}
1139
Trond Myklebust4f14c192014-02-12 19:15:06 -05001140static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001141{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001142 struct nfs_client *clp = state->owner->so_server->nfs_client;
1143 bool need_recover = false;
1144
1145 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1146 need_recover = true;
1147 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1148 need_recover = true;
1149 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1150 need_recover = true;
1151 if (need_recover)
1152 nfs4_state_mark_reclaim_nograce(clp, state);
1153}
1154
Trond Myklebuste999e802014-02-10 18:20:47 -05001155static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1156 nfs4_stateid *stateid)
1157{
1158 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1159 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001160 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1161 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001162 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001163 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001164 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1165 return true;
1166 return false;
1167}
1168
Trond Myklebust226056c2014-02-11 10:41:07 -05001169static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1170 nfs4_stateid *stateid, fmode_t fmode)
1171{
1172 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1173 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1174 case FMODE_WRITE:
1175 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1176 break;
1177 case FMODE_READ:
1178 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1179 break;
1180 case 0:
1181 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1183 clear_bit(NFS_OPEN_STATE, &state->flags);
1184 }
1185 if (stateid == NULL)
1186 return;
1187 if (!nfs_need_update_open_stateid(state, stateid))
1188 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001189 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001190 nfs4_stateid_copy(&state->stateid, stateid);
1191 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001192}
1193
1194static void nfs_clear_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1195{
1196 write_seqlock(&state->seqlock);
1197 nfs_clear_open_stateid_locked(state, stateid, fmode);
1198 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001199 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1200 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001201}
1202
Trond Myklebust003707c2007-07-05 18:07:55 -04001203static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1204{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001205 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001206 case FMODE_READ:
1207 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1208 break;
1209 case FMODE_WRITE:
1210 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1211 break;
1212 case FMODE_READ|FMODE_WRITE:
1213 set_bit(NFS_O_RDWR_STATE, &state->flags);
1214 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001215 if (!nfs_need_update_open_stateid(state, stateid))
1216 return;
1217 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1218 nfs4_stateid_copy(&state->stateid, stateid);
1219 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001220}
1221
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001224 /*
1225 * Protect the call to nfs4_state_set_mode_locked and
1226 * serialise the stateid update
1227 */
1228 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001229 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001230 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001231 set_bit(NFS_DELEGATED_STATE, &state->flags);
1232 }
1233 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001234 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001235 write_sequnlock(&state->seqlock);
1236 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001238 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001241static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001242{
1243 struct nfs_inode *nfsi = NFS_I(state->inode);
1244 struct nfs_delegation *deleg_cur;
1245 int ret = 0;
1246
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001247 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001248
1249 rcu_read_lock();
1250 deleg_cur = rcu_dereference(nfsi->delegation);
1251 if (deleg_cur == NULL)
1252 goto no_delegation;
1253
1254 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001255 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001256 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001257 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001258 goto no_delegation_unlock;
1259
1260 if (delegation == NULL)
1261 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001262 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001263 goto no_delegation_unlock;
1264
Trond Myklebustb7391f42008-12-23 15:21:52 -05001265 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001266 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001267 ret = 1;
1268no_delegation_unlock:
1269 spin_unlock(&deleg_cur->lock);
1270no_delegation:
1271 rcu_read_unlock();
1272
1273 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001274 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001275 ret = 1;
1276 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001277 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1278 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001279
1280 return ret;
1281}
1282
1283
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001284static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285{
1286 struct nfs_delegation *delegation;
1287
1288 rcu_read_lock();
1289 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001291 rcu_read_unlock();
1292 return;
1293 }
1294 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001295 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001296}
1297
Trond Myklebust6ee41262007-07-08 14:11:36 -04001298static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001299{
1300 struct nfs4_state *state = opendata->state;
1301 struct nfs_inode *nfsi = NFS_I(state->inode);
1302 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001303 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001304 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001305 nfs4_stateid stateid;
1306 int ret = -EAGAIN;
1307
Trond Myklebustaac00a82007-07-05 19:02:21 -04001308 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311 if (can_open_cached(state, fmode, open_mode)) {
1312 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001313 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001314 goto out_return_state;
1315 }
1316 spin_unlock(&state->owner->so_lock);
1317 }
Trond Myklebust34310432008-12-23 15:21:38 -05001318 rcu_read_lock();
1319 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001320 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001321 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001322 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001323 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001324 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001325 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001326 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001327 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001328 if (!opendata->is_recover) {
1329 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1330 if (ret != 0)
1331 goto out;
1332 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001333 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001334
1335 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001336 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001337 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001339out:
1340 return ERR_PTR(ret);
1341out_return_state:
1342 atomic_inc(&state->count);
1343 return state;
1344}
1345
Andy Adamsone23008e2012-10-02 21:07:32 -04001346static void
1347nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1348{
1349 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1350 struct nfs_delegation *delegation;
1351 int delegation_flags = 0;
1352
1353 rcu_read_lock();
1354 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1355 if (delegation)
1356 delegation_flags = delegation->flags;
1357 rcu_read_unlock();
1358 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1359 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1360 "returning a delegation for "
1361 "OPEN(CLAIM_DELEGATE_CUR)\n",
1362 clp->cl_hostname);
1363 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1364 nfs_inode_set_delegation(state->inode,
1365 data->owner->so_cred,
1366 &data->o_res);
1367 else
1368 nfs_inode_reclaim_delegation(state->inode,
1369 data->owner->so_cred,
1370 &data->o_res);
1371}
1372
1373/*
1374 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1375 * and update the nfs4_state.
1376 */
1377static struct nfs4_state *
1378_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1379{
1380 struct inode *inode = data->state->inode;
1381 struct nfs4_state *state = data->state;
1382 int ret;
1383
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001384 if (!data->rpc_done) {
1385 if (data->rpc_status) {
1386 ret = data->rpc_status;
1387 goto err;
1388 }
1389 /* cached opens have already been processed */
1390 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001391 }
1392
Andy Adamsone23008e2012-10-02 21:07:32 -04001393 ret = nfs_refresh_inode(inode, &data->f_attr);
1394 if (ret)
1395 goto err;
1396
1397 if (data->o_res.delegation_type != 0)
1398 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001399update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001400 update_open_stateid(state, &data->o_res.stateid, NULL,
1401 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001402 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001403
1404 return state;
1405err:
1406 return ERR_PTR(ret);
1407
1408}
1409
1410static struct nfs4_state *
1411_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412{
1413 struct inode *inode;
1414 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001415 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416
Trond Myklebustaac00a82007-07-05 19:02:21 -04001417 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001418 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 goto out;
1420 }
1421
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001422 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001424 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001425 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001426 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001427 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001428 goto err;
1429 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 state = nfs4_get_open_state(inode, data->owner);
1431 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001432 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001433 if (data->o_res.delegation_type != 0)
1434 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001435 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001436 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001439 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001441err_put_inode:
1442 iput(inode);
1443err:
1444 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445}
1446
Andy Adamsone23008e2012-10-02 21:07:32 -04001447static struct nfs4_state *
1448nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1449{
1450 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1451 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1452 return _nfs4_opendata_to_nfs4_state(data);
1453}
1454
Trond Myklebust864472e2006-01-03 09:55:15 +01001455static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1456{
1457 struct nfs_inode *nfsi = NFS_I(state->inode);
1458 struct nfs_open_context *ctx;
1459
1460 spin_lock(&state->inode->i_lock);
1461 list_for_each_entry(ctx, &nfsi->open_files, list) {
1462 if (ctx->state != state)
1463 continue;
1464 get_nfs_open_context(ctx);
1465 spin_unlock(&state->inode->i_lock);
1466 return ctx;
1467 }
1468 spin_unlock(&state->inode->i_lock);
1469 return ERR_PTR(-ENOENT);
1470}
1471
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001472static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1473 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001474{
1475 struct nfs4_opendata *opendata;
1476
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001477 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001478 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001479 if (opendata == NULL)
1480 return ERR_PTR(-ENOMEM);
1481 opendata->state = state;
1482 atomic_inc(&state->count);
1483 return opendata;
1484}
1485
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001486static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001487{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001488 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001489 int ret;
1490
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001491 opendata->o_arg.open_flags = 0;
1492 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001493 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1494 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1495 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001496 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001497 if (ret != 0)
1498 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001499 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001500 if (IS_ERR(newstate))
1501 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001502 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001503 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001504 return 0;
1505}
1506
1507static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1508{
Trond Myklebust864472e2006-01-03 09:55:15 +01001509 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001510 int ret;
1511
Trond Myklebust4f14c192014-02-12 19:15:06 -05001512 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1513 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1514 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001516 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001517 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001518 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001519 smp_rmb();
1520 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001521 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001522 if (ret != 0)
1523 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001524 if (newstate != state)
1525 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001526 }
1527 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001528 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001529 if (ret != 0)
1530 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001531 if (newstate != state)
1532 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001533 }
1534 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001535 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001536 if (ret != 0)
1537 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001538 if (newstate != state)
1539 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001540 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001541 /*
1542 * We may have performed cached opens for all three recoveries.
1543 * Check if we need to update the current stateid.
1544 */
1545 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001546 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001547 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001549 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001550 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001551 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001552 return 0;
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
1556 * OPEN_RECLAIM:
1557 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001559static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001561 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001562 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 int status;
1565
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001566 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1567 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001568 if (IS_ERR(opendata))
1569 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001570 rcu_read_lock();
1571 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001572 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001573 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001574 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001575 opendata->o_arg.u.delegation_type = delegation_type;
1576 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001577 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return status;
1579}
1580
Trond Myklebust539cd032007-06-05 11:46:42 -04001581static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
1583 struct nfs_server *server = NFS_SERVER(state->inode);
1584 struct nfs4_exception exception = { };
1585 int err;
1586 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001587 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001588 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001589 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1590 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001591 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001592 break;
1593 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 } while (exception.retry);
1595 return err;
1596}
1597
Trond Myklebust864472e2006-01-03 09:55:15 +01001598static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1599{
1600 struct nfs_open_context *ctx;
1601 int ret;
1602
1603 ctx = nfs4_state_find_open_context(state);
1604 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001605 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001606 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 put_nfs_open_context(ctx);
1608 return ret;
1609}
1610
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001611static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001613 switch (err) {
1614 default:
1615 printk(KERN_ERR "NFS: %s: unhandled error "
1616 "%d.\n", __func__, err);
1617 case 0:
1618 case -ENOENT:
1619 case -ESTALE:
1620 break;
1621 case -NFS4ERR_BADSESSION:
1622 case -NFS4ERR_BADSLOT:
1623 case -NFS4ERR_BAD_HIGH_SLOT:
1624 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1625 case -NFS4ERR_DEADSESSION:
1626 set_bit(NFS_DELEGATED_STATE, &state->flags);
1627 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1628 return -EAGAIN;
1629 case -NFS4ERR_STALE_CLIENTID:
1630 case -NFS4ERR_STALE_STATEID:
1631 set_bit(NFS_DELEGATED_STATE, &state->flags);
1632 case -NFS4ERR_EXPIRED:
1633 /* Don't recall a delegation if it was lost */
1634 nfs4_schedule_lease_recovery(server->nfs_client);
1635 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001636 case -NFS4ERR_MOVED:
1637 nfs4_schedule_migration_recovery(server);
1638 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001639 case -NFS4ERR_LEASE_MOVED:
1640 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1641 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001642 case -NFS4ERR_DELEG_REVOKED:
1643 case -NFS4ERR_ADMIN_REVOKED:
1644 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001645 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001646 nfs_inode_find_state_and_recover(state->inode,
1647 stateid);
1648 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001649 return 0;
1650 case -NFS4ERR_DELAY:
1651 case -NFS4ERR_GRACE:
1652 set_bit(NFS_DELEGATED_STATE, &state->flags);
1653 ssleep(1);
1654 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001655 case -ENOMEM:
1656 case -NFS4ERR_DENIED:
1657 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1658 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 return err;
1661}
1662
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001663int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1664{
1665 struct nfs_server *server = NFS_SERVER(state->inode);
1666 struct nfs4_opendata *opendata;
1667 int err;
1668
1669 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1670 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1671 if (IS_ERR(opendata))
1672 return PTR_ERR(opendata);
1673 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1674 err = nfs4_open_recover(opendata, state);
1675 nfs4_opendata_put(opendata);
1676 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1677}
1678
Chuck Leverbe05c862013-08-09 12:49:47 -04001679static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1680{
1681 struct nfs4_opendata *data = calldata;
1682
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001683 nfs40_setup_sequence(data->o_arg.server, &data->c_arg.seq_args,
1684 &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001685}
1686
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001687static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1688{
1689 struct nfs4_opendata *data = calldata;
1690
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001691 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001692
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001693 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001694 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001695 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001696 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001697 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001698 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001699 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001700}
1701
1702static void nfs4_open_confirm_release(void *calldata)
1703{
1704 struct nfs4_opendata *data = calldata;
1705 struct nfs4_state *state = NULL;
1706
1707 /* If this request hasn't been cancelled, do nothing */
1708 if (data->cancelled == 0)
1709 goto out_free;
1710 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001711 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001712 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001713 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001714 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001715 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001716out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001717 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001718}
1719
1720static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001721 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001722 .rpc_call_done = nfs4_open_confirm_done,
1723 .rpc_release = nfs4_open_confirm_release,
1724};
1725
1726/*
1727 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1728 */
1729static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1730{
1731 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1732 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001733 struct rpc_message msg = {
1734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1735 .rpc_argp = &data->c_arg,
1736 .rpc_resp = &data->c_res,
1737 .rpc_cred = data->owner->so_cred,
1738 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001739 struct rpc_task_setup task_setup_data = {
1740 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001741 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001742 .callback_ops = &nfs4_open_confirm_ops,
1743 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001744 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001745 .flags = RPC_TASK_ASYNC,
1746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 int status;
1748
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001749 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001750 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001751 data->rpc_done = 0;
1752 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001753 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001754 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001755 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001756 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001757 status = nfs4_wait_for_completion_rpc_task(task);
1758 if (status != 0) {
1759 data->cancelled = 1;
1760 smp_wmb();
1761 } else
1762 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001763 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 return status;
1765}
1766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769 struct nfs4_opendata *data = calldata;
1770 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001771 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001772
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001774 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001775 /*
1776 * Check if we still need to send an OPEN call, or if we can use
1777 * a delegation instead.
1778 */
1779 if (data->state != NULL) {
1780 struct nfs_delegation *delegation;
1781
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001782 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001783 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001784 rcu_read_lock();
1785 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001786 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001787 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001788 can_open_delegated(delegation, data->o_arg.fmode))
1789 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001790 rcu_read_unlock();
1791 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001792 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001793 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001794 switch (data->o_arg.claim) {
1795 case NFS4_OPEN_CLAIM_PREVIOUS:
1796 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1797 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001798 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001799 case NFS4_OPEN_CLAIM_FH:
1800 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001801 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1802 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001803 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001804 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001805 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001806 &data->o_res.seq_res,
1807 task) != 0)
1808 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001809
1810 /* Set the create mode (note dependency on the session type) */
1811 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1812 if (data->o_arg.open_flags & O_EXCL) {
1813 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1814 if (nfs4_has_persistent_session(clp))
1815 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1816 else if (clp->cl_mvops->minor_version > 0)
1817 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1818 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001819 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001820unlock_no_action:
1821 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001822out_no_action:
1823 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001824out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001825 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001826}
1827
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828static void nfs4_open_done(struct rpc_task *task, void *calldata)
1829{
1830 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001832 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001833
Trond Myklebust14516c32010-07-31 14:29:06 -04001834 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1835 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001836
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001837 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001838 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1839 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001840 case S_IFREG:
1841 break;
1842 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001844 break;
1845 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001846 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001847 break;
1848 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001849 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001850 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001851 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001852 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001853 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1854 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001855 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001856 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001857}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001858
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001859static void nfs4_open_release(void *calldata)
1860{
1861 struct nfs4_opendata *data = calldata;
1862 struct nfs4_state *state = NULL;
1863
1864 /* If this request hasn't been cancelled, do nothing */
1865 if (data->cancelled == 0)
1866 goto out_free;
1867 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001868 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001869 goto out_free;
1870 /* In case we need an open_confirm, no cleanup! */
1871 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1872 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001873 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001874 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001875 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001876out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001877 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001878}
1879
1880static const struct rpc_call_ops nfs4_open_ops = {
1881 .rpc_call_prepare = nfs4_open_prepare,
1882 .rpc_call_done = nfs4_open_done,
1883 .rpc_release = nfs4_open_release,
1884};
1885
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001886static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001887{
1888 struct inode *dir = data->dir->d_inode;
1889 struct nfs_server *server = NFS_SERVER(dir);
1890 struct nfs_openargs *o_arg = &data->o_arg;
1891 struct nfs_openres *o_res = &data->o_res;
1892 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001893 struct rpc_message msg = {
1894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1895 .rpc_argp = o_arg,
1896 .rpc_resp = o_res,
1897 .rpc_cred = data->owner->so_cred,
1898 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001899 struct rpc_task_setup task_setup_data = {
1900 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001901 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001902 .callback_ops = &nfs4_open_ops,
1903 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001904 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001905 .flags = RPC_TASK_ASYNC,
1906 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001907 int status;
1908
Chuck Levera9c92d62013-08-09 12:48:18 -04001909 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001910 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001911 data->rpc_done = 0;
1912 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001913 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001914 data->is_recover = 0;
1915 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001916 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001917 data->is_recover = 1;
1918 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001919 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001920 if (IS_ERR(task))
1921 return PTR_ERR(task);
1922 status = nfs4_wait_for_completion_rpc_task(task);
1923 if (status != 0) {
1924 data->cancelled = 1;
1925 smp_wmb();
1926 } else
1927 status = data->rpc_status;
1928 rpc_put_task(task);
1929
1930 return status;
1931}
1932
1933static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1934{
1935 struct inode *dir = data->dir->d_inode;
1936 struct nfs_openres *o_res = &data->o_res;
1937 int status;
1938
1939 status = nfs4_run_open_task(data, 1);
1940 if (status != 0 || !data->rpc_done)
1941 return status;
1942
Trond Myklebust6926afd2012-01-07 13:22:46 -05001943 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1944
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001945 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1946 status = _nfs4_proc_open_confirm(data);
1947 if (status != 0)
1948 return status;
1949 }
1950
1951 return status;
1952}
1953
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001954static int nfs4_opendata_access(struct rpc_cred *cred,
1955 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001956 struct nfs4_state *state, fmode_t fmode,
1957 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001958{
1959 struct nfs_access_entry cache;
1960 u32 mask;
1961
1962 /* access call failed or for some reason the server doesn't
1963 * support any access modes -- defer access call until later */
1964 if (opendata->o_res.access_supported == 0)
1965 return 0;
1966
1967 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001968 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001969 * write mode bits, but POSIX allows the creating process to write.
1970 * use openflags to check for exec, because fmode won't
1971 * always have FMODE_EXEC set when file open for exec. */
1972 if (openflags & __FMODE_EXEC) {
1973 /* ONLY check for exec rights */
1974 mask = MAY_EXEC;
1975 } else if (fmode & FMODE_READ)
1976 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001977
1978 cache.cred = cred;
1979 cache.jiffies = jiffies;
1980 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1981 nfs_access_add_cache(state->inode, &cache);
1982
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001983 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001984 return 0;
1985
1986 /* even though OPEN succeeded, access is denied. Close the file */
1987 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001988 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001989}
1990
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001991/*
1992 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1993 */
1994static int _nfs4_proc_open(struct nfs4_opendata *data)
1995{
1996 struct inode *dir = data->dir->d_inode;
1997 struct nfs_server *server = NFS_SERVER(dir);
1998 struct nfs_openargs *o_arg = &data->o_arg;
1999 struct nfs_openres *o_res = &data->o_res;
2000 int status;
2001
2002 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002003 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002004 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002005 if (status != 0) {
2006 if (status == -NFS4ERR_BADNAME &&
2007 !(o_arg->open_flags & O_CREAT))
2008 return -ENOENT;
2009 return status;
2010 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002011
Trond Myklebust6926afd2012-01-07 13:22:46 -05002012 nfs_fattr_map_and_free_names(server, &data->f_attr);
2013
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002014 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002015 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002016 if (o_arg->open_flags & O_EXCL)
2017 data->file_created = 1;
2018 else if (o_res->cinfo.before != o_res->cinfo.after)
2019 data->file_created = 1;
2020 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002021 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2022 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002024 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002026 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04002029 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002030 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Andy Adamsond83217c2011-03-01 01:34:17 +00002033static int nfs4_recover_expired_lease(struct nfs_server *server)
2034{
2035 return nfs4_client_recover_expired_lease(server->nfs_client);
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/*
2039 * OPEN_EXPIRED:
2040 * reclaim state on the server after a network partition.
2041 * Assumes caller holds the appropriate lock
2042 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002043static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002045 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002046 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002048 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002049 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002050 if (IS_ERR(opendata))
2051 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002052 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002053 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002054 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002055 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002056 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
2058
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002059static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002060{
Trond Myklebust539cd032007-06-05 11:46:42 -04002061 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002062 struct nfs4_exception exception = { };
2063 int err;
2064
2065 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002066 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002067 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002068 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2069 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002070 switch (err) {
2071 default:
2072 goto out;
2073 case -NFS4ERR_GRACE:
2074 case -NFS4ERR_DELAY:
2075 nfs4_handle_exception(server, err, &exception);
2076 err = 0;
2077 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002078 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002079out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002080 return err;
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Trond Myklebust864472e2006-01-03 09:55:15 +01002088 ctx = nfs4_state_find_open_context(state);
2089 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002090 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002091 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002092 put_nfs_open_context(ctx);
2093 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002096#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002097static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002098{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002099 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002100 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002101 struct nfs_delegation *delegation;
2102 struct rpc_cred *cred = NULL;
2103 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002104
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002105 /* If a state reset has been done, test_stateid is unneeded */
2106 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
2107 return;
2108
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002109 /* Get the delegation credential for use by test/free_stateid */
2110 rcu_read_lock();
2111 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
2112 if (delegation != NULL &&
2113 nfs4_stateid_match(&delegation->stateid, stateid)) {
2114 cred = get_rpccred(delegation->cred);
2115 rcu_read_unlock();
2116 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002117 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002118 } else
2119 rcu_read_unlock();
2120
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002121 if (status != NFS_OK) {
2122 /* Free the stateid unless the server explicitly
2123 * informs us the stateid is unrecognized. */
2124 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002125 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04002126 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002127
Bryan Schumaker69388672012-09-26 15:25:52 -04002128 write_seqlock(&state->seqlock);
2129 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2130 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002131 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2132 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002133
2134 if (cred != NULL)
2135 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002136}
2137
2138/**
2139 * nfs41_check_open_stateid - possibly free an open stateid
2140 *
2141 * @state: NFSv4 state for an inode
2142 *
2143 * Returns NFS_OK if recovery for this stateid is now finished.
2144 * Otherwise a negative NFS4ERR value is returned.
2145 */
2146static int nfs41_check_open_stateid(struct nfs4_state *state)
2147{
2148 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002149 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002150 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002151 int status;
2152
2153 /* If a state reset has been done, test_stateid is unneeded */
2154 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2155 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2156 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2157 return -NFS4ERR_BAD_STATEID;
2158
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002159 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002160 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002161 if (status != NFS_OK) {
2162 /* Free the stateid unless the server explicitly
2163 * informs us the stateid is unrecognized. */
2164 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002165 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002166
2167 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2168 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2169 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002170 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002171 }
2172 return status;
2173}
2174
2175static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2176{
Chuck Levereb64cf92012-07-11 16:30:05 -04002177 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002178
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002179 nfs41_clear_delegation_stateid(state);
2180 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002181 if (status != NFS_OK)
2182 status = nfs4_open_expired(sp, state);
2183 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002184}
2185#endif
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002188 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2189 * fields corresponding to attributes that were used to store the verifier.
2190 * Make sure we clobber those fields in the later setattr call
2191 */
2192static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2193{
2194 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2195 !(sattr->ia_valid & ATTR_ATIME_SET))
2196 sattr->ia_valid |= ATTR_ATIME;
2197
2198 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2199 !(sattr->ia_valid & ATTR_MTIME_SET))
2200 sattr->ia_valid |= ATTR_MTIME;
2201}
2202
Trond Myklebustc21443c2013-02-07 14:26:21 -05002203static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2204 fmode_t fmode,
2205 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002206 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002207{
2208 struct nfs4_state_owner *sp = opendata->owner;
2209 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002210 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002211 struct nfs4_state *state;
2212 unsigned int seq;
2213 int ret;
2214
2215 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2216
2217 ret = _nfs4_proc_open(opendata);
2218 if (ret != 0)
2219 goto out;
2220
2221 state = nfs4_opendata_to_nfs4_state(opendata);
2222 ret = PTR_ERR(state);
2223 if (IS_ERR(state))
2224 goto out;
2225 if (server->caps & NFS_CAP_POSIX_LOCK)
2226 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2227
Trond Myklebust275bb302013-05-29 13:11:28 -04002228 dentry = opendata->dentry;
2229 if (dentry->d_inode == NULL) {
2230 /* FIXME: Is this d_drop() ever needed? */
2231 d_drop(dentry);
2232 dentry = d_add_unique(dentry, igrab(state->inode));
2233 if (dentry == NULL) {
2234 dentry = opendata->dentry;
2235 } else if (dentry != ctx->dentry) {
2236 dput(ctx->dentry);
2237 ctx->dentry = dget(dentry);
2238 }
2239 nfs_set_verifier(dentry,
2240 nfs_save_change_attribute(opendata->dir->d_inode));
2241 }
2242
Trond Myklebustc21443c2013-02-07 14:26:21 -05002243 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2244 if (ret != 0)
2245 goto out;
2246
Trond Myklebust3efb9722013-05-29 13:17:04 -04002247 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002248 if (dentry->d_inode == state->inode) {
2249 nfs_inode_attach_open_context(ctx);
2250 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2251 nfs4_schedule_stateid_recovery(server, state);
2252 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002253out:
2254 return ret;
2255}
2256
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002257/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 */
Andy Adamson82be4172012-05-23 05:02:35 -04002260static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002261 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002262 int flags,
2263 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002264 struct nfs4_label *label,
2265 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
2267 struct nfs4_state_owner *sp;
2268 struct nfs4_state *state = NULL;
2269 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002270 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002271 struct dentry *dentry = ctx->dentry;
2272 struct rpc_cred *cred = ctx->cred;
2273 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2274 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002275 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002276 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002277 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002281 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2282 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2284 goto out_err;
2285 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002286 status = nfs4_recover_expired_lease(server);
2287 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002288 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002289 if (dentry->d_inode != NULL)
2290 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002291 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002292 if (dentry->d_inode)
2293 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002294 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002295 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002296 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002297 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
David Quigley14c43f72013-05-22 12:50:43 -04002299 if (label) {
2300 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2301 if (IS_ERR(olabel)) {
2302 status = PTR_ERR(olabel);
2303 goto err_opendata_put;
2304 }
2305 }
2306
Andy Adamson82be4172012-05-23 05:02:35 -04002307 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2308 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2309 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002310 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002311 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002312 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002313 if (dentry->d_inode != NULL)
2314 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002315
Trond Myklebust3efb9722013-05-29 13:17:04 -04002316 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002317 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002318 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002319 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002320
Trond Myklebust549b19c2013-04-16 18:42:34 -04002321 if ((opendata->o_arg.open_flags & O_EXCL) &&
2322 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002323 nfs4_exclusive_attrset(opendata, sattr);
2324
2325 nfs_fattr_init(opendata->o_res.f_attr);
2326 status = nfs4_do_setattr(state->inode, cred,
2327 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002328 state, label, olabel);
2329 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002330 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002331 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002332 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002333 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002334 }
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002335 if (opendata->file_created)
2336 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002337
2338 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2339 *ctx_th = opendata->f_attr.mdsthreshold;
2340 else
2341 kfree(opendata->f_attr.mdsthreshold);
2342 opendata->f_attr.mdsthreshold = NULL;
2343
David Quigley14c43f72013-05-22 12:50:43 -04002344 nfs4_label_free(olabel);
2345
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002346 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002349err_free_label:
2350 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002351err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002352 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002353 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002354err_put_state_owner:
2355 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return status;
2358}
2359
2360
Andy Adamson82be4172012-05-23 05:02:35 -04002361static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002362 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002363 int flags,
2364 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002365 struct nfs4_label *label,
2366 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002368 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 struct nfs4_exception exception = { };
2370 struct nfs4_state *res;
2371 int status;
2372
2373 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002374 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002375 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002376 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (status == 0)
2378 break;
2379 /* NOTE: BAD_SEQID means the server and client disagree about the
2380 * book-keeping w.r.t. state-changing operations
2381 * (OPEN/CLOSE/LOCK/LOCKU...)
2382 * It is actually a sign of a bug on the client or on the server.
2383 *
2384 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002385 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * have unhashed the old state_owner for us, and that we can
2387 * therefore safely retry using a new one. We should still warn
2388 * the user though...
2389 */
2390 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002391 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002392 " returned a bad sequence-id error!\n",
2393 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 exception.retry = 1;
2395 continue;
2396 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002397 /*
2398 * BAD_STATEID on OPEN means that the server cancelled our
2399 * state before it received the OPEN_CONFIRM.
2400 * Recover by retrying the request as per the discussion
2401 * on Page 181 of RFC3530.
2402 */
2403 if (status == -NFS4ERR_BAD_STATEID) {
2404 exception.retry = 1;
2405 continue;
2406 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002407 if (status == -EAGAIN) {
2408 /* We must have found a delegation */
2409 exception.retry = 1;
2410 continue;
2411 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002412 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2413 continue;
2414 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 status, &exception));
2416 } while (exception.retry);
2417 return res;
2418}
2419
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002420static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2421 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002422 struct nfs4_state *state, struct nfs4_label *ilabel,
2423 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002425 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002427 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 .iap = sattr,
2429 .server = server,
2430 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002431 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 };
2433 struct nfs_setattrres res = {
2434 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002435 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 .server = server,
2437 };
2438 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2440 .rpc_argp = &arg,
2441 .rpc_resp = &res,
2442 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002444 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002445 fmode_t fmode;
2446 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002447 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
David Quigleyaa9c2662013-05-22 12:50:44 -04002449 arg.bitmask = nfs4_bitmask(server, ilabel);
2450 if (ilabel)
2451 arg.bitmask = nfs4_bitmask(server, olabel);
2452
Trond Myklebust0e574af2005-10-27 22:12:38 -04002453 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Trond Myklebustee3ae842013-04-29 10:35:36 -04002455 /* Servers should only apply open mode checks for file size changes */
2456 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2457 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2458
2459 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2460 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002461 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002462 struct nfs_lockowner lockowner = {
2463 .l_owner = current->files,
2464 .l_pid = current->tgid,
2465 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002466 if (!nfs4_valid_open_stateid(state))
2467 return -EBADF;
2468 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2469 &lockowner) == -EIO)
2470 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002471 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002472 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Bryan Schumaker7c513052011-03-24 17:12:24 +00002474 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002475 if (status == 0 && state != NULL)
2476 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002477 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002480static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2481 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002482 struct nfs4_state *state, struct nfs4_label *ilabel,
2483 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002485 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002486 struct nfs4_exception exception = {
2487 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002488 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002489 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 int err;
2491 do {
David Quigley1775fd32013-05-22 12:50:42 -04002492 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002493 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002494 switch (err) {
2495 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002496 if (!(sattr->ia_valid & ATTR_SIZE)) {
2497 pr_warn_once("NFSv4: server %s is incorrectly "
2498 "applying open mode checks to "
2499 "a SETATTR that is not "
2500 "changing file size.\n",
2501 server->nfs_client->cl_hostname);
2502 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002503 if (state && !(state->state & FMODE_WRITE)) {
2504 err = -EBADF;
2505 if (sattr->ia_valid & ATTR_OPEN)
2506 err = -EACCES;
2507 goto out;
2508 }
2509 }
2510 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002512out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return err;
2514}
2515
2516struct nfs4_closedata {
2517 struct inode *inode;
2518 struct nfs4_state *state;
2519 struct nfs_closeargs arg;
2520 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002521 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002522 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002523 bool roc;
2524 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525};
2526
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002527static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002528{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002529 struct nfs4_closedata *calldata = data;
2530 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002531 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002532
Fred Isamanf7e89172011-01-06 11:36:32 +00002533 if (calldata->roc)
2534 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002535 nfs4_put_open_state(calldata->state);
2536 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002537 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002538 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002539 kfree(calldata);
2540}
2541
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002542static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002544 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 struct nfs_server *server = NFS_SERVER(calldata->inode);
2547
Chuck Levera3ca5652012-03-01 17:00:40 -05002548 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002549 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2550 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002551 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /* hmm. we are done with the inode, and in the process of freeing
2553 * the state_owner. we keep this around to process errors
2554 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 switch (task->tk_status) {
2556 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002557 if (calldata->roc)
2558 pnfs_roc_set_barrier(state->inode,
2559 calldata->roc_barrier);
Trond Myklebust226056c2014-02-11 10:41:07 -05002560 nfs_clear_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002561 renew_lease(server, calldata->timestamp);
Trond Myklebust226056c2014-02-11 10:41:07 -05002562 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002563 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002565 case -NFS4ERR_OLD_STATEID:
2566 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002568 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002569 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 default:
Trond Myklebust69794ad2013-11-20 12:57:19 -05002571 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002572 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002573 goto out_release;
2574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Trond Myklebust226056c2014-02-11 10:41:07 -05002576 nfs_clear_open_stateid(state, NULL, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002577out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002578 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002579 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002580 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002583static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002585 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002586 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002587 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002588 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002589
Chuck Levera3ca5652012-03-01 17:00:40 -05002590 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002591 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002592 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002593
Trond Myklebust88069f72009-12-08 08:33:16 -05002594 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2595 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002596 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002597 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002598 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002599 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002600 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2601 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2602 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002603 }
2604 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002605 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2606 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2607 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002608 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002609 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002610 if (!nfs4_valid_open_stateid(state))
2611 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002612 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002613
2614 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002615 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002616 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002617 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002618
Fred Isamanf7e89172011-01-06 11:36:32 +00002619 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002620 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002621 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002622 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2623 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002624 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002625 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002626 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002627
Trond Myklebust516a6af2005-10-27 22:12:41 -04002628 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002629 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002630 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002631 &calldata->arg.seq_args,
2632 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002633 task) != 0)
2634 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002635 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002636 return;
2637out_no_action:
2638 task->tk_action = NULL;
2639out_wait:
2640 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002643static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002644 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002645 .rpc_call_done = nfs4_close_done,
2646 .rpc_release = nfs4_free_closedata,
2647};
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649/*
2650 * It is possible for data to be read/written from a mem-mapped file
2651 * after the sys_close call (which hits the vfs layer as a flush).
2652 * This means that we can't safely call nfsv4 close on a file until
2653 * the inode is cleared. This in turn means that we are not good
2654 * NFSv4 citizens - we do not indicate to the server to update the file's
2655 * share state even when we are done with one of the three share
2656 * stateid's in the inode.
2657 *
2658 * NOTE: Caller must be holding the sp->so_owner semaphore!
2659 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002660int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002662 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002664 struct nfs4_state_owner *sp = state->owner;
2665 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2668 .rpc_cred = state->owner->so_cred,
2669 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002670 struct rpc_task_setup task_setup_data = {
2671 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002672 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002673 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002674 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002675 .flags = RPC_TASK_ASYNC,
2676 };
Trond Myklebust95121352005-10-18 14:20:12 -07002677 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002679 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2680 &task_setup_data.rpc_client, &msg);
2681
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002682 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002684 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002685 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002686 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002688 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002689 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002691 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002692 if (calldata->arg.seqid == NULL)
2693 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002694 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002695 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002696 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002697 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002698 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002699 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002700 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002701
Trond Myklebust1174dd12010-12-21 10:52:24 -05002702 msg.rpc_argp = &calldata->arg;
2703 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002704 task_setup_data.callback_data = calldata;
2705 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002706 if (IS_ERR(task))
2707 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002708 status = 0;
2709 if (wait)
2710 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002711 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002712 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002713out_free_calldata:
2714 kfree(calldata);
2715out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002716 nfs4_put_open_state(state);
2717 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002718 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Trond Myklebust2b484292010-09-17 10:56:51 -04002721static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002722nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
2723 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002726 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2727
2728 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Trond Myklebust565277f2007-10-15 18:17:53 -04002730 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002731 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04002732
2733 nfs4_label_release_security(label);
2734
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002735 if (IS_ERR(state))
2736 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Trond Myklebust1185a552009-12-03 15:54:02 -05002740static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002741{
2742 if (ctx->state == NULL)
2743 return;
2744 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002745 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002746 else
Al Viro643168c2011-06-22 18:20:23 -04002747 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Trond Myklebustb944dba2013-11-04 15:20:20 -05002750#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
2751#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
2752#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_CHANGE_SECURITY_LABEL - 1UL)
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2755{
Benny Halevy43652ad2009-04-01 09:21:54 -04002756 struct nfs4_server_caps_arg args = {
2757 .fhandle = fhandle,
2758 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 struct nfs4_server_caps_res res = {};
2760 struct rpc_message msg = {
2761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002762 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 .rpc_resp = &res,
2764 };
2765 int status;
2766
Bryan Schumaker7c513052011-03-24 17:12:24 +00002767 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05002769 /* Sanity check the server answers */
2770 switch (server->nfs_client->cl_minorversion) {
2771 case 0:
2772 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
2773 res.attr_bitmask[2] = 0;
2774 break;
2775 case 1:
2776 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
2777 break;
2778 case 2:
2779 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
2780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002782 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2783 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2784 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2785 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05002786 NFS_CAP_CTIME|NFS_CAP_MTIME|
2787 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06002788 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
2789 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 server->caps |= NFS_CAP_ACLS;
2791 if (res.has_links != 0)
2792 server->caps |= NFS_CAP_HARDLINKS;
2793 if (res.has_symlinks != 0)
2794 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002795 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2796 server->caps |= NFS_CAP_FILEID;
2797 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2798 server->caps |= NFS_CAP_MODE;
2799 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2800 server->caps |= NFS_CAP_NLINK;
2801 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2802 server->caps |= NFS_CAP_OWNER;
2803 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2804 server->caps |= NFS_CAP_OWNER_GROUP;
2805 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2806 server->caps |= NFS_CAP_ATIME;
2807 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2808 server->caps |= NFS_CAP_CTIME;
2809 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2810 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002811#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2812 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2813 server->caps |= NFS_CAP_SECURITY_LABEL;
2814#endif
2815 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2816 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05002817 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002818
Trond Myklebusta65318b2009-03-11 14:10:28 -04002819 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2820 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2821 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05002822 server->cache_consistency_bitmask[2] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002824 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 return status;
2828}
2829
Trond Myklebust55a97592006-06-09 09:34:19 -04002830int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831{
2832 struct nfs4_exception exception = { };
2833 int err;
2834 do {
2835 err = nfs4_handle_exception(server,
2836 _nfs4_server_capabilities(server, fhandle),
2837 &exception);
2838 } while (exception.retry);
2839 return err;
2840}
2841
2842static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2843 struct nfs_fsinfo *info)
2844{
David Quigleyaa9c2662013-05-22 12:50:44 -04002845 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002847 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 };
2849 struct nfs4_lookup_res res = {
2850 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002851 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 .fh = fhandle,
2853 };
2854 struct rpc_message msg = {
2855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2856 .rpc_argp = &args,
2857 .rpc_resp = &res,
2858 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002859
David Quigleyaa9c2662013-05-22 12:50:44 -04002860 bitmask[0] = nfs4_fattr_bitmap[0];
2861 bitmask[1] = nfs4_fattr_bitmap[1];
2862 /*
2863 * Process the label in the upcoming getfattr
2864 */
2865 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2866
Trond Myklebust0e574af2005-10-27 22:12:38 -04002867 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002868 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2872 struct nfs_fsinfo *info)
2873{
2874 struct nfs4_exception exception = { };
2875 int err;
2876 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002877 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002878 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002879 switch (err) {
2880 case 0:
2881 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002882 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002883 default:
2884 err = nfs4_handle_exception(server, err, &exception);
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002887out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return err;
2889}
2890
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002891static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2892 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2893{
Trond Myklebustc2190662013-08-26 19:23:04 -04002894 struct rpc_auth_create_args auth_args = {
2895 .pseudoflavor = flavor,
2896 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002897 struct rpc_auth *auth;
2898 int ret;
2899
Trond Myklebustc2190662013-08-26 19:23:04 -04002900 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002901 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002902 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002903 goto out;
2904 }
2905 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002906out:
2907 return ret;
2908}
2909
Chuck Lever9a744ba2013-03-16 15:56:02 -04002910/*
2911 * Retry pseudoroot lookup with various security flavors. We do this when:
2912 *
2913 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2914 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2915 *
2916 * Returns zero on success, or a negative NFS4ERR value, or a
2917 * negative errno value.
2918 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002919static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002920 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002922 /* Per 3530bis 15.33.5 */
2923 static const rpc_authflavor_t flav_array[] = {
2924 RPC_AUTH_GSS_KRB5P,
2925 RPC_AUTH_GSS_KRB5I,
2926 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002927 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002928 RPC_AUTH_NULL,
2929 };
2930 int status = -EPERM;
2931 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04002933 if (server->auth_info.flavor_len > 0) {
2934 /* try each flavor specified by user */
2935 for (i = 0; i < server->auth_info.flavor_len; i++) {
2936 status = nfs4_lookup_root_sec(server, fhandle, info,
2937 server->auth_info.flavors[i]);
2938 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2939 continue;
2940 break;
2941 }
2942 } else {
2943 /* no flavors specified by user, try default list */
2944 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
2945 status = nfs4_lookup_root_sec(server, fhandle, info,
2946 flav_array[i]);
2947 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
2948 continue;
2949 break;
2950 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002951 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002952
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002953 /*
2954 * -EACCESS could mean that the user doesn't have correct permissions
2955 * to access the mount. It could also mean that we tried to mount
2956 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2957 * existing mount programs don't handle -EACCES very well so it should
2958 * be mapped to -EPERM instead.
2959 */
2960 if (status == -EACCES)
2961 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002962 return status;
2963}
2964
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002965static int nfs4_do_find_root_sec(struct nfs_server *server,
2966 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2967{
2968 int mv = server->nfs_client->cl_minorversion;
2969 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2970}
2971
2972/**
2973 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2974 * @server: initialized nfs_server handle
2975 * @fhandle: we fill in the pseudo-fs root file handle
2976 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002977 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002978 *
2979 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002980 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002981int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002982 struct nfs_fsinfo *info,
2983 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002984{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002985 int status;
2986
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002987 switch (auth_probe) {
2988 case false:
2989 status = nfs4_lookup_root(server, fhandle, info);
2990 if (status != -NFS4ERR_WRONGSEC)
2991 break;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002992 default:
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002993 status = nfs4_do_find_root_sec(server, fhandle, info);
Trond Myklebust5e6b1992013-09-07 12:58:57 -04002994 }
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 if (status == 0)
2997 status = nfs4_server_capabilities(server, fhandle);
2998 if (status == 0)
2999 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003000
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003001 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Bryan Schumakerbae36242012-05-10 15:07:31 -04003004static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3005 struct nfs_fsinfo *info)
3006{
3007 int error;
3008 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003009 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003010
3011 error = nfs4_server_capabilities(server, mntfh);
3012 if (error < 0) {
3013 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3014 return error;
3015 }
3016
David Quigley14c43f72013-05-22 12:50:43 -04003017 label = nfs4_label_alloc(server, GFP_KERNEL);
3018 if (IS_ERR(label))
3019 return PTR_ERR(label);
3020
David Quigley1775fd32013-05-22 12:50:42 -04003021 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003022 if (error < 0) {
3023 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003024 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003025 }
3026
3027 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3028 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3029 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3030
David Quigley14c43f72013-05-22 12:50:43 -04003031err_free_label:
3032 nfs4_label_free(label);
3033
Bryan Schumakerbae36242012-05-10 15:07:31 -04003034 return error;
3035}
3036
Manoj Naik6b97fd32006-06-09 09:34:29 -04003037/*
3038 * Get locations and (maybe) other attributes of a referral.
3039 * Note that we'll actually follow the referral later when
3040 * we detect fsid mismatch in inode revalidation
3041 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003042static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3043 const struct qstr *name, struct nfs_fattr *fattr,
3044 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003045{
3046 int status = -ENOMEM;
3047 struct page *page = NULL;
3048 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003049
3050 page = alloc_page(GFP_KERNEL);
3051 if (page == NULL)
3052 goto out;
3053 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3054 if (locations == NULL)
3055 goto out;
3056
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003057 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003058 if (status != 0)
3059 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003060
3061 /*
3062 * If the fsid didn't change, this is a migration event, not a
3063 * referral. Cause us to drop into the exception handler, which
3064 * will kick off migration recovery.
3065 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003066 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003067 dprintk("%s: server did not return a different fsid for"
3068 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003069 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003070 goto out;
3071 }
Andy Adamson533eb462011-06-13 18:25:56 -04003072 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3073 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003074
Andy Adamson533eb462011-06-13 18:25:56 -04003075 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003076 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003077 memset(fhandle, 0, sizeof(struct nfs_fh));
3078out:
3079 if (page)
3080 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003081 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003082 return status;
3083}
3084
David Quigley1775fd32013-05-22 12:50:42 -04003085static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3086 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
3088 struct nfs4_getattr_arg args = {
3089 .fh = fhandle,
3090 .bitmask = server->attr_bitmask,
3091 };
3092 struct nfs4_getattr_res res = {
3093 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003094 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .server = server,
3096 };
3097 struct rpc_message msg = {
3098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3099 .rpc_argp = &args,
3100 .rpc_resp = &res,
3101 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003102
3103 args.bitmask = nfs4_bitmask(server, label);
3104
Trond Myklebust0e574af2005-10-27 22:12:38 -04003105 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003106 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
David Quigley1775fd32013-05-22 12:50:42 -04003109static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3110 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 struct nfs4_exception exception = { };
3113 int err;
3114 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003115 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3116 trace_nfs4_getattr(server, fhandle, fattr, err);
3117 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 &exception);
3119 } while (exception.retry);
3120 return err;
3121}
3122
3123/*
3124 * The file is not closed if it is opened due to the a request to change
3125 * the size of the file. The open call will not be needed once the
3126 * VFS layer lookup-intents are implemented.
3127 *
3128 * Close is called when the inode is destroyed.
3129 * If we haven't opened the file for O_WRONLY, we
3130 * need to in the size_change case to obtain a stateid.
3131 *
3132 * Got race?
3133 * Because OPEN is always done by name in nfsv4, it is
3134 * possible that we opened a different file by the same
3135 * name. We can recognize this race condition, but we
3136 * can't do anything about it besides returning an error.
3137 *
3138 * This will be fixed with VFS changes (lookup-intent).
3139 */
3140static int
3141nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3142 struct iattr *sattr)
3143{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003144 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003145 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003146 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003147 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 int status;
3149
Benny Halevy8a1636c2010-07-14 15:43:57 -04003150 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003151 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003152
Trond Myklebust0e574af2005-10-27 22:12:38 -04003153 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Andy Adamson26699402012-05-30 16:12:24 -04003155 /* Deal with open(O_TRUNC) */
3156 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003157 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003158
3159 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003160 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003161 return 0;
3162
Trond Myklebustd5308382005-11-04 15:33:38 -05003163 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003164 if (sattr->ia_valid & ATTR_FILE) {
3165 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003166
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003167 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003168 if (ctx) {
3169 cred = ctx->cred;
3170 state = ctx->state;
3171 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003172 }
3173
David Quigley14c43f72013-05-22 12:50:43 -04003174 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3175 if (IS_ERR(label))
3176 return PTR_ERR(label);
3177
3178 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003179 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04003180 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003181 nfs_setsecurity(inode, fattr, label);
3182 }
David Quigley14c43f72013-05-22 12:50:43 -04003183 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return status;
3185}
3186
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003187static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3188 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003189 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003190{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003191 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003192 int status;
3193 struct nfs4_lookup_arg args = {
3194 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003195 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003196 .name = name,
3197 };
3198 struct nfs4_lookup_res res = {
3199 .server = server,
3200 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003201 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003202 .fh = fhandle,
3203 };
3204 struct rpc_message msg = {
3205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3206 .rpc_argp = &args,
3207 .rpc_resp = &res,
3208 };
3209
David Quigleyaa9c2662013-05-22 12:50:44 -04003210 args.bitmask = nfs4_bitmask(server, label);
3211
David Howells2b3de442006-08-22 20:06:09 -04003212 nfs_fattr_init(fattr);
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003215 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 dprintk("NFS reply lookup: %d\n", status);
3217 return status;
3218}
3219
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003220static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003221{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003222 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003223 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003224 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3225 fattr->nlink = 2;
3226}
3227
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003228static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3229 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003230 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003231{
3232 struct nfs4_exception exception = { };
3233 struct rpc_clnt *client = *clnt;
3234 int err;
3235 do {
David Quigley1775fd32013-05-22 12:50:42 -04003236 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003237 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003238 switch (err) {
3239 case -NFS4ERR_BADNAME:
3240 err = -ENOENT;
3241 goto out;
3242 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003243 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003244 goto out;
3245 case -NFS4ERR_WRONGSEC:
3246 err = -EPERM;
3247 if (client != *clnt)
3248 goto out;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003249 client = nfs4_create_sec_client(client, dir, name);
3250 if (IS_ERR(client))
3251 return PTR_ERR(client);
3252
3253 exception.retry = 1;
3254 break;
3255 default:
3256 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3257 }
3258 } while (exception.retry);
3259
3260out:
3261 if (err == 0)
3262 *clnt = client;
3263 else if (client != *clnt)
3264 rpc_shutdown_client(client);
3265
3266 return err;
3267}
3268
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003269static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003270 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3271 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003273 int status;
3274 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003275
David Quigley1775fd32013-05-22 12:50:42 -04003276 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003277 if (client != NFS_CLIENT(dir)) {
3278 rpc_shutdown_client(client);
3279 nfs_fixup_secinfo_attributes(fattr);
3280 }
3281 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282}
3283
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003284struct rpc_clnt *
3285nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3286 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3287{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003288 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003289 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003290
David Quigley1775fd32013-05-22 12:50:42 -04003291 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003292 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003293 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003294 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003295}
3296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3298{
Trond Myklebust76b32992007-08-10 17:45:11 -04003299 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 struct nfs4_accessargs args = {
3301 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003302 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003304 struct nfs4_accessres res = {
3305 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 struct rpc_message msg = {
3308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3309 .rpc_argp = &args,
3310 .rpc_resp = &res,
3311 .rpc_cred = entry->cred,
3312 };
3313 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003314 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316 /*
3317 * Determine which access bits we want to ask for...
3318 */
3319 if (mode & MAY_READ)
3320 args.access |= NFS4_ACCESS_READ;
3321 if (S_ISDIR(inode->i_mode)) {
3322 if (mode & MAY_WRITE)
3323 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3324 if (mode & MAY_EXEC)
3325 args.access |= NFS4_ACCESS_LOOKUP;
3326 } else {
3327 if (mode & MAY_WRITE)
3328 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3329 if (mode & MAY_EXEC)
3330 args.access |= NFS4_ACCESS_EXECUTE;
3331 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003332
3333 res.fattr = nfs_alloc_fattr();
3334 if (res.fattr == NULL)
3335 return -ENOMEM;
3336
Bryan Schumaker7c513052011-03-24 17:12:24 +00003337 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003339 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003340 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003342 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return status;
3344}
3345
3346static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3347{
3348 struct nfs4_exception exception = { };
3349 int err;
3350 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003351 err = _nfs4_proc_access(inode, entry);
3352 trace_nfs4_access(inode, err);
3353 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 &exception);
3355 } while (exception.retry);
3356 return err;
3357}
3358
3359/*
3360 * TODO: For the time being, we don't try to get any attributes
3361 * along with any of the zero-copy operations READ, READDIR,
3362 * READLINK, WRITE.
3363 *
3364 * In the case of the first three, we want to put the GETATTR
3365 * after the read-type operation -- this is because it is hard
3366 * to predict the length of a GETATTR response in v4, and thus
3367 * align the READ data correctly. This means that the GETATTR
3368 * may end up partially falling into the page cache, and we should
3369 * shift it into the 'tail' of the xdr_buf before processing.
3370 * To do this efficiently, we need to know the total length
3371 * of data received, which doesn't seem to be available outside
3372 * of the RPC layer.
3373 *
3374 * In the case of WRITE, we also want to put the GETATTR after
3375 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003376 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 *
3378 * Both of these changes to the XDR layer would in fact be quite
3379 * minor, but I decided to leave them for a subsequent patch.
3380 */
3381static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3382 unsigned int pgbase, unsigned int pglen)
3383{
3384 struct nfs4_readlink args = {
3385 .fh = NFS_FH(inode),
3386 .pgbase = pgbase,
3387 .pglen = pglen,
3388 .pages = &page,
3389 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003390 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 struct rpc_message msg = {
3392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3393 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003394 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
3396
Bryan Schumaker7c513052011-03-24 17:12:24 +00003397 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 -07003398}
3399
3400static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3401 unsigned int pgbase, unsigned int pglen)
3402{
3403 struct nfs4_exception exception = { };
3404 int err;
3405 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003406 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3407 trace_nfs4_readlink(inode, err);
3408 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 &exception);
3410 } while (exception.retry);
3411 return err;
3412}
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003415 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417static int
3418nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003419 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420{
David Quigleyaa9c2662013-05-22 12:50:44 -04003421 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003422 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 struct nfs4_state *state;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003424 int opened = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 int status = 0;
3426
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003427 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3428 if (IS_ERR(ctx))
3429 return PTR_ERR(ctx);
3430
David Quigleyaa9c2662013-05-22 12:50:44 -04003431 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3432
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003433 sattr->ia_mode &= ~current_umask();
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003434 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, &opened);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (IS_ERR(state)) {
3436 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003440 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003441 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 return status;
3443}
3444
3445static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3446{
Trond Myklebust16e42952005-10-27 22:12:44 -04003447 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003448 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003450 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003452 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003453 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003454 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3457 .rpc_argp = &args,
3458 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003460 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003461
Bryan Schumaker7c513052011-03-24 17:12:24 +00003462 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003463 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003464 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return status;
3466}
3467
3468static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3469{
3470 struct nfs4_exception exception = { };
3471 int err;
3472 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003473 err = _nfs4_proc_remove(dir, name);
3474 trace_nfs4_remove(dir, name, err);
3475 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 &exception);
3477 } while (exception.retry);
3478 return err;
3479}
3480
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003481static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003483 struct nfs_server *server = NFS_SERVER(dir);
3484 struct nfs_removeargs *args = msg->rpc_argp;
3485 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003487 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003489 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003490
3491 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003494static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3495{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003496 nfs4_setup_sequence(NFS_SERVER(data->dir),
3497 &data->args.seq_args,
3498 &data->res.seq_res,
3499 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500}
3501
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003502static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003504 struct nfs_unlinkdata *data = task->tk_calldata;
3505 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003506
Trond Myklebust14516c32010-07-31 14:29:06 -04003507 if (!nfs4_sequence_done(task, &res->seq_res))
3508 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003509 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003510 return 0;
3511 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003512 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513}
3514
Jeff Laytond3d41522010-09-17 17:31:57 -04003515static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3516{
3517 struct nfs_server *server = NFS_SERVER(dir);
3518 struct nfs_renameargs *arg = msg->rpc_argp;
3519 struct nfs_renameres *res = msg->rpc_resp;
3520
3521 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003522 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003523 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003524}
3525
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003526static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3527{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003528 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3529 &data->args.seq_args,
3530 &data->res.seq_res,
3531 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003532}
3533
3534static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3535 struct inode *new_dir)
3536{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003537 struct nfs_renamedata *data = task->tk_calldata;
3538 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003539
3540 if (!nfs4_sequence_done(task, &res->seq_res))
3541 return 0;
3542 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3543 return 0;
3544
3545 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003546 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003547 return 1;
3548}
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3551{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003552 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 struct nfs4_link_arg arg = {
3554 .fh = NFS_FH(inode),
3555 .dir_fh = NFS_FH(dir),
3556 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003557 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003559 struct nfs4_link_res res = {
3560 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003561 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003562 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 struct rpc_message msg = {
3564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3565 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003566 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003568 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Trond Myklebust136f2622010-04-16 16:22:49 -04003570 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003571 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003572 goto out;
3573
David Quigley14c43f72013-05-22 12:50:43 -04003574 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3575 if (IS_ERR(res.label)) {
3576 status = PTR_ERR(res.label);
3577 goto out;
3578 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003579 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003580
Bryan Schumaker7c513052011-03-24 17:12:24 +00003581 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003582 if (!status) {
3583 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003584 status = nfs_post_op_update_inode(inode, res.fattr);
3585 if (!status)
3586 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003587 }
David Quigley14c43f72013-05-22 12:50:43 -04003588
3589
3590 nfs4_label_free(res.label);
3591
Trond Myklebust136f2622010-04-16 16:22:49 -04003592out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003593 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 return status;
3595}
3596
3597static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3598{
3599 struct nfs4_exception exception = { };
3600 int err;
3601 do {
3602 err = nfs4_handle_exception(NFS_SERVER(inode),
3603 _nfs4_proc_link(inode, dir, name),
3604 &exception);
3605 } while (exception.retry);
3606 return err;
3607}
3608
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003609struct nfs4_createdata {
3610 struct rpc_message msg;
3611 struct nfs4_create_arg arg;
3612 struct nfs4_create_res res;
3613 struct nfs_fh fh;
3614 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003615 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003616};
3617
3618static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3619 struct qstr *name, struct iattr *sattr, u32 ftype)
3620{
3621 struct nfs4_createdata *data;
3622
3623 data = kzalloc(sizeof(*data), GFP_KERNEL);
3624 if (data != NULL) {
3625 struct nfs_server *server = NFS_SERVER(dir);
3626
David Quigley14c43f72013-05-22 12:50:43 -04003627 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3628 if (IS_ERR(data->label))
3629 goto out_free;
3630
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003631 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3632 data->msg.rpc_argp = &data->arg;
3633 data->msg.rpc_resp = &data->res;
3634 data->arg.dir_fh = NFS_FH(dir);
3635 data->arg.server = server;
3636 data->arg.name = name;
3637 data->arg.attrs = sattr;
3638 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003639 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003640 data->res.server = server;
3641 data->res.fh = &data->fh;
3642 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003643 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003644 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003645 }
3646 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003647out_free:
3648 kfree(data);
3649 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003650}
3651
3652static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3653{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003654 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003655 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003656 if (status == 0) {
3657 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003658 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 }
3660 return status;
3661}
3662
3663static void nfs4_free_createdata(struct nfs4_createdata *data)
3664{
David Quigley14c43f72013-05-22 12:50:43 -04003665 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003666 kfree(data);
3667}
3668
Chuck Lever4f390c12006-08-22 20:06:22 -04003669static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003670 struct page *page, unsigned int len, struct iattr *sattr,
3671 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003673 struct nfs4_createdata *data;
3674 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675
Chuck Lever94a6d752006-08-22 20:06:23 -04003676 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003677 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003678
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003679 status = -ENOMEM;
3680 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3681 if (data == NULL)
3682 goto out;
3683
3684 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3685 data->arg.u.symlink.pages = &page;
3686 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003687 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003689 status = nfs4_do_create(dir, dentry, data);
3690
3691 nfs4_free_createdata(data);
3692out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return status;
3694}
3695
Chuck Lever4f390c12006-08-22 20:06:22 -04003696static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003697 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
3699 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003700 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003702
3703 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003706 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3707 trace_nfs4_symlink(dir, &dentry->d_name, err);
3708 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 &exception);
3710 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003711
3712 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return err;
3714}
3715
3716static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003717 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003719 struct nfs4_createdata *data;
3720 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003722 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3723 if (data == NULL)
3724 goto out;
3725
David Quigley1775fd32013-05-22 12:50:42 -04003726 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003727 status = nfs4_do_create(dir, dentry, data);
3728
3729 nfs4_free_createdata(data);
3730out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 return status;
3732}
3733
3734static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3735 struct iattr *sattr)
3736{
3737 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003738 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003740
David Quigleyaa9c2662013-05-22 12:50:44 -04003741 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3742
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003743 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003745 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3746 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3747 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 &exception);
3749 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003750 nfs4_label_release_security(label);
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 return err;
3753}
3754
3755static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003756 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 struct inode *dir = dentry->d_inode;
3759 struct nfs4_readdir_arg args = {
3760 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003761 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 .pgbase = 0,
3763 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003764 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003765 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 };
3767 struct nfs4_readdir_res res;
3768 struct rpc_message msg = {
3769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3770 .rpc_argp = &args,
3771 .rpc_resp = &res,
3772 .rpc_cred = cred,
3773 };
3774 int status;
3775
Al Viro6de14722013-09-16 10:53:17 -04003776 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
3777 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003778 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003779 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003781 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003782 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003783 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003784 status += args.pgbase;
3785 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003786
3787 nfs_invalidate_atime(dir);
3788
Harvey Harrison3110ff82008-05-02 13:42:44 -07003789 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return status;
3791}
3792
3793static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003794 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 struct nfs4_exception exception = { };
3797 int err;
3798 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003799 err = _nfs4_proc_readdir(dentry, cred, cookie,
3800 pages, count, plus);
3801 trace_nfs4_readdir(dentry->d_inode, err);
3802 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 &exception);
3804 } while (exception.retry);
3805 return err;
3806}
3807
3808static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003811 struct nfs4_createdata *data;
3812 int mode = sattr->ia_mode;
3813 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003815 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3816 if (data == NULL)
3817 goto out;
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003820 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003822 data->arg.ftype = NF4BLK;
3823 data->arg.u.device.specdata1 = MAJOR(rdev);
3824 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003827 data->arg.ftype = NF4CHR;
3828 data->arg.u.device.specdata1 = MAJOR(rdev);
3829 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003830 } else if (!S_ISSOCK(mode)) {
3831 status = -EINVAL;
3832 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
David Quigley1775fd32013-05-22 12:50:42 -04003834
David Quigleyaa9c2662013-05-22 12:50:44 -04003835 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003836 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003837out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003838 nfs4_free_createdata(data);
3839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 return status;
3841}
3842
3843static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3844 struct iattr *sattr, dev_t rdev)
3845{
3846 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003847 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003849
David Quigleyaa9c2662013-05-22 12:50:44 -04003850 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3851
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003852 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003854 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3855 trace_nfs4_mknod(dir, &dentry->d_name, err);
3856 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 &exception);
3858 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003859
3860 nfs4_label_release_security(label);
3861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return err;
3863}
3864
3865static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3866 struct nfs_fsstat *fsstat)
3867{
3868 struct nfs4_statfs_arg args = {
3869 .fh = fhandle,
3870 .bitmask = server->attr_bitmask,
3871 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003872 struct nfs4_statfs_res res = {
3873 .fsstat = fsstat,
3874 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3877 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003878 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
3880
Trond Myklebust0e574af2005-10-27 22:12:38 -04003881 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003882 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
3885static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3886{
3887 struct nfs4_exception exception = { };
3888 int err;
3889 do {
3890 err = nfs4_handle_exception(server,
3891 _nfs4_proc_statfs(server, fhandle, fsstat),
3892 &exception);
3893 } while (exception.retry);
3894 return err;
3895}
3896
3897static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3898 struct nfs_fsinfo *fsinfo)
3899{
3900 struct nfs4_fsinfo_arg args = {
3901 .fh = fhandle,
3902 .bitmask = server->attr_bitmask,
3903 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003904 struct nfs4_fsinfo_res res = {
3905 .fsinfo = fsinfo,
3906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 struct rpc_message msg = {
3908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3909 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003910 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 };
3912
Bryan Schumaker7c513052011-03-24 17:12:24 +00003913 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914}
3915
3916static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3917{
3918 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003919 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 int err;
3921
3922 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003923 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003924 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003925 if (err == 0) {
3926 struct nfs_client *clp = server->nfs_client;
3927
3928 spin_lock(&clp->cl_lock);
3929 clp->cl_lease_time = fsinfo->lease_time * HZ;
3930 clp->cl_last_renewal = now;
3931 spin_unlock(&clp->cl_lock);
3932 break;
3933 }
3934 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 } while (exception.retry);
3936 return err;
3937}
3938
3939static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3940{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003941 int error;
3942
Trond Myklebust0e574af2005-10-27 22:12:38 -04003943 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003944 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003945 if (error == 0) {
3946 /* block layout checks this! */
3947 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003948 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003949 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003950
3951 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952}
3953
3954static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3955 struct nfs_pathconf *pathconf)
3956{
3957 struct nfs4_pathconf_arg args = {
3958 .fh = fhandle,
3959 .bitmask = server->attr_bitmask,
3960 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003961 struct nfs4_pathconf_res res = {
3962 .pathconf = pathconf,
3963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3966 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003967 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 };
3969
3970 /* None of the pathconf attributes are mandatory to implement */
3971 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3972 memset(pathconf, 0, sizeof(*pathconf));
3973 return 0;
3974 }
3975
Trond Myklebust0e574af2005-10-27 22:12:38 -04003976 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003977 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
3980static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3981 struct nfs_pathconf *pathconf)
3982{
3983 struct nfs4_exception exception = { };
3984 int err;
3985
3986 do {
3987 err = nfs4_handle_exception(server,
3988 _nfs4_proc_pathconf(server, fhandle, pathconf),
3989 &exception);
3990 } while (exception.retry);
3991 return err;
3992}
3993
Trond Myklebust5521abf2013-03-16 20:54:34 -04003994int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003995 const struct nfs_open_context *ctx,
3996 const struct nfs_lock_context *l_ctx,
3997 fmode_t fmode)
3998{
3999 const struct nfs_lockowner *lockowner = NULL;
4000
4001 if (l_ctx != NULL)
4002 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004003 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004004}
4005EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4006
Trond Myklebust5521abf2013-03-16 20:54:34 -04004007static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4008 const struct nfs_open_context *ctx,
4009 const struct nfs_lock_context *l_ctx,
4010 fmode_t fmode)
4011{
4012 nfs4_stateid current_stateid;
4013
Trond Myklebuste1253be2014-03-05 08:44:23 -05004014 /* If the current stateid represents a lost lock, then exit */
4015 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4016 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004017 return nfs4_stateid_match(stateid, &current_stateid);
4018}
4019
4020static bool nfs4_error_stateid_expired(int err)
4021{
4022 switch (err) {
4023 case -NFS4ERR_DELEG_REVOKED:
4024 case -NFS4ERR_ADMIN_REVOKED:
4025 case -NFS4ERR_BAD_STATEID:
4026 case -NFS4ERR_STALE_STATEID:
4027 case -NFS4ERR_OLD_STATEID:
4028 case -NFS4ERR_OPENMODE:
4029 case -NFS4ERR_EXPIRED:
4030 return true;
4031 }
4032 return false;
4033}
4034
Benny Halevyd20581a2011-05-22 19:52:03 +03004035void __nfs4_read_done_cb(struct nfs_read_data *data)
4036{
Fred Isamancd841602012-04-20 14:47:44 -04004037 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004038}
4039
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004040static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041{
Fred Isamancd841602012-04-20 14:47:44 -04004042 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004044 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004045 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004046 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004047 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004049
Benny Halevyd20581a2011-05-22 19:52:03 +03004050 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05004052 renew_lease(server, data->timestamp);
4053 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054}
4055
Trond Myklebust5521abf2013-03-16 20:54:34 -04004056static bool nfs4_read_stateid_changed(struct rpc_task *task,
4057 struct nfs_readargs *args)
4058{
4059
4060 if (!nfs4_error_stateid_expired(task->tk_status) ||
4061 nfs4_stateid_is_current(&args->stateid,
4062 args->context,
4063 args->lock_context,
4064 FMODE_READ))
4065 return false;
4066 rpc_restart_call_prepare(task);
4067 return true;
4068}
4069
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004070static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
4071{
4072
4073 dprintk("--> %s\n", __func__);
4074
4075 if (!nfs4_sequence_done(task, &data->res.seq_res))
4076 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004077 if (nfs4_read_stateid_changed(task, &data->args))
4078 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004079 return data->read_done_cb ? data->read_done_cb(task, data) :
4080 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004081}
4082
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004083static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004086 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004087 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Chuck Levera9c92d62013-08-09 12:48:18 -04004088 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
NeilBrownef1820f2013-09-04 17:04:49 +10004091static int nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004092{
Trond Myklebust9b206142013-03-17 15:52:00 -04004093 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004094 &data->args.seq_args,
4095 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004096 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004097 return 0;
4098 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4099 data->args.lock_context, FMODE_READ) == -EIO)
4100 return -EIO;
4101 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4102 return -EIO;
4103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104}
4105
Fred Isamanb029bc92011-03-03 15:13:42 +00004106static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
Fred Isamancd841602012-04-20 14:47:44 -04004108 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004110 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004111 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004112 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004113 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004115 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04004117 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004118 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120}
4121
Trond Myklebust5521abf2013-03-16 20:54:34 -04004122static bool nfs4_write_stateid_changed(struct rpc_task *task,
4123 struct nfs_writeargs *args)
4124{
4125
4126 if (!nfs4_error_stateid_expired(task->tk_status) ||
4127 nfs4_stateid_is_current(&args->stateid,
4128 args->context,
4129 args->lock_context,
4130 FMODE_WRITE))
4131 return false;
4132 rpc_restart_call_prepare(task);
4133 return true;
4134}
4135
Fred Isamanb029bc92011-03-03 15:13:42 +00004136static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
4137{
4138 if (!nfs4_sequence_done(task, &data->res.seq_res))
4139 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004140 if (nfs4_write_stateid_changed(task, &data->args))
4141 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03004142 return data->write_done_cb ? data->write_done_cb(task, data) :
4143 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00004144}
4145
Trond Myklebust5a37f852012-04-28 14:55:16 -04004146static
4147bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00004148{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004149 const struct nfs_pgio_header *hdr = data->header;
4150
4151 /* Don't request attributes for pNFS or O_DIRECT writes */
4152 if (data->ds_clp != NULL || hdr->dreq != NULL)
4153 return false;
4154 /* Otherwise, request attributes if and only if we don't hold
4155 * a delegation
4156 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004157 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004158}
Fred Isamana69aef12011-03-03 15:13:47 +00004159
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004160static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Fred Isamancd841602012-04-20 14:47:44 -04004162 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004163
Trond Myklebust5a37f852012-04-28 14:55:16 -04004164 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004165 data->args.bitmask = NULL;
4166 data->res.fattr = NULL;
4167 } else
4168 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004169
Fred Isamanb029bc92011-03-03 15:13:42 +00004170 if (!data->write_done_cb)
4171 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004172 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 data->timestamp = jiffies;
4174
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004175 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004176 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
NeilBrownef1820f2013-09-04 17:04:49 +10004179static int nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004180{
Trond Myklebust9b206142013-03-17 15:52:00 -04004181 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004182 &data->args.seq_args,
4183 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004184 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004185 return 0;
4186 if (nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4187 data->args.lock_context, FMODE_WRITE) == -EIO)
4188 return -EIO;
4189 if (unlikely(test_bit(NFS_CONTEXT_BAD, &data->args.context->flags)))
4190 return -EIO;
4191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
Fred Isaman0b7c0152012-04-20 14:47:39 -04004194static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4195{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004196 nfs4_setup_sequence(NFS_SERVER(data->inode),
4197 &data->args.seq_args,
4198 &data->res.seq_res,
4199 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
Fred Isaman0b7c0152012-04-20 14:47:39 -04004202static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004205
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004206 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004207 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004208 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004209 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
Fred Isaman0b7c0152012-04-20 14:47:39 -04004214static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004215{
4216 if (!nfs4_sequence_done(task, &data->res.seq_res))
4217 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004218 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004219}
4220
Fred Isaman0b7c0152012-04-20 14:47:39 -04004221static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004223 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004224
Fred Isaman0b7c0152012-04-20 14:47:39 -04004225 if (data->commit_done_cb == NULL)
4226 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004227 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004228 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004229 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230}
4231
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004232struct nfs4_renewdata {
4233 struct nfs_client *client;
4234 unsigned long timestamp;
4235};
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237/*
4238 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4239 * standalone procedure for queueing an asynchronous RENEW.
4240 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004241static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004242{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004243 struct nfs4_renewdata *data = calldata;
4244 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004245
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004246 if (atomic_read(&clp->cl_count) > 1)
4247 nfs4_schedule_state_renewal(clp);
4248 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004249 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004250}
4251
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004252static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004254 struct nfs4_renewdata *data = calldata;
4255 struct nfs_client *clp = data->client;
4256 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004258 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004259 switch (task->tk_status) {
4260 case 0:
4261 break;
4262 case -NFS4ERR_LEASE_MOVED:
4263 nfs4_schedule_lease_moved_recovery(clp);
4264 break;
4265 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004266 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004267 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4268 return;
4269 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004270 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004271 return;
4272 }
4273 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004275 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276}
4277
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004278static const struct rpc_call_ops nfs4_renew_ops = {
4279 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004280 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004281};
4282
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004283static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 struct rpc_message msg = {
4286 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4287 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004288 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004290 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004292 if (renew_flags == 0)
4293 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004294 if (!atomic_inc_not_zero(&clp->cl_count))
4295 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004296 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004297 if (data == NULL)
4298 return -ENOMEM;
4299 data->client = clp;
4300 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004301 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004302 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303}
4304
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004305static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
4307 struct rpc_message msg = {
4308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4309 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004310 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 };
4312 unsigned long now = jiffies;
4313 int status;
4314
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004315 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (status < 0)
4317 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004318 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 0;
4320}
4321
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004322static inline int nfs4_server_supports_acls(struct nfs_server *server)
4323{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004324 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004325}
4326
Trond Myklebust21f498c2012-08-24 10:59:25 -04004327/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4328 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004329 * the stack.
4330 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004331#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004332
Neil Hormane9e3d722011-03-04 19:26:03 -05004333static int buf_to_pages_noslab(const void *buf, size_t buflen,
4334 struct page **pages, unsigned int *pgbase)
4335{
4336 struct page *newpage, **spages;
4337 int rc = 0;
4338 size_t len;
4339 spages = pages;
4340
4341 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004342 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004343 newpage = alloc_page(GFP_KERNEL);
4344
4345 if (newpage == NULL)
4346 goto unwind;
4347 memcpy(page_address(newpage), buf, len);
4348 buf += len;
4349 buflen -= len;
4350 *pages++ = newpage;
4351 rc++;
4352 } while (buflen != 0);
4353
4354 return rc;
4355
4356unwind:
4357 for(; rc > 0; rc--)
4358 __free_page(spages[rc-1]);
4359 return -ENOMEM;
4360}
4361
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004362struct nfs4_cached_acl {
4363 int cached;
4364 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004365 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004366};
4367
4368static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004369{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004370 struct nfs_inode *nfsi = NFS_I(inode);
4371
4372 spin_lock(&inode->i_lock);
4373 kfree(nfsi->nfs4_acl);
4374 nfsi->nfs4_acl = acl;
4375 spin_unlock(&inode->i_lock);
4376}
4377
4378static void nfs4_zap_acl_attr(struct inode *inode)
4379{
4380 nfs4_set_cached_acl(inode, NULL);
4381}
4382
4383static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4384{
4385 struct nfs_inode *nfsi = NFS_I(inode);
4386 struct nfs4_cached_acl *acl;
4387 int ret = -ENOENT;
4388
4389 spin_lock(&inode->i_lock);
4390 acl = nfsi->nfs4_acl;
4391 if (acl == NULL)
4392 goto out;
4393 if (buf == NULL) /* user is just asking for length */
4394 goto out_len;
4395 if (acl->cached == 0)
4396 goto out;
4397 ret = -ERANGE; /* see getxattr(2) man page */
4398 if (acl->len > buflen)
4399 goto out;
4400 memcpy(buf, acl->data, acl->len);
4401out_len:
4402 ret = acl->len;
4403out:
4404 spin_unlock(&inode->i_lock);
4405 return ret;
4406}
4407
Sachin Prabhu5794d212012-04-17 14:36:40 +01004408static 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 +00004409{
4410 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004411 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004412
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004413 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004414 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004415 if (acl == NULL)
4416 goto out;
4417 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004418 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004419 } else {
4420 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4421 if (acl == NULL)
4422 goto out;
4423 acl->cached = 0;
4424 }
4425 acl->len = acl_len;
4426out:
4427 nfs4_set_cached_acl(inode, acl);
4428}
4429
Andy Adamsonbf118a32011-12-07 11:55:27 -05004430/*
4431 * The getxattr API returns the required buffer length when called with a
4432 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4433 * the required buf. On a NULL buf, we send a page of data to the server
4434 * guessing that the ACL request can be serviced by a page. If so, we cache
4435 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4436 * the cache. If not so, we throw away the page, and cache the required
4437 * length. The next getxattr call will then produce another round trip to
4438 * the server, this time with the input buf of the required size.
4439 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004440static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004441{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004442 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004443 struct nfs_getaclargs args = {
4444 .fh = NFS_FH(inode),
4445 .acl_pages = pages,
4446 .acl_len = buflen,
4447 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004448 struct nfs_getaclres res = {
4449 .acl_len = buflen,
4450 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004451 struct rpc_message msg = {
4452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4453 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004454 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004455 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004456 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4457 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004458
Andy Adamsonbf118a32011-12-07 11:55:27 -05004459 /* As long as we're doing a round trip to the server anyway,
4460 * let's be prepared for a page of acl data. */
4461 if (npages == 0)
4462 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004463 if (npages > ARRAY_SIZE(pages))
4464 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004465
Andy Adamsonbf118a32011-12-07 11:55:27 -05004466 for (i = 0; i < npages; i++) {
4467 pages[i] = alloc_page(GFP_KERNEL);
4468 if (!pages[i])
4469 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004470 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004471
4472 /* for decoding across pages */
4473 res.acl_scratch = alloc_page(GFP_KERNEL);
4474 if (!res.acl_scratch)
4475 goto out_free;
4476
Andy Adamsonbf118a32011-12-07 11:55:27 -05004477 args.acl_len = npages * PAGE_SIZE;
4478 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004479
Peng Taode040be2012-01-10 22:42:47 +08004480 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004481 __func__, buf, buflen, npages, args.acl_len);
4482 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4483 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004484 if (ret)
4485 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004486
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004487 /* Handle the case where the passed-in buffer is too short */
4488 if (res.acl_flags & NFS4_ACL_TRUNC) {
4489 /* Did the user only issue a request for the acl length? */
4490 if (buf == NULL)
4491 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004492 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004493 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004494 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004495 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004496 if (buf) {
4497 if (res.acl_len > buflen) {
4498 ret = -ERANGE;
4499 goto out_free;
4500 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004501 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004502 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004503out_ok:
4504 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004505out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004506 for (i = 0; i < npages; i++)
4507 if (pages[i])
4508 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004509 if (res.acl_scratch)
4510 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004511 return ret;
4512}
4513
Trond Myklebust16b42892006-08-24 12:27:15 -04004514static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4515{
4516 struct nfs4_exception exception = { };
4517 ssize_t ret;
4518 do {
4519 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004520 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04004521 if (ret >= 0)
4522 break;
4523 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4524 } while (exception.retry);
4525 return ret;
4526}
4527
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004528static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4529{
4530 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004531 int ret;
4532
4533 if (!nfs4_server_supports_acls(server))
4534 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004535 ret = nfs_revalidate_inode(server, inode);
4536 if (ret < 0)
4537 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004538 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4539 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004540 ret = nfs4_read_cached_acl(inode, buf, buflen);
4541 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004542 /* -ENOENT is returned if there is no ACL or if there is an ACL
4543 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004544 return ret;
4545 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004546}
4547
Trond Myklebust16b42892006-08-24 12:27:15 -04004548static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004549{
4550 struct nfs_server *server = NFS_SERVER(inode);
4551 struct page *pages[NFS4ACL_MAXPAGES];
4552 struct nfs_setaclargs arg = {
4553 .fh = NFS_FH(inode),
4554 .acl_pages = pages,
4555 .acl_len = buflen,
4556 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004557 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4560 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004561 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004562 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004563 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004564 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004565
4566 if (!nfs4_server_supports_acls(server))
4567 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004568 if (npages > ARRAY_SIZE(pages))
4569 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004570 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4571 if (i < 0)
4572 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004573 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004574 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004575
4576 /*
4577 * Free each page after tx, so the only ref left is
4578 * held by the network stack
4579 */
4580 for (; i > 0; i--)
4581 put_page(pages[i-1]);
4582
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004583 /*
4584 * Acl update can result in inode attribute update.
4585 * so mark the attribute cache invalid.
4586 */
4587 spin_lock(&inode->i_lock);
4588 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4589 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004590 nfs_access_zap_cache(inode);
4591 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004592 return ret;
4593}
4594
Trond Myklebust16b42892006-08-24 12:27:15 -04004595static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4596{
4597 struct nfs4_exception exception = { };
4598 int err;
4599 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004600 err = __nfs4_proc_set_acl(inode, buf, buflen);
4601 trace_nfs4_set_acl(inode, err);
4602 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04004603 &exception);
4604 } while (exception.retry);
4605 return err;
4606}
4607
David Quigleyaa9c2662013-05-22 12:50:44 -04004608#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4609static int _nfs4_get_security_label(struct inode *inode, void *buf,
4610 size_t buflen)
4611{
4612 struct nfs_server *server = NFS_SERVER(inode);
4613 struct nfs_fattr fattr;
4614 struct nfs4_label label = {0, 0, buflen, buf};
4615
4616 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004617 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004618 .fh = NFS_FH(inode),
4619 .bitmask = bitmask,
4620 };
4621 struct nfs4_getattr_res res = {
4622 .fattr = &fattr,
4623 .label = &label,
4624 .server = server,
4625 };
4626 struct rpc_message msg = {
4627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004628 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004629 .rpc_resp = &res,
4630 };
4631 int ret;
4632
4633 nfs_fattr_init(&fattr);
4634
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004635 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004636 if (ret)
4637 return ret;
4638 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4639 return -ENOENT;
4640 if (buflen < label.len)
4641 return -ERANGE;
4642 return 0;
4643}
4644
4645static int nfs4_get_security_label(struct inode *inode, void *buf,
4646 size_t buflen)
4647{
4648 struct nfs4_exception exception = { };
4649 int err;
4650
4651 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4652 return -EOPNOTSUPP;
4653
4654 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004655 err = _nfs4_get_security_label(inode, buf, buflen);
4656 trace_nfs4_get_security_label(inode, err);
4657 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004658 &exception);
4659 } while (exception.retry);
4660 return err;
4661}
4662
4663static int _nfs4_do_set_security_label(struct inode *inode,
4664 struct nfs4_label *ilabel,
4665 struct nfs_fattr *fattr,
4666 struct nfs4_label *olabel)
4667{
4668
4669 struct iattr sattr = {0};
4670 struct nfs_server *server = NFS_SERVER(inode);
4671 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004672 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004673 .fh = NFS_FH(inode),
4674 .iap = &sattr,
4675 .server = server,
4676 .bitmask = bitmask,
4677 .label = ilabel,
4678 };
4679 struct nfs_setattrres res = {
4680 .fattr = fattr,
4681 .label = olabel,
4682 .server = server,
4683 };
4684 struct rpc_message msg = {
4685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004686 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004687 .rpc_resp = &res,
4688 };
4689 int status;
4690
Jeff Layton12207f62013-11-01 10:49:32 -04004691 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004692
Jeff Layton12207f62013-11-01 10:49:32 -04004693 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004694 if (status)
4695 dprintk("%s failed: %d\n", __func__, status);
4696
4697 return status;
4698}
4699
4700static int nfs4_do_set_security_label(struct inode *inode,
4701 struct nfs4_label *ilabel,
4702 struct nfs_fattr *fattr,
4703 struct nfs4_label *olabel)
4704{
4705 struct nfs4_exception exception = { };
4706 int err;
4707
4708 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004709 err = _nfs4_do_set_security_label(inode, ilabel,
4710 fattr, olabel);
4711 trace_nfs4_set_security_label(inode, err);
4712 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004713 &exception);
4714 } while (exception.retry);
4715 return err;
4716}
4717
4718static int
4719nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4720{
4721 struct nfs4_label ilabel, *olabel = NULL;
4722 struct nfs_fattr fattr;
4723 struct rpc_cred *cred;
4724 struct inode *inode = dentry->d_inode;
4725 int status;
4726
4727 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4728 return -EOPNOTSUPP;
4729
4730 nfs_fattr_init(&fattr);
4731
4732 ilabel.pi = 0;
4733 ilabel.lfs = 0;
4734 ilabel.label = (char *)buf;
4735 ilabel.len = buflen;
4736
4737 cred = rpc_lookup_cred();
4738 if (IS_ERR(cred))
4739 return PTR_ERR(cred);
4740
4741 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4742 if (IS_ERR(olabel)) {
4743 status = -PTR_ERR(olabel);
4744 goto out;
4745 }
4746
4747 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4748 if (status == 0)
4749 nfs_setsecurity(inode, &fattr, olabel);
4750
4751 nfs4_label_free(olabel);
4752out:
4753 put_rpccred(cred);
4754 return status;
4755}
4756#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4757
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004760nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004762 struct nfs_client *clp = server->nfs_client;
4763
4764 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 return 0;
4766 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004767 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004768 case -NFS4ERR_ADMIN_REVOKED:
4769 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004770 if (state == NULL)
4771 break;
4772 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004773 case -NFS4ERR_OPENMODE:
4774 if (state == NULL)
4775 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004776 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004777 goto recovery_failed;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004778 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004779 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004780 if (state != NULL) {
4781 if (nfs4_schedule_stateid_recovery(server, state) < 0)
Chuck Lever9f51a782013-10-17 14:13:13 -04004782 goto recovery_failed;
Trond Myklebust5d422302013-03-14 16:57:48 -04004783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004785 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004786 nfs4_schedule_lease_recovery(clp);
4787 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -04004788 case -NFS4ERR_MOVED:
4789 if (nfs4_schedule_migration_recovery(server) < 0)
4790 goto recovery_failed;
4791 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04004792 case -NFS4ERR_LEASE_MOVED:
4793 nfs4_schedule_lease_moved_recovery(clp);
4794 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004795#if defined(CONFIG_NFS_V4_1)
4796 case -NFS4ERR_BADSESSION:
4797 case -NFS4ERR_BADSLOT:
4798 case -NFS4ERR_BAD_HIGH_SLOT:
4799 case -NFS4ERR_DEADSESSION:
4800 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4801 case -NFS4ERR_SEQ_FALSE_RETRY:
4802 case -NFS4ERR_SEQ_MISORDERED:
4803 dprintk("%s ERROR %d, Reset session\n", __func__,
4804 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004805 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4a82fd72013-11-15 16:36:16 -05004806 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004807#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004809 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004810 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004812 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 case -NFS4ERR_OLD_STATEID:
Chuck Leverf1478c12013-10-17 14:13:07 -04004814 goto restart_call;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 }
4816 task->tk_status = nfs4_map_errors(task->tk_status);
4817 return 0;
Chuck Lever9f51a782013-10-17 14:13:13 -04004818recovery_failed:
Trond Myklebust5d422302013-03-14 16:57:48 -04004819 task->tk_status = -EIO;
4820 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004821wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004822 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004823 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4824 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Chuck Lever519ae252013-10-17 14:13:19 -04004825 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
4826 goto recovery_failed;
Chuck Leverf1478c12013-10-17 14:13:07 -04004827restart_call:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004828 task->tk_status = 0;
4829 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Chuck Leverf0920752012-05-21 22:45:41 -04004832static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4833 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004834{
4835 __be32 verf[2];
4836
Chuck Lever2c820d92012-05-21 22:45:33 -04004837 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4838 /* An impossible timestamp guarantees this value
4839 * will never match a generated boot time. */
4840 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004841 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004842 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004843 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004844 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4845 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004846 }
Chuck Levercd937102012-03-02 17:14:31 -05004847 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4848}
4849
Chuck Levere984a552012-09-14 17:24:21 -04004850static unsigned int
4851nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4852 char *buf, size_t len)
4853{
4854 unsigned int result;
4855
4856 rcu_read_lock();
4857 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4858 clp->cl_ipaddr,
4859 rpc_peeraddr2str(clp->cl_rpcclient,
4860 RPC_DISPLAY_ADDR),
4861 rpc_peeraddr2str(clp->cl_rpcclient,
4862 RPC_DISPLAY_PROTO));
4863 rcu_read_unlock();
4864 return result;
4865}
4866
4867static unsigned int
4868nfs4_init_uniform_client_string(const struct nfs_client *clp,
4869 char *buf, size_t len)
4870{
Trond Myklebust55b59292013-07-24 16:36:35 -04004871 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004872
4873 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004874 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4875 clp->rpc_ops->version,
4876 clp->cl_minorversion,
4877 nfs4_client_id_uniquifier,
4878 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004879 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4880 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004881 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004882}
4883
Chuck Lever706cb8d2014-03-12 12:51:47 -04004884/*
4885 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
4886 * services. Advertise one based on the address family of the
4887 * clientaddr.
4888 */
4889static unsigned int
4890nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
4891{
4892 if (strchr(clp->cl_ipaddr, ':') != NULL)
4893 return scnprintf(buf, len, "tcp6");
4894 else
4895 return scnprintf(buf, len, "tcp");
4896}
4897
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004898/**
4899 * nfs4_proc_setclientid - Negotiate client ID
4900 * @clp: state data structure
4901 * @program: RPC program for NFSv4 callback service
4902 * @port: IP port number for NFS4 callback service
4903 * @cred: RPC credential to use for this call
4904 * @res: where to place the result
4905 *
4906 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4907 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004908int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4909 unsigned short port, struct rpc_cred *cred,
4910 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911{
4912 nfs4_verifier sc_verifier;
4913 struct nfs4_setclientid setclientid = {
4914 .sc_verifier = &sc_verifier,
4915 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004916 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 };
4918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4920 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004921 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004922 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004924 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925
Chuck Leverde734832012-07-11 16:30:50 -04004926 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004927 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004928 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4929 setclientid.sc_name_len =
4930 nfs4_init_uniform_client_string(clp,
4931 setclientid.sc_name,
4932 sizeof(setclientid.sc_name));
4933 else
4934 setclientid.sc_name_len =
4935 nfs4_init_nonuniform_client_string(clp,
4936 setclientid.sc_name,
4937 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004938 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04004939 setclientid.sc_netid_len =
4940 nfs4_init_callback_netid(clp,
4941 setclientid.sc_netid,
4942 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04004943 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004944 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 clp->cl_ipaddr, port >> 8, port & 255);
4946
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004947 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4948 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4949 setclientid.sc_name_len, setclientid.sc_name);
4950 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004951 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004952 dprintk("NFS reply setclientid: %d\n", status);
4953 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004956/**
4957 * nfs4_proc_setclientid_confirm - Confirm client ID
4958 * @clp: state data structure
4959 * @res: result of a previous SETCLIENTID
4960 * @cred: RPC credential to use for this call
4961 *
4962 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4963 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004964int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004965 struct nfs4_setclientid_res *arg,
4966 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 struct rpc_message msg = {
4969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004970 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004971 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 int status;
4974
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004975 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4976 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4977 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004978 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004979 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004980 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 return status;
4982}
4983
Trond Myklebustfe650402006-01-03 09:55:18 +01004984struct nfs4_delegreturndata {
4985 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004986 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004987 struct nfs_fh fh;
4988 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004989 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004990 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004991 int rpc_status;
4992};
4993
Trond Myklebustfe650402006-01-03 09:55:18 +01004994static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4995{
4996 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004997
Trond Myklebust14516c32010-07-31 14:29:06 -04004998 if (!nfs4_sequence_done(task, &data->res.seq_res))
4999 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005000
Trond Myklebustca8acf82013-08-13 10:36:56 -04005001 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005002 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005003 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005004 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005005 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005006 case -NFS4ERR_ADMIN_REVOKED:
5007 case -NFS4ERR_DELEG_REVOKED:
5008 case -NFS4ERR_BAD_STATEID:
5009 case -NFS4ERR_OLD_STATEID:
5010 case -NFS4ERR_STALE_STATEID:
5011 case -NFS4ERR_EXPIRED:
5012 task->tk_status = 0;
5013 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005014 default:
5015 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
5016 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005017 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005018 return;
5019 }
5020 }
5021 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005022}
5023
5024static void nfs4_delegreturn_release(void *calldata)
5025{
Trond Myklebustfe650402006-01-03 09:55:18 +01005026 kfree(calldata);
5027}
5028
Andy Adamson938e1012009-04-01 09:22:28 -04005029static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5030{
5031 struct nfs4_delegreturndata *d_data;
5032
5033 d_data = (struct nfs4_delegreturndata *)data;
5034
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005035 nfs4_setup_sequence(d_data->res.server,
5036 &d_data->args.seq_args,
5037 &d_data->res.seq_res,
5038 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005039}
Andy Adamson938e1012009-04-01 09:22:28 -04005040
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005041static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005042 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005043 .rpc_call_done = nfs4_delegreturn_done,
5044 .rpc_release = nfs4_delegreturn_release,
5045};
5046
Trond Myklebuste6f81072008-01-24 18:14:34 -05005047static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005048{
5049 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005050 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005051 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005052 struct rpc_message msg = {
5053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5054 .rpc_cred = cred,
5055 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005056 struct rpc_task_setup task_setup_data = {
5057 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005058 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005059 .callback_ops = &nfs4_delegreturn_ops,
5060 .flags = RPC_TASK_ASYNC,
5061 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005062 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005063
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005064 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005065 if (data == NULL)
5066 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005067 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01005068 data->args.fhandle = &data->fh;
5069 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005070 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005071 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005072 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005073 data->res.fattr = &data->fattr;
5074 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005075 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005076 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005077 data->rpc_status = 0;
5078
Trond Myklebustc970aa82007-07-14 15:39:59 -04005079 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005080 msg.rpc_argp = &data->args;
5081 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005082 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005083 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005084 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005085 if (!issync)
5086 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005087 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005088 if (status != 0)
5089 goto out;
5090 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005091 if (status == 0)
5092 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5093 else
5094 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005095out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005096 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005097 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098}
5099
Trond Myklebuste6f81072008-01-24 18:14:34 -05005100int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101{
5102 struct nfs_server *server = NFS_SERVER(inode);
5103 struct nfs4_exception exception = { };
5104 int err;
5105 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005106 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04005107 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 switch (err) {
5109 case -NFS4ERR_STALE_STATEID:
5110 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 case 0:
5112 return 0;
5113 }
5114 err = nfs4_handle_exception(server, err, &exception);
5115 } while (exception.retry);
5116 return err;
5117}
5118
5119#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5120#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5121
5122/*
5123 * sleep, with exponential backoff, and retry the LOCK operation.
5124 */
5125static unsigned long
5126nfs4_set_lock_task_retry(unsigned long timeout)
5127{
Colin Cross416ad3c2013-05-06 23:50:06 +00005128 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 timeout <<= 1;
5130 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5131 return NFS4_LOCK_MAXTIMEOUT;
5132 return timeout;
5133}
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5136{
5137 struct inode *inode = state->inode;
5138 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005139 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005140 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005142 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005144 struct nfs_lockt_res res = {
5145 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 };
5147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5149 .rpc_argp = &arg,
5150 .rpc_resp = &res,
5151 .rpc_cred = state->owner->so_cred,
5152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 struct nfs4_lock_state *lsp;
5154 int status;
5155
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005156 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005157 status = nfs4_set_lock_state(state, request);
5158 if (status != 0)
5159 goto out;
5160 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005161 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005162 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005163 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005164 switch (status) {
5165 case 0:
5166 request->fl_type = F_UNLCK;
5167 break;
5168 case -NFS4ERR_DENIED:
5169 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005171 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005172 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005173out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 return status;
5175}
5176
5177static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5178{
5179 struct nfs4_exception exception = { };
5180 int err;
5181
5182 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005183 err = _nfs4_proc_getlk(state, cmd, request);
5184 trace_nfs4_get_lock(request, state, cmd, err);
5185 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 &exception);
5187 } while (exception.retry);
5188 return err;
5189}
5190
5191static int do_vfs_lock(struct file *file, struct file_lock *fl)
5192{
5193 int res = 0;
5194 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5195 case FL_POSIX:
5196 res = posix_lock_file_wait(file, fl);
5197 break;
5198 case FL_FLOCK:
5199 res = flock_lock_file_wait(file, fl);
5200 break;
5201 default:
5202 BUG();
5203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return res;
5205}
5206
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005207struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005208 struct nfs_locku_args arg;
5209 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005210 struct nfs4_lock_state *lsp;
5211 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005212 struct file_lock fl;
5213 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005214 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005215};
5216
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005217static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5218 struct nfs_open_context *ctx,
5219 struct nfs4_lock_state *lsp,
5220 struct nfs_seqid *seqid)
5221{
5222 struct nfs4_unlockdata *p;
5223 struct inode *inode = lsp->ls_state->inode;
5224
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005225 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005226 if (p == NULL)
5227 return NULL;
5228 p->arg.fh = NFS_FH(inode);
5229 p->arg.fl = &p->fl;
5230 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005231 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005232 p->arg.stateid = &lsp->ls_stateid;
5233 p->lsp = lsp;
5234 atomic_inc(&lsp->ls_count);
5235 /* Ensure we don't close file until we're done freeing locks! */
5236 p->ctx = get_nfs_open_context(ctx);
5237 memcpy(&p->fl, fl, sizeof(p->fl));
5238 p->server = NFS_SERVER(inode);
5239 return p;
5240}
5241
Trond Myklebust06f814a2006-01-03 09:55:07 +01005242static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005243{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005244 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005245 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005246 nfs4_put_lock_state(calldata->lsp);
5247 put_nfs_open_context(calldata->ctx);
5248 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005249}
5250
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005251static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005252{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005253 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005254
Trond Myklebust14516c32010-07-31 14:29:06 -04005255 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5256 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005257 switch (task->tk_status) {
5258 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005259 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5260 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005261 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005262 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005263 case -NFS4ERR_BAD_STATEID:
5264 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005265 case -NFS4ERR_STALE_STATEID:
5266 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005267 break;
5268 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005269 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005270 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005271 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005272 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005273}
5274
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005275static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005276{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005277 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005279 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005280 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005281 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005282 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005283 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005284 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005285 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005286 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005287 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005288 &calldata->res.seq_res,
5289 task) != 0)
5290 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005291 return;
5292out_no_action:
5293 task->tk_action = NULL;
5294out_wait:
5295 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005298static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005299 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005300 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005301 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005302};
5303
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005304static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5305 struct nfs_open_context *ctx,
5306 struct nfs4_lock_state *lsp,
5307 struct nfs_seqid *seqid)
5308{
5309 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005310 struct rpc_message msg = {
5311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5312 .rpc_cred = ctx->cred,
5313 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005314 struct rpc_task_setup task_setup_data = {
5315 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005316 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005317 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005318 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005319 .flags = RPC_TASK_ASYNC,
5320 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005321
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005322 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5323 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5324
Frank Filz137d6ac2007-07-09 15:32:29 -07005325 /* Ensure this is an unlock - when canceling a lock, the
5326 * canceled lock is passed in, and it won't be an unlock.
5327 */
5328 fl->fl_type = F_UNLCK;
5329
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005330 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5331 if (data == NULL) {
5332 nfs_free_seqid(seqid);
5333 return ERR_PTR(-ENOMEM);
5334 }
5335
Chuck Levera9c92d62013-08-09 12:48:18 -04005336 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005337 msg.rpc_argp = &data->arg;
5338 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005339 task_setup_data.callback_data = data;
5340 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005341}
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5344{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005345 struct inode *inode = state->inode;
5346 struct nfs4_state_owner *sp = state->owner;
5347 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005348 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005349 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005350 struct rpc_task *task;
5351 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005352 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353
Trond Myklebust9b073572006-06-29 16:38:34 -04005354 status = nfs4_set_lock_state(state, request);
5355 /* Unlock _before_ we do the RPC call */
5356 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005357 /* Exclude nfs_delegation_claim_locks() */
5358 mutex_lock(&sp->so_delegreturn_mutex);
5359 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005360 down_read(&nfsi->rwsem);
5361 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5362 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005363 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005364 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005365 }
5366 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005367 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005368 if (status != 0)
5369 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005370 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005371 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005372 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5373 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005374 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005375 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005376 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005377 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005378 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005379 status = PTR_ERR(task);
5380 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005381 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005382 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005383 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005384out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005385 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005386 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388}
5389
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005390struct nfs4_lockdata {
5391 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005392 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005393 struct nfs4_lock_state *lsp;
5394 struct nfs_open_context *ctx;
5395 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005396 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005397 int rpc_status;
5398 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005399 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005400};
5401
5402static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005403 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5404 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405{
5406 struct nfs4_lockdata *p;
5407 struct inode *inode = lsp->ls_state->inode;
5408 struct nfs_server *server = NFS_SERVER(inode);
5409
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005410 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005411 if (p == NULL)
5412 return NULL;
5413
5414 p->arg.fh = NFS_FH(inode);
5415 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005416 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005417 if (p->arg.open_seqid == NULL)
5418 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005419 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005420 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005421 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005422 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005423 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005424 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005425 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005426 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005427 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005428 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005429 atomic_inc(&lsp->ls_count);
5430 p->ctx = get_nfs_open_context(ctx);
5431 memcpy(&p->fl, fl, sizeof(p->fl));
5432 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005433out_free_seqid:
5434 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005435out_free:
5436 kfree(p);
5437 return NULL;
5438}
5439
5440static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5441{
5442 struct nfs4_lockdata *data = calldata;
5443 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Harvey Harrison3110ff82008-05-02 13:42:44 -07005445 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005446 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005447 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005448 /* Do we need to do an open_to_lock_owner? */
5449 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005450 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005451 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005452 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005453 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005454 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005455 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005456 } else
5457 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005458 if (!nfs4_valid_open_stateid(state)) {
5459 data->rpc_status = -EBADF;
5460 task->tk_action = NULL;
5461 goto out_release_open_seqid;
5462 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005463 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005464 if (nfs4_setup_sequence(data->server,
5465 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005466 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005467 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005468 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005469out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005470 nfs_release_seqid(data->arg.open_seqid);
5471out_release_lock_seqid:
5472 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005473out_wait:
5474 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005475 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005476}
5477
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005478static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5479{
5480 struct nfs4_lockdata *data = calldata;
5481
Harvey Harrison3110ff82008-05-02 13:42:44 -07005482 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005483
Trond Myklebust14516c32010-07-31 14:29:06 -04005484 if (!nfs4_sequence_done(task, &data->res.seq_res))
5485 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005486
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005487 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005488 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005489 if (data->rpc_status == 0)
5490 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5491 else
5492 goto out;
5493 }
5494 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005495 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005496 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005497 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005498 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005499out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005500 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005501}
5502
5503static void nfs4_lock_release(void *calldata)
5504{
5505 struct nfs4_lockdata *data = calldata;
5506
Harvey Harrison3110ff82008-05-02 13:42:44 -07005507 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005508 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005509 if (data->cancelled != 0) {
5510 struct rpc_task *task;
5511 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5512 data->arg.lock_seqid);
5513 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005514 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005515 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005516 } else
5517 nfs_free_seqid(data->arg.lock_seqid);
5518 nfs4_put_lock_state(data->lsp);
5519 put_nfs_open_context(data->ctx);
5520 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005521 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005522}
5523
5524static const struct rpc_call_ops nfs4_lock_ops = {
5525 .rpc_call_prepare = nfs4_lock_prepare,
5526 .rpc_call_done = nfs4_lock_done,
5527 .rpc_release = nfs4_lock_release,
5528};
5529
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005530static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5531{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005532 switch (error) {
5533 case -NFS4ERR_ADMIN_REVOKED:
5534 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005535 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005536 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005537 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005538 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005539 break;
5540 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005541 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005542 case -NFS4ERR_EXPIRED:
5543 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005544 };
5545}
5546
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005547static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005548{
5549 struct nfs4_lockdata *data;
5550 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005551 struct rpc_message msg = {
5552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5553 .rpc_cred = state->owner->so_cred,
5554 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005555 struct rpc_task_setup task_setup_data = {
5556 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005557 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005558 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005559 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005560 .flags = RPC_TASK_ASYNC,
5561 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005562 int ret;
5563
Harvey Harrison3110ff82008-05-02 13:42:44 -07005564 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005565 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005566 fl->fl_u.nfs4_fl.owner,
5567 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005568 if (data == NULL)
5569 return -ENOMEM;
5570 if (IS_SETLKW(cmd))
5571 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005572 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005573 msg.rpc_argp = &data->arg;
5574 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005575 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005576 if (recovery_type > NFS_LOCK_NEW) {
5577 if (recovery_type == NFS_LOCK_RECLAIM)
5578 data->arg.reclaim = NFS_LOCK_RECLAIM;
5579 nfs4_set_sequence_privileged(&data->arg.seq_args);
5580 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005581 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005582 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005583 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005584 ret = nfs4_wait_for_completion_rpc_task(task);
5585 if (ret == 0) {
5586 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005587 if (ret)
5588 nfs4_handle_setlk_error(data->server, data->lsp,
5589 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005590 } else
5591 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005592 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005593 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005594 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595}
5596
5597static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5598{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005599 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005600 struct nfs4_exception exception = {
5601 .inode = state->inode,
5602 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005603 int err;
5604
5605 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005606 /* Cache the lock if possible... */
5607 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5608 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005609 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005610 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005611 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005612 break;
5613 nfs4_handle_exception(server, err, &exception);
5614 } while (exception.retry);
5615 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616}
5617
5618static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5619{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005620 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005621 struct nfs4_exception exception = {
5622 .inode = state->inode,
5623 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005624 int err;
5625
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005626 err = nfs4_set_lock_state(state, request);
5627 if (err != 0)
5628 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005629 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005630 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
5631 return 0;
5632 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005633 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5635 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005637 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005638 switch (err) {
5639 default:
5640 goto out;
5641 case -NFS4ERR_GRACE:
5642 case -NFS4ERR_DELAY:
5643 nfs4_handle_exception(server, err, &exception);
5644 err = 0;
5645 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005646 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005647out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649}
5650
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005651#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005652/**
5653 * nfs41_check_expired_locks - possibly free a lock stateid
5654 *
5655 * @state: NFSv4 state for an inode
5656 *
5657 * Returns NFS_OK if recovery for this stateid is now finished.
5658 * Otherwise a negative NFS4ERR value is returned.
5659 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005660static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005661{
Chuck Levereb64cf92012-07-11 16:30:05 -04005662 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005663 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005664 struct nfs_server *server = NFS_SERVER(state->inode);
5665
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005666 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005667 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005668 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5669
5670 status = nfs41_test_stateid(server,
5671 &lsp->ls_stateid,
5672 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005673 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005674 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005675 /* Free the stateid unless the server
5676 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005677 if (status != -NFS4ERR_BAD_STATEID)
5678 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005679 &lsp->ls_stateid,
5680 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005681 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005682 ret = status;
5683 }
5684 }
5685 };
5686
5687 return ret;
5688}
5689
5690static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5691{
5692 int status = NFS_OK;
5693
5694 if (test_bit(LK_STATE_IN_USE, &state->flags))
5695 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005696 if (status != NFS_OK)
5697 status = nfs4_lock_expired(state, request);
5698 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005699}
5700#endif
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5703{
Trond Myklebust9a99af42013-02-04 20:17:49 -05005704 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005705 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005706 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005707 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005708 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005710 if ((fl_flags & FL_POSIX) &&
5711 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5712 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005713 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005714 status = nfs4_set_lock_state(state, request);
5715 if (status != 0)
5716 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005717 request->fl_flags |= FL_ACCESS;
5718 status = do_vfs_lock(request->fl_file, request);
5719 if (status < 0)
5720 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005721 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005722 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005723 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005724 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005725 request->fl_flags = fl_flags & ~FL_SLEEP;
5726 status = do_vfs_lock(request->fl_file, request);
5727 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005728 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05005729 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5730 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005731 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005732 if (status != 0)
Trond Myklebust9a99af42013-02-04 20:17:49 -05005733 goto out;
5734 down_read(&nfsi->rwsem);
5735 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5736 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005737 goto out_unlock;
Trond Myklebust9a99af42013-02-04 20:17:49 -05005738 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005739 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005740 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005741 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005742 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5743 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005744out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005745 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005746out:
5747 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 return status;
5749}
5750
5751static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5752{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005753 struct nfs4_exception exception = {
5754 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005755 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005756 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 int err;
5758
5759 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005760 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005761 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005762 if (err == -NFS4ERR_DENIED)
5763 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005765 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 } while (exception.retry);
5767 return err;
5768}
5769
5770static int
5771nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5772{
5773 struct nfs_open_context *ctx;
5774 struct nfs4_state *state;
5775 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5776 int status;
5777
5778 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005779 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 state = ctx->state;
5781
5782 if (request->fl_start < 0 || request->fl_end < 0)
5783 return -EINVAL;
5784
Trond Myklebustd9531262009-07-21 19:22:38 -04005785 if (IS_GETLK(cmd)) {
5786 if (state != NULL)
5787 return nfs4_proc_getlk(state, F_GETLK, request);
5788 return 0;
5789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5792 return -EINVAL;
5793
Trond Myklebustd9531262009-07-21 19:22:38 -04005794 if (request->fl_type == F_UNLCK) {
5795 if (state != NULL)
5796 return nfs4_proc_unlck(state, cmd, request);
5797 return 0;
5798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
Trond Myklebustd9531262009-07-21 19:22:38 -04005800 if (state == NULL)
5801 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005802 /*
5803 * Don't rely on the VFS having checked the file open mode,
5804 * since it won't do this for flock() locks.
5805 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005806 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005807 case F_RDLCK:
5808 if (!(filp->f_mode & FMODE_READ))
5809 return -EBADF;
5810 break;
5811 case F_WRLCK:
5812 if (!(filp->f_mode & FMODE_WRITE))
5813 return -EBADF;
5814 }
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 do {
5817 status = nfs4_proc_setlk(state, cmd, request);
5818 if ((status != -EAGAIN) || IS_SETLK(cmd))
5819 break;
5820 timeout = nfs4_set_lock_task_retry(timeout);
5821 status = -ERESTARTSYS;
5822 if (signalled())
5823 break;
5824 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 return status;
5826}
5827
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005828int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005829{
5830 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005831 int err;
5832
5833 err = nfs4_set_lock_state(state, fl);
5834 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005835 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04005837 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005838}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005839
Trond Myklebustcf470c32012-03-07 13:49:12 -05005840struct nfs_release_lockowner_data {
5841 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005842 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005843 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005844 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04005845 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005846};
5847
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005848static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
5849{
5850 struct nfs_release_lockowner_data *data = calldata;
5851 nfs40_setup_sequence(data->server,
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005852 &data->args.seq_args, &data->res.seq_res, task);
Chuck Lever60ea6812013-10-17 14:13:47 -04005853 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005854}
5855
5856static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
5857{
5858 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04005859 struct nfs_server *server = data->server;
5860
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005861 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04005862
5863 switch (task->tk_status) {
5864 case 0:
5865 renew_lease(server, data->timestamp);
5866 break;
5867 case -NFS4ERR_STALE_CLIENTID:
5868 case -NFS4ERR_EXPIRED:
5869 case -NFS4ERR_LEASE_MOVED:
5870 case -NFS4ERR_DELAY:
5871 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN)
5872 rpc_restart_call_prepare(task);
5873 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005874}
5875
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005876static void nfs4_release_lockowner_release(void *calldata)
5877{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005878 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005879 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005880 kfree(calldata);
5881}
5882
Trond Myklebust17280172012-03-11 13:11:00 -04005883static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005884 .rpc_call_prepare = nfs4_release_lockowner_prepare,
5885 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005886 .rpc_release = nfs4_release_lockowner_release,
5887};
5888
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005889static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005890{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005891 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005892 struct rpc_message msg = {
5893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5894 };
5895
5896 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005897 return -EINVAL;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005898
Trond Myklebustcf470c32012-03-07 13:49:12 -05005899 data = kmalloc(sizeof(*data), GFP_NOFS);
5900 if (!data)
5901 return -ENOMEM;
5902 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005903 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005904 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5905 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5906 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04005907
Trond Myklebustcf470c32012-03-07 13:49:12 -05005908 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08005909 msg.rpc_resp = &data->res;
5910 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05005911 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5912 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005913}
5914
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005915#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5916
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005917static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5918 const void *buf, size_t buflen,
5919 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005920{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005921 if (strcmp(key, "") != 0)
5922 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005923
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005924 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005925}
5926
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005927static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5928 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005929{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005930 if (strcmp(key, "") != 0)
5931 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005932
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005933 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005934}
5935
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005936static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5937 size_t list_len, const char *name,
5938 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005939{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005940 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005941
J. Bruce Fields096455a2006-03-20 23:23:42 -05005942 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5943 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005944
5945 if (list && len <= list_len)
5946 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005947 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005948}
5949
David Quigleyc9bccef2013-05-22 12:50:45 -04005950#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5951static inline int nfs4_server_supports_labels(struct nfs_server *server)
5952{
5953 return server->caps & NFS_CAP_SECURITY_LABEL;
5954}
5955
5956static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5957 const void *buf, size_t buflen,
5958 int flags, int type)
5959{
5960 if (security_ismaclabel(key))
5961 return nfs4_set_security_label(dentry, buf, buflen);
5962
5963 return -EOPNOTSUPP;
5964}
5965
5966static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5967 void *buf, size_t buflen, int type)
5968{
5969 if (security_ismaclabel(key))
5970 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5971 return -EOPNOTSUPP;
5972}
5973
5974static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5975 size_t list_len, const char *name,
5976 size_t name_len, int type)
5977{
5978 size_t len = 0;
5979
5980 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5981 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5982 if (list && len <= list_len)
5983 security_inode_listsecurity(dentry->d_inode, list, len);
5984 }
5985 return len;
5986}
5987
5988static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5989 .prefix = XATTR_SECURITY_PREFIX,
5990 .list = nfs4_xattr_list_nfs4_label,
5991 .get = nfs4_xattr_get_nfs4_label,
5992 .set = nfs4_xattr_set_nfs4_label,
5993};
5994#endif
5995
5996
Andy Adamson533eb462011-06-13 18:25:56 -04005997/*
5998 * nfs_fhget will use either the mounted_on_fileid or the fileid
5999 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006000static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6001{
Andy Adamson533eb462011-06-13 18:25:56 -04006002 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6003 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6004 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006005 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006006 return;
6007
6008 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006009 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006010 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6011 fattr->nlink = 2;
6012}
6013
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006014static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6015 const struct qstr *name,
6016 struct nfs4_fs_locations *fs_locations,
6017 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006018{
6019 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006020 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006021 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006022 };
6023 struct nfs4_fs_locations_arg args = {
6024 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006025 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006026 .page = page,
6027 .bitmask = bitmask,
6028 };
Benny Halevy22958462009-04-01 09:22:02 -04006029 struct nfs4_fs_locations_res res = {
6030 .fs_locations = fs_locations,
6031 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006032 struct rpc_message msg = {
6033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6034 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006035 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006036 };
6037 int status;
6038
Harvey Harrison3110ff82008-05-02 13:42:44 -07006039 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006040
6041 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6042 * is not supported */
6043 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6044 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6045 else
6046 bitmask[0] |= FATTR4_WORD0_FILEID;
6047
Trond Myklebustc228fd32007-01-13 02:28:11 -05006048 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006049 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006050 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006051 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006052 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006053 return status;
6054}
6055
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006056int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6057 const struct qstr *name,
6058 struct nfs4_fs_locations *fs_locations,
6059 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006060{
6061 struct nfs4_exception exception = { };
6062 int err;
6063 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006064 err = _nfs4_proc_fs_locations(client, dir, name,
6065 fs_locations, page);
6066 trace_nfs4_get_fs_locations(dir, name, err);
6067 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006068 &exception);
6069 } while (exception.retry);
6070 return err;
6071}
6072
Chuck Leverb03d7352013-10-17 14:12:50 -04006073/*
6074 * This operation also signals the server that this client is
6075 * performing migration recovery. The server can stop returning
6076 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6077 * appended to this compound to identify the client ID which is
6078 * performing recovery.
6079 */
6080static int _nfs40_proc_get_locations(struct inode *inode,
6081 struct nfs4_fs_locations *locations,
6082 struct page *page, struct rpc_cred *cred)
6083{
6084 struct nfs_server *server = NFS_SERVER(inode);
6085 struct rpc_clnt *clnt = server->client;
6086 u32 bitmask[2] = {
6087 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6088 };
6089 struct nfs4_fs_locations_arg args = {
6090 .clientid = server->nfs_client->cl_clientid,
6091 .fh = NFS_FH(inode),
6092 .page = page,
6093 .bitmask = bitmask,
6094 .migration = 1, /* skip LOOKUP */
6095 .renew = 1, /* append RENEW */
6096 };
6097 struct nfs4_fs_locations_res res = {
6098 .fs_locations = locations,
6099 .migration = 1,
6100 .renew = 1,
6101 };
6102 struct rpc_message msg = {
6103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6104 .rpc_argp = &args,
6105 .rpc_resp = &res,
6106 .rpc_cred = cred,
6107 };
6108 unsigned long now = jiffies;
6109 int status;
6110
6111 nfs_fattr_init(&locations->fattr);
6112 locations->server = server;
6113 locations->nlocations = 0;
6114
6115 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6116 nfs4_set_sequence_privileged(&args.seq_args);
6117 status = nfs4_call_sync_sequence(clnt, server, &msg,
6118 &args.seq_args, &res.seq_res);
6119 if (status)
6120 return status;
6121
6122 renew_lease(server, now);
6123 return 0;
6124}
6125
6126#ifdef CONFIG_NFS_V4_1
6127
6128/*
6129 * This operation also signals the server that this client is
6130 * performing migration recovery. The server can stop asserting
6131 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6132 * performing this operation is identified in the SEQUENCE
6133 * operation in this compound.
6134 *
6135 * When the client supports GETATTR(fs_locations_info), it can
6136 * be plumbed in here.
6137 */
6138static int _nfs41_proc_get_locations(struct inode *inode,
6139 struct nfs4_fs_locations *locations,
6140 struct page *page, struct rpc_cred *cred)
6141{
6142 struct nfs_server *server = NFS_SERVER(inode);
6143 struct rpc_clnt *clnt = server->client;
6144 u32 bitmask[2] = {
6145 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6146 };
6147 struct nfs4_fs_locations_arg args = {
6148 .fh = NFS_FH(inode),
6149 .page = page,
6150 .bitmask = bitmask,
6151 .migration = 1, /* skip LOOKUP */
6152 };
6153 struct nfs4_fs_locations_res res = {
6154 .fs_locations = locations,
6155 .migration = 1,
6156 };
6157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6159 .rpc_argp = &args,
6160 .rpc_resp = &res,
6161 .rpc_cred = cred,
6162 };
6163 int status;
6164
6165 nfs_fattr_init(&locations->fattr);
6166 locations->server = server;
6167 locations->nlocations = 0;
6168
6169 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6170 nfs4_set_sequence_privileged(&args.seq_args);
6171 status = nfs4_call_sync_sequence(clnt, server, &msg,
6172 &args.seq_args, &res.seq_res);
6173 if (status == NFS4_OK &&
6174 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6175 status = -NFS4ERR_LEASE_MOVED;
6176 return status;
6177}
6178
6179#endif /* CONFIG_NFS_V4_1 */
6180
6181/**
6182 * nfs4_proc_get_locations - discover locations for a migrated FSID
6183 * @inode: inode on FSID that is migrating
6184 * @locations: result of query
6185 * @page: buffer
6186 * @cred: credential to use for this operation
6187 *
6188 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6189 * operation failed, or a negative errno if a local error occurred.
6190 *
6191 * On success, "locations" is filled in, but if the server has
6192 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6193 * asserted.
6194 *
6195 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6196 * from this client that require migration recovery.
6197 */
6198int nfs4_proc_get_locations(struct inode *inode,
6199 struct nfs4_fs_locations *locations,
6200 struct page *page, struct rpc_cred *cred)
6201{
6202 struct nfs_server *server = NFS_SERVER(inode);
6203 struct nfs_client *clp = server->nfs_client;
6204 const struct nfs4_mig_recovery_ops *ops =
6205 clp->cl_mvops->mig_recovery_ops;
6206 struct nfs4_exception exception = { };
6207 int status;
6208
6209 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6210 (unsigned long long)server->fsid.major,
6211 (unsigned long long)server->fsid.minor,
6212 clp->cl_hostname);
6213 nfs_display_fhandle(NFS_FH(inode), __func__);
6214
6215 do {
6216 status = ops->get_locations(inode, locations, page, cred);
6217 if (status != -NFS4ERR_DELAY)
6218 break;
6219 nfs4_handle_exception(server, status, &exception);
6220 } while (exception.retry);
6221 return status;
6222}
6223
Chuck Lever44c99932013-10-17 14:13:30 -04006224/*
6225 * This operation also signals the server that this client is
6226 * performing "lease moved" recovery. The server can stop
6227 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6228 * is appended to this compound to identify the client ID which is
6229 * performing recovery.
6230 */
6231static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6232{
6233 struct nfs_server *server = NFS_SERVER(inode);
6234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6235 struct rpc_clnt *clnt = server->client;
6236 struct nfs4_fsid_present_arg args = {
6237 .fh = NFS_FH(inode),
6238 .clientid = clp->cl_clientid,
6239 .renew = 1, /* append RENEW */
6240 };
6241 struct nfs4_fsid_present_res res = {
6242 .renew = 1,
6243 };
6244 struct rpc_message msg = {
6245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6246 .rpc_argp = &args,
6247 .rpc_resp = &res,
6248 .rpc_cred = cred,
6249 };
6250 unsigned long now = jiffies;
6251 int status;
6252
6253 res.fh = nfs_alloc_fhandle();
6254 if (res.fh == NULL)
6255 return -ENOMEM;
6256
6257 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6258 nfs4_set_sequence_privileged(&args.seq_args);
6259 status = nfs4_call_sync_sequence(clnt, server, &msg,
6260 &args.seq_args, &res.seq_res);
6261 nfs_free_fhandle(res.fh);
6262 if (status)
6263 return status;
6264
6265 do_renew_lease(clp, now);
6266 return 0;
6267}
6268
6269#ifdef CONFIG_NFS_V4_1
6270
6271/*
6272 * This operation also signals the server that this client is
6273 * performing "lease moved" recovery. The server can stop asserting
6274 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6275 * this operation is identified in the SEQUENCE operation in this
6276 * compound.
6277 */
6278static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6279{
6280 struct nfs_server *server = NFS_SERVER(inode);
6281 struct rpc_clnt *clnt = server->client;
6282 struct nfs4_fsid_present_arg args = {
6283 .fh = NFS_FH(inode),
6284 };
6285 struct nfs4_fsid_present_res res = {
6286 };
6287 struct rpc_message msg = {
6288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6289 .rpc_argp = &args,
6290 .rpc_resp = &res,
6291 .rpc_cred = cred,
6292 };
6293 int status;
6294
6295 res.fh = nfs_alloc_fhandle();
6296 if (res.fh == NULL)
6297 return -ENOMEM;
6298
6299 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6300 nfs4_set_sequence_privileged(&args.seq_args);
6301 status = nfs4_call_sync_sequence(clnt, server, &msg,
6302 &args.seq_args, &res.seq_res);
6303 nfs_free_fhandle(res.fh);
6304 if (status == NFS4_OK &&
6305 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6306 status = -NFS4ERR_LEASE_MOVED;
6307 return status;
6308}
6309
6310#endif /* CONFIG_NFS_V4_1 */
6311
6312/**
6313 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6314 * @inode: inode on FSID to check
6315 * @cred: credential to use for this operation
6316 *
6317 * Server indicates whether the FSID is present, moved, or not
6318 * recognized. This operation is necessary to clear a LEASE_MOVED
6319 * condition for this client ID.
6320 *
6321 * Returns NFS4_OK if the FSID is present on this server,
6322 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6323 * NFS4ERR code if some error occurred on the server, or a
6324 * negative errno if a local failure occurred.
6325 */
6326int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6327{
6328 struct nfs_server *server = NFS_SERVER(inode);
6329 struct nfs_client *clp = server->nfs_client;
6330 const struct nfs4_mig_recovery_ops *ops =
6331 clp->cl_mvops->mig_recovery_ops;
6332 struct nfs4_exception exception = { };
6333 int status;
6334
6335 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6336 (unsigned long long)server->fsid.major,
6337 (unsigned long long)server->fsid.minor,
6338 clp->cl_hostname);
6339 nfs_display_fhandle(NFS_FH(inode), __func__);
6340
6341 do {
6342 status = ops->fsid_present(inode, cred);
6343 if (status != -NFS4ERR_DELAY)
6344 break;
6345 nfs4_handle_exception(server, status, &exception);
6346 } while (exception.retry);
6347 return status;
6348}
6349
Andy Adamson5ec16a82013-08-08 10:57:55 -04006350/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006351 * If 'use_integrity' is true and the state managment nfs_client
6352 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6353 * and the machine credential as per RFC3530bis and RFC5661 Security
6354 * Considerations sections. Otherwise, just use the user cred with the
6355 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006356 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006357static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006358{
6359 int status;
6360 struct nfs4_secinfo_arg args = {
6361 .dir_fh = NFS_FH(dir),
6362 .name = name,
6363 };
6364 struct nfs4_secinfo_res res = {
6365 .flavors = flavors,
6366 };
6367 struct rpc_message msg = {
6368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6369 .rpc_argp = &args,
6370 .rpc_resp = &res,
6371 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006372 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006373 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006374
6375 if (use_integrity) {
6376 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006377 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6378 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006379 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006380
6381 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006382
6383 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6384 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6385
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006386 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6387 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006388 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006389
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006390 if (cred)
6391 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006392
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006393 return status;
6394}
6395
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006396int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6397 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006398{
6399 struct nfs4_exception exception = { };
6400 int err;
6401 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006402 err = -NFS4ERR_WRONGSEC;
6403
6404 /* try to use integrity protection with machine cred */
6405 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6406 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6407
6408 /*
6409 * if unable to use integrity protection, or SECINFO with
6410 * integrity protection returns NFS4ERR_WRONGSEC (which is
6411 * disallowed by spec, but exists in deployed servers) use
6412 * the current filesystem's rpc_client and the user cred.
6413 */
6414 if (err == -NFS4ERR_WRONGSEC)
6415 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6416
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006417 trace_nfs4_secinfo(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006419 &exception);
6420 } while (exception.retry);
6421 return err;
6422}
6423
Andy Adamson557134a2009-04-01 09:21:53 -04006424#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006425/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006426 * Check the exchange flags returned by the server for invalid flags, having
6427 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6428 * DS flags set.
6429 */
6430static int nfs4_check_cl_exchange_flags(u32 flags)
6431{
6432 if (flags & ~EXCHGID4_FLAG_MASK_R)
6433 goto out_inval;
6434 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6435 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6436 goto out_inval;
6437 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6438 goto out_inval;
6439 return NFS_OK;
6440out_inval:
6441 return -NFS4ERR_INVAL;
6442}
6443
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006444static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006445nfs41_same_server_scope(struct nfs41_server_scope *a,
6446 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006447{
6448 if (a->server_scope_sz == b->server_scope_sz &&
6449 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6450 return true;
6451
6452 return false;
6453}
6454
Andy Adamson357f54d2010-12-14 10:11:57 -05006455/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006456 * nfs4_proc_bind_conn_to_session()
6457 *
6458 * The 4.1 client currently uses the same TCP connection for the
6459 * fore and backchannel.
6460 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006461int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006462{
6463 int status;
6464 struct nfs41_bind_conn_to_session_res res;
6465 struct rpc_message msg = {
6466 .rpc_proc =
6467 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
6468 .rpc_argp = clp,
6469 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006470 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006471 };
6472
6473 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006474
6475 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
6476 if (unlikely(res.session == NULL)) {
6477 status = -ENOMEM;
6478 goto out;
6479 }
6480
6481 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006482 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006483 if (status == 0) {
6484 if (memcmp(res.session->sess_id.data,
6485 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6486 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6487 status = -EIO;
6488 goto out_session;
6489 }
6490 if (res.dir != NFS4_CDFS4_BOTH) {
6491 dprintk("NFS: %s: Unexpected direction from server\n",
6492 __func__);
6493 status = -EIO;
6494 goto out_session;
6495 }
6496 if (res.use_conn_in_rdma_mode) {
6497 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6498 __func__);
6499 status = -EIO;
6500 goto out_session;
6501 }
6502 }
6503out_session:
6504 kfree(res.session);
6505out:
6506 dprintk("<-- %s status= %d\n", __func__, status);
6507 return status;
6508}
6509
6510/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006511 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6512 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006513 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006514static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6515 .how = SP4_MACH_CRED,
6516 .enforce.u.words = {
6517 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6518 1 << (OP_EXCHANGE_ID - 32) |
6519 1 << (OP_CREATE_SESSION - 32) |
6520 1 << (OP_DESTROY_SESSION - 32) |
6521 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006522 },
6523 .allow.u.words = {
6524 [0] = 1 << (OP_CLOSE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006525 1 << (OP_LOCKU) |
6526 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006527 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006528 1 << (OP_SECINFO_NO_NAME - 32) |
6529 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006530 1 << (OP_FREE_STATEID - 32) |
6531 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006532 }
6533};
6534
6535/*
6536 * Select the state protection mode for client `clp' given the server results
6537 * from exchange_id in `sp'.
6538 *
6539 * Returns 0 on success, negative errno otherwise.
6540 */
6541static int nfs4_sp4_select_mode(struct nfs_client *clp,
6542 struct nfs41_state_protection *sp)
6543{
6544 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6545 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6546 1 << (OP_EXCHANGE_ID - 32) |
6547 1 << (OP_CREATE_SESSION - 32) |
6548 1 << (OP_DESTROY_SESSION - 32) |
6549 1 << (OP_DESTROY_CLIENTID - 32)
6550 };
6551 unsigned int i;
6552
6553 if (sp->how == SP4_MACH_CRED) {
6554 /* Print state protect result */
6555 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6556 for (i = 0; i <= LAST_NFS4_OP; i++) {
6557 if (test_bit(i, sp->enforce.u.longs))
6558 dfprintk(MOUNT, " enforce op %d\n", i);
6559 if (test_bit(i, sp->allow.u.longs))
6560 dfprintk(MOUNT, " allow op %d\n", i);
6561 }
6562
6563 /* make sure nothing is on enforce list that isn't supported */
6564 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6565 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6566 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6567 return -EINVAL;
6568 }
6569 }
6570
6571 /*
6572 * Minimal mode - state operations are allowed to use machine
6573 * credential. Note this already happens by default, so the
6574 * client doesn't have to do anything more than the negotiation.
6575 *
6576 * NOTE: we don't care if EXCHANGE_ID is in the list -
6577 * we're already using the machine cred for exchange_id
6578 * and will never use a different cred.
6579 */
6580 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6581 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6582 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6583 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6584 dfprintk(MOUNT, "sp4_mach_cred:\n");
6585 dfprintk(MOUNT, " minimal mode enabled\n");
6586 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6587 } else {
6588 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6589 return -EINVAL;
6590 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006591
6592 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
6593 test_bit(OP_LOCKU, sp->allow.u.longs)) {
6594 dfprintk(MOUNT, " cleanup mode enabled\n");
6595 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
6596 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006597
6598 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
6599 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
6600 dfprintk(MOUNT, " secinfo mode enabled\n");
6601 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
6602 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006603
6604 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
6605 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
6606 dfprintk(MOUNT, " stateid mode enabled\n");
6607 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
6608 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04006609
6610 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
6611 dfprintk(MOUNT, " write mode enabled\n");
6612 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
6613 }
6614
6615 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
6616 dfprintk(MOUNT, " commit mode enabled\n");
6617 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
6618 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006619 }
6620
6621 return 0;
6622}
6623
6624/*
6625 * _nfs4_proc_exchange_id()
6626 *
6627 * Wrapper for EXCHANGE_ID operation.
6628 */
6629static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
6630 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04006631{
6632 nfs4_verifier verifier;
6633 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05006634 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04006635 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04006636#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04006637 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04006638 EXCHGID4_FLAG_BIND_PRINC_STATEID |
6639 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
6640#else
6641 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
6642 EXCHGID4_FLAG_BIND_PRINC_STATEID,
6643#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04006644 };
6645 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04006646 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04006647 };
6648 int status;
6649 struct rpc_message msg = {
6650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
6651 .rpc_argp = &args,
6652 .rpc_resp = &res,
6653 .rpc_cred = cred,
6654 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006655
Chuck Leverf0920752012-05-21 22:45:41 -04006656 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006657 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6658 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006659 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6660 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6661 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006662
Chuck Leveracdeb692012-05-21 22:46:16 -04006663 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006664 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006665 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006666 status = -ENOMEM;
6667 goto out;
6668 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006669
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006670 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006671 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006672 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006673 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006674 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006675 }
6676
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006677 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006678 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006679 status = -ENOMEM;
6680 goto out_server_scope;
6681 }
6682
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006683 switch (sp4_how) {
6684 case SP4_NONE:
6685 args.state_protect.how = SP4_NONE;
6686 break;
6687
6688 case SP4_MACH_CRED:
6689 args.state_protect = nfs4_sp4_mach_cred_request;
6690 break;
6691
6692 default:
6693 /* unsupported! */
6694 WARN_ON_ONCE(1);
6695 status = -EINVAL;
6696 goto out_server_scope;
6697 }
6698
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006699 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006700 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006701 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006702 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006703
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006704 if (status == 0)
6705 status = nfs4_sp4_select_mode(clp, &res.state_protect);
6706
Chuck Lever177313f2012-05-21 22:44:58 -04006707 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006708 clp->cl_clientid = res.clientid;
6709 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6710 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6711 clp->cl_seqid = res.seqid;
6712
Chuck Leveracdeb692012-05-21 22:46:16 -04006713 kfree(clp->cl_serverowner);
6714 clp->cl_serverowner = res.server_owner;
6715 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006716
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006717 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006718 kfree(clp->cl_implid);
6719 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006720
Chuck Lever177313f2012-05-21 22:44:58 -04006721 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006722 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006723 res.server_scope)) {
6724 dprintk("%s: server_scope mismatch detected\n",
6725 __func__);
6726 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006727 kfree(clp->cl_serverscope);
6728 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006729 }
6730
Chuck Lever177313f2012-05-21 22:44:58 -04006731 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006732 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006733 goto out;
6734 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006735 } else
6736 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006737
Chuck Leveracdeb692012-05-21 22:46:16 -04006738out_server_owner:
6739 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006740out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006741 kfree(res.server_scope);
6742out:
Chuck Lever177313f2012-05-21 22:44:58 -04006743 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006744 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006745 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006746 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006747 clp->cl_implid->date.seconds,
6748 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006749 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006750 return status;
6751}
6752
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006753/*
6754 * nfs4_proc_exchange_id()
6755 *
6756 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6757 *
6758 * Since the clientid has expired, all compounds using sessions
6759 * associated with the stale clientid will be returning
6760 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
6761 * be in some phase of session reset.
6762 *
6763 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
6764 */
6765int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
6766{
6767 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
6768 int status;
6769
6770 /* try SP4_MACH_CRED if krb5i/p */
6771 if (authflavor == RPC_AUTH_GSS_KRB5I ||
6772 authflavor == RPC_AUTH_GSS_KRB5P) {
6773 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
6774 if (!status)
6775 return 0;
6776 }
6777
6778 /* try SP4_NONE */
6779 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
6780}
6781
Trond Myklebust66245532012-05-25 17:18:09 -04006782static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6783 struct rpc_cred *cred)
6784{
6785 struct rpc_message msg = {
6786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6787 .rpc_argp = clp,
6788 .rpc_cred = cred,
6789 };
6790 int status;
6791
6792 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006793 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006794 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006795 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006796 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6797 return status;
6798}
6799
6800static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6801 struct rpc_cred *cred)
6802{
6803 unsigned int loop;
6804 int ret;
6805
6806 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6807 ret = _nfs4_proc_destroy_clientid(clp, cred);
6808 switch (ret) {
6809 case -NFS4ERR_DELAY:
6810 case -NFS4ERR_CLIENTID_BUSY:
6811 ssleep(1);
6812 break;
6813 default:
6814 return ret;
6815 }
6816 }
6817 return 0;
6818}
6819
6820int nfs4_destroy_clientid(struct nfs_client *clp)
6821{
6822 struct rpc_cred *cred;
6823 int ret = 0;
6824
6825 if (clp->cl_mvops->minor_version < 1)
6826 goto out;
6827 if (clp->cl_exchange_flags == 0)
6828 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006829 if (clp->cl_preserve_clid)
6830 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006831 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006832 ret = nfs4_proc_destroy_clientid(clp, cred);
6833 if (cred)
6834 put_rpccred(cred);
6835 switch (ret) {
6836 case 0:
6837 case -NFS4ERR_STALE_CLIENTID:
6838 clp->cl_exchange_flags = 0;
6839 }
6840out:
6841 return ret;
6842}
6843
Andy Adamson2050f0c2009-04-01 09:22:30 -04006844struct nfs4_get_lease_time_data {
6845 struct nfs4_get_lease_time_args *args;
6846 struct nfs4_get_lease_time_res *res;
6847 struct nfs_client *clp;
6848};
6849
6850static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6851 void *calldata)
6852{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006853 struct nfs4_get_lease_time_data *data =
6854 (struct nfs4_get_lease_time_data *)calldata;
6855
6856 dprintk("--> %s\n", __func__);
6857 /* just setup sequence, do not trigger session recovery
6858 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006859 nfs41_setup_sequence(data->clp->cl_session,
6860 &data->args->la_seq_args,
6861 &data->res->lr_seq_res,
6862 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006863 dprintk("<-- %s\n", __func__);
6864}
6865
6866/*
6867 * Called from nfs4_state_manager thread for session setup, so don't recover
6868 * from sequence operation or clientid errors.
6869 */
6870static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6871{
6872 struct nfs4_get_lease_time_data *data =
6873 (struct nfs4_get_lease_time_data *)calldata;
6874
6875 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006876 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6877 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006878 switch (task->tk_status) {
6879 case -NFS4ERR_DELAY:
6880 case -NFS4ERR_GRACE:
6881 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6882 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6883 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006884 /* fall through */
6885 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006886 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006887 return;
6888 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006889 dprintk("<-- %s\n", __func__);
6890}
6891
Trond Myklebust17280172012-03-11 13:11:00 -04006892static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006893 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6894 .rpc_call_done = nfs4_get_lease_time_done,
6895};
6896
6897int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6898{
6899 struct rpc_task *task;
6900 struct nfs4_get_lease_time_args args;
6901 struct nfs4_get_lease_time_res res = {
6902 .lr_fsinfo = fsinfo,
6903 };
6904 struct nfs4_get_lease_time_data data = {
6905 .args = &args,
6906 .res = &res,
6907 .clp = clp,
6908 };
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6911 .rpc_argp = &args,
6912 .rpc_resp = &res,
6913 };
6914 struct rpc_task_setup task_setup = {
6915 .rpc_client = clp->cl_rpcclient,
6916 .rpc_message = &msg,
6917 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006918 .callback_data = &data,
6919 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006920 };
6921 int status;
6922
Chuck Levera9c92d62013-08-09 12:48:18 -04006923 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006924 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006925 dprintk("--> %s\n", __func__);
6926 task = rpc_run_task(&task_setup);
6927
6928 if (IS_ERR(task))
6929 status = PTR_ERR(task);
6930 else {
6931 status = task->tk_status;
6932 rpc_put_task(task);
6933 }
6934 dprintk("<-- %s return %d\n", __func__, status);
6935
6936 return status;
6937}
6938
Andy Adamsonfc931582009-04-01 09:22:31 -04006939/*
6940 * Initialize the values to be used by the client in CREATE_SESSION
6941 * If nfs4_init_session set the fore channel request and response sizes,
6942 * use them.
6943 *
6944 * Set the back channel max_resp_sz_cached to zero to force the client to
6945 * always set csa_cachethis to FALSE because the current implementation
6946 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6947 */
6948static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6949{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006950 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006951
Andy Adamson18aad3d2013-06-26 12:21:49 -04006952 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6953 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6954
Andy Adamsonfc931582009-04-01 09:22:31 -04006955 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006956 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6957 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006958 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006959 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006960
6961 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006962 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006963 __func__,
6964 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006965 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006966
6967 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006968 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6969 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6970 args->bc_attrs.max_resp_sz_cached = 0;
6971 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6972 args->bc_attrs.max_reqs = 1;
6973
6974 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6975 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6976 __func__,
6977 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6978 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6979 args->bc_attrs.max_reqs);
6980}
6981
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006982static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006983{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006984 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6985 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6986
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006987 if (rcvd->max_resp_sz > sent->max_resp_sz)
6988 return -EINVAL;
6989 /*
6990 * Our requested max_ops is the minimum we need; we're not
6991 * prepared to break up compounds into smaller pieces than that.
6992 * So, no point even trying to continue if the server won't
6993 * cooperate:
6994 */
6995 if (rcvd->max_ops < sent->max_ops)
6996 return -EINVAL;
6997 if (rcvd->max_reqs == 0)
6998 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006999 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7000 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007001 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007002}
7003
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007004static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
7005{
7006 struct nfs4_channel_attrs *sent = &args->bc_attrs;
7007 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007008
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007009 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7010 return -EINVAL;
7011 if (rcvd->max_resp_sz < sent->max_resp_sz)
7012 return -EINVAL;
7013 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7014 return -EINVAL;
7015 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007016 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007017 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007018 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007019 return -EINVAL;
7020 return 0;
7021}
Andy Adamson8d353012009-04-01 09:22:32 -04007022
Andy Adamson8d353012009-04-01 09:22:32 -04007023static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
7024 struct nfs4_session *session)
7025{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007026 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007027
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007028 ret = nfs4_verify_fore_channel_attrs(args, session);
7029 if (ret)
7030 return ret;
7031 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04007032}
7033
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007034static int _nfs4_proc_create_session(struct nfs_client *clp,
7035 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007036{
7037 struct nfs4_session *session = clp->cl_session;
7038 struct nfs41_create_session_args args = {
7039 .client = clp,
7040 .cb_program = NFS4_CALLBACK,
7041 };
7042 struct nfs41_create_session_res res = {
7043 .client = clp,
7044 };
7045 struct rpc_message msg = {
7046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7047 .rpc_argp = &args,
7048 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007049 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007050 };
7051 int status;
7052
7053 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007054 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007055
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007056 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007057 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007058
Trond Myklebust43095d32012-11-20 11:13:12 -05007059 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007060 /* Verify the session's negotiated channel_attrs values */
7061 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04007062 /* Increment the clientid slot sequence id */
7063 clp->cl_seqid++;
7064 }
7065
7066 return status;
7067}
7068
7069/*
7070 * Issues a CREATE_SESSION operation to the server.
7071 * It is the responsibility of the caller to verify the session is
7072 * expired before calling this routine.
7073 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007074int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007075{
7076 int status;
7077 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007078 struct nfs4_session *session = clp->cl_session;
7079
7080 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7081
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007082 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007083 if (status)
7084 goto out;
7085
Andy Adamsonaacd5532011-11-09 13:58:21 -05007086 /* Init or reset the session slot tables */
7087 status = nfs4_setup_session_slot_tables(session);
7088 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007089 if (status)
7090 goto out;
7091
7092 ptr = (unsigned *)&session->sess_id.data[0];
7093 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7094 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007095out:
7096 dprintk("<-- %s\n", __func__);
7097 return status;
7098}
7099
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007100/*
7101 * Issue the over-the-wire RPC DESTROY_SESSION.
7102 * The caller must serialize access to this routine.
7103 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007104int nfs4_proc_destroy_session(struct nfs4_session *session,
7105 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007106{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007107 struct rpc_message msg = {
7108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7109 .rpc_argp = session,
7110 .rpc_cred = cred,
7111 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007112 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007113
7114 dprintk("--> nfs4_proc_destroy_session\n");
7115
7116 /* session is still being setup */
7117 if (session->clp->cl_cons_state != NFS_CS_READY)
7118 return status;
7119
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007120 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007121 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007122
7123 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007124 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007125 "Session has been destroyed regardless...\n", status);
7126
7127 dprintk("<-- nfs4_proc_destroy_session\n");
7128 return status;
7129}
7130
Trond Myklebust7b38c362012-05-23 13:23:31 -04007131/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007132 * Renew the cl_session lease.
7133 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007134struct nfs4_sequence_data {
7135 struct nfs_client *clp;
7136 struct nfs4_sequence_args args;
7137 struct nfs4_sequence_res res;
7138};
7139
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007140static void nfs41_sequence_release(void *data)
7141{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007142 struct nfs4_sequence_data *calldata = data;
7143 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007144
Alexandros Batsakis71358402010-02-05 03:45:05 -08007145 if (atomic_read(&clp->cl_count) > 1)
7146 nfs4_schedule_state_renewal(clp);
7147 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007148 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007149}
7150
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007151static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7152{
7153 switch(task->tk_status) {
7154 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007155 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7156 return -EAGAIN;
7157 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007158 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007159 }
7160 return 0;
7161}
7162
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007163static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007164{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007165 struct nfs4_sequence_data *calldata = data;
7166 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007167
Trond Myklebust14516c32010-07-31 14:29:06 -04007168 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7169 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007170
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007171 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007172 if (task->tk_status < 0) {
7173 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007174 if (atomic_read(&clp->cl_count) == 1)
7175 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007176
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007177 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7178 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007179 return;
7180 }
7181 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007182 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007183out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007184 dprintk("<-- %s\n", __func__);
7185}
7186
7187static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7188{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007189 struct nfs4_sequence_data *calldata = data;
7190 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007191 struct nfs4_sequence_args *args;
7192 struct nfs4_sequence_res *res;
7193
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007194 args = task->tk_msg.rpc_argp;
7195 res = task->tk_msg.rpc_resp;
7196
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007197 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007198}
7199
7200static const struct rpc_call_ops nfs41_sequence_ops = {
7201 .rpc_call_done = nfs41_sequence_call_done,
7202 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007203 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007204};
7205
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007206static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7207 struct rpc_cred *cred,
7208 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007209{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007210 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7213 .rpc_cred = cred,
7214 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007215 struct rpc_task_setup task_setup_data = {
7216 .rpc_client = clp->cl_rpcclient,
7217 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007218 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007219 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007220 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007221
Alexandros Batsakis71358402010-02-05 03:45:05 -08007222 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007223 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007224 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007225 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007226 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007227 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007228 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007229 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007230 if (is_privileged)
7231 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007232 msg.rpc_argp = &calldata->args;
7233 msg.rpc_resp = &calldata->res;
7234 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007235 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007236
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007237 return rpc_run_task(&task_setup_data);
7238}
7239
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007240static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007241{
7242 struct rpc_task *task;
7243 int ret = 0;
7244
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007245 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
7246 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007247 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007248 if (IS_ERR(task))
7249 ret = PTR_ERR(task);
7250 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007251 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007252 dprintk("<-- %s status=%d\n", __func__, ret);
7253 return ret;
7254}
7255
7256static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7257{
7258 struct rpc_task *task;
7259 int ret;
7260
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007261 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007262 if (IS_ERR(task)) {
7263 ret = PTR_ERR(task);
7264 goto out;
7265 }
7266 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05007267 if (!ret) {
7268 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
7269
7270 if (task->tk_status == 0)
7271 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007272 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05007273 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007274 rpc_put_task(task);
7275out:
7276 dprintk("<-- %s status=%d\n", __func__, ret);
7277 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007278}
7279
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007280struct nfs4_reclaim_complete_data {
7281 struct nfs_client *clp;
7282 struct nfs41_reclaim_complete_args arg;
7283 struct nfs41_reclaim_complete_res res;
7284};
7285
7286static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7287{
7288 struct nfs4_reclaim_complete_data *calldata = data;
7289
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007290 nfs41_setup_sequence(calldata->clp->cl_session,
7291 &calldata->arg.seq_args,
7292 &calldata->res.seq_res,
7293 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007294}
7295
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007296static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7297{
7298 switch(task->tk_status) {
7299 case 0:
7300 case -NFS4ERR_COMPLETE_ALREADY:
7301 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7302 break;
7303 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007304 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007305 /* fall through */
7306 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007307 return -EAGAIN;
7308 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007309 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007310 }
7311 return 0;
7312}
7313
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007314static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7315{
7316 struct nfs4_reclaim_complete_data *calldata = data;
7317 struct nfs_client *clp = calldata->clp;
7318 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7319
7320 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007321 if (!nfs41_sequence_done(task, res))
7322 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007323
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007324 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007325 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7326 rpc_restart_call_prepare(task);
7327 return;
7328 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007329 dprintk("<-- %s\n", __func__);
7330}
7331
7332static void nfs4_free_reclaim_complete_data(void *data)
7333{
7334 struct nfs4_reclaim_complete_data *calldata = data;
7335
7336 kfree(calldata);
7337}
7338
7339static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7340 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7341 .rpc_call_done = nfs4_reclaim_complete_done,
7342 .rpc_release = nfs4_free_reclaim_complete_data,
7343};
7344
7345/*
7346 * Issue a global reclaim complete.
7347 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007348static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7349 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007350{
7351 struct nfs4_reclaim_complete_data *calldata;
7352 struct rpc_task *task;
7353 struct rpc_message msg = {
7354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007355 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007356 };
7357 struct rpc_task_setup task_setup_data = {
7358 .rpc_client = clp->cl_rpcclient,
7359 .rpc_message = &msg,
7360 .callback_ops = &nfs4_reclaim_complete_call_ops,
7361 .flags = RPC_TASK_ASYNC,
7362 };
7363 int status = -ENOMEM;
7364
7365 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007366 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007367 if (calldata == NULL)
7368 goto out;
7369 calldata->clp = clp;
7370 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007371
Chuck Levera9c92d62013-08-09 12:48:18 -04007372 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007373 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007374 msg.rpc_argp = &calldata->arg;
7375 msg.rpc_resp = &calldata->res;
7376 task_setup_data.callback_data = calldata;
7377 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007378 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007379 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007380 goto out;
7381 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007382 status = nfs4_wait_for_completion_rpc_task(task);
7383 if (status == 0)
7384 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007385 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007386 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007387out:
7388 dprintk("<-- %s status=%d\n", __func__, status);
7389 return status;
7390}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007391
7392static void
7393nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7394{
7395 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007396 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007397 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007398
7399 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007400 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7401 * right now covering the LAYOUTGET we are about to send.
7402 * However, that is not so catastrophic, and there seems
7403 * to be no way to prevent it completely.
7404 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007405 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007406 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007407 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00007408 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
7409 NFS_I(lgp->args.inode)->layout,
7410 lgp->args.ctx->state)) {
7411 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00007412 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007413}
7414
7415static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7416{
7417 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007418 struct inode *inode = lgp->args.inode;
7419 struct nfs_server *server = NFS_SERVER(inode);
7420 struct pnfs_layout_hdr *lo;
7421 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007422 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007423
Boaz Harroshed7e5422014-01-22 20:34:54 +02007424 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007425
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007426 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007427 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007428
7429 switch (task->tk_status) {
7430 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007431 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007432 /*
7433 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
7434 * (or clients) writing to the same RAID stripe
7435 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007436 case -NFS4ERR_LAYOUTTRYLATER:
Boaz Harroshed7e5422014-01-22 20:34:54 +02007437 /*
7438 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7439 * existing layout before getting a new one).
7440 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007441 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007442 timeo = rpc_get_timeout(task->tk_client);
7443 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007444 now = jiffies;
7445 if (time_after(giveup, now)) {
7446 unsigned long delay;
7447
7448 /* Delay for:
7449 * - Not less then NFS4_POLL_RETRY_MIN.
7450 * - One last time a jiffie before we give up
7451 * - exponential backoff (time_now minus start_attempt)
7452 */
7453 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7454 min((giveup - now - 1),
7455 now - lgp->args.timestamp));
7456
7457 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7458 __func__, delay);
7459 rpc_delay(task, delay);
7460 task->tk_status = 0;
7461 rpc_restart_call_prepare(task);
7462 goto out; /* Do not call nfs4_async_handle_error() */
7463 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007464 break;
7465 case -NFS4ERR_EXPIRED:
7466 case -NFS4ERR_BAD_STATEID:
7467 spin_lock(&inode->i_lock);
7468 lo = NFS_I(inode)->layout;
7469 if (!lo || list_empty(&lo->plh_segs)) {
7470 spin_unlock(&inode->i_lock);
7471 /* If the open stateid was bad, then recover it. */
7472 state = lgp->args.ctx->state;
7473 } else {
7474 LIST_HEAD(head);
7475
7476 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7477 spin_unlock(&inode->i_lock);
7478 /* Mark the bad layout state as invalid, then
7479 * retry using the open stateid. */
7480 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007481 }
7482 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007483 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
7484 rpc_restart_call_prepare(task);
7485out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007486 dprintk("<-- %s\n", __func__);
7487}
7488
Idan Kedar85541162012-08-02 11:47:10 +03007489static size_t max_response_pages(struct nfs_server *server)
7490{
7491 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7492 return nfs_page_array_len(0, max_resp_sz);
7493}
7494
7495static void nfs4_free_pages(struct page **pages, size_t size)
7496{
7497 int i;
7498
7499 if (!pages)
7500 return;
7501
7502 for (i = 0; i < size; i++) {
7503 if (!pages[i])
7504 break;
7505 __free_page(pages[i]);
7506 }
7507 kfree(pages);
7508}
7509
7510static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7511{
7512 struct page **pages;
7513 int i;
7514
7515 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7516 if (!pages) {
7517 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7518 return NULL;
7519 }
7520
7521 for (i = 0; i < size; i++) {
7522 pages[i] = alloc_page(gfp_flags);
7523 if (!pages[i]) {
7524 dprintk("%s: failed to allocate page\n", __func__);
7525 nfs4_free_pages(pages, size);
7526 return NULL;
7527 }
7528 }
7529
7530 return pages;
7531}
7532
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007533static void nfs4_layoutget_release(void *calldata)
7534{
7535 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007536 struct inode *inode = lgp->args.inode;
7537 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007538 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007539
7540 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03007541 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007542 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007543 put_nfs_open_context(lgp->args.ctx);
7544 kfree(calldata);
7545 dprintk("<-- %s\n", __func__);
7546}
7547
7548static const struct rpc_call_ops nfs4_layoutget_call_ops = {
7549 .rpc_call_prepare = nfs4_layoutget_prepare,
7550 .rpc_call_done = nfs4_layoutget_done,
7551 .rpc_release = nfs4_layoutget_release,
7552};
7553
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007554struct pnfs_layout_segment *
7555nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007556{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007557 struct inode *inode = lgp->args.inode;
7558 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03007559 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007560 struct rpc_task *task;
7561 struct rpc_message msg = {
7562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
7563 .rpc_argp = &lgp->args,
7564 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04007565 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007566 };
7567 struct rpc_task_setup task_setup_data = {
7568 .rpc_client = server->client,
7569 .rpc_message = &msg,
7570 .callback_ops = &nfs4_layoutget_call_ops,
7571 .callback_data = lgp,
7572 .flags = RPC_TASK_ASYNC,
7573 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007574 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007575 int status = 0;
7576
7577 dprintk("--> %s\n", __func__);
7578
Idan Kedar85541162012-08-02 11:47:10 +03007579 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
7580 if (!lgp->args.layout.pages) {
7581 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007582 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03007583 }
7584 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05007585 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03007586
Weston Andros Adamson35124a02011-03-24 16:48:21 -04007587 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007588 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04007589 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05007590
7591 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
7592 pnfs_get_layout_hdr(NFS_I(inode)->layout);
7593
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007594 task = rpc_run_task(&task_setup_data);
7595 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007596 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007597 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00007598 if (status == 0)
7599 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007600 trace_nfs4_layoutget(lgp->args.ctx,
7601 &lgp->args.range,
7602 &lgp->res.range,
7603 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05007604 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
7605 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007606 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007607 rpc_put_task(task);
7608 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04007609 if (status)
7610 return ERR_PTR(status);
7611 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007612}
7613
Benny Halevycbe82602011-05-22 19:52:37 +03007614static void
7615nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
7616{
7617 struct nfs4_layoutreturn *lrp = calldata;
7618
7619 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007620 nfs41_setup_sequence(lrp->clp->cl_session,
7621 &lrp->args.seq_args,
7622 &lrp->res.seq_res,
7623 task);
Benny Halevycbe82602011-05-22 19:52:37 +03007624}
7625
7626static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
7627{
7628 struct nfs4_layoutreturn *lrp = calldata;
7629 struct nfs_server *server;
7630
7631 dprintk("--> %s\n", __func__);
7632
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007633 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03007634 return;
7635
7636 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05007637 switch (task->tk_status) {
7638 default:
7639 task->tk_status = 0;
7640 case 0:
7641 break;
7642 case -NFS4ERR_DELAY:
7643 if (nfs4_async_handle_error(task, server, NULL) != -EAGAIN)
7644 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007645 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03007646 return;
7647 }
Benny Halevycbe82602011-05-22 19:52:37 +03007648 dprintk("<-- %s\n", __func__);
7649}
7650
7651static void nfs4_layoutreturn_release(void *calldata)
7652{
7653 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04007654 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03007655
7656 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04007657 spin_lock(&lo->plh_inode->i_lock);
7658 if (lrp->res.lrs_present)
7659 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
7660 lo->plh_block_lgets--;
7661 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04007662 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03007663 kfree(calldata);
7664 dprintk("<-- %s\n", __func__);
7665}
7666
7667static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
7668 .rpc_call_prepare = nfs4_layoutreturn_prepare,
7669 .rpc_call_done = nfs4_layoutreturn_done,
7670 .rpc_release = nfs4_layoutreturn_release,
7671};
7672
7673int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
7674{
7675 struct rpc_task *task;
7676 struct rpc_message msg = {
7677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
7678 .rpc_argp = &lrp->args,
7679 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04007680 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03007681 };
7682 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04007683 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03007684 .rpc_message = &msg,
7685 .callback_ops = &nfs4_layoutreturn_call_ops,
7686 .callback_data = lrp,
7687 };
7688 int status;
7689
7690 dprintk("--> %s\n", __func__);
Chuck Levera9c92d62013-08-09 12:48:18 -04007691 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03007692 task = rpc_run_task(&task_setup_data);
7693 if (IS_ERR(task))
7694 return PTR_ERR(task);
7695 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007696 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03007697 dprintk("<-- %s status=%d\n", __func__, status);
7698 rpc_put_task(task);
7699 return status;
7700}
7701
Andy Adamson7f11d8d2011-07-30 20:52:35 -04007702/*
7703 * Retrieve the list of Data Server devices from the MDS.
7704 */
7705static int _nfs4_getdevicelist(struct nfs_server *server,
7706 const struct nfs_fh *fh,
7707 struct pnfs_devicelist *devlist)
7708{
7709 struct nfs4_getdevicelist_args args = {
7710 .fh = fh,
7711 .layoutclass = server->pnfs_curr_ld->id,
7712 };
7713 struct nfs4_getdevicelist_res res = {
7714 .devlist = devlist,
7715 };
7716 struct rpc_message msg = {
7717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
7718 .rpc_argp = &args,
7719 .rpc_resp = &res,
7720 };
7721 int status;
7722
7723 dprintk("--> %s\n", __func__);
7724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
7725 &res.seq_res, 0);
7726 dprintk("<-- %s status=%d\n", __func__, status);
7727 return status;
7728}
7729
7730int nfs4_proc_getdevicelist(struct nfs_server *server,
7731 const struct nfs_fh *fh,
7732 struct pnfs_devicelist *devlist)
7733{
7734 struct nfs4_exception exception = { };
7735 int err;
7736
7737 do {
7738 err = nfs4_handle_exception(server,
7739 _nfs4_getdevicelist(server, fh, devlist),
7740 &exception);
7741 } while (exception.retry);
7742
7743 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7744 err, devlist->num_devs);
7745
7746 return err;
7747}
7748EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7749
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007750static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007751_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7752 struct pnfs_device *pdev,
7753 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007754{
7755 struct nfs4_getdeviceinfo_args args = {
7756 .pdev = pdev,
7757 };
7758 struct nfs4_getdeviceinfo_res res = {
7759 .pdev = pdev,
7760 };
7761 struct rpc_message msg = {
7762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7763 .rpc_argp = &args,
7764 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007765 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007766 };
7767 int status;
7768
7769 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007770 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007771 dprintk("<-- %s status=%d\n", __func__, status);
7772
7773 return status;
7774}
7775
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007776int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7777 struct pnfs_device *pdev,
7778 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007779{
7780 struct nfs4_exception exception = { };
7781 int err;
7782
7783 do {
7784 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007785 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007786 &exception);
7787 } while (exception.retry);
7788 return err;
7789}
7790EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7791
Andy Adamson863a3c62011-03-23 13:27:54 +00007792static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7793{
7794 struct nfs4_layoutcommit_data *data = calldata;
7795 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007796 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007797
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007798 nfs41_setup_sequence(session,
7799 &data->args.seq_args,
7800 &data->res.seq_res,
7801 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007802}
7803
7804static void
7805nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7806{
7807 struct nfs4_layoutcommit_data *data = calldata;
7808 struct nfs_server *server = NFS_SERVER(data->args.inode);
7809
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007810 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007811 return;
7812
7813 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007814 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7815 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7816 case -NFS4ERR_BADLAYOUT: /* no layout */
7817 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007818 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007819 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007820 break;
7821 default:
7822 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7823 rpc_restart_call_prepare(task);
7824 return;
7825 }
7826 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007827}
7828
7829static void nfs4_layoutcommit_release(void *calldata)
7830{
7831 struct nfs4_layoutcommit_data *data = calldata;
7832
Andy Adamsondb29c082011-07-30 20:52:38 -04007833 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05007834 nfs_post_op_update_inode_force_wcc(data->args.inode,
7835 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00007836 put_rpccred(data->cred);
7837 kfree(data);
7838}
7839
7840static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7841 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7842 .rpc_call_done = nfs4_layoutcommit_done,
7843 .rpc_release = nfs4_layoutcommit_release,
7844};
7845
7846int
Andy Adamsonef311532011-03-12 02:58:10 -05007847nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007848{
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7851 .rpc_argp = &data->args,
7852 .rpc_resp = &data->res,
7853 .rpc_cred = data->cred,
7854 };
7855 struct rpc_task_setup task_setup_data = {
7856 .task = &data->task,
7857 .rpc_client = NFS_CLIENT(data->args.inode),
7858 .rpc_message = &msg,
7859 .callback_ops = &nfs4_layoutcommit_ops,
7860 .callback_data = data,
7861 .flags = RPC_TASK_ASYNC,
7862 };
7863 struct rpc_task *task;
7864 int status = 0;
7865
7866 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7867 "lbw: %llu inode %lu\n",
7868 data->task.tk_pid, sync,
7869 data->args.lastbytewritten,
7870 data->args.inode->i_ino);
7871
Chuck Levera9c92d62013-08-09 12:48:18 -04007872 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007873 task = rpc_run_task(&task_setup_data);
7874 if (IS_ERR(task))
7875 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007876 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007877 goto out;
7878 status = nfs4_wait_for_completion_rpc_task(task);
7879 if (status != 0)
7880 goto out;
7881 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007882 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007883out:
7884 dprintk("%s: status %d\n", __func__, status);
7885 rpc_put_task(task);
7886 return status;
7887}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007888
Andy Adamson97431202013-08-08 10:57:56 -04007889/**
7890 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7891 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7892 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007893static int
7894_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007895 struct nfs_fsinfo *info,
7896 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007897{
7898 struct nfs41_secinfo_no_name_args args = {
7899 .style = SECINFO_STYLE_CURRENT_FH,
7900 };
7901 struct nfs4_secinfo_res res = {
7902 .flavors = flavors,
7903 };
7904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7906 .rpc_argp = &args,
7907 .rpc_resp = &res,
7908 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007909 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007910 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007911 int status;
7912
7913 if (use_integrity) {
7914 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007915 cred = nfs4_get_clid_cred(server->nfs_client);
7916 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007917 }
7918
7919 dprintk("--> %s\n", __func__);
7920 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
7921 &res.seq_res, 0);
7922 dprintk("<-- %s status=%d\n", __func__, status);
7923
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007924 if (cred)
7925 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007926
7927 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007928}
7929
7930static int
7931nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7932 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7933{
7934 struct nfs4_exception exception = { };
7935 int err;
7936 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04007937 /* first try using integrity protection */
7938 err = -NFS4ERR_WRONGSEC;
7939
7940 /* try to use integrity protection with machine cred */
7941 if (_nfs4_is_integrity_protected(server->nfs_client))
7942 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7943 flavors, true);
7944
7945 /*
7946 * if unable to use integrity protection, or SECINFO with
7947 * integrity protection returns NFS4ERR_WRONGSEC (which is
7948 * disallowed by spec, but exists in deployed servers) use
7949 * the current filesystem's rpc_client and the user cred.
7950 */
7951 if (err == -NFS4ERR_WRONGSEC)
7952 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
7953 flavors, false);
7954
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007955 switch (err) {
7956 case 0:
7957 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007958 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007959 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007960 default:
7961 err = nfs4_handle_exception(server, err, &exception);
7962 }
7963 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007964out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007965 return err;
7966}
7967
7968static int
7969nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7970 struct nfs_fsinfo *info)
7971{
7972 int err;
7973 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04007974 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007975 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007976 struct nfs4_secinfo4 *secinfo;
7977 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007978
7979 page = alloc_page(GFP_KERNEL);
7980 if (!page) {
7981 err = -ENOMEM;
7982 goto out;
7983 }
7984
7985 flavors = page_address(page);
7986 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7987
7988 /*
7989 * Fall back on "guess and check" method if
7990 * the server doesn't support SECINFO_NO_NAME
7991 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05007992 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007993 err = nfs4_find_root_sec(server, fhandle, info);
7994 goto out_freepage;
7995 }
7996 if (err)
7997 goto out_freepage;
7998
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04007999 for (i = 0; i < flavors->num_flavors; i++) {
8000 secinfo = &flavors->flavors[i];
8001
8002 switch (secinfo->flavor) {
8003 case RPC_AUTH_NULL:
8004 case RPC_AUTH_UNIX:
8005 case RPC_AUTH_GSS:
8006 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8007 &secinfo->flavor_info);
8008 break;
8009 default:
8010 flavor = RPC_AUTH_MAXFLAVOR;
8011 break;
8012 }
8013
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008014 if (!nfs_auth_info_match(&server->auth_info, flavor))
8015 flavor = RPC_AUTH_MAXFLAVOR;
8016
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008017 if (flavor != RPC_AUTH_MAXFLAVOR) {
8018 err = nfs4_lookup_root_sec(server, fhandle,
8019 info, flavor);
8020 if (!err)
8021 break;
8022 }
8023 }
8024
8025 if (flavor == RPC_AUTH_MAXFLAVOR)
8026 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008027
8028out_freepage:
8029 put_page(page);
8030 if (err == -EACCES)
8031 return -EPERM;
8032out:
8033 return err;
8034}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008035
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008036static int _nfs41_test_stateid(struct nfs_server *server,
8037 nfs4_stateid *stateid,
8038 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008039{
8040 int status;
8041 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008042 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008043 };
8044 struct nfs41_test_stateid_res res;
8045 struct rpc_message msg = {
8046 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8047 .rpc_argp = &args,
8048 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008049 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008050 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008051 struct rpc_clnt *rpc_client = server->client;
8052
8053 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8054 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008055
Chuck Lever38527b12012-07-11 16:30:23 -04008056 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008057 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008058 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008059 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008060 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008061 if (status != NFS_OK) {
8062 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008063 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008064 }
8065 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008066 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008067}
8068
Chuck Lever38527b12012-07-11 16:30:23 -04008069/**
8070 * nfs41_test_stateid - perform a TEST_STATEID operation
8071 *
8072 * @server: server / transport on which to perform the operation
8073 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008074 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008075 *
8076 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8077 * Otherwise a negative NFS4ERR value is returned if the operation
8078 * failed or the state ID is not currently valid.
8079 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008080static int nfs41_test_stateid(struct nfs_server *server,
8081 nfs4_stateid *stateid,
8082 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008083{
8084 struct nfs4_exception exception = { };
8085 int err;
8086 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008087 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008088 if (err != -NFS4ERR_DELAY)
8089 break;
8090 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008091 } while (exception.retry);
8092 return err;
8093}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008094
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008095struct nfs_free_stateid_data {
8096 struct nfs_server *server;
8097 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008098 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008099};
8100
8101static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8102{
8103 struct nfs_free_stateid_data *data = calldata;
8104 nfs41_setup_sequence(nfs4_get_session(data->server),
8105 &data->args.seq_args,
8106 &data->res.seq_res,
8107 task);
8108}
8109
8110static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8111{
8112 struct nfs_free_stateid_data *data = calldata;
8113
8114 nfs41_sequence_done(task, &data->res.seq_res);
8115
8116 switch (task->tk_status) {
8117 case -NFS4ERR_DELAY:
8118 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
8119 rpc_restart_call_prepare(task);
8120 }
8121}
8122
8123static void nfs41_free_stateid_release(void *calldata)
8124{
8125 kfree(calldata);
8126}
8127
Trond Myklebust17f26b12013-08-21 15:48:42 -04008128static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008129 .rpc_call_prepare = nfs41_free_stateid_prepare,
8130 .rpc_call_done = nfs41_free_stateid_done,
8131 .rpc_release = nfs41_free_stateid_release,
8132};
8133
8134static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8135 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008136 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008137 bool privileged)
8138{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008139 struct rpc_message msg = {
8140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008141 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008142 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008143 struct rpc_task_setup task_setup = {
8144 .rpc_client = server->client,
8145 .rpc_message = &msg,
8146 .callback_ops = &nfs41_free_stateid_ops,
8147 .flags = RPC_TASK_ASYNC,
8148 };
8149 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008150
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008151 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8152 &task_setup.rpc_client, &msg);
8153
Chuck Lever38527b12012-07-11 16:30:23 -04008154 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008155 data = kmalloc(sizeof(*data), GFP_NOFS);
8156 if (!data)
8157 return ERR_PTR(-ENOMEM);
8158 data->server = server;
8159 nfs4_stateid_copy(&data->args.stateid, stateid);
8160
8161 task_setup.callback_data = data;
8162
8163 msg.rpc_argp = &data->args;
8164 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008165 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008166 if (privileged)
8167 nfs4_set_sequence_privileged(&data->args.seq_args);
8168
8169 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008170}
8171
Chuck Lever38527b12012-07-11 16:30:23 -04008172/**
8173 * nfs41_free_stateid - perform a FREE_STATEID operation
8174 *
8175 * @server: server / transport on which to perform the operation
8176 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008177 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008178 *
8179 * Returns NFS_OK if the server freed "stateid". Otherwise a
8180 * negative NFS4ERR value is returned.
8181 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008182static int nfs41_free_stateid(struct nfs_server *server,
8183 nfs4_stateid *stateid,
8184 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008185{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008186 struct rpc_task *task;
8187 int ret;
8188
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008189 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008190 if (IS_ERR(task))
8191 return PTR_ERR(task);
8192 ret = rpc_wait_for_completion_task(task);
8193 if (!ret)
8194 ret = task->tk_status;
8195 rpc_put_task(task);
8196 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008197}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008198
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008199static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
8200{
8201 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008202 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008203
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008204 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008205 nfs4_free_lock_state(server, lsp);
8206 if (IS_ERR(task))
8207 return PTR_ERR(task);
8208 rpc_put_task(task);
8209 return 0;
8210}
8211
Trond Myklebust36281ca2012-03-04 18:13:56 -05008212static bool nfs41_match_stateid(const nfs4_stateid *s1,
8213 const nfs4_stateid *s2)
8214{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008215 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008216 return false;
8217
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008218 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008219 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008220 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008221 return true;
8222
8223 return false;
8224}
8225
Andy Adamson557134a2009-04-01 09:21:53 -04008226#endif /* CONFIG_NFS_V4_1 */
8227
Trond Myklebust36281ca2012-03-04 18:13:56 -05008228static bool nfs4_match_stateid(const nfs4_stateid *s1,
8229 const nfs4_stateid *s2)
8230{
Trond Myklebustf597c532012-03-04 18:13:56 -05008231 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008232}
8233
8234
Trond Myklebust17280172012-03-11 13:11:00 -04008235static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008236 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008237 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 .recover_open = nfs4_open_reclaim,
8239 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008240 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008241 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008242};
8243
Andy Adamson591d71c2009-04-01 09:22:47 -04008244#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008245static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008246 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8247 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8248 .recover_open = nfs4_open_reclaim,
8249 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008250 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008251 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008252 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008253};
8254#endif /* CONFIG_NFS_V4_1 */
8255
Trond Myklebust17280172012-03-11 13:11:00 -04008256static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008257 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008258 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008259 .recover_open = nfs4_open_expired,
8260 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008261 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008262};
8263
Andy Adamson591d71c2009-04-01 09:22:47 -04008264#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008265static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008266 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8267 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008268 .recover_open = nfs41_open_expired,
8269 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008270 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008271};
8272#endif /* CONFIG_NFS_V4_1 */
8273
Trond Myklebust17280172012-03-11 13:11:00 -04008274static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008275 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008276 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008277 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008278};
8279
8280#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008281static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008282 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008283 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008284 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008285};
8286#endif
8287
Chuck Leverec011fe2013-10-17 14:12:39 -04008288static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008289 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008290 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008291};
8292
8293#if defined(CONFIG_NFS_V4_1)
8294static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008295 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008296 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008297};
8298#endif /* CONFIG_NFS_V4_1 */
8299
Trond Myklebust97dc1352010-06-16 09:52:26 -04008300static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8301 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008302 .init_caps = NFS_CAP_READDIRPLUS
8303 | NFS_CAP_ATOMIC_OPEN
8304 | NFS_CAP_CHANGE_ATTR
8305 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008306 .init_client = nfs40_init_client,
8307 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008308 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008309 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008310 .free_lock_state = nfs4_release_lockowner,
Chuck Lever9915ea72013-08-09 12:48:27 -04008311 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008312 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8313 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8314 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008315 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008316};
8317
8318#if defined(CONFIG_NFS_V4_1)
8319static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8320 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008321 .init_caps = NFS_CAP_READDIRPLUS
8322 | NFS_CAP_ATOMIC_OPEN
8323 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04008324 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008325 | NFS_CAP_STATEID_NFSV41
8326 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008327 .init_client = nfs41_init_client,
8328 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008329 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008330 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008331 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008332 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008333 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8334 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8335 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008336 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008337};
8338#endif
8339
Steve Dickson42c2c422013-05-22 12:50:38 -04008340#if defined(CONFIG_NFS_V4_2)
8341static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8342 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008343 .init_caps = NFS_CAP_READDIRPLUS
8344 | NFS_CAP_ATOMIC_OPEN
8345 | NFS_CAP_CHANGE_ATTR
8346 | NFS_CAP_POSIX_LOCK
8347 | NFS_CAP_STATEID_NFSV41
8348 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008349 .init_client = nfs41_init_client,
8350 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008351 .match_stateid = nfs41_match_stateid,
8352 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008353 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008354 .call_sync_ops = &nfs41_call_sync_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008355 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8356 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8357 .state_renewal_ops = &nfs41_state_renewal_ops,
8358};
8359#endif
8360
Trond Myklebust97dc1352010-06-16 09:52:26 -04008361const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8362 [0] = &nfs_v4_0_minor_ops,
8363#if defined(CONFIG_NFS_V4_1)
8364 [1] = &nfs_v4_1_minor_ops,
8365#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008366#if defined(CONFIG_NFS_V4_2)
8367 [2] = &nfs_v4_2_minor_ops,
8368#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008369};
8370
Trond Myklebust17f26b12013-08-21 15:48:42 -04008371static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008372 .create = nfs_create,
8373 .lookup = nfs_lookup,
8374 .atomic_open = nfs_atomic_open,
8375 .link = nfs_link,
8376 .unlink = nfs_unlink,
8377 .symlink = nfs_symlink,
8378 .mkdir = nfs_mkdir,
8379 .rmdir = nfs_rmdir,
8380 .mknod = nfs_mknod,
8381 .rename = nfs_rename,
8382 .permission = nfs_permission,
8383 .getattr = nfs_getattr,
8384 .setattr = nfs_setattr,
8385 .getxattr = generic_getxattr,
8386 .setxattr = generic_setxattr,
8387 .listxattr = generic_listxattr,
8388 .removexattr = generic_removexattr,
8389};
8390
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008391static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008392 .permission = nfs_permission,
8393 .getattr = nfs_getattr,
8394 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008395 .getxattr = generic_getxattr,
8396 .setxattr = generic_setxattr,
8397 .listxattr = generic_listxattr,
8398 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008399};
8400
David Howells509de812006-08-22 20:06:11 -04008401const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008402 .version = 4, /* protocol version */
8403 .dentry_ops = &nfs4_dentry_operations,
8404 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008405 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008406 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008407 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008408 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008409 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008410 .getattr = nfs4_proc_getattr,
8411 .setattr = nfs4_proc_setattr,
8412 .lookup = nfs4_proc_lookup,
8413 .access = nfs4_proc_access,
8414 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008415 .create = nfs4_proc_create,
8416 .remove = nfs4_proc_remove,
8417 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008418 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008419 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008420 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008421 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008422 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008423 .link = nfs4_proc_link,
8424 .symlink = nfs4_proc_symlink,
8425 .mkdir = nfs4_proc_mkdir,
8426 .rmdir = nfs4_proc_remove,
8427 .readdir = nfs4_proc_readdir,
8428 .mknod = nfs4_proc_mknod,
8429 .statfs = nfs4_proc_statfs,
8430 .fsinfo = nfs4_proc_fsinfo,
8431 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008432 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008433 .decode_dirent = nfs4_decode_dirent,
8434 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04008435 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04008436 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05008437 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008438 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04008439 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04008440 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008441 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008442 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008443 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008444 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008445 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008446 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008447 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008448 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008449 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008450 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008451 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008452 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04008453 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008454 .create_server = nfs4_create_server,
8455 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008456};
8457
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008458static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
8459 .prefix = XATTR_NAME_NFSV4_ACL,
8460 .list = nfs4_xattr_list_nfs4_acl,
8461 .get = nfs4_xattr_get_nfs4_acl,
8462 .set = nfs4_xattr_set_nfs4_acl,
8463};
8464
8465const struct xattr_handler *nfs4_xattr_handlers[] = {
8466 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008467#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8468 &nfs4_xattr_nfs4_label_handler,
8469#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008470 NULL
8471};
8472
Linus Torvalds1da177e2005-04-16 15:20:36 -07008473/*
8474 * Local variables:
8475 * c-basic-offset: 8
8476 * End:
8477 */