blob: 872ff6756723b9e1a54b7e55737cc276542b8197 [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>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.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"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static 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 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400258 FATTR4_WORD0_TYPE
259 | FATTR4_WORD0_CHANGE
260 | FATTR4_WORD0_SIZE
261 | FATTR4_WORD0_FSID
262 | FATTR4_WORD0_FILEID
263 | FATTR4_WORD0_FS_LOCATIONS,
264 FATTR4_WORD1_MODE
265 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Al Viro0dbb4c62006-10-19 23:28:49 -0700279 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000282 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
284 return;
285 }
286
287 readdir->cookie = 0;
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
289 if (cookie == 2)
290 return;
291
292 /*
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
297 * instead of 1 or 2.
298 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800299 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (cookie == 0) {
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000311 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 *p++ = xdr_one; /* next */
315 *p++ = xdr_zero; /* cookie, first word */
316 *p++ = xdr_two; /* cookie, second word */
317 *p++ = xdr_two; /* entry len */
318 memcpy(p, "..\0\0", 4); /* entry */
319 p++;
320 *p++ = xdr_one; /* bitmap length */
321 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
322 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000323 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 readdir->pgbase = (char *)p - (char *)start;
326 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800327 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Trond Myklebust26d36302016-09-22 13:39:05 -0400330static void nfs4_test_and_free_stateid(struct nfs_server *server,
331 nfs4_stateid *stateid,
332 struct rpc_cred *cred)
333{
334 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
335
336 ops->test_and_free_expired(server, stateid, cred);
337}
338
339static void __nfs4_free_revoked_stateid(struct nfs_server *server,
340 nfs4_stateid *stateid,
341 struct rpc_cred *cred)
342{
343 stateid->type = NFS4_REVOKED_STATEID_TYPE;
344 nfs4_test_and_free_stateid(server, stateid, cred);
345}
346
347static void nfs4_free_revoked_stateid(struct nfs_server *server,
348 const nfs4_stateid *stateid,
349 struct rpc_cred *cred)
350{
351 nfs4_stateid tmp;
352
353 nfs4_stateid_copy(&tmp, stateid);
354 __nfs4_free_revoked_stateid(server, &tmp, cred);
355}
356
NeilBrown8478eaa2014-09-18 16:09:27 +1000357static long nfs4_update_delay(long *timeout)
358{
359 long ret;
360 if (!timeout)
361 return NFS4_POLL_RETRY_MAX;
362 if (*timeout <= 0)
363 *timeout = NFS4_POLL_RETRY_MIN;
364 if (*timeout > NFS4_POLL_RETRY_MAX)
365 *timeout = NFS4_POLL_RETRY_MAX;
366 ret = *timeout;
367 *timeout <<= 1;
368 return ret;
369}
370
Trond Myklebust65de8722008-12-23 15:21:44 -0500371static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
372{
373 int res = 0;
374
375 might_sleep();
376
NeilBrown8478eaa2014-09-18 16:09:27 +1000377 freezable_schedule_timeout_killable_unsafe(
378 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 if (fatal_signal_pending(current))
380 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381 return res;
382}
383
384/* This is the error handling routine for processes that are allowed
385 * to sleep.
386 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400387static int nfs4_do_handle_exception(struct nfs_server *server,
388 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500389{
390 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500391 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400392 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500393 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500394 int ret = errorcode;
395
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400396 exception->delay = 0;
397 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400399
400 if (stateid == NULL && state != NULL)
401 stateid = &state->stateid;
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403 switch(errorcode) {
404 case 0:
405 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_DELEG_REVOKED:
407 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400408 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400410 if (inode != NULL && stateid != NULL) {
411 nfs_inode_find_state_and_recover(inode,
412 stateid);
413 goto wait_on_recovery;
414 }
415 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400416 if (inode) {
417 int err;
418
419 err = nfs_async_inode_return_delegation(inode,
420 stateid);
421 if (err == 0)
422 goto wait_on_recovery;
423 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
424 exception->retry = 1;
425 break;
426 }
427 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500428 if (state == NULL)
429 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400430 ret = nfs4_schedule_stateid_recovery(server, state);
431 if (ret < 0)
432 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500433 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500435 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436 nfs4_schedule_lease_recovery(clp);
437 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400438 case -NFS4ERR_MOVED:
439 ret = nfs4_schedule_migration_recovery(server);
440 if (ret < 0)
441 break;
442 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400443 case -NFS4ERR_LEASE_MOVED:
444 nfs4_schedule_lease_moved_recovery(clp);
445 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500446#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400447 case -NFS4ERR_BADSESSION:
448 case -NFS4ERR_BADSLOT:
449 case -NFS4ERR_BAD_HIGH_SLOT:
450 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
451 case -NFS4ERR_DEADSESSION:
452 case -NFS4ERR_SEQ_FALSE_RETRY:
453 case -NFS4ERR_SEQ_MISORDERED:
454 dprintk("%s ERROR: %d Reset session\n", __func__,
455 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400456 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400457 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500458#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500459 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500460 if (exception->timeout > HZ) {
461 /* We have retried a decent amount, time to
462 * fail
463 */
464 ret = -EBUSY;
465 break;
466 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400468 nfs_inc_server_stats(server, NFSIOS_DELAY);
469 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400470 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400471 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400472 exception->delay = 1;
473 return 0;
474
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400475 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500476 case -NFS4ERR_OLD_STATEID:
477 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800478 break;
479 case -NFS4ERR_BADOWNER:
480 /* The following works around a Linux server bug! */
481 case -NFS4ERR_BADNAME:
482 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
483 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
484 exception->retry = 1;
485 printk(KERN_WARNING "NFS: v4 server %s "
486 "does not accept raw "
487 "uid/gids. "
488 "Reenabling the idmapper.\n",
489 server->nfs_client->cl_hostname);
490 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500491 }
492 /* We failed to handle the error */
493 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500494wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400495 exception->recovering = 1;
496 return 0;
497}
498
499/* This is the error handling routine for processes that are allowed
500 * to sleep.
501 */
502int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
503{
504 struct nfs_client *clp = server->nfs_client;
505 int ret;
506
507 ret = nfs4_do_handle_exception(server, errorcode, exception);
508 if (exception->delay) {
509 ret = nfs4_delay(server->client, &exception->timeout);
510 goto out_retry;
511 }
512 if (exception->recovering) {
513 ret = nfs4_wait_clnt_recover(clp);
514 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
515 return -EIO;
516 goto out_retry;
517 }
518 return ret;
519out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500520 if (ret == 0)
521 exception->retry = 1;
522 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500523}
524
Trond Myklebust037fc982015-09-20 15:51:00 -0400525static int
526nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
527 int errorcode, struct nfs4_exception *exception)
528{
529 struct nfs_client *clp = server->nfs_client;
530 int ret;
531
532 ret = nfs4_do_handle_exception(server, errorcode, exception);
533 if (exception->delay) {
534 rpc_delay(task, nfs4_update_delay(&exception->timeout));
535 goto out_retry;
536 }
537 if (exception->recovering) {
538 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
539 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
540 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
541 goto out_retry;
542 }
543 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
544 ret = -EIO;
545 return ret;
546out_retry:
547 if (ret == 0)
548 exception->retry = 1;
549 return ret;
550}
551
552static int
553nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
554 struct nfs4_state *state, long *timeout)
555{
556 struct nfs4_exception exception = {
557 .state = state,
558 };
559
560 if (task->tk_status >= 0)
561 return 0;
562 if (timeout)
563 exception.timeout = *timeout;
564 task->tk_status = nfs4_async_handle_exception(task, server,
565 task->tk_status,
566 &exception);
567 if (exception.delay && timeout)
568 *timeout = exception.timeout;
569 if (exception.retry)
570 return -EAGAIN;
571 return 0;
572}
573
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400574/*
575 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
576 * or 'false' otherwise.
577 */
578static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
579{
580 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
581
582 if (flavor == RPC_AUTH_GSS_KRB5I ||
583 flavor == RPC_AUTH_GSS_KRB5P)
584 return true;
585
586 return false;
587}
Trond Myklebust65de8722008-12-23 15:21:44 -0500588
Trond Myklebust452e9352010-07-31 14:29:06 -0400589static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 spin_lock(&clp->cl_lock);
592 if (time_before(clp->cl_last_renewal,timestamp))
593 clp->cl_last_renewal = timestamp;
594 spin_unlock(&clp->cl_lock);
595}
596
Trond Myklebust452e9352010-07-31 14:29:06 -0400597static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
598{
Trond Myklebustbe824162015-07-05 14:50:46 -0400599 struct nfs_client *clp = server->nfs_client;
600
601 if (!nfs4_has_session(clp))
602 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400603}
604
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400605struct nfs4_call_sync_data {
606 const struct nfs_server *seq_server;
607 struct nfs4_sequence_args *seq_args;
608 struct nfs4_sequence_res *seq_res;
609};
610
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800611void nfs4_init_sequence(struct nfs4_sequence_args *args,
612 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400613{
614 args->sa_slot = NULL;
615 args->sa_cache_this = cache_reply;
616 args->sa_privileged = 0;
617
618 res->sr_slot = NULL;
619}
620
621static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
622{
623 args->sa_privileged = 1;
624}
625
Peng Taocb04ad22014-06-11 05:24:15 +0800626int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
627 struct nfs4_sequence_args *args,
628 struct nfs4_sequence_res *res,
629 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400630{
Chuck Lever3bd23842013-08-09 12:49:19 -0400631 struct nfs4_slot *slot;
632
633 /* slot already allocated? */
634 if (res->sr_slot != NULL)
635 goto out_start;
636
637 spin_lock(&tbl->slot_tbl_lock);
638 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
639 goto out_sleep;
640
641 slot = nfs4_alloc_slot(tbl);
642 if (IS_ERR(slot)) {
643 if (slot == ERR_PTR(-ENOMEM))
644 task->tk_timeout = HZ >> 2;
645 goto out_sleep;
646 }
647 spin_unlock(&tbl->slot_tbl_lock);
648
Trond Myklebust0a014a42016-09-22 13:38:51 -0400649 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400650 args->sa_slot = slot;
651 res->sr_slot = slot;
652
653out_start:
654 rpc_call_start(task);
655 return 0;
656
657out_sleep:
658 if (args->sa_privileged)
659 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
660 NULL, RPC_PRIORITY_PRIVILEGED);
661 else
662 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
663 spin_unlock(&tbl->slot_tbl_lock);
664 return -EAGAIN;
665}
Peng Taocb04ad22014-06-11 05:24:15 +0800666EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400667
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400668static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400669{
670 struct nfs4_slot *slot = res->sr_slot;
671 struct nfs4_slot_table *tbl;
672
Chuck Lever3bd23842013-08-09 12:49:19 -0400673 tbl = slot->table;
674 spin_lock(&tbl->slot_tbl_lock);
675 if (!nfs41_wake_and_assign_slot(tbl, slot))
676 nfs4_free_slot(tbl, slot);
677 spin_unlock(&tbl->slot_tbl_lock);
678
679 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400680}
681
682static int nfs40_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
684{
685 if (res->sr_slot != NULL)
686 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400687 return 1;
688}
689
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400690#if defined(CONFIG_NFS_V4_1)
691
Trond Myklebustd185a332010-06-16 09:52:25 -0400692static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400693{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500694 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400695 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500697 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400698
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500700 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500701
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400702 /* Bump the slot sequence number */
703 if (slot->seq_done)
704 slot->seq_nr++;
705 slot->seq_done = 0;
706
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500707 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500708 /* Be nice to the server: try to ensure that the last transmitted
709 * value for highest_user_slotid <= target_highest_slotid
710 */
711 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
712 send_new_highest_used_slotid = true;
713
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500714 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500715 send_new_highest_used_slotid = false;
716 goto out_unlock;
717 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500719
720 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
721 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500722out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500723 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400724 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500725 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400726 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400727 if (waitqueue_active(&tbl->slot_waitq))
728 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400729}
730
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400731static int nfs41_sequence_process(struct rpc_task *task,
732 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400733{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500735 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400736 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500737 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500738 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400739
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500740 if (slot == NULL)
741 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400742 /* don't increment the sequence number if the task wasn't sent */
743 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400744 goto out;
745
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500746 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500747
Trond Myklebustac20d162012-12-15 15:36:07 -0500748 if (slot->interrupted) {
749 slot->interrupted = 0;
750 interrupted = true;
751 }
752
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500754 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400755 switch (res->sr_status) {
756 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400757 /* If previous op on slot was interrupted and we reused
758 * the seq# and got a reply from the cache, then retry
759 */
760 if (task->tk_status == -EREMOTEIO && interrupted) {
761 ++slot->seq_nr;
762 goto retry_nowait;
763 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400764 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400765 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500766 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500768 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400769 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
770 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500771 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500773 case 1:
774 /*
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
777 * operation..
778 * Mark the slot as having hosted an interrupted RPC call.
779 */
780 slot->interrupted = 1;
781 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 case -NFS4ERR_DELAY:
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
785 * of RFC5661.
786 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400788 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500789 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500790 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400791 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500792 case -NFS4ERR_BADSLOT:
793 /*
794 * The slot id we used was probably retired. Try again
795 * using a different slot id.
796 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500797 goto retry_nowait;
798 case -NFS4ERR_SEQ_MISORDERED:
799 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500800 * Was the last operation on this sequence interrupted?
801 * If so, retry after bumping the sequence number.
802 */
803 if (interrupted) {
804 ++slot->seq_nr;
805 goto retry_nowait;
806 }
807 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500808 * Could this slot have been previously retired?
809 * If so, then the server may be expecting seq_nr = 1!
810 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500811 if (slot->seq_nr != 1) {
812 slot->seq_nr = 1;
813 goto retry_nowait;
814 }
815 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500816 case -NFS4ERR_SEQ_FALSE_RETRY:
817 ++slot->seq_nr;
818 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500819 case -NFS4ERR_DEADSESSION:
820 case -NFS4ERR_BADSESSION:
821 nfs4_schedule_session_recovery(session, res->sr_status);
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168ab2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001092 if (cinfo->atomic && cinfo->before == dir->i_version) {
1093 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1094 nfsi->attrtimeo_timestamp = jiffies;
1095 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001097 if (cinfo->before != dir->i_version)
1098 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1099 NFS_INO_INVALID_ACL;
1100 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001101 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001102 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001103 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 spin_unlock(&dir->i_lock);
1105}
1106
1107struct nfs4_opendata {
1108 struct kref kref;
1109 struct nfs_openargs o_arg;
1110 struct nfs_openres o_res;
1111 struct nfs_open_confirmargs c_arg;
1112 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001113 struct nfs4_string owner_name;
1114 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001115 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001117 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001119 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct nfs4_state_owner *owner;
1121 struct nfs4_state *state;
1122 struct iattr attrs;
1123 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001124 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001125 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001126 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001127 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int cancelled;
1129};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001130
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001131static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1132 int err, struct nfs4_exception *exception)
1133{
1134 if (err != -EINVAL)
1135 return false;
1136 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1137 return false;
1138 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1139 exception->retry = 1;
1140 return true;
1141}
1142
Trond Myklebust6ae37332015-01-30 14:21:14 -05001143static u32
1144nfs4_map_atomic_open_share(struct nfs_server *server,
1145 fmode_t fmode, int openflags)
1146{
1147 u32 res = 0;
1148
1149 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1150 case FMODE_READ:
1151 res = NFS4_SHARE_ACCESS_READ;
1152 break;
1153 case FMODE_WRITE:
1154 res = NFS4_SHARE_ACCESS_WRITE;
1155 break;
1156 case FMODE_READ|FMODE_WRITE:
1157 res = NFS4_SHARE_ACCESS_BOTH;
1158 }
1159 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1160 goto out;
1161 /* Want no delegation if we're using O_DIRECT */
1162 if (openflags & O_DIRECT)
1163 res |= NFS4_SHARE_WANT_NO_DELEG;
1164out:
1165 return res;
1166}
1167
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001168static enum open_claim_type4
1169nfs4_map_atomic_open_claim(struct nfs_server *server,
1170 enum open_claim_type4 claim)
1171{
1172 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1173 return claim;
1174 switch (claim) {
1175 default:
1176 return claim;
1177 case NFS4_OPEN_CLAIM_FH:
1178 return NFS4_OPEN_CLAIM_NULL;
1179 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1180 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1181 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1182 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1183 }
1184}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187{
1188 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001189 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 p->o_res.seqid = p->o_arg.seqid;
1191 p->c_res.seqid = p->c_arg.seqid;
1192 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001193 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001195 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196}
1197
Al Viro82a2c1b2011-06-22 18:30:55 -04001198static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001199 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001200 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001201 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001202 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001203 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204{
Al Viro82a2c1b2011-06-22 18:30:55 -04001205 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001206 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001208 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001209 struct nfs4_opendata *p;
1210
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001211 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001212 if (p == NULL)
1213 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001214
1215 p->f_label = nfs4_label_alloc(server, gfp_mask);
1216 if (IS_ERR(p->f_label))
1217 goto err_free_p;
1218
Kinglong Meea49c2692015-07-27 15:31:38 +08001219 p->a_label = nfs4_label_alloc(server, gfp_mask);
1220 if (IS_ERR(p->a_label))
1221 goto err_free_f;
1222
Trond Myklebust63f5f792015-01-23 19:19:25 -05001223 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1224 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001225 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001226 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001227 nfs_sb_active(dentry->d_sb);
1228 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001229 p->dir = parent;
1230 p->owner = sp;
1231 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001232 p->o_arg.open_flags = flags;
1233 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001234 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001235 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001236 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1237 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001238 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1239 * will return permission denied for all bits until close */
1240 if (!(flags & O_EXCL)) {
1241 /* ask server to check for all possible rights as results
1242 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 switch (p->o_arg.claim) {
1244 default:
1245 break;
1246 case NFS4_OPEN_CLAIM_NULL:
1247 case NFS4_OPEN_CLAIM_FH:
1248 p->o_arg.access = NFS4_ACCESS_READ |
1249 NFS4_ACCESS_MODIFY |
1250 NFS4_ACCESS_EXTEND |
1251 NFS4_ACCESS_EXECUTE;
1252 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001253 }
David Howells7539bba2006-08-22 20:06:09 -04001254 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001255 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1256 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001257 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001259 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001260 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001261 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001262 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001263 case NFS4_OPEN_CLAIM_NULL:
1264 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1265 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1266 p->o_arg.fh = NFS_FH(dir);
1267 break;
1268 case NFS4_OPEN_CLAIM_PREVIOUS:
1269 case NFS4_OPEN_CLAIM_FH:
1270 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1271 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001272 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001273 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001274 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001275 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001276
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 p->o_arg.u.attrs = &p->attrs;
1278 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001279
1280 verf[0] = jiffies;
1281 verf[1] = current->pid;
1282 memcpy(p->o_arg.u.verifier.data, verf,
1283 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001284 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285 p->c_arg.fh = &p->o_res.fh;
1286 p->c_arg.stateid = &p->o_res.stateid;
1287 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001288 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001289 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001291
1292err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001293 nfs4_label_free(p->a_label);
1294err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001295 nfs4_label_free(p->f_label);
1296err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297 kfree(p);
1298err:
1299 dput(parent);
1300 return NULL;
1301}
1302
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001303static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001304{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 struct nfs4_opendata *p = container_of(kref,
1306 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001307 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001308
1309 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001310 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001311 if (p->state != NULL)
1312 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001313 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001314
Kinglong Meea49c2692015-07-27 15:31:38 +08001315 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001316 nfs4_label_free(p->f_label);
1317
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001319 dput(p->dentry);
1320 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001321 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001322 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 kfree(p);
1324}
1325
1326static void nfs4_opendata_put(struct nfs4_opendata *p)
1327{
1328 if (p != NULL)
1329 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001330}
1331
Trond Myklebust06f814a2006-01-03 09:55:07 +01001332static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1333{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001334 int ret;
1335
Trond Myklebust06f814a2006-01-03 09:55:07 +01001336 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001337 return ret;
1338}
1339
Trond Myklebust24311f82015-09-20 10:50:17 -04001340static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1341 fmode_t fmode)
1342{
1343 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1344 case FMODE_READ|FMODE_WRITE:
1345 return state->n_rdwr != 0;
1346 case FMODE_WRITE:
1347 return state->n_wronly != 0;
1348 case FMODE_READ:
1349 return state->n_rdonly != 0;
1350 }
1351 WARN_ON_ONCE(1);
1352 return false;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001356{
1357 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358
Trond Myklebust536e43d2012-01-17 22:04:26 -05001359 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360 goto out;
1361 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1364 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 break;
1366 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001367 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1368 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 break;
1370 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001371 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1372 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001374out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001375 return ret;
1376}
1377
Trond Myklebust2a606182015-08-19 22:30:00 -05001378static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1379 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001381 if (delegation == NULL)
1382 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001385 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1386 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001387 switch (claim) {
1388 case NFS4_OPEN_CLAIM_NULL:
1389 case NFS4_OPEN_CLAIM_FH:
1390 break;
1391 case NFS4_OPEN_CLAIM_PREVIOUS:
1392 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1393 break;
1394 default:
1395 return 0;
1396 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001397 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001398 return 1;
1399}
1400
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001402{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001403 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001404 case FMODE_WRITE:
1405 state->n_wronly++;
1406 break;
1407 case FMODE_READ:
1408 state->n_rdonly++;
1409 break;
1410 case FMODE_READ|FMODE_WRITE:
1411 state->n_rdwr++;
1412 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001413 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001414}
1415
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001416#ifdef CONFIG_NFS_V4_1
1417static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1418{
1419 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1420 return true;
1421 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1422 return true;
1423 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1424 return true;
1425 return false;
1426}
1427#endif /* CONFIG_NFS_V4_1 */
1428
Trond Myklebust4f14c192014-02-12 19:15:06 -05001429static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001430{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001431 struct nfs_client *clp = state->owner->so_server->nfs_client;
1432 bool need_recover = false;
1433
1434 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1435 need_recover = true;
1436 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1437 need_recover = true;
1438 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1439 need_recover = true;
1440 if (need_recover)
1441 nfs4_state_mark_reclaim_nograce(clp, state);
1442}
1443
Trond Myklebuste999e802014-02-10 18:20:47 -05001444static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001445 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001446{
1447 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1448 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001449 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001450 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001451 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001452 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001453 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001454 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1455 return true;
1456 return false;
1457}
1458
Trond Myklebustf95549c2015-01-23 18:06:09 -05001459static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1460{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001461 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1462 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463 if (state->n_wronly)
1464 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1465 if (state->n_rdonly)
1466 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 if (state->n_rdwr)
1468 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001469 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001470}
1471
Trond Myklebust226056c2014-02-11 10:41:07 -05001472static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1473 nfs4_stateid *stateid, fmode_t fmode)
1474{
1475 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1476 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1477 case FMODE_WRITE:
1478 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1479 break;
1480 case FMODE_READ:
1481 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1482 break;
1483 case 0:
1484 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1485 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1486 clear_bit(NFS_OPEN_STATE, &state->flags);
1487 }
1488 if (stateid == NULL)
1489 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001490 /* Handle OPEN+OPEN_DOWNGRADE races */
1491 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1492 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001493 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001494 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001495 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001496 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001497 nfs4_stateid_copy(&state->stateid, stateid);
1498 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499}
1500
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001501static void nfs_clear_open_stateid(struct nfs4_state *state,
1502 nfs4_stateid *arg_stateid,
1503 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001504{
1505 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001506 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1507 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1508 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001509 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001510 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1511 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001512}
1513
Trond Myklebust1393d962016-09-22 13:39:13 -04001514static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1515 const nfs4_stateid *stateid, fmode_t fmode,
1516 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001517{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001518 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001519 case FMODE_READ:
1520 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1521 break;
1522 case FMODE_WRITE:
1523 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1524 break;
1525 case FMODE_READ|FMODE_WRITE:
1526 set_bit(NFS_O_RDWR_STATE, &state->flags);
1527 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001528 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001529 return;
1530 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1531 nfs4_stateid_copy(&state->stateid, stateid);
1532 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001533}
1534
Trond Myklebust1393d962016-09-22 13:39:13 -04001535static void __update_open_stateid(struct nfs4_state *state,
1536 const nfs4_stateid *open_stateid,
1537 const nfs4_stateid *deleg_stateid,
1538 fmode_t fmode,
1539 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001541 /*
1542 * Protect the call to nfs4_state_set_mode_locked and
1543 * serialise the stateid update
1544 */
Andrew Elble361cad32015-12-02 09:20:57 -05001545 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001546 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001547 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001548 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001549 set_bit(NFS_DELEGATED_STATE, &state->flags);
1550 }
1551 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001552 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001553 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001554 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001555 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Trond Myklebust1393d962016-09-22 13:39:13 -04001558static int update_open_stateid(struct nfs4_state *state,
1559 const nfs4_stateid *open_stateid,
1560 const nfs4_stateid *delegation,
1561 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001562{
Trond Myklebust1393d962016-09-22 13:39:13 -04001563 struct nfs_server *server = NFS_SERVER(state->inode);
1564 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001565 struct nfs_inode *nfsi = NFS_I(state->inode);
1566 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001567 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001568 int ret = 0;
1569
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001570 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001571
1572 rcu_read_lock();
1573 deleg_cur = rcu_dereference(nfsi->delegation);
1574 if (deleg_cur == NULL)
1575 goto no_delegation;
1576
1577 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001578 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001579 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001580 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001581 goto no_delegation_unlock;
1582
1583 if (delegation == NULL)
1584 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001585 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001586 goto no_delegation_unlock;
1587
Trond Myklebustb7391f42008-12-23 15:21:52 -05001588 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001589 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1590 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001591 ret = 1;
1592no_delegation_unlock:
1593 spin_unlock(&deleg_cur->lock);
1594no_delegation:
1595 rcu_read_unlock();
1596
1597 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001598 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001599 ret = 1;
1600 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001601 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001602 nfs4_schedule_state_manager(clp);
1603 if (freeme.type != 0)
1604 nfs4_test_and_free_stateid(server, &freeme,
1605 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001606
1607 return ret;
1608}
1609
Trond Myklebust39071e62015-01-24 15:07:56 -05001610static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1611 const nfs4_stateid *stateid)
1612{
1613 struct nfs4_state *state = lsp->ls_state;
1614 bool ret = false;
1615
1616 spin_lock(&state->state_lock);
1617 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1618 goto out_noupdate;
1619 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1620 goto out_noupdate;
1621 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1622 ret = true;
1623out_noupdate:
1624 spin_unlock(&state->state_lock);
1625 return ret;
1626}
Trond Myklebust34310432008-12-23 15:21:38 -05001627
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001628static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001629{
1630 struct nfs_delegation *delegation;
1631
1632 rcu_read_lock();
1633 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 rcu_read_unlock();
1636 return;
1637 }
1638 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001639 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001640}
1641
Trond Myklebust6ee41262007-07-08 14:11:36 -04001642static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643{
1644 struct nfs4_state *state = opendata->state;
1645 struct nfs_inode *nfsi = NFS_I(state->inode);
1646 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001647 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001648 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001649 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001650 nfs4_stateid stateid;
1651 int ret = -EAGAIN;
1652
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001654 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001655 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001656 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001657 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001658 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001659 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001660 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001661 rcu_read_lock();
1662 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001663 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001664 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001665 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001666 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001668 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001670 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001671 if (!opendata->is_recover) {
1672 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1673 if (ret != 0)
1674 goto out;
1675 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001677
1678 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001679 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001680 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682out:
1683 return ERR_PTR(ret);
1684out_return_state:
1685 atomic_inc(&state->count);
1686 return state;
1687}
1688
Andy Adamsone23008e2012-10-02 21:07:32 -04001689static void
1690nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1691{
1692 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1693 struct nfs_delegation *delegation;
1694 int delegation_flags = 0;
1695
1696 rcu_read_lock();
1697 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1698 if (delegation)
1699 delegation_flags = delegation->flags;
1700 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001701 switch (data->o_arg.claim) {
1702 default:
1703 break;
1704 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1705 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001706 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1707 "returning a delegation for "
1708 "OPEN(CLAIM_DELEGATE_CUR)\n",
1709 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001710 return;
1711 }
1712 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001713 nfs_inode_set_delegation(state->inode,
1714 data->owner->so_cred,
1715 &data->o_res);
1716 else
1717 nfs_inode_reclaim_delegation(state->inode,
1718 data->owner->so_cred,
1719 &data->o_res);
1720}
1721
1722/*
1723 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1724 * and update the nfs4_state.
1725 */
1726static struct nfs4_state *
1727_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1728{
1729 struct inode *inode = data->state->inode;
1730 struct nfs4_state *state = data->state;
1731 int ret;
1732
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001733 if (!data->rpc_done) {
1734 if (data->rpc_status) {
1735 ret = data->rpc_status;
1736 goto err;
1737 }
1738 /* cached opens have already been processed */
1739 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001740 }
1741
Andy Adamsone23008e2012-10-02 21:07:32 -04001742 ret = nfs_refresh_inode(inode, &data->f_attr);
1743 if (ret)
1744 goto err;
1745
1746 if (data->o_res.delegation_type != 0)
1747 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001748update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001749 update_open_stateid(state, &data->o_res.stateid, NULL,
1750 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001751 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001752
1753 return state;
1754err:
1755 return ERR_PTR(ret);
1756
1757}
1758
1759static struct nfs4_state *
1760_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761{
1762 struct inode *inode;
1763 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765
Trond Myklebustaac00a82007-07-05 19:02:21 -04001766 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001767 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001768 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769 goto out;
1770 }
1771
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001772 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001775 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001776 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001777 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001778 goto err;
1779 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001780 state = nfs4_get_open_state(inode, data->owner);
1781 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001782 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 if (data->o_res.delegation_type != 0)
1784 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001785 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001788out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001789 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001790 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001791err_put_inode:
1792 iput(inode);
1793err:
1794 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001795}
1796
Andy Adamsone23008e2012-10-02 21:07:32 -04001797static struct nfs4_state *
1798nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1799{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001800 struct nfs4_state *ret;
1801
Andy Adamsone23008e2012-10-02 21:07:32 -04001802 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001803 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1804 else
1805 ret = _nfs4_opendata_to_nfs4_state(data);
1806 nfs4_sequence_free_slot(&data->o_res.seq_res);
1807 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001808}
1809
Trond Myklebust864472e2006-01-03 09:55:15 +01001810static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1811{
1812 struct nfs_inode *nfsi = NFS_I(state->inode);
1813 struct nfs_open_context *ctx;
1814
1815 spin_lock(&state->inode->i_lock);
1816 list_for_each_entry(ctx, &nfsi->open_files, list) {
1817 if (ctx->state != state)
1818 continue;
1819 get_nfs_open_context(ctx);
1820 spin_unlock(&state->inode->i_lock);
1821 return ctx;
1822 }
1823 spin_unlock(&state->inode->i_lock);
1824 return ERR_PTR(-ENOENT);
1825}
1826
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001827static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1828 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001829{
1830 struct nfs4_opendata *opendata;
1831
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001832 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001833 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001834 if (opendata == NULL)
1835 return ERR_PTR(-ENOMEM);
1836 opendata->state = state;
1837 atomic_inc(&state->count);
1838 return opendata;
1839}
1840
Trond Myklebust24311f82015-09-20 10:50:17 -04001841static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1842 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001843{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001844 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001845 int ret;
1846
Trond Myklebust24311f82015-09-20 10:50:17 -04001847 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001848 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001849 opendata->o_arg.open_flags = 0;
1850 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001851 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1852 NFS_SB(opendata->dentry->d_sb),
1853 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001854 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1855 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1856 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001857 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001858 if (ret != 0)
1859 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001860 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001861 if (IS_ERR(newstate))
1862 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001863 if (newstate != opendata->state)
1864 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001865 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001866 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001867}
1868
1869static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1870{
Trond Myklebust864472e2006-01-03 09:55:15 +01001871 int ret;
1872
Trond Myklebust4f14c192014-02-12 19:15:06 -05001873 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1874 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1875 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1876 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001877 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001878 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001879 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001880 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001881 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1882 if (ret != 0)
1883 return ret;
1884 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1885 if (ret != 0)
1886 return ret;
1887 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1888 if (ret != 0)
1889 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001890 /*
1891 * We may have performed cached opens for all three recoveries.
1892 * Check if we need to update the current stateid.
1893 */
1894 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001895 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001896 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001897 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001898 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001899 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001900 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001901 return 0;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904/*
1905 * OPEN_RECLAIM:
1906 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001908static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001910 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001911 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001912 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 int status;
1914
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001915 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1916 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001917 if (IS_ERR(opendata))
1918 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001919 rcu_read_lock();
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001921 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001922 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001923 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 opendata->o_arg.u.delegation_type = delegation_type;
1925 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001926 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return status;
1928}
1929
Trond Myklebust539cd032007-06-05 11:46:42 -04001930static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
1932 struct nfs_server *server = NFS_SERVER(state->inode);
1933 struct nfs4_exception exception = { };
1934 int err;
1935 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001936 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001937 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001938 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1939 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001940 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001941 break;
1942 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 } while (exception.retry);
1944 return err;
1945}
1946
Trond Myklebust864472e2006-01-03 09:55:15 +01001947static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1948{
1949 struct nfs_open_context *ctx;
1950 int ret;
1951
1952 ctx = nfs4_state_find_open_context(state);
1953 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001954 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001955 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001956 put_nfs_open_context(ctx);
1957 return ret;
1958}
1959
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001960static 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 -07001961{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001962 switch (err) {
1963 default:
1964 printk(KERN_ERR "NFS: %s: unhandled error "
1965 "%d.\n", __func__, err);
1966 case 0:
1967 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001968 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001969 case -ESTALE:
1970 break;
1971 case -NFS4ERR_BADSESSION:
1972 case -NFS4ERR_BADSLOT:
1973 case -NFS4ERR_BAD_HIGH_SLOT:
1974 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1975 case -NFS4ERR_DEADSESSION:
1976 set_bit(NFS_DELEGATED_STATE, &state->flags);
1977 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1978 return -EAGAIN;
1979 case -NFS4ERR_STALE_CLIENTID:
1980 case -NFS4ERR_STALE_STATEID:
1981 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 /* Don't recall a delegation if it was lost */
1983 nfs4_schedule_lease_recovery(server->nfs_client);
1984 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001985 case -NFS4ERR_MOVED:
1986 nfs4_schedule_migration_recovery(server);
1987 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001988 case -NFS4ERR_LEASE_MOVED:
1989 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1990 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001991 case -NFS4ERR_DELEG_REVOKED:
1992 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001993 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001994 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001995 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001996 nfs_inode_find_state_and_recover(state->inode,
1997 stateid);
1998 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001999 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002000 case -NFS4ERR_DELAY:
2001 case -NFS4ERR_GRACE:
2002 set_bit(NFS_DELEGATED_STATE, &state->flags);
2003 ssleep(1);
2004 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002005 case -ENOMEM:
2006 case -NFS4ERR_DENIED:
2007 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2008 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return err;
2011}
2012
Trond Myklebust24311f82015-09-20 10:50:17 -04002013int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2014 struct nfs4_state *state, const nfs4_stateid *stateid,
2015 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002016{
2017 struct nfs_server *server = NFS_SERVER(state->inode);
2018 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002019 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002020
2021 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2022 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2023 if (IS_ERR(opendata))
2024 return PTR_ERR(opendata);
2025 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002026 write_seqlock(&state->seqlock);
2027 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2028 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002029 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2030 switch (type & (FMODE_READ|FMODE_WRITE)) {
2031 case FMODE_READ|FMODE_WRITE:
2032 case FMODE_WRITE:
2033 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2034 if (err)
2035 break;
2036 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2037 if (err)
2038 break;
2039 case FMODE_READ:
2040 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2041 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002042 nfs4_opendata_put(opendata);
2043 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2044}
2045
Chuck Leverbe05c862013-08-09 12:49:47 -04002046static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2047{
2048 struct nfs4_opendata *data = calldata;
2049
Peng Taocb04ad22014-06-11 05:24:15 +08002050 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2051 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002052}
2053
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002054static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2055{
2056 struct nfs4_opendata *data = calldata;
2057
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002058 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002059
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002061 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002062 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002063 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002064 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002065 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002066 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067}
2068
2069static void nfs4_open_confirm_release(void *calldata)
2070{
2071 struct nfs4_opendata *data = calldata;
2072 struct nfs4_state *state = NULL;
2073
2074 /* If this request hasn't been cancelled, do nothing */
2075 if (data->cancelled == 0)
2076 goto out_free;
2077 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002078 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002079 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002080 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002081 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002082 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002083out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002084 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002085}
2086
2087static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002088 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002089 .rpc_call_done = nfs4_open_confirm_done,
2090 .rpc_release = nfs4_open_confirm_release,
2091};
2092
2093/*
2094 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2095 */
2096static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2097{
David Howells2b0143b2015-03-17 22:25:59 +00002098 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002099 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002100 struct rpc_message msg = {
2101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2102 .rpc_argp = &data->c_arg,
2103 .rpc_resp = &data->c_res,
2104 .rpc_cred = data->owner->so_cred,
2105 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002106 struct rpc_task_setup task_setup_data = {
2107 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002108 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002109 .callback_ops = &nfs4_open_confirm_ops,
2110 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002111 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002112 .flags = RPC_TASK_ASYNC,
2113 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 int status;
2115
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002116 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002117 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002118 data->rpc_done = 0;
2119 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002120 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002121 if (data->is_recover)
2122 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002123 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002124 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002125 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002126 status = nfs4_wait_for_completion_rpc_task(task);
2127 if (status != 0) {
2128 data->cancelled = 1;
2129 smp_wmb();
2130 } else
2131 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002132 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return status;
2134}
2135
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002136static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002138 struct nfs4_opendata *data = calldata;
2139 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002140 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002141 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002142
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002143 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002144 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002145 /*
2146 * Check if we still need to send an OPEN call, or if we can use
2147 * a delegation instead.
2148 */
2149 if (data->state != NULL) {
2150 struct nfs_delegation *delegation;
2151
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002152 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002153 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002154 rcu_read_lock();
2155 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002156 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002157 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002158 rcu_read_unlock();
2159 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002160 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002161 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002162 switch (claim) {
2163 default:
2164 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002165 case NFS4_OPEN_CLAIM_PREVIOUS:
2166 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2167 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002168 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002169 case NFS4_OPEN_CLAIM_FH:
2170 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002171 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2172 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002173 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002174 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002175 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002176 &data->o_res.seq_res,
2177 task) != 0)
2178 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002179
2180 /* Set the create mode (note dependency on the session type) */
2181 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2182 if (data->o_arg.open_flags & O_EXCL) {
2183 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2184 if (nfs4_has_persistent_session(clp))
2185 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2186 else if (clp->cl_mvops->minor_version > 0)
2187 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2188 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002189 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002190unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002191 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002192 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002193out_no_action:
2194 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002195out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002196 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002197}
2198
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002199static void nfs4_open_done(struct rpc_task *task, void *calldata)
2200{
2201 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002203 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002204
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002205 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002206 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002207
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002209 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2210 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002211 case S_IFREG:
2212 break;
2213 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002214 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002215 break;
2216 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002218 break;
2219 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002221 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002222 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002223 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002224 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2225 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002226 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002229
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002230static void nfs4_open_release(void *calldata)
2231{
2232 struct nfs4_opendata *data = calldata;
2233 struct nfs4_state *state = NULL;
2234
2235 /* If this request hasn't been cancelled, do nothing */
2236 if (data->cancelled == 0)
2237 goto out_free;
2238 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002239 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002240 goto out_free;
2241 /* In case we need an open_confirm, no cleanup! */
2242 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2243 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002244 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002245 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002246 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002247out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002248 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002249}
2250
2251static const struct rpc_call_ops nfs4_open_ops = {
2252 .rpc_call_prepare = nfs4_open_prepare,
2253 .rpc_call_done = nfs4_open_done,
2254 .rpc_release = nfs4_open_release,
2255};
2256
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002257static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258{
David Howells2b0143b2015-03-17 22:25:59 +00002259 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260 struct nfs_server *server = NFS_SERVER(dir);
2261 struct nfs_openargs *o_arg = &data->o_arg;
2262 struct nfs_openres *o_res = &data->o_res;
2263 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002264 struct rpc_message msg = {
2265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2266 .rpc_argp = o_arg,
2267 .rpc_resp = o_res,
2268 .rpc_cred = data->owner->so_cred,
2269 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002270 struct rpc_task_setup task_setup_data = {
2271 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002272 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002273 .callback_ops = &nfs4_open_ops,
2274 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002275 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002276 .flags = RPC_TASK_ASYNC,
2277 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 int status;
2279
Chuck Levera9c92d62013-08-09 12:48:18 -04002280 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002281 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002282 data->rpc_done = 0;
2283 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002284 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002285 data->is_recover = 0;
2286 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002287 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002288 data->is_recover = 1;
2289 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002290 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002291 if (IS_ERR(task))
2292 return PTR_ERR(task);
2293 status = nfs4_wait_for_completion_rpc_task(task);
2294 if (status != 0) {
2295 data->cancelled = 1;
2296 smp_wmb();
2297 } else
2298 status = data->rpc_status;
2299 rpc_put_task(task);
2300
2301 return status;
2302}
2303
2304static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2305{
David Howells2b0143b2015-03-17 22:25:59 +00002306 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002307 struct nfs_openres *o_res = &data->o_res;
2308 int status;
2309
2310 status = nfs4_run_open_task(data, 1);
2311 if (status != 0 || !data->rpc_done)
2312 return status;
2313
Trond Myklebust6926afd2012-01-07 13:22:46 -05002314 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2315
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002316 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2317 status = _nfs4_proc_open_confirm(data);
2318 if (status != 0)
2319 return status;
2320 }
2321
2322 return status;
2323}
2324
Trond Myklebustf3792d62014-07-10 08:54:32 -04002325/*
2326 * Additional permission checks in order to distinguish between an
2327 * open for read, and an open for execute. This works around the
2328 * fact that NFSv4 OPEN treats read and execute permissions as being
2329 * the same.
2330 * Note that in the non-execute case, we want to turn off permission
2331 * checking if we just created a new file (POSIX open() semantics).
2332 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002333static int nfs4_opendata_access(struct rpc_cred *cred,
2334 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002335 struct nfs4_state *state, fmode_t fmode,
2336 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002337{
2338 struct nfs_access_entry cache;
2339 u32 mask;
2340
2341 /* access call failed or for some reason the server doesn't
2342 * support any access modes -- defer access call until later */
2343 if (opendata->o_res.access_supported == 0)
2344 return 0;
2345
2346 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002347 /*
2348 * Use openflags to check for exec, because fmode won't
2349 * always have FMODE_EXEC set when file open for exec.
2350 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002351 if (openflags & __FMODE_EXEC) {
2352 /* ONLY check for exec rights */
2353 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002354 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002355 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356
2357 cache.cred = cred;
2358 cache.jiffies = jiffies;
2359 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2360 nfs_access_add_cache(state->inode, &cache);
2361
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002362 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002363 return 0;
2364
2365 /* even though OPEN succeeded, access is denied. Close the file */
2366 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002367 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002368}
2369
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002370/*
2371 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2372 */
2373static int _nfs4_proc_open(struct nfs4_opendata *data)
2374{
David Howells2b0143b2015-03-17 22:25:59 +00002375 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376 struct nfs_server *server = NFS_SERVER(dir);
2377 struct nfs_openargs *o_arg = &data->o_arg;
2378 struct nfs_openres *o_res = &data->o_res;
2379 int status;
2380
2381 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002382 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002384 if (status != 0) {
2385 if (status == -NFS4ERR_BADNAME &&
2386 !(o_arg->open_flags & O_CREAT))
2387 return -ENOENT;
2388 return status;
2389 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002390
Trond Myklebust6926afd2012-01-07 13:22:46 -05002391 nfs_fattr_map_and_free_names(server, &data->f_attr);
2392
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002393 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002394 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002395 if (o_arg->open_flags & O_EXCL)
2396 data->file_created = 1;
2397 else if (o_res->cinfo.before != o_res->cinfo.after)
2398 data->file_created = 1;
2399 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002400 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2401 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002403 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002405 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 }
2407 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002408 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410}
2411
Andy Adamsond83217c2011-03-01 01:34:17 +00002412static int nfs4_recover_expired_lease(struct nfs_server *server)
2413{
2414 return nfs4_client_recover_expired_lease(server->nfs_client);
2415}
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/*
2418 * OPEN_EXPIRED:
2419 * reclaim state on the server after a network partition.
2420 * Assumes caller holds the appropriate lock
2421 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002422static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002424 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002425 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002427 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002428 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002429 if (IS_ERR(opendata))
2430 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002431 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002432 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002433 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002434 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002435 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002438static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002439{
Trond Myklebust539cd032007-06-05 11:46:42 -04002440 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002441 struct nfs4_exception exception = { };
2442 int err;
2443
2444 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002445 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002446 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002447 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2448 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002449 switch (err) {
2450 default:
2451 goto out;
2452 case -NFS4ERR_GRACE:
2453 case -NFS4ERR_DELAY:
2454 nfs4_handle_exception(server, err, &exception);
2455 err = 0;
2456 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002457 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002458out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002459 return err;
2460}
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2463{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002465 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Trond Myklebust864472e2006-01-03 09:55:15 +01002467 ctx = nfs4_state_find_open_context(state);
2468 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002469 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002470 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002471 put_nfs_open_context(ctx);
2472 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Trond Myklebust41020b62016-09-22 13:38:58 -04002475static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2476 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002477{
Trond Myklebust41020b62016-09-22 13:38:58 -04002478 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002479 write_seqlock(&state->seqlock);
2480 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2481 write_sequnlock(&state->seqlock);
2482 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2483}
2484
2485static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2486{
2487 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002488 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002489}
2490
2491static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2492{
2493 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2494 nfs40_clear_delegation_stateid(state);
2495 return nfs4_open_expired(sp, state);
2496}
2497
Trond Myklebust45870d62016-09-22 13:38:59 -04002498static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2501{
2502 return -NFS4ERR_BAD_STATEID;
2503}
2504
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002505#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002506static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2507 nfs4_stateid *stateid,
2508 struct rpc_cred *cred)
2509{
2510 int status;
2511
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002512 switch (stateid->type) {
2513 default:
2514 break;
2515 case NFS4_INVALID_STATEID_TYPE:
2516 case NFS4_SPECIAL_STATEID_TYPE:
2517 return -NFS4ERR_BAD_STATEID;
2518 case NFS4_REVOKED_STATEID_TYPE:
2519 goto out_free;
2520 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002521
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002522 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523 switch (status) {
2524 case -NFS4ERR_EXPIRED:
2525 case -NFS4ERR_ADMIN_REVOKED:
2526 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002527 break;
2528 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002529 return status;
2530 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002531out_free:
2532 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002533 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002534 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002535}
2536
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002537static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002538{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002539 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002541 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 struct rpc_cred *cred;
2543 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002544
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002545 /* Get the delegation credential for use by test/free_stateid */
2546 rcu_read_lock();
2547 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002548 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002549 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002550 return;
2551 }
2552
2553 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002554 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2555 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002556 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002557 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002558 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002559
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002560 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2561 rcu_read_unlock();
2562 return;
2563 }
2564
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565 cred = get_rpccred(delegation->cred);
2566 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002567 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002568 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002569 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002570 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002571
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002572 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002573}
2574
2575/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002576 * nfs41_check_expired_locks - possibly free a lock stateid
2577 *
2578 * @state: NFSv4 state for an inode
2579 *
2580 * Returns NFS_OK if recovery for this stateid is now finished.
2581 * Otherwise a negative NFS4ERR value is returned.
2582 */
2583static int nfs41_check_expired_locks(struct nfs4_state *state)
2584{
2585 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002586 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002587 struct nfs_server *server = NFS_SERVER(state->inode);
2588
2589 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2590 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002591
2592 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002593 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2594 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2595 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2596
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002597 atomic_inc(&lsp->ls_count);
2598 spin_unlock(&state->state_lock);
2599
2600 nfs4_put_lock_state(prev);
2601 prev = lsp;
2602
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603 status = nfs41_test_and_free_expired_stateid(server,
2604 &lsp->ls_stateid,
2605 cred);
2606 trace_nfs4_test_lock_stateid(state, lsp, status);
2607 if (status == -NFS4ERR_EXPIRED ||
2608 status == -NFS4ERR_BAD_STATEID) {
2609 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002610 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611 if (!recover_lost_locks)
2612 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2613 } else if (status != NFS_OK) {
2614 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002615 nfs4_put_lock_state(prev);
2616 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002617 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002618 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002619 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002620 }
2621 spin_unlock(&state->state_lock);
2622 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002623out:
2624 return ret;
2625}
2626
2627/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628 * nfs41_check_open_stateid - possibly free an open stateid
2629 *
2630 * @state: NFSv4 state for an inode
2631 *
2632 * Returns NFS_OK if recovery for this stateid is now finished.
2633 * Otherwise a negative NFS4ERR value is returned.
2634 */
2635static int nfs41_check_open_stateid(struct nfs4_state *state)
2636{
2637 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002638 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002639 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002640 int status;
2641
Trond Myklebustb134fc42016-09-22 13:39:16 -04002642 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002643 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2644 if (nfs4_have_delegation(state->inode, state->state))
2645 return NFS_OK;
2646 return -NFS4ERR_OPENMODE;
2647 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002648 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002649 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002650 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002651 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002652 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002653 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2654 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2655 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002656 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002657 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002658 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002659 if (status != NFS_OK)
2660 return status;
2661 if (nfs_open_stateid_recover_openmode(state))
2662 return -NFS4ERR_OPENMODE;
2663 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002664}
2665
2666static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2667{
Chuck Levereb64cf92012-07-11 16:30:05 -04002668 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002669
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002670 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 status = nfs41_check_expired_locks(state);
2672 if (status != NFS_OK)
2673 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002674 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002675 if (status != NFS_OK)
2676 status = nfs4_open_expired(sp, state);
2677 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002678}
2679#endif
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002682 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2683 * fields corresponding to attributes that were used to store the verifier.
2684 * Make sure we clobber those fields in the later setattr call
2685 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2687 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002688{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002689 const u32 *attrset = opendata->o_res.attrset;
2690
2691 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002692 !(sattr->ia_valid & ATTR_ATIME_SET))
2693 sattr->ia_valid |= ATTR_ATIME;
2694
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002695 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002696 !(sattr->ia_valid & ATTR_MTIME_SET))
2697 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002698
2699 /* Except MODE, it seems harmless of setting twice. */
2700 if ((attrset[1] & FATTR4_WORD1_MODE))
2701 sattr->ia_valid &= ~ATTR_MODE;
2702
2703 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2704 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002705}
2706
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2708 fmode_t fmode,
2709 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002710 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711{
2712 struct nfs4_state_owner *sp = opendata->owner;
2713 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002714 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 struct nfs4_state *state;
2716 unsigned int seq;
2717 int ret;
2718
2719 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2720
2721 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002722 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002723 goto out;
2724
2725 state = nfs4_opendata_to_nfs4_state(opendata);
2726 ret = PTR_ERR(state);
2727 if (IS_ERR(state))
2728 goto out;
2729 if (server->caps & NFS_CAP_POSIX_LOCK)
2730 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002731 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2732 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002733
Trond Myklebust275bb302013-05-29 13:11:28 -04002734 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002735 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002736 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002738 alias = d_exact_alias(dentry, state->inode);
2739 if (!alias)
2740 alias = d_splice_alias(igrab(state->inode), dentry);
2741 /* d_splice_alias() can't fail here - it's a non-directory */
2742 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002743 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002744 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002745 }
2746 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002747 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002748 }
2749
Trond Myklebustc21443c2013-02-07 14:26:21 -05002750 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2751 if (ret != 0)
2752 goto out;
2753
Trond Myklebust3efb9722013-05-29 13:17:04 -04002754 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002755 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002756 nfs_inode_attach_open_context(ctx);
2757 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2758 nfs4_schedule_stateid_recovery(server, state);
2759 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002760out:
2761 return ret;
2762}
2763
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002764/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002765 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 */
Andy Adamson82be4172012-05-23 05:02:35 -04002767static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002768 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002769 int flags,
2770 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002771 struct nfs4_label *label,
2772 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
2774 struct nfs4_state_owner *sp;
2775 struct nfs4_state *state = NULL;
2776 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002777 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002778 struct dentry *dentry = ctx->dentry;
2779 struct rpc_cred *cred = ctx->cred;
2780 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2781 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002782 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002783 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002784 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002788 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2789 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2791 goto out_err;
2792 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002793 status = nfs4_recover_expired_lease(server);
2794 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002795 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002796 if (d_really_is_positive(dentry))
2797 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002798 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002799 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002800 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002801 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002802 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002803 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002804 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
David Quigley14c43f72013-05-22 12:50:43 -04002806 if (label) {
2807 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2808 if (IS_ERR(olabel)) {
2809 status = PTR_ERR(olabel);
2810 goto err_opendata_put;
2811 }
2812 }
2813
Trond Myklebuste911b812014-03-26 13:24:37 -07002814 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2815 if (!opendata->f_attr.mdsthreshold) {
2816 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2817 if (!opendata->f_attr.mdsthreshold)
2818 goto err_free_label;
2819 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002820 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002821 }
David Howells2b0143b2015-03-17 22:25:59 +00002822 if (d_really_is_positive(dentry))
2823 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002824
Trond Myklebust3efb9722013-05-29 13:17:04 -04002825 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002826 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002827 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002828 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002829
NeilBrownefcbc042015-07-30 13:00:56 +10002830 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002831 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002832 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002833 /*
2834 * send create attributes which was not set by open
2835 * with an extra setattr.
2836 */
2837 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2838 nfs_fattr_init(opendata->o_res.f_attr);
2839 status = nfs4_do_setattr(state->inode, cred,
2840 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002841 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002842 if (status == 0) {
2843 nfs_setattr_update_inode(state->inode, sattr,
2844 opendata->o_res.f_attr);
2845 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2846 }
David Quigley1775fd32013-05-22 12:50:42 -04002847 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002848 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002849 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002850 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002851
Trond Myklebuste911b812014-03-26 13:24:37 -07002852 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002853 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002854 opendata->f_attr.mdsthreshold = NULL;
2855 }
Andy Adamson82be4172012-05-23 05:02:35 -04002856
David Quigley14c43f72013-05-22 12:50:43 -04002857 nfs4_label_free(olabel);
2858
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002859 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002862err_free_label:
2863 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002864err_opendata_put:
2865 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002866err_put_state_owner:
2867 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return status;
2870}
2871
2872
Andy Adamson82be4172012-05-23 05:02:35 -04002873static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002874 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002875 int flags,
2876 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002877 struct nfs4_label *label,
2878 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002880 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 struct nfs4_exception exception = { };
2882 struct nfs4_state *res;
2883 int status;
2884
2885 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002886 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002887 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002888 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (status == 0)
2890 break;
2891 /* NOTE: BAD_SEQID means the server and client disagree about the
2892 * book-keeping w.r.t. state-changing operations
2893 * (OPEN/CLOSE/LOCK/LOCKU...)
2894 * It is actually a sign of a bug on the client or on the server.
2895 *
2896 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002897 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * have unhashed the old state_owner for us, and that we can
2899 * therefore safely retry using a new one. We should still warn
2900 * the user though...
2901 */
2902 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002903 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002904 " returned a bad sequence-id error!\n",
2905 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 exception.retry = 1;
2907 continue;
2908 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002909 /*
2910 * BAD_STATEID on OPEN means that the server cancelled our
2911 * state before it received the OPEN_CONFIRM.
2912 * Recover by retrying the request as per the discussion
2913 * on Page 181 of RFC3530.
2914 */
2915 if (status == -NFS4ERR_BAD_STATEID) {
2916 exception.retry = 1;
2917 continue;
2918 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002919 if (status == -EAGAIN) {
2920 /* We must have found a delegation */
2921 exception.retry = 1;
2922 continue;
2923 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002924 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2925 continue;
2926 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 status, &exception));
2928 } while (exception.retry);
2929 return res;
2930}
2931
Trond Myklebust8487c472016-06-26 08:44:35 -04002932static int _nfs4_do_setattr(struct inode *inode,
2933 struct nfs_setattrargs *arg,
2934 struct nfs_setattrres *res,
2935 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002936 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002937{
2938 struct nfs_server *server = NFS_SERVER(inode);
2939 struct rpc_message msg = {
2940 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2941 .rpc_argp = arg,
2942 .rpc_resp = res,
2943 .rpc_cred = cred,
2944 };
2945 struct rpc_cred *delegation_cred = NULL;
2946 unsigned long timestamp = jiffies;
2947 fmode_t fmode;
2948 bool truncate;
2949 int status;
2950
2951 nfs_fattr_init(res->fattr);
2952
2953 /* Servers should only apply open mode checks for file size changes */
2954 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2955 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2956
2957 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2958 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002959 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002960 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002961 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002962 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002963 l_ctx = nfs_get_lock_context(ctx);
2964 if (IS_ERR(l_ctx))
2965 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002966 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2967 &arg->stateid, &delegation_cred);
2968 nfs_put_lock_context(l_ctx);
2969 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002970 return -EBADF;
2971 } else
2972 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2973 if (delegation_cred)
2974 msg.rpc_cred = delegation_cred;
2975
2976 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2977
2978 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002979 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002980 renew_lease(server, timestamp);
2981 trace_nfs4_setattr(inode, &arg->stateid, status);
2982 return status;
2983}
2984
2985static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2986 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002987 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002988 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002990 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002991 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002993 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .iap = sattr,
2995 .server = server,
2996 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002997 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 };
2999 struct nfs_setattrres res = {
3000 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003001 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 .server = server,
3003 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003004 struct nfs4_exception exception = {
3005 .state = state,
3006 .inode = inode,
3007 .stateid = &arg.stateid,
3008 };
3009 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
David Quigleyaa9c2662013-05-22 12:50:44 -04003011 arg.bitmask = nfs4_bitmask(server, ilabel);
3012 if (ilabel)
3013 arg.bitmask = nfs4_bitmask(server, olabel);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003016 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003017 switch (err) {
3018 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003019 if (!(sattr->ia_valid & ATTR_SIZE)) {
3020 pr_warn_once("NFSv4: server %s is incorrectly "
3021 "applying open mode checks to "
3022 "a SETATTR that is not "
3023 "changing file size.\n",
3024 server->nfs_client->cl_hostname);
3025 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003026 if (state && !(state->state & FMODE_WRITE)) {
3027 err = -EBADF;
3028 if (sattr->ia_valid & ATTR_OPEN)
3029 err = -EACCES;
3030 goto out;
3031 }
3032 }
3033 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return err;
3037}
3038
Peng Tao500d7012015-09-22 11:35:22 +08003039static bool
3040nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3041{
3042 if (inode == NULL || !nfs_have_layout(inode))
3043 return false;
3044
3045 return pnfs_wait_on_layoutreturn(inode, task);
3046}
3047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048struct nfs4_closedata {
3049 struct inode *inode;
3050 struct nfs4_state *state;
3051 struct nfs_closeargs arg;
3052 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003053 struct {
3054 struct nfs4_layoutreturn_args arg;
3055 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003056 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003057 u32 roc_barrier;
3058 bool roc;
3059 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003060 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003061 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062};
3063
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003064static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003065{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003066 struct nfs4_closedata *calldata = data;
3067 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003068 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003069
Trond Myklebustcf805162016-11-15 14:56:07 -05003070 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003071 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3072 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003073 nfs4_put_open_state(calldata->state);
3074 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003075 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003076 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003077 kfree(calldata);
3078}
3079
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003080static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003082 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003085 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
Chuck Levera3ca5652012-03-01 17:00:40 -05003087 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003088 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3089 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003090 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003091
3092 /* Handle Layoutreturn errors */
3093 if (calldata->arg.lr_args && task->tk_status != 0) {
3094 switch (calldata->res.lr_ret) {
3095 default:
3096 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3097 break;
3098 case 0:
3099 calldata->arg.lr_args = NULL;
3100 calldata->res.lr_res = NULL;
3101 break;
3102 case -NFS4ERR_ADMIN_REVOKED:
3103 case -NFS4ERR_DELEG_REVOKED:
3104 case -NFS4ERR_EXPIRED:
3105 case -NFS4ERR_BAD_STATEID:
3106 case -NFS4ERR_OLD_STATEID:
3107 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3108 case -NFS4ERR_WRONG_CRED:
3109 calldata->arg.lr_args = NULL;
3110 calldata->res.lr_res = NULL;
3111 calldata->res.lr_ret = 0;
3112 rpc_restart_call_prepare(task);
3113 return;
3114 }
3115 }
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* hmm. we are done with the inode, and in the process of freeing
3118 * the state_owner. we keep this around to process errors
3119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 switch (task->tk_status) {
3121 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003122 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003123 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003124 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003125 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003127 case -NFS4ERR_EXPIRED:
3128 nfs4_free_revoked_stateid(server,
3129 &calldata->arg.stateid,
3130 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003131 case -NFS4ERR_OLD_STATEID:
3132 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003133 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003134 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003135 rpc_restart_call_prepare(task);
3136 goto out_release;
3137 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003138 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003141 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003142 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003143 goto out_release;
3144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003146 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3147 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003148out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003149 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003150 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003151 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003154static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003156 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003157 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003158 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003159 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003160 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003161
Chuck Levera3ca5652012-03-01 17:00:40 -05003162 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003163 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003164 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003165
Trond Myklebust88069f72009-12-08 08:33:16 -05003166 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003167 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003168 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3169 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3170 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003171 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003172 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003173 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003174 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003175 if (state->n_rdonly == 0)
3176 call_close |= is_rdonly;
3177 else if (is_rdonly)
3178 calldata->arg.fmode |= FMODE_READ;
3179 if (state->n_wronly == 0)
3180 call_close |= is_wronly;
3181 else if (is_wronly)
3182 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003183 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3184 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003185 } else if (is_rdwr)
3186 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3187
Trond Myklebust5cc78612016-11-14 11:19:56 -05003188 if (!nfs4_valid_open_stateid(state) ||
3189 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003190 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003191 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003192
3193 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003194 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003195 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003196 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003197
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003198 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003199 nfs_release_seqid(calldata->arg.seqid);
3200 goto out_wait;
3201 }
3202
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003203 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003204 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003205
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003206 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003207 /* Close-to-open cache consistency revalidation */
3208 if (!nfs4_have_delegation(inode, FMODE_READ))
3209 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3210 else
3211 calldata->arg.bitmask = NULL;
3212 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003213
Trond Myklebust6ae37332015-01-30 14:21:14 -05003214 calldata->arg.share_access =
3215 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3216 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003217
Trond Myklebust516a6af2005-10-27 22:12:41 -04003218 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003219 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003220 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003221 &calldata->arg.seq_args,
3222 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003223 task) != 0)
3224 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003225 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003226 return;
3227out_no_action:
3228 task->tk_action = NULL;
3229out_wait:
3230 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003233static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003234 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003235 .rpc_call_done = nfs4_close_done,
3236 .rpc_release = nfs4_free_closedata,
3237};
3238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239/*
3240 * It is possible for data to be read/written from a mem-mapped file
3241 * after the sys_close call (which hits the vfs layer as a flush).
3242 * This means that we can't safely call nfsv4 close on a file until
3243 * the inode is cleared. This in turn means that we are not good
3244 * NFSv4 citizens - we do not indicate to the server to update the file's
3245 * share state even when we are done with one of the three share
3246 * stateid's in the inode.
3247 *
3248 * NOTE: Caller must be holding the sp->so_owner semaphore!
3249 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003250int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003252 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003253 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003255 struct nfs4_state_owner *sp = state->owner;
3256 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003257 struct rpc_message msg = {
3258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3259 .rpc_cred = state->owner->so_cred,
3260 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003261 struct rpc_task_setup task_setup_data = {
3262 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003263 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003264 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003265 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003266 .flags = RPC_TASK_ASYNC,
3267 };
Trond Myklebust95121352005-10-18 14:20:12 -07003268 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003270 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3271 &task_setup_data.rpc_client, &msg);
3272
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003273 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003275 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003276 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003277 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003279 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003281 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3282 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003283 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003284 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003285 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003286 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003287 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003288 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003289 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003290 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003291 calldata->lr.roc = pnfs_roc(state->inode,
3292 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3293 if (calldata->lr.roc) {
3294 calldata->arg.lr_args = &calldata->lr.arg;
3295 calldata->res.lr_res = &calldata->lr.res;
3296 }
Al Viro643168c2011-06-22 18:20:23 -04003297 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003298
Trond Myklebust1174dd12010-12-21 10:52:24 -05003299 msg.rpc_argp = &calldata->arg;
3300 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003301 task_setup_data.callback_data = calldata;
3302 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003303 if (IS_ERR(task))
3304 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003305 status = 0;
3306 if (wait)
3307 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003308 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003309 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003310out_free_calldata:
3311 kfree(calldata);
3312out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003313 nfs4_put_open_state(state);
3314 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003315 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Trond Myklebust2b484292010-09-17 10:56:51 -04003318static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003319nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3320 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003323 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3324
3325 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Trond Myklebust565277f2007-10-15 18:17:53 -04003327 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003328 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003329
3330 nfs4_label_release_security(label);
3331
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003332 if (IS_ERR(state))
3333 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003334 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Trond Myklebust1185a552009-12-03 15:54:02 -05003337static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003338{
3339 if (ctx->state == NULL)
3340 return;
3341 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003342 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003343 else
Al Viro643168c2011-06-22 18:20:23 -04003344 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003345}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Trond Myklebustb944dba2013-11-04 15:20:20 -05003347#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3348#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003349#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3352{
Kinglong Mee8c612822015-08-26 21:12:58 +08003353 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003354 struct nfs4_server_caps_arg args = {
3355 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003356 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003357 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 struct nfs4_server_caps_res res = {};
3359 struct rpc_message msg = {
3360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003361 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 .rpc_resp = &res,
3363 };
3364 int status;
3365
Kinglong Mee8c612822015-08-26 21:12:58 +08003366 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3367 FATTR4_WORD0_FH_EXPIRE_TYPE |
3368 FATTR4_WORD0_LINK_SUPPORT |
3369 FATTR4_WORD0_SYMLINK_SUPPORT |
3370 FATTR4_WORD0_ACLSUPPORT;
3371 if (minorversion)
3372 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3373
Bryan Schumaker7c513052011-03-24 17:12:24 +00003374 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003376 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003377 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003378 case 0:
3379 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3380 res.attr_bitmask[2] = 0;
3381 break;
3382 case 1:
3383 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3384 break;
3385 case 2:
3386 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003389 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3390 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3391 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3392 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003393 NFS_CAP_CTIME|NFS_CAP_MTIME|
3394 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003395 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3396 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 server->caps |= NFS_CAP_ACLS;
3398 if (res.has_links != 0)
3399 server->caps |= NFS_CAP_HARDLINKS;
3400 if (res.has_symlinks != 0)
3401 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003402 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3403 server->caps |= NFS_CAP_FILEID;
3404 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3405 server->caps |= NFS_CAP_MODE;
3406 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3407 server->caps |= NFS_CAP_NLINK;
3408 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3409 server->caps |= NFS_CAP_OWNER;
3410 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3411 server->caps |= NFS_CAP_OWNER_GROUP;
3412 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3413 server->caps |= NFS_CAP_ATIME;
3414 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3415 server->caps |= NFS_CAP_CTIME;
3416 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3417 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003418#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3419 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3420 server->caps |= NFS_CAP_SECURITY_LABEL;
3421#endif
3422 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3423 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003424 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003425
Trond Myklebusta65318b2009-03-11 14:10:28 -04003426 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3427 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3428 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003429 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003430 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3431 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003433 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 return status;
3437}
3438
Trond Myklebust55a97592006-06-09 09:34:19 -04003439int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440{
3441 struct nfs4_exception exception = { };
3442 int err;
3443 do {
3444 err = nfs4_handle_exception(server,
3445 _nfs4_server_capabilities(server, fhandle),
3446 &exception);
3447 } while (exception.retry);
3448 return err;
3449}
3450
3451static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3452 struct nfs_fsinfo *info)
3453{
David Quigleyaa9c2662013-05-22 12:50:44 -04003454 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003456 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 };
3458 struct nfs4_lookup_res res = {
3459 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003460 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 .fh = fhandle,
3462 };
3463 struct rpc_message msg = {
3464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3465 .rpc_argp = &args,
3466 .rpc_resp = &res,
3467 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003468
David Quigleyaa9c2662013-05-22 12:50:44 -04003469 bitmask[0] = nfs4_fattr_bitmap[0];
3470 bitmask[1] = nfs4_fattr_bitmap[1];
3471 /*
3472 * Process the label in the upcoming getfattr
3473 */
3474 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3475
Trond Myklebust0e574af2005-10-27 22:12:38 -04003476 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003477 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478}
3479
3480static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3481 struct nfs_fsinfo *info)
3482{
3483 struct nfs4_exception exception = { };
3484 int err;
3485 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003486 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003487 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003488 switch (err) {
3489 case 0:
3490 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003491 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003492 default:
3493 err = nfs4_handle_exception(server, err, &exception);
3494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003496out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return err;
3498}
3499
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003500static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3501 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3502{
Trond Myklebustc2190662013-08-26 19:23:04 -04003503 struct rpc_auth_create_args auth_args = {
3504 .pseudoflavor = flavor,
3505 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003506 struct rpc_auth *auth;
3507 int ret;
3508
Trond Myklebustc2190662013-08-26 19:23:04 -04003509 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003510 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003511 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003512 goto out;
3513 }
3514 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003515out:
3516 return ret;
3517}
3518
Chuck Lever9a744ba2013-03-16 15:56:02 -04003519/*
3520 * Retry pseudoroot lookup with various security flavors. We do this when:
3521 *
3522 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3523 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3524 *
3525 * Returns zero on success, or a negative NFS4ERR value, or a
3526 * negative errno value.
3527 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003528static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003529 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003531 /* Per 3530bis 15.33.5 */
3532 static const rpc_authflavor_t flav_array[] = {
3533 RPC_AUTH_GSS_KRB5P,
3534 RPC_AUTH_GSS_KRB5I,
3535 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003536 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003537 RPC_AUTH_NULL,
3538 };
3539 int status = -EPERM;
3540 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003542 if (server->auth_info.flavor_len > 0) {
3543 /* try each flavor specified by user */
3544 for (i = 0; i < server->auth_info.flavor_len; i++) {
3545 status = nfs4_lookup_root_sec(server, fhandle, info,
3546 server->auth_info.flavors[i]);
3547 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3548 continue;
3549 break;
3550 }
3551 } else {
3552 /* no flavors specified by user, try default list */
3553 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3554 status = nfs4_lookup_root_sec(server, fhandle, info,
3555 flav_array[i]);
3556 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3557 continue;
3558 break;
3559 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003560 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003561
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003562 /*
3563 * -EACCESS could mean that the user doesn't have correct permissions
3564 * to access the mount. It could also mean that we tried to mount
3565 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3566 * existing mount programs don't handle -EACCES very well so it should
3567 * be mapped to -EPERM instead.
3568 */
3569 if (status == -EACCES)
3570 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003571 return status;
3572}
3573
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003574/**
3575 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3576 * @server: initialized nfs_server handle
3577 * @fhandle: we fill in the pseudo-fs root file handle
3578 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003579 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003580 *
3581 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003582 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003583int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003584 struct nfs_fsinfo *info,
3585 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003586{
Andre Przywarac7757072015-04-23 17:17:40 +01003587 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003588
Andre Przywarac7757072015-04-23 17:17:40 +01003589 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003590 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003591
3592 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003593 status = server->nfs_client->cl_mvops->find_root_sec(server,
3594 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003595
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 if (status == 0)
3597 status = nfs4_server_capabilities(server, fhandle);
3598 if (status == 0)
3599 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003600
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003601 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Bryan Schumakerbae36242012-05-10 15:07:31 -04003604static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3605 struct nfs_fsinfo *info)
3606{
3607 int error;
3608 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003609 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003610
3611 error = nfs4_server_capabilities(server, mntfh);
3612 if (error < 0) {
3613 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3614 return error;
3615 }
3616
David Quigley14c43f72013-05-22 12:50:43 -04003617 label = nfs4_label_alloc(server, GFP_KERNEL);
3618 if (IS_ERR(label))
3619 return PTR_ERR(label);
3620
David Quigley1775fd32013-05-22 12:50:42 -04003621 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003622 if (error < 0) {
3623 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003624 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003625 }
3626
3627 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3628 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3629 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3630
David Quigley14c43f72013-05-22 12:50:43 -04003631err_free_label:
3632 nfs4_label_free(label);
3633
Bryan Schumakerbae36242012-05-10 15:07:31 -04003634 return error;
3635}
3636
Manoj Naik6b97fd32006-06-09 09:34:29 -04003637/*
3638 * Get locations and (maybe) other attributes of a referral.
3639 * Note that we'll actually follow the referral later when
3640 * we detect fsid mismatch in inode revalidation
3641 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003642static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3643 const struct qstr *name, struct nfs_fattr *fattr,
3644 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003645{
3646 int status = -ENOMEM;
3647 struct page *page = NULL;
3648 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003649
3650 page = alloc_page(GFP_KERNEL);
3651 if (page == NULL)
3652 goto out;
3653 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3654 if (locations == NULL)
3655 goto out;
3656
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003657 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003658 if (status != 0)
3659 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003660
3661 /*
3662 * If the fsid didn't change, this is a migration event, not a
3663 * referral. Cause us to drop into the exception handler, which
3664 * will kick off migration recovery.
3665 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003666 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003667 dprintk("%s: server did not return a different fsid for"
3668 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003669 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003670 goto out;
3671 }
Andy Adamson533eb462011-06-13 18:25:56 -04003672 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3673 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003674
Andy Adamson533eb462011-06-13 18:25:56 -04003675 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003676 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003677 memset(fhandle, 0, sizeof(struct nfs_fh));
3678out:
3679 if (page)
3680 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003681 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003682 return status;
3683}
3684
David Quigley1775fd32013-05-22 12:50:42 -04003685static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3686 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
3688 struct nfs4_getattr_arg args = {
3689 .fh = fhandle,
3690 .bitmask = server->attr_bitmask,
3691 };
3692 struct nfs4_getattr_res res = {
3693 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003694 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 .server = server,
3696 };
3697 struct rpc_message msg = {
3698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3699 .rpc_argp = &args,
3700 .rpc_resp = &res,
3701 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003702
3703 args.bitmask = nfs4_bitmask(server, label);
3704
Trond Myklebust0e574af2005-10-27 22:12:38 -04003705 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003706 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
David Quigley1775fd32013-05-22 12:50:42 -04003709static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3710 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct nfs4_exception exception = { };
3713 int err;
3714 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003715 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3716 trace_nfs4_getattr(server, fhandle, fattr, err);
3717 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 &exception);
3719 } while (exception.retry);
3720 return err;
3721}
3722
3723/*
3724 * The file is not closed if it is opened due to the a request to change
3725 * the size of the file. The open call will not be needed once the
3726 * VFS layer lookup-intents are implemented.
3727 *
3728 * Close is called when the inode is destroyed.
3729 * If we haven't opened the file for O_WRONLY, we
3730 * need to in the size_change case to obtain a stateid.
3731 *
3732 * Got race?
3733 * Because OPEN is always done by name in nfsv4, it is
3734 * possible that we opened a different file by the same
3735 * name. We can recognize this race condition, but we
3736 * can't do anything about it besides returning an error.
3737 *
3738 * This will be fixed with VFS changes (lookup-intent).
3739 */
3740static int
3741nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3742 struct iattr *sattr)
3743{
David Howells2b0143b2015-03-17 22:25:59 +00003744 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003745 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003746 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003747 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 int status;
3749
Peng Tao88ac8152014-09-12 11:04:10 +08003750 if (pnfs_ld_layoutret_on_setattr(inode) &&
3751 sattr->ia_valid & ATTR_SIZE &&
3752 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003753 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003754
Trond Myklebust0e574af2005-10-27 22:12:38 -04003755 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
Andy Adamson26699402012-05-30 16:12:24 -04003757 /* Deal with open(O_TRUNC) */
3758 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003759 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003760
3761 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003762 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003763 return 0;
3764
Trond Myklebustd5308382005-11-04 15:33:38 -05003765 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003766 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003767
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003768 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003769 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003770 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003771 }
3772
David Quigley14c43f72013-05-22 12:50:43 -04003773 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3774 if (IS_ERR(label))
3775 return PTR_ERR(label);
3776
NeilBrown29b59f92016-10-13 15:26:47 +11003777 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003778 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003779 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003780 nfs_setsecurity(inode, fattr, label);
3781 }
David Quigley14c43f72013-05-22 12:50:43 -04003782 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 return status;
3784}
3785
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003786static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3787 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003788 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003789{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003790 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003791 int status;
3792 struct nfs4_lookup_arg args = {
3793 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003794 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003795 .name = name,
3796 };
3797 struct nfs4_lookup_res res = {
3798 .server = server,
3799 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003800 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003801 .fh = fhandle,
3802 };
3803 struct rpc_message msg = {
3804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3805 .rpc_argp = &args,
3806 .rpc_resp = &res,
3807 };
3808
David Quigleyaa9c2662013-05-22 12:50:44 -04003809 args.bitmask = nfs4_bitmask(server, label);
3810
David Howells2b3de442006-08-22 20:06:09 -04003811 nfs_fattr_init(fattr);
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003814 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 dprintk("NFS reply lookup: %d\n", status);
3816 return status;
3817}
3818
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003819static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003820{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003821 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003822 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003823 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3824 fattr->nlink = 2;
3825}
3826
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003827static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003828 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003829 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003830{
3831 struct nfs4_exception exception = { };
3832 struct rpc_clnt *client = *clnt;
3833 int err;
3834 do {
David Quigley1775fd32013-05-22 12:50:42 -04003835 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003836 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837 switch (err) {
3838 case -NFS4ERR_BADNAME:
3839 err = -ENOENT;
3840 goto out;
3841 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003842 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003843 if (err == -NFS4ERR_MOVED)
3844 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003845 goto out;
3846 case -NFS4ERR_WRONGSEC:
3847 err = -EPERM;
3848 if (client != *clnt)
3849 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003850 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003851 if (IS_ERR(client))
3852 return PTR_ERR(client);
3853
3854 exception.retry = 1;
3855 break;
3856 default:
3857 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3858 }
3859 } while (exception.retry);
3860
3861out:
3862 if (err == 0)
3863 *clnt = client;
3864 else if (client != *clnt)
3865 rpc_shutdown_client(client);
3866
3867 return err;
3868}
3869
Al Virobeffb8f2016-07-20 16:34:42 -04003870static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003871 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3872 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003874 int status;
3875 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003876
David Quigley1775fd32013-05-22 12:50:42 -04003877 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003878 if (client != NFS_CLIENT(dir)) {
3879 rpc_shutdown_client(client);
3880 nfs_fixup_secinfo_attributes(fattr);
3881 }
3882 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003885struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003886nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003887 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3888{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003889 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003890 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003891
David Quigley1775fd32013-05-22 12:50:42 -04003892 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003893 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003894 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003895 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003896}
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3899{
Trond Myklebust76b32992007-08-10 17:45:11 -04003900 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 struct nfs4_accessargs args = {
3902 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003903 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003905 struct nfs4_accessres res = {
3906 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003907 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 struct rpc_message msg = {
3909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3910 .rpc_argp = &args,
3911 .rpc_resp = &res,
3912 .rpc_cred = entry->cred,
3913 };
3914 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003915 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
3917 /*
3918 * Determine which access bits we want to ask for...
3919 */
3920 if (mode & MAY_READ)
3921 args.access |= NFS4_ACCESS_READ;
3922 if (S_ISDIR(inode->i_mode)) {
3923 if (mode & MAY_WRITE)
3924 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3925 if (mode & MAY_EXEC)
3926 args.access |= NFS4_ACCESS_LOOKUP;
3927 } else {
3928 if (mode & MAY_WRITE)
3929 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3930 if (mode & MAY_EXEC)
3931 args.access |= NFS4_ACCESS_EXECUTE;
3932 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003933
3934 res.fattr = nfs_alloc_fattr();
3935 if (res.fattr == NULL)
3936 return -ENOMEM;
3937
Bryan Schumaker7c513052011-03-24 17:12:24 +00003938 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003940 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003941 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003943 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return status;
3945}
3946
3947static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3948{
3949 struct nfs4_exception exception = { };
3950 int err;
3951 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003952 err = _nfs4_proc_access(inode, entry);
3953 trace_nfs4_access(inode, err);
3954 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 &exception);
3956 } while (exception.retry);
3957 return err;
3958}
3959
3960/*
3961 * TODO: For the time being, we don't try to get any attributes
3962 * along with any of the zero-copy operations READ, READDIR,
3963 * READLINK, WRITE.
3964 *
3965 * In the case of the first three, we want to put the GETATTR
3966 * after the read-type operation -- this is because it is hard
3967 * to predict the length of a GETATTR response in v4, and thus
3968 * align the READ data correctly. This means that the GETATTR
3969 * may end up partially falling into the page cache, and we should
3970 * shift it into the 'tail' of the xdr_buf before processing.
3971 * To do this efficiently, we need to know the total length
3972 * of data received, which doesn't seem to be available outside
3973 * of the RPC layer.
3974 *
3975 * In the case of WRITE, we also want to put the GETATTR after
3976 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003977 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 *
3979 * Both of these changes to the XDR layer would in fact be quite
3980 * minor, but I decided to leave them for a subsequent patch.
3981 */
3982static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3983 unsigned int pgbase, unsigned int pglen)
3984{
3985 struct nfs4_readlink args = {
3986 .fh = NFS_FH(inode),
3987 .pgbase = pgbase,
3988 .pglen = pglen,
3989 .pages = &page,
3990 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003991 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct rpc_message msg = {
3993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3994 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003995 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 };
3997
Bryan Schumaker7c513052011-03-24 17:12:24 +00003998 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 -07003999}
4000
4001static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4002 unsigned int pgbase, unsigned int pglen)
4003{
4004 struct nfs4_exception exception = { };
4005 int err;
4006 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004007 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4008 trace_nfs4_readlink(inode, err);
4009 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 &exception);
4011 } while (exception.retry);
4012 return err;
4013}
4014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018static int
4019nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004020 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004022 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004023 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004024 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 int status = 0;
4027
NeilBrown532d4de2016-10-13 15:26:47 +11004028 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004029 if (IS_ERR(ctx))
4030 return PTR_ERR(ctx);
4031
David Quigleyaa9c2662013-05-22 12:50:44 -04004032 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4033
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004034 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4035 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004036 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (IS_ERR(state)) {
4038 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004039 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004042 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004043 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return status;
4045}
4046
Al Virobeffb8f2016-07-20 16:34:42 -04004047static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
Trond Myklebust16e42952005-10-27 22:12:44 -04004049 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004050 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004052 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004054 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004055 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004056 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4059 .rpc_argp = &args,
4060 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004062 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004063
Bryan Schumaker7c513052011-03-24 17:12:24 +00004064 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004065 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004066 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 return status;
4068}
4069
Al Virobeffb8f2016-07-20 16:34:42 -04004070static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct nfs4_exception exception = { };
4073 int err;
4074 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004075 err = _nfs4_proc_remove(dir, name);
4076 trace_nfs4_remove(dir, name, err);
4077 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 &exception);
4079 } while (exception.retry);
4080 return err;
4081}
4082
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004083static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004085 struct nfs_server *server = NFS_SERVER(dir);
4086 struct nfs_removeargs *args = msg->rpc_argp;
4087 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004089 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004091 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004092
4093 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
4095
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004096static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4097{
Al Viro884be172016-04-28 23:56:31 -04004098 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004099 &data->args.seq_args,
4100 &data->res.seq_res,
4101 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004104static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004106 struct nfs_unlinkdata *data = task->tk_calldata;
4107 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004108
Trond Myklebust14516c32010-07-31 14:29:06 -04004109 if (!nfs4_sequence_done(task, &res->seq_res))
4110 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004111 if (nfs4_async_handle_error(task, res->server, NULL,
4112 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004113 return 0;
4114 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Jeff Laytond3d41522010-09-17 17:31:57 -04004118static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4119{
4120 struct nfs_server *server = NFS_SERVER(dir);
4121 struct nfs_renameargs *arg = msg->rpc_argp;
4122 struct nfs_renameres *res = msg->rpc_resp;
4123
4124 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004125 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004126 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004127}
4128
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004129static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4130{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004131 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4132 &data->args.seq_args,
4133 &data->res.seq_res,
4134 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004135}
4136
4137static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4138 struct inode *new_dir)
4139{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004140 struct nfs_renamedata *data = task->tk_calldata;
4141 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004142
4143 if (!nfs4_sequence_done(task, &res->seq_res))
4144 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004145 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004146 return 0;
4147
4148 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004149 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004150 return 1;
4151}
4152
Al Virobeffb8f2016-07-20 16:34:42 -04004153static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004155 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 struct nfs4_link_arg arg = {
4157 .fh = NFS_FH(inode),
4158 .dir_fh = NFS_FH(dir),
4159 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004160 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004162 struct nfs4_link_res res = {
4163 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004164 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004165 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 struct rpc_message msg = {
4167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4168 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004169 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004171 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Trond Myklebust136f2622010-04-16 16:22:49 -04004173 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004174 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004175 goto out;
4176
David Quigley14c43f72013-05-22 12:50:43 -04004177 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4178 if (IS_ERR(res.label)) {
4179 status = PTR_ERR(res.label);
4180 goto out;
4181 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004182 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004183
Bryan Schumaker7c513052011-03-24 17:12:24 +00004184 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004185 if (!status) {
4186 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004187 status = nfs_post_op_update_inode(inode, res.fattr);
4188 if (!status)
4189 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004190 }
David Quigley14c43f72013-05-22 12:50:43 -04004191
4192
4193 nfs4_label_free(res.label);
4194
Trond Myklebust136f2622010-04-16 16:22:49 -04004195out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004196 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 return status;
4198}
4199
Al Virobeffb8f2016-07-20 16:34:42 -04004200static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
4202 struct nfs4_exception exception = { };
4203 int err;
4204 do {
4205 err = nfs4_handle_exception(NFS_SERVER(inode),
4206 _nfs4_proc_link(inode, dir, name),
4207 &exception);
4208 } while (exception.retry);
4209 return err;
4210}
4211
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212struct nfs4_createdata {
4213 struct rpc_message msg;
4214 struct nfs4_create_arg arg;
4215 struct nfs4_create_res res;
4216 struct nfs_fh fh;
4217 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004218 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004219};
4220
4221static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004222 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223{
4224 struct nfs4_createdata *data;
4225
4226 data = kzalloc(sizeof(*data), GFP_KERNEL);
4227 if (data != NULL) {
4228 struct nfs_server *server = NFS_SERVER(dir);
4229
David Quigley14c43f72013-05-22 12:50:43 -04004230 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4231 if (IS_ERR(data->label))
4232 goto out_free;
4233
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4235 data->msg.rpc_argp = &data->arg;
4236 data->msg.rpc_resp = &data->res;
4237 data->arg.dir_fh = NFS_FH(dir);
4238 data->arg.server = server;
4239 data->arg.name = name;
4240 data->arg.attrs = sattr;
4241 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004242 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004243 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004244 data->res.server = server;
4245 data->res.fh = &data->fh;
4246 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004247 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004248 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 }
4250 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004251out_free:
4252 kfree(data);
4253 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004254}
4255
4256static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4257{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004258 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004259 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004260 if (status == 0) {
4261 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004262 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004263 }
4264 return status;
4265}
4266
4267static void nfs4_free_createdata(struct nfs4_createdata *data)
4268{
David Quigley14c43f72013-05-22 12:50:43 -04004269 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004270 kfree(data);
4271}
4272
Chuck Lever4f390c12006-08-22 20:06:22 -04004273static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004274 struct page *page, unsigned int len, struct iattr *sattr,
4275 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004277 struct nfs4_createdata *data;
4278 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Chuck Lever94a6d752006-08-22 20:06:23 -04004280 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004281 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004282
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004283 status = -ENOMEM;
4284 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4285 if (data == NULL)
4286 goto out;
4287
4288 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4289 data->arg.u.symlink.pages = &page;
4290 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004291 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004293 status = nfs4_do_create(dir, dentry, data);
4294
4295 nfs4_free_createdata(data);
4296out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 return status;
4298}
4299
Chuck Lever4f390c12006-08-22 20:06:22 -04004300static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004301 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
4303 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004304 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004306
4307 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4308
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004310 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4311 trace_nfs4_symlink(dir, &dentry->d_name, err);
4312 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 &exception);
4314 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004315
4316 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return err;
4318}
4319
4320static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004321 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323 struct nfs4_createdata *data;
4324 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004326 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4327 if (data == NULL)
4328 goto out;
4329
David Quigley1775fd32013-05-22 12:50:42 -04004330 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004331 status = nfs4_do_create(dir, dentry, data);
4332
4333 nfs4_free_createdata(data);
4334out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return status;
4336}
4337
4338static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4339 struct iattr *sattr)
4340{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004341 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004343 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004345
David Quigleyaa9c2662013-05-22 12:50:44 -04004346 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4347
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004348 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4349 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004351 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4352 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4353 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 &exception);
4355 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004356 nfs4_label_release_security(label);
4357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 return err;
4359}
4360
4361static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004362 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363{
David Howells2b0143b2015-03-17 22:25:59 +00004364 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 struct nfs4_readdir_arg args = {
4366 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004367 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 .pgbase = 0,
4369 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004370 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004371 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 };
4373 struct nfs4_readdir_res res;
4374 struct rpc_message msg = {
4375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4376 .rpc_argp = &args,
4377 .rpc_resp = &res,
4378 .rpc_cred = cred,
4379 };
4380 int status;
4381
Al Viro6de14722013-09-16 10:53:17 -04004382 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4383 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004384 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004385 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004387 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 -05004388 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004389 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004390 status += args.pgbase;
4391 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004392
4393 nfs_invalidate_atime(dir);
4394
Harvey Harrison3110ff82008-05-02 13:42:44 -07004395 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return status;
4397}
4398
4399static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004400 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401{
4402 struct nfs4_exception exception = { };
4403 int err;
4404 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004405 err = _nfs4_proc_readdir(dentry, cred, cookie,
4406 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004407 trace_nfs4_readdir(d_inode(dentry), err);
4408 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 &exception);
4410 } while (exception.retry);
4411 return err;
4412}
4413
4414static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004415 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004417 struct nfs4_createdata *data;
4418 int mode = sattr->ia_mode;
4419 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4422 if (data == NULL)
4423 goto out;
4424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004428 data->arg.ftype = NF4BLK;
4429 data->arg.u.device.specdata1 = MAJOR(rdev);
4430 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004433 data->arg.ftype = NF4CHR;
4434 data->arg.u.device.specdata1 = MAJOR(rdev);
4435 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004436 } else if (!S_ISSOCK(mode)) {
4437 status = -EINVAL;
4438 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 }
David Quigley1775fd32013-05-22 12:50:42 -04004440
David Quigleyaa9c2662013-05-22 12:50:44 -04004441 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004443out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004444 nfs4_free_createdata(data);
4445out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 return status;
4447}
4448
4449static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4450 struct iattr *sattr, dev_t rdev)
4451{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004452 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004454 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004456
David Quigleyaa9c2662013-05-22 12:50:44 -04004457 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4458
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004459 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4460 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004462 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4463 trace_nfs4_mknod(dir, &dentry->d_name, err);
4464 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 &exception);
4466 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004467
4468 nfs4_label_release_security(label);
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 return err;
4471}
4472
4473static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4474 struct nfs_fsstat *fsstat)
4475{
4476 struct nfs4_statfs_arg args = {
4477 .fh = fhandle,
4478 .bitmask = server->attr_bitmask,
4479 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004480 struct nfs4_statfs_res res = {
4481 .fsstat = fsstat,
4482 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 struct rpc_message msg = {
4484 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4485 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004486 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 };
4488
Trond Myklebust0e574af2005-10-27 22:12:38 -04004489 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004490 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
4493static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4494{
4495 struct nfs4_exception exception = { };
4496 int err;
4497 do {
4498 err = nfs4_handle_exception(server,
4499 _nfs4_proc_statfs(server, fhandle, fsstat),
4500 &exception);
4501 } while (exception.retry);
4502 return err;
4503}
4504
4505static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4506 struct nfs_fsinfo *fsinfo)
4507{
4508 struct nfs4_fsinfo_arg args = {
4509 .fh = fhandle,
4510 .bitmask = server->attr_bitmask,
4511 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004512 struct nfs4_fsinfo_res res = {
4513 .fsinfo = fsinfo,
4514 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 struct rpc_message msg = {
4516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4517 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004518 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 };
4520
Bryan Schumaker7c513052011-03-24 17:12:24 +00004521 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522}
4523
4524static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4525{
4526 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004527 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 int err;
4529
4530 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004531 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004532 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004533 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004534 nfs4_set_lease_period(server->nfs_client,
4535 fsinfo->lease_time * HZ,
4536 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004537 break;
4538 }
4539 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 } while (exception.retry);
4541 return err;
4542}
4543
4544static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4545{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004546 int error;
4547
Trond Myklebust0e574af2005-10-27 22:12:38 -04004548 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004549 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004550 if (error == 0) {
4551 /* block layout checks this! */
4552 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004553 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004554 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004555
4556 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
4559static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4560 struct nfs_pathconf *pathconf)
4561{
4562 struct nfs4_pathconf_arg args = {
4563 .fh = fhandle,
4564 .bitmask = server->attr_bitmask,
4565 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004566 struct nfs4_pathconf_res res = {
4567 .pathconf = pathconf,
4568 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 struct rpc_message msg = {
4570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4571 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004572 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 };
4574
4575 /* None of the pathconf attributes are mandatory to implement */
4576 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4577 memset(pathconf, 0, sizeof(*pathconf));
4578 return 0;
4579 }
4580
Trond Myklebust0e574af2005-10-27 22:12:38 -04004581 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004582 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583}
4584
4585static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4586 struct nfs_pathconf *pathconf)
4587{
4588 struct nfs4_exception exception = { };
4589 int err;
4590
4591 do {
4592 err = nfs4_handle_exception(server,
4593 _nfs4_proc_pathconf(server, fhandle, pathconf),
4594 &exception);
4595 } while (exception.retry);
4596 return err;
4597}
4598
Trond Myklebust5521abf2013-03-16 20:54:34 -04004599int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004600 const struct nfs_open_context *ctx,
4601 const struct nfs_lock_context *l_ctx,
4602 fmode_t fmode)
4603{
NeilBrown17393472016-10-13 15:26:47 +11004604 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004605}
4606EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4607
Trond Myklebust5521abf2013-03-16 20:54:34 -04004608static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4609 const struct nfs_open_context *ctx,
4610 const struct nfs_lock_context *l_ctx,
4611 fmode_t fmode)
4612{
4613 nfs4_stateid current_stateid;
4614
Trond Myklebuste1253be2014-03-05 08:44:23 -05004615 /* If the current stateid represents a lost lock, then exit */
4616 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4617 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004618 return nfs4_stateid_match(stateid, &current_stateid);
4619}
4620
4621static bool nfs4_error_stateid_expired(int err)
4622{
4623 switch (err) {
4624 case -NFS4ERR_DELEG_REVOKED:
4625 case -NFS4ERR_ADMIN_REVOKED:
4626 case -NFS4ERR_BAD_STATEID:
4627 case -NFS4ERR_STALE_STATEID:
4628 case -NFS4ERR_OLD_STATEID:
4629 case -NFS4ERR_OPENMODE:
4630 case -NFS4ERR_EXPIRED:
4631 return true;
4632 }
4633 return false;
4634}
4635
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004636static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004641 if (task->tk_status < 0) {
4642 struct nfs4_exception exception = {
4643 .inode = hdr->inode,
4644 .state = hdr->args.context->state,
4645 .stateid = &hdr->args.stateid,
4646 };
4647 task->tk_status = nfs4_async_handle_exception(task,
4648 server, task->tk_status, &exception);
4649 if (exception.retry) {
4650 rpc_restart_call_prepare(task);
4651 return -EAGAIN;
4652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004654
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004656 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658}
4659
Trond Myklebust5521abf2013-03-16 20:54:34 -04004660static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004661 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004662{
4663
4664 if (!nfs4_error_stateid_expired(task->tk_status) ||
4665 nfs4_stateid_is_current(&args->stateid,
4666 args->context,
4667 args->lock_context,
4668 FMODE_READ))
4669 return false;
4670 rpc_restart_call_prepare(task);
4671 return true;
4672}
4673
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004675{
4676
4677 dprintk("--> %s\n", __func__);
4678
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004680 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004682 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004683 if (task->tk_status > 0)
4684 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004685 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4686 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004687}
4688
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4690 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004692 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004693 if (!hdr->pgio_done_cb)
4694 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004695 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004696 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697}
4698
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4700 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004701{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004702 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4703 &hdr->args.seq_args,
4704 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004705 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004706 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004707 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4708 hdr->args.lock_context,
4709 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004710 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004711 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004712 return -EIO;
4713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714}
4715
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004716static int nfs4_write_done_cb(struct rpc_task *task,
4717 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004720
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004722 if (task->tk_status < 0) {
4723 struct nfs4_exception exception = {
4724 .inode = hdr->inode,
4725 .state = hdr->args.context->state,
4726 .stateid = &hdr->args.stateid,
4727 };
4728 task->tk_status = nfs4_async_handle_exception(task,
4729 NFS_SERVER(inode), task->tk_status,
4730 &exception);
4731 if (exception.retry) {
4732 rpc_restart_call_prepare(task);
4733 return -EAGAIN;
4734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004736 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004737 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004738 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004739 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004740 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741}
4742
Trond Myklebust5521abf2013-03-16 20:54:34 -04004743static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004744 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004745{
4746
4747 if (!nfs4_error_stateid_expired(task->tk_status) ||
4748 nfs4_stateid_is_current(&args->stateid,
4749 args->context,
4750 args->lock_context,
4751 FMODE_WRITE))
4752 return false;
4753 rpc_restart_call_prepare(task);
4754 return true;
4755}
4756
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004757static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004758{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004759 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004760 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004762 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004763 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4764 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004765}
4766
Trond Myklebust5a37f852012-04-28 14:55:16 -04004767static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004768bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004769{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004770 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004771 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004772 return false;
4773 /* Otherwise, request attributes if and only if we don't hold
4774 * a delegation
4775 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004776 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004777}
Fred Isamana69aef12011-03-03 15:13:47 +00004778
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004779static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4780 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004783
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4785 hdr->args.bitmask = NULL;
4786 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004787 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004788 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004789
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004790 if (!hdr->pgio_done_cb)
4791 hdr->pgio_done_cb = nfs4_write_done_cb;
4792 hdr->res.server = server;
4793 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004795 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004796 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
Fred Isaman0b7c0152012-04-20 14:47:39 -04004799static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4800{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004801 nfs4_setup_sequence(NFS_SERVER(data->inode),
4802 &data->args.seq_args,
4803 &data->res.seq_res,
4804 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Fred Isaman0b7c0152012-04-20 14:47:39 -04004807static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004810
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004811 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004812 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4813 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004814 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004815 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Fred Isaman0b7c0152012-04-20 14:47:39 -04004820static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004821{
4822 if (!nfs4_sequence_done(task, &data->res.seq_res))
4823 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004824 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004825}
4826
Fred Isaman0b7c0152012-04-20 14:47:39 -04004827static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004829 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004830
Fred Isaman0b7c0152012-04-20 14:47:39 -04004831 if (data->commit_done_cb == NULL)
4832 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004833 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004834 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004835 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004838struct nfs4_renewdata {
4839 struct nfs_client *client;
4840 unsigned long timestamp;
4841};
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843/*
4844 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4845 * standalone procedure for queueing an asynchronous RENEW.
4846 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004847static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004848{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004849 struct nfs4_renewdata *data = calldata;
4850 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004851
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004852 if (atomic_read(&clp->cl_count) > 1)
4853 nfs4_schedule_state_renewal(clp);
4854 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004855 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004856}
4857
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004858static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004860 struct nfs4_renewdata *data = calldata;
4861 struct nfs_client *clp = data->client;
4862 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004864 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004865 switch (task->tk_status) {
4866 case 0:
4867 break;
4868 case -NFS4ERR_LEASE_MOVED:
4869 nfs4_schedule_lease_moved_recovery(clp);
4870 break;
4871 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004872 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004873 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4874 return;
4875 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004876 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004877 return;
4878 }
4879 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004881 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004884static const struct rpc_call_ops nfs4_renew_ops = {
4885 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004886 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004887};
4888
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004889static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
4891 struct rpc_message msg = {
4892 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4893 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004894 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004896 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004898 if (renew_flags == 0)
4899 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004900 if (!atomic_inc_not_zero(&clp->cl_count))
4901 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004902 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004903 if (data == NULL)
4904 return -ENOMEM;
4905 data->client = clp;
4906 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004907 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004908 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004911static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
4913 struct rpc_message msg = {
4914 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4915 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004916 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 };
4918 unsigned long now = jiffies;
4919 int status;
4920
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004921 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (status < 0)
4923 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004924 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return 0;
4926}
4927
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004928static inline int nfs4_server_supports_acls(struct nfs_server *server)
4929{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004930 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004931}
4932
Trond Myklebust21f498c2012-08-24 10:59:25 -04004933/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4934 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004935 * the stack.
4936 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004937#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004938
Neil Hormane9e3d722011-03-04 19:26:03 -05004939static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004940 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004941{
4942 struct page *newpage, **spages;
4943 int rc = 0;
4944 size_t len;
4945 spages = pages;
4946
4947 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004948 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004949 newpage = alloc_page(GFP_KERNEL);
4950
4951 if (newpage == NULL)
4952 goto unwind;
4953 memcpy(page_address(newpage), buf, len);
4954 buf += len;
4955 buflen -= len;
4956 *pages++ = newpage;
4957 rc++;
4958 } while (buflen != 0);
4959
4960 return rc;
4961
4962unwind:
4963 for(; rc > 0; rc--)
4964 __free_page(spages[rc-1]);
4965 return -ENOMEM;
4966}
4967
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004968struct nfs4_cached_acl {
4969 int cached;
4970 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004971 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004972};
4973
4974static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004975{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004976 struct nfs_inode *nfsi = NFS_I(inode);
4977
4978 spin_lock(&inode->i_lock);
4979 kfree(nfsi->nfs4_acl);
4980 nfsi->nfs4_acl = acl;
4981 spin_unlock(&inode->i_lock);
4982}
4983
4984static void nfs4_zap_acl_attr(struct inode *inode)
4985{
4986 nfs4_set_cached_acl(inode, NULL);
4987}
4988
4989static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4990{
4991 struct nfs_inode *nfsi = NFS_I(inode);
4992 struct nfs4_cached_acl *acl;
4993 int ret = -ENOENT;
4994
4995 spin_lock(&inode->i_lock);
4996 acl = nfsi->nfs4_acl;
4997 if (acl == NULL)
4998 goto out;
4999 if (buf == NULL) /* user is just asking for length */
5000 goto out_len;
5001 if (acl->cached == 0)
5002 goto out;
5003 ret = -ERANGE; /* see getxattr(2) man page */
5004 if (acl->len > buflen)
5005 goto out;
5006 memcpy(buf, acl->data, acl->len);
5007out_len:
5008 ret = acl->len;
5009out:
5010 spin_unlock(&inode->i_lock);
5011 return ret;
5012}
5013
Sachin Prabhu5794d212012-04-17 14:36:40 +01005014static 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 +00005015{
5016 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005017 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005018
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005019 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005020 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021 if (acl == NULL)
5022 goto out;
5023 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005024 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005025 } else {
5026 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5027 if (acl == NULL)
5028 goto out;
5029 acl->cached = 0;
5030 }
5031 acl->len = acl_len;
5032out:
5033 nfs4_set_cached_acl(inode, acl);
5034}
5035
Andy Adamsonbf118a32011-12-07 11:55:27 -05005036/*
5037 * The getxattr API returns the required buffer length when called with a
5038 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5039 * the required buf. On a NULL buf, we send a page of data to the server
5040 * guessing that the ACL request can be serviced by a page. If so, we cache
5041 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5042 * the cache. If not so, we throw away the page, and cache the required
5043 * length. The next getxattr call will then produce another round trip to
5044 * the server, this time with the input buf of the required size.
5045 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005046static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005047{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005048 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005049 struct nfs_getaclargs args = {
5050 .fh = NFS_FH(inode),
5051 .acl_pages = pages,
5052 .acl_len = buflen,
5053 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005054 struct nfs_getaclres res = {
5055 .acl_len = buflen,
5056 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005057 struct rpc_message msg = {
5058 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5059 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005060 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005061 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005062 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5063 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005064
Andy Adamsonbf118a32011-12-07 11:55:27 -05005065 /* As long as we're doing a round trip to the server anyway,
5066 * let's be prepared for a page of acl data. */
5067 if (npages == 0)
5068 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005069 if (npages > ARRAY_SIZE(pages))
5070 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005071
Andy Adamsonbf118a32011-12-07 11:55:27 -05005072 for (i = 0; i < npages; i++) {
5073 pages[i] = alloc_page(GFP_KERNEL);
5074 if (!pages[i])
5075 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005076 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005077
5078 /* for decoding across pages */
5079 res.acl_scratch = alloc_page(GFP_KERNEL);
5080 if (!res.acl_scratch)
5081 goto out_free;
5082
Andy Adamsonbf118a32011-12-07 11:55:27 -05005083 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005084
Peng Taode040be2012-01-10 22:42:47 +08005085 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005086 __func__, buf, buflen, npages, args.acl_len);
5087 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5088 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 if (ret)
5090 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005091
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005092 /* Handle the case where the passed-in buffer is too short */
5093 if (res.acl_flags & NFS4_ACL_TRUNC) {
5094 /* Did the user only issue a request for the acl length? */
5095 if (buf == NULL)
5096 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005097 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005098 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005099 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005100 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005101 if (buf) {
5102 if (res.acl_len > buflen) {
5103 ret = -ERANGE;
5104 goto out_free;
5105 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005106 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005107 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005108out_ok:
5109 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005110out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005111 for (i = 0; i < npages; i++)
5112 if (pages[i])
5113 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005114 if (res.acl_scratch)
5115 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005116 return ret;
5117}
5118
Trond Myklebust16b42892006-08-24 12:27:15 -04005119static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5120{
5121 struct nfs4_exception exception = { };
5122 ssize_t ret;
5123 do {
5124 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005125 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005126 if (ret >= 0)
5127 break;
5128 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5129 } while (exception.retry);
5130 return ret;
5131}
5132
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005133static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5134{
5135 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005136 int ret;
5137
5138 if (!nfs4_server_supports_acls(server))
5139 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005140 ret = nfs_revalidate_inode(server, inode);
5141 if (ret < 0)
5142 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005143 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5144 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005145 ret = nfs4_read_cached_acl(inode, buf, buflen);
5146 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005147 /* -ENOENT is returned if there is no ACL or if there is an ACL
5148 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005149 return ret;
5150 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005151}
5152
Trond Myklebust16b42892006-08-24 12:27:15 -04005153static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005154{
5155 struct nfs_server *server = NFS_SERVER(inode);
5156 struct page *pages[NFS4ACL_MAXPAGES];
5157 struct nfs_setaclargs arg = {
5158 .fh = NFS_FH(inode),
5159 .acl_pages = pages,
5160 .acl_len = buflen,
5161 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005162 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005163 struct rpc_message msg = {
5164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5165 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005166 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005167 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005168 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005169 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005170
5171 if (!nfs4_server_supports_acls(server))
5172 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005173 if (npages > ARRAY_SIZE(pages))
5174 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005175 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005176 if (i < 0)
5177 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005178 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005179 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005180
5181 /*
5182 * Free each page after tx, so the only ref left is
5183 * held by the network stack
5184 */
5185 for (; i > 0; i--)
5186 put_page(pages[i-1]);
5187
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005188 /*
5189 * Acl update can result in inode attribute update.
5190 * so mark the attribute cache invalid.
5191 */
5192 spin_lock(&inode->i_lock);
5193 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5194 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005195 nfs_access_zap_cache(inode);
5196 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005197 return ret;
5198}
5199
Trond Myklebust16b42892006-08-24 12:27:15 -04005200static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5201{
5202 struct nfs4_exception exception = { };
5203 int err;
5204 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005205 err = __nfs4_proc_set_acl(inode, buf, buflen);
5206 trace_nfs4_set_acl(inode, err);
5207 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005208 &exception);
5209 } while (exception.retry);
5210 return err;
5211}
5212
David Quigleyaa9c2662013-05-22 12:50:44 -04005213#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5214static int _nfs4_get_security_label(struct inode *inode, void *buf,
5215 size_t buflen)
5216{
5217 struct nfs_server *server = NFS_SERVER(inode);
5218 struct nfs_fattr fattr;
5219 struct nfs4_label label = {0, 0, buflen, buf};
5220
5221 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005222 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 .fh = NFS_FH(inode),
5224 .bitmask = bitmask,
5225 };
5226 struct nfs4_getattr_res res = {
5227 .fattr = &fattr,
5228 .label = &label,
5229 .server = server,
5230 };
5231 struct rpc_message msg = {
5232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005233 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005234 .rpc_resp = &res,
5235 };
5236 int ret;
5237
5238 nfs_fattr_init(&fattr);
5239
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005240 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005241 if (ret)
5242 return ret;
5243 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5244 return -ENOENT;
5245 if (buflen < label.len)
5246 return -ERANGE;
5247 return 0;
5248}
5249
5250static int nfs4_get_security_label(struct inode *inode, void *buf,
5251 size_t buflen)
5252{
5253 struct nfs4_exception exception = { };
5254 int err;
5255
5256 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5257 return -EOPNOTSUPP;
5258
5259 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005260 err = _nfs4_get_security_label(inode, buf, buflen);
5261 trace_nfs4_get_security_label(inode, err);
5262 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005263 &exception);
5264 } while (exception.retry);
5265 return err;
5266}
5267
5268static int _nfs4_do_set_security_label(struct inode *inode,
5269 struct nfs4_label *ilabel,
5270 struct nfs_fattr *fattr,
5271 struct nfs4_label *olabel)
5272{
5273
5274 struct iattr sattr = {0};
5275 struct nfs_server *server = NFS_SERVER(inode);
5276 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005277 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005278 .fh = NFS_FH(inode),
5279 .iap = &sattr,
5280 .server = server,
5281 .bitmask = bitmask,
5282 .label = ilabel,
5283 };
5284 struct nfs_setattrres res = {
5285 .fattr = fattr,
5286 .label = olabel,
5287 .server = server,
5288 };
5289 struct rpc_message msg = {
5290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005291 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005292 .rpc_resp = &res,
5293 };
5294 int status;
5295
Jeff Layton12207f62013-11-01 10:49:32 -04005296 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005297
Jeff Layton12207f62013-11-01 10:49:32 -04005298 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005299 if (status)
5300 dprintk("%s failed: %d\n", __func__, status);
5301
5302 return status;
5303}
5304
5305static int nfs4_do_set_security_label(struct inode *inode,
5306 struct nfs4_label *ilabel,
5307 struct nfs_fattr *fattr,
5308 struct nfs4_label *olabel)
5309{
5310 struct nfs4_exception exception = { };
5311 int err;
5312
5313 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005314 err = _nfs4_do_set_security_label(inode, ilabel,
5315 fattr, olabel);
5316 trace_nfs4_set_security_label(inode, err);
5317 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005318 &exception);
5319 } while (exception.retry);
5320 return err;
5321}
5322
5323static int
Al Viro59301222016-05-27 10:19:30 -04005324nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005325{
5326 struct nfs4_label ilabel, *olabel = NULL;
5327 struct nfs_fattr fattr;
5328 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005329 int status;
5330
5331 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5332 return -EOPNOTSUPP;
5333
5334 nfs_fattr_init(&fattr);
5335
5336 ilabel.pi = 0;
5337 ilabel.lfs = 0;
5338 ilabel.label = (char *)buf;
5339 ilabel.len = buflen;
5340
5341 cred = rpc_lookup_cred();
5342 if (IS_ERR(cred))
5343 return PTR_ERR(cred);
5344
5345 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5346 if (IS_ERR(olabel)) {
5347 status = -PTR_ERR(olabel);
5348 goto out;
5349 }
5350
5351 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5352 if (status == 0)
5353 nfs_setsecurity(inode, &fattr, olabel);
5354
5355 nfs4_label_free(olabel);
5356out:
5357 put_rpccred(cred);
5358 return status;
5359}
5360#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5361
5362
Chuck Leverf0920752012-05-21 22:45:41 -04005363static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5364 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005365{
5366 __be32 verf[2];
5367
Chuck Lever2c820d92012-05-21 22:45:33 -04005368 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5369 /* An impossible timestamp guarantees this value
5370 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005371 verf[0] = cpu_to_be32(U32_MAX);
5372 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005373 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005374 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005375 u64 ns = ktime_to_ns(nn->boot_time);
5376
5377 verf[0] = cpu_to_be32(ns >> 32);
5378 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005379 }
Chuck Levercd937102012-03-02 17:14:31 -05005380 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5381}
5382
Jeff Laytona3192682015-06-09 19:43:59 -04005383static int
5384nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005385{
Jeff Laytona3192682015-06-09 19:43:59 -04005386 size_t len;
5387 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005388
Trond Myklebustceb3a162015-01-03 15:16:04 -05005389 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005390 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005391
Jeff Laytona3192682015-06-09 19:43:59 -04005392 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005393 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005394 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5395 1 +
5396 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5397 1;
5398 rcu_read_unlock();
5399
5400 if (len > NFS4_OPAQUE_LIMIT + 1)
5401 return -EINVAL;
5402
5403 /*
5404 * Since this string is allocated at mount time, and held until the
5405 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5406 * about a memory-reclaim deadlock.
5407 */
5408 str = kmalloc(len, GFP_KERNEL);
5409 if (!str)
5410 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005411
Chuck Levere984a552012-09-14 17:24:21 -04005412 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005413 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005414 clp->cl_ipaddr,
5415 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5416 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005417 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005418
Jeff Laytona3192682015-06-09 19:43:59 -04005419 clp->cl_owner_id = str;
5420 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005421}
5422
Jeff Layton873e3852015-06-09 19:44:00 -04005423static int
5424nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005425{
Jeff Layton873e3852015-06-09 19:44:00 -04005426 size_t len;
5427 char *str;
5428
5429 len = 10 + 10 + 1 + 10 + 1 +
5430 strlen(nfs4_client_id_uniquifier) + 1 +
5431 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5432
5433 if (len > NFS4_OPAQUE_LIMIT + 1)
5434 return -EINVAL;
5435
5436 /*
5437 * Since this string is allocated at mount time, and held until the
5438 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5439 * about a memory-reclaim deadlock.
5440 */
5441 str = kmalloc(len, GFP_KERNEL);
5442 if (!str)
5443 return -ENOMEM;
5444
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005445 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005446 clp->rpc_ops->version, clp->cl_minorversion,
5447 nfs4_client_id_uniquifier,
5448 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005449 clp->cl_owner_id = str;
5450 return 0;
5451}
5452
5453static int
5454nfs4_init_uniform_client_string(struct nfs_client *clp)
5455{
Jeff Layton873e3852015-06-09 19:44:00 -04005456 size_t len;
5457 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005458
5459 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005460 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005461
5462 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005463 return nfs4_init_uniquifier_client_string(clp);
5464
5465 len = 10 + 10 + 1 + 10 + 1 +
5466 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5467
5468 if (len > NFS4_OPAQUE_LIMIT + 1)
5469 return -EINVAL;
5470
5471 /*
5472 * Since this string is allocated at mount time, and held until the
5473 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5474 * about a memory-reclaim deadlock.
5475 */
5476 str = kmalloc(len, GFP_KERNEL);
5477 if (!str)
5478 return -ENOMEM;
5479
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005480 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005481 clp->rpc_ops->version, clp->cl_minorversion,
5482 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005483 clp->cl_owner_id = str;
5484 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005485}
5486
Chuck Lever706cb8d2014-03-12 12:51:47 -04005487/*
5488 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5489 * services. Advertise one based on the address family of the
5490 * clientaddr.
5491 */
5492static unsigned int
5493nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5494{
5495 if (strchr(clp->cl_ipaddr, ':') != NULL)
5496 return scnprintf(buf, len, "tcp6");
5497 else
5498 return scnprintf(buf, len, "tcp");
5499}
5500
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005501static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5502{
5503 struct nfs4_setclientid *sc = calldata;
5504
5505 if (task->tk_status == 0)
5506 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5507}
5508
5509static const struct rpc_call_ops nfs4_setclientid_ops = {
5510 .rpc_call_done = nfs4_setclientid_done,
5511};
5512
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513/**
5514 * nfs4_proc_setclientid - Negotiate client ID
5515 * @clp: state data structure
5516 * @program: RPC program for NFSv4 callback service
5517 * @port: IP port number for NFS4 callback service
5518 * @cred: RPC credential to use for this call
5519 * @res: where to place the result
5520 *
5521 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5522 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005523int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5524 unsigned short port, struct rpc_cred *cred,
5525 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
5527 nfs4_verifier sc_verifier;
5528 struct nfs4_setclientid setclientid = {
5529 .sc_verifier = &sc_verifier,
5530 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005531 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 };
5533 struct rpc_message msg = {
5534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5535 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005536 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005537 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005539 struct rpc_task *task;
5540 struct rpc_task_setup task_setup_data = {
5541 .rpc_client = clp->cl_rpcclient,
5542 .rpc_message = &msg,
5543 .callback_ops = &nfs4_setclientid_ops,
5544 .callback_data = &setclientid,
5545 .flags = RPC_TASK_TIMEOUT,
5546 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005547 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
Chuck Leverde734832012-07-11 16:30:50 -04005549 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005550 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005551
5552 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5553 status = nfs4_init_uniform_client_string(clp);
5554 else
Jeff Laytona3192682015-06-09 19:43:59 -04005555 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005556
5557 if (status)
5558 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005559
Chuck Leverde734832012-07-11 16:30:50 -04005560 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005561 setclientid.sc_netid_len =
5562 nfs4_init_callback_netid(clp,
5563 setclientid.sc_netid,
5564 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005565 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005566 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 clp->cl_ipaddr, port >> 8, port & 255);
5568
Jeff Layton3a6bb732015-06-09 19:43:57 -04005569 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005570 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005571 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005572 task = rpc_run_task(&task_setup_data);
5573 if (IS_ERR(task)) {
5574 status = PTR_ERR(task);
5575 goto out;
5576 }
5577 status = task->tk_status;
5578 if (setclientid.sc_cred) {
5579 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5580 put_rpccred(setclientid.sc_cred);
5581 }
5582 rpc_put_task(task);
5583out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005584 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005585 dprintk("NFS reply setclientid: %d\n", status);
5586 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587}
5588
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005589/**
5590 * nfs4_proc_setclientid_confirm - Confirm client ID
5591 * @clp: state data structure
5592 * @res: result of a previous SETCLIENTID
5593 * @cred: RPC credential to use for this call
5594 *
5595 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5596 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005597int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005598 struct nfs4_setclientid_res *arg,
5599 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 struct rpc_message msg = {
5602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005603 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005604 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 int status;
5607
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005608 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5609 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5610 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005611 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005612 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005613 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 return status;
5615}
5616
Trond Myklebustfe650402006-01-03 09:55:18 +01005617struct nfs4_delegreturndata {
5618 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005619 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005620 struct nfs_fh fh;
5621 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005622 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005623 struct {
5624 struct nfs4_layoutreturn_args arg;
5625 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005626 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005627 u32 roc_barrier;
5628 bool roc;
5629 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005630 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005631 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005632 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005633};
5634
Trond Myklebustfe650402006-01-03 09:55:18 +01005635static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5636{
5637 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005638
Trond Myklebust14516c32010-07-31 14:29:06 -04005639 if (!nfs4_sequence_done(task, &data->res.seq_res))
5640 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005641
Trond Myklebustca8acf82013-08-13 10:36:56 -04005642 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005643
5644 /* Handle Layoutreturn errors */
5645 if (data->args.lr_args && task->tk_status != 0) {
5646 switch(data->res.lr_ret) {
5647 default:
5648 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5649 break;
5650 case 0:
5651 data->args.lr_args = NULL;
5652 data->res.lr_res = NULL;
5653 break;
5654 case -NFS4ERR_ADMIN_REVOKED:
5655 case -NFS4ERR_DELEG_REVOKED:
5656 case -NFS4ERR_EXPIRED:
5657 case -NFS4ERR_BAD_STATEID:
5658 case -NFS4ERR_OLD_STATEID:
5659 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5660 case -NFS4ERR_WRONG_CRED:
5661 data->args.lr_args = NULL;
5662 data->res.lr_res = NULL;
5663 data->res.lr_ret = 0;
5664 rpc_restart_call_prepare(task);
5665 return;
5666 }
5667 }
5668
Ricardo Labiaga79708862009-12-07 09:23:21 -05005669 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005670 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005671 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005672 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005673 case -NFS4ERR_ADMIN_REVOKED:
5674 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005675 case -NFS4ERR_EXPIRED:
5676 nfs4_free_revoked_stateid(data->res.server,
5677 data->args.stateid,
5678 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005679 case -NFS4ERR_BAD_STATEID:
5680 case -NFS4ERR_OLD_STATEID:
5681 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005682 task->tk_status = 0;
5683 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005684 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005685 if (nfs4_async_handle_error(task, data->res.server,
5686 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005687 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005688 return;
5689 }
5690 }
5691 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005692}
5693
5694static void nfs4_delegreturn_release(void *calldata)
5695{
Peng Tao039b7562014-07-03 13:05:02 +08005696 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005697 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005698
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005699 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005700 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005701 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5702 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005703 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005704 nfs_iput_and_deactive(inode);
5705 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 kfree(calldata);
5707}
5708
Andy Adamson938e1012009-04-01 09:22:28 -04005709static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5710{
5711 struct nfs4_delegreturndata *d_data;
5712
5713 d_data = (struct nfs4_delegreturndata *)data;
5714
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005715 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005716 return;
5717
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005718 nfs4_setup_sequence(d_data->res.server,
5719 &d_data->args.seq_args,
5720 &d_data->res.seq_res,
5721 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005722}
Andy Adamson938e1012009-04-01 09:22:28 -04005723
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005724static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005725 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005726 .rpc_call_done = nfs4_delegreturn_done,
5727 .rpc_release = nfs4_delegreturn_release,
5728};
5729
Trond Myklebuste6f81072008-01-24 18:14:34 -05005730static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005731{
5732 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005733 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005734 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005735 struct rpc_message msg = {
5736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5737 .rpc_cred = cred,
5738 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005739 struct rpc_task_setup task_setup_data = {
5740 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005741 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005742 .callback_ops = &nfs4_delegreturn_ops,
5743 .flags = RPC_TASK_ASYNC,
5744 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005745 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005746
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005747 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005748 if (data == NULL)
5749 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005750 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005751
5752 nfs4_state_protect(server->nfs_client,
5753 NFS_SP4_MACH_CRED_CLEANUP,
5754 &task_setup_data.rpc_client, &msg);
5755
Trond Myklebustfe650402006-01-03 09:55:18 +01005756 data->args.fhandle = &data->fh;
5757 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005758 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005759 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005760 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005761 data->res.fattr = &data->fattr;
5762 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005763 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005764 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005765 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005766 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005767 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005768 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005769 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005770 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005771 if (data->lr.roc) {
5772 data->args.lr_args = &data->lr.arg;
5773 data->res.lr_res = &data->lr.res;
5774 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005775 } else if (data->lr.roc) {
5776 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5777 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005778 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005779
Trond Myklebustc970aa82007-07-14 15:39:59 -04005780 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005781 msg.rpc_argp = &data->args;
5782 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005783 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005784 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005785 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005786 if (!issync)
5787 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005788 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005789 if (status != 0)
5790 goto out;
5791 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005792out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005793 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005794 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795}
5796
Trond Myklebuste6f81072008-01-24 18:14:34 -05005797int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
5799 struct nfs_server *server = NFS_SERVER(inode);
5800 struct nfs4_exception exception = { };
5801 int err;
5802 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005803 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005804 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 switch (err) {
5806 case -NFS4ERR_STALE_STATEID:
5807 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 case 0:
5809 return 0;
5810 }
5811 err = nfs4_handle_exception(server, err, &exception);
5812 } while (exception.retry);
5813 return err;
5814}
5815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5817{
5818 struct inode *inode = state->inode;
5819 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005820 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005821 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005823 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005825 struct nfs_lockt_res res = {
5826 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 };
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5830 .rpc_argp = &arg,
5831 .rpc_resp = &res,
5832 .rpc_cred = state->owner->so_cred,
5833 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 struct nfs4_lock_state *lsp;
5835 int status;
5836
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005837 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005838 status = nfs4_set_lock_state(state, request);
5839 if (status != 0)
5840 goto out;
5841 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005842 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005843 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005844 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005845 switch (status) {
5846 case 0:
5847 request->fl_type = F_UNLCK;
5848 break;
5849 case -NFS4ERR_DENIED:
5850 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005852 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005853 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005854out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 return status;
5856}
5857
5858static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5859{
5860 struct nfs4_exception exception = { };
5861 int err;
5862
5863 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005864 err = _nfs4_proc_getlk(state, cmd, request);
5865 trace_nfs4_get_lock(request, state, cmd, err);
5866 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 &exception);
5868 } while (exception.retry);
5869 return err;
5870}
5871
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005872struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005873 struct nfs_locku_args arg;
5874 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875 struct nfs4_lock_state *lsp;
5876 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005877 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005878 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005879 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005880};
5881
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005882static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5883 struct nfs_open_context *ctx,
5884 struct nfs4_lock_state *lsp,
5885 struct nfs_seqid *seqid)
5886{
5887 struct nfs4_unlockdata *p;
5888 struct inode *inode = lsp->ls_state->inode;
5889
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005890 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005891 if (p == NULL)
5892 return NULL;
5893 p->arg.fh = NFS_FH(inode);
5894 p->arg.fl = &p->fl;
5895 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005896 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005897 p->lsp = lsp;
5898 atomic_inc(&lsp->ls_count);
5899 /* Ensure we don't close file until we're done freeing locks! */
5900 p->ctx = get_nfs_open_context(ctx);
5901 memcpy(&p->fl, fl, sizeof(p->fl));
5902 p->server = NFS_SERVER(inode);
5903 return p;
5904}
5905
Trond Myklebust06f814a2006-01-03 09:55:07 +01005906static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005907{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005908 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005909 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005910 nfs4_put_lock_state(calldata->lsp);
5911 put_nfs_open_context(calldata->ctx);
5912 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005913}
5914
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005915static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005917 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005918
Trond Myklebust14516c32010-07-31 14:29:06 -04005919 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5920 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005921 switch (task->tk_status) {
5922 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005923 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005924 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005925 if (nfs4_update_lock_stateid(calldata->lsp,
5926 &calldata->res.stateid))
5927 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005928 case -NFS4ERR_ADMIN_REVOKED:
5929 case -NFS4ERR_EXPIRED:
5930 nfs4_free_revoked_stateid(calldata->server,
5931 &calldata->arg.stateid,
5932 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005933 case -NFS4ERR_BAD_STATEID:
5934 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005935 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005936 if (!nfs4_stateid_match(&calldata->arg.stateid,
5937 &calldata->lsp->ls_stateid))
5938 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939 break;
5940 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005941 if (nfs4_async_handle_error(task, calldata->server,
5942 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005943 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005944 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005945 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005946}
5947
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005948static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005949{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005950 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005952 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005953 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005954 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005955 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005956 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005957 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005958 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005959 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005960 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005961 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005962 &calldata->res.seq_res,
5963 task) != 0)
5964 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005965 return;
5966out_no_action:
5967 task->tk_action = NULL;
5968out_wait:
5969 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005972static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005973 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005974 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005975 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005976};
5977
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005978static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5979 struct nfs_open_context *ctx,
5980 struct nfs4_lock_state *lsp,
5981 struct nfs_seqid *seqid)
5982{
5983 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005984 struct rpc_message msg = {
5985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5986 .rpc_cred = ctx->cred,
5987 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005988 struct rpc_task_setup task_setup_data = {
5989 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005990 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005991 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005992 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005993 .flags = RPC_TASK_ASYNC,
5994 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005996 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5997 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5998
Frank Filz137d6ac2007-07-09 15:32:29 -07005999 /* Ensure this is an unlock - when canceling a lock, the
6000 * canceled lock is passed in, and it won't be an unlock.
6001 */
6002 fl->fl_type = F_UNLCK;
6003
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006004 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6005 if (data == NULL) {
6006 nfs_free_seqid(seqid);
6007 return ERR_PTR(-ENOMEM);
6008 }
6009
Chuck Levera9c92d62013-08-09 12:48:18 -04006010 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006011 msg.rpc_argp = &data->arg;
6012 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006013 task_setup_data.callback_data = data;
6014 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006015}
6016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6018{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006019 struct inode *inode = state->inode;
6020 struct nfs4_state_owner *sp = state->owner;
6021 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006022 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006023 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006024 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006025 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006026 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006027 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
Trond Myklebust9b073572006-06-29 16:38:34 -04006029 status = nfs4_set_lock_state(state, request);
6030 /* Unlock _before_ we do the RPC call */
6031 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006032 /* Exclude nfs_delegation_claim_locks() */
6033 mutex_lock(&sp->so_delegreturn_mutex);
6034 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006035 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006036 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006037 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006038 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006039 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006040 }
6041 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006042 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006043 if (status != 0)
6044 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006045 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006046 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006047 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6048 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006049 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6050 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006051 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006052 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006053 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006054 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006055 status = PTR_ERR(task);
6056 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006057 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006059 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006060out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006061 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006062 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006063 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064}
6065
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066struct nfs4_lockdata {
6067 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006068 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006069 struct nfs4_lock_state *lsp;
6070 struct nfs_open_context *ctx;
6071 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006072 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006073 int rpc_status;
6074 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006075 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076};
6077
6078static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006079 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6080 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081{
6082 struct nfs4_lockdata *p;
6083 struct inode *inode = lsp->ls_state->inode;
6084 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006085 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006087 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006088 if (p == NULL)
6089 return NULL;
6090
6091 p->arg.fh = NFS_FH(inode);
6092 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006093 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006094 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006095 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006096 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6097 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006098 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006099 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006100 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006101 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006102 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006103 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006105 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006106 atomic_inc(&lsp->ls_count);
6107 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006108 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006109 memcpy(&p->fl, fl, sizeof(p->fl));
6110 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006111out_free_seqid:
6112 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113out_free:
6114 kfree(p);
6115 return NULL;
6116}
6117
6118static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6119{
6120 struct nfs4_lockdata *data = calldata;
6121 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122
Harvey Harrison3110ff82008-05-02 13:42:44 -07006123 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006124 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006125 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006126 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006127 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006128 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006129 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006130 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006131 nfs4_stateid_copy(&data->arg.open_stateid,
6132 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006133 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006134 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006135 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006136 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006137 nfs4_stateid_copy(&data->arg.lock_stateid,
6138 &data->lsp->ls_stateid);
6139 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006140 if (!nfs4_valid_open_stateid(state)) {
6141 data->rpc_status = -EBADF;
6142 task->tk_action = NULL;
6143 goto out_release_open_seqid;
6144 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006145 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006146 if (nfs4_setup_sequence(data->server,
6147 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006148 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006149 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006150 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006151out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006152 nfs_release_seqid(data->arg.open_seqid);
6153out_release_lock_seqid:
6154 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006155out_wait:
6156 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006157 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006158}
6159
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006160static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6161{
6162 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006163 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006164
Harvey Harrison3110ff82008-05-02 13:42:44 -07006165 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006166
Trond Myklebust14516c32010-07-31 14:29:06 -04006167 if (!nfs4_sequence_done(task, &data->res.seq_res))
6168 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006169
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006171 switch (task->tk_status) {
6172 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006173 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006174 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006175 if (data->arg.new_lock) {
6176 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006177 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006178 rpc_restart_call_prepare(task);
6179 break;
6180 }
6181 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006182 if (data->arg.new_lock_owner != 0) {
6183 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6184 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6185 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6186 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6187 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006188 break;
6189 case -NFS4ERR_BAD_STATEID:
6190 case -NFS4ERR_OLD_STATEID:
6191 case -NFS4ERR_STALE_STATEID:
6192 case -NFS4ERR_EXPIRED:
6193 if (data->arg.new_lock_owner != 0) {
6194 if (!nfs4_stateid_match(&data->arg.open_stateid,
6195 &lsp->ls_state->open_stateid))
6196 rpc_restart_call_prepare(task);
6197 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6198 &lsp->ls_stateid))
6199 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006200 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006201 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202}
6203
6204static void nfs4_lock_release(void *calldata)
6205{
6206 struct nfs4_lockdata *data = calldata;
6207
Harvey Harrison3110ff82008-05-02 13:42:44 -07006208 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006209 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006210 if (data->cancelled != 0) {
6211 struct rpc_task *task;
6212 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6213 data->arg.lock_seqid);
6214 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006215 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 } else
6218 nfs_free_seqid(data->arg.lock_seqid);
6219 nfs4_put_lock_state(data->lsp);
6220 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006221 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006223 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006224}
6225
6226static const struct rpc_call_ops nfs4_lock_ops = {
6227 .rpc_call_prepare = nfs4_lock_prepare,
6228 .rpc_call_done = nfs4_lock_done,
6229 .rpc_release = nfs4_lock_release,
6230};
6231
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006232static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6233{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006234 switch (error) {
6235 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006236 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006237 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006238 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006239 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006240 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006241 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006242 break;
6243 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006244 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006245 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006246 };
6247}
6248
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006249static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006250{
6251 struct nfs4_lockdata *data;
6252 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006253 struct rpc_message msg = {
6254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6255 .rpc_cred = state->owner->so_cred,
6256 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006257 struct rpc_task_setup task_setup_data = {
6258 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006259 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006260 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006261 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006262 .flags = RPC_TASK_ASYNC,
6263 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006264 int ret;
6265
Harvey Harrison3110ff82008-05-02 13:42:44 -07006266 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006267 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006268 fl->fl_u.nfs4_fl.owner,
6269 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006270 if (data == NULL)
6271 return -ENOMEM;
6272 if (IS_SETLKW(cmd))
6273 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006274 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006275 msg.rpc_argp = &data->arg;
6276 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006277 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006278 if (recovery_type > NFS_LOCK_NEW) {
6279 if (recovery_type == NFS_LOCK_RECLAIM)
6280 data->arg.reclaim = NFS_LOCK_RECLAIM;
6281 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006282 } else
6283 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006284 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006285 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006286 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006287 ret = nfs4_wait_for_completion_rpc_task(task);
6288 if (ret == 0) {
6289 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006290 if (ret)
6291 nfs4_handle_setlk_error(data->server, data->lsp,
6292 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 } else
6294 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006295 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006296 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006297 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299}
6300
6301static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6302{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006303 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006304 struct nfs4_exception exception = {
6305 .inode = state->inode,
6306 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006307 int err;
6308
6309 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006310 /* Cache the lock if possible... */
6311 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6312 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006313 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006314 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006315 break;
6316 nfs4_handle_exception(server, err, &exception);
6317 } while (exception.retry);
6318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319}
6320
6321static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6322{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006323 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006324 struct nfs4_exception exception = {
6325 .inode = state->inode,
6326 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006327 int err;
6328
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006329 err = nfs4_set_lock_state(state, request);
6330 if (err != 0)
6331 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006332 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006333 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6334 return 0;
6335 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006336 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006337 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6338 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006339 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006340 switch (err) {
6341 default:
6342 goto out;
6343 case -NFS4ERR_GRACE:
6344 case -NFS4ERR_DELAY:
6345 nfs4_handle_exception(server, err, &exception);
6346 err = 0;
6347 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006348 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006349out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006350 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351}
6352
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006353#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006354static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6355{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006356 struct nfs4_lock_state *lsp;
6357 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006358
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006359 status = nfs4_set_lock_state(state, request);
6360 if (status != 0)
6361 return status;
6362 lsp = request->fl_u.nfs4_fl.owner;
6363 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6364 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6365 return 0;
6366 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006367 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006368}
6369#endif
6370
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6372{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006373 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006374 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006375 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006376 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
Trond Myklebust01c3b862006-06-29 16:38:39 -04006378 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006379 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006380 if (status < 0)
6381 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006382 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006383 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006384 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006385 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006386 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006387 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006388 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006389 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006390 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006391 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006392 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006393 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006394 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006395 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006396out:
6397 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 return status;
6399}
6400
6401static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6402{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006403 struct nfs4_exception exception = {
6404 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006405 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006406 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 int err;
6408
6409 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006410 err = _nfs4_proc_setlk(state, cmd, request);
6411 if (err == -NFS4ERR_DENIED)
6412 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006414 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 } while (exception.retry);
6416 return err;
6417}
6418
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006419#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6420#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6421
6422static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006423nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6424 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006425{
6426 int status = -ERESTARTSYS;
6427 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6428
6429 while(!signalled()) {
6430 status = nfs4_proc_setlk(state, cmd, request);
6431 if ((status != -EAGAIN) || IS_SETLK(cmd))
6432 break;
6433 freezable_schedule_timeout_interruptible(timeout);
6434 timeout *= 2;
6435 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6436 status = -ERESTARTSYS;
6437 }
6438 return status;
6439}
6440
Jeff Laytona1d617d82016-09-17 18:17:39 -04006441#ifdef CONFIG_NFS_V4_1
6442struct nfs4_lock_waiter {
6443 struct task_struct *task;
6444 struct inode *inode;
6445 struct nfs_lowner *owner;
6446 bool notified;
6447};
6448
6449static int
6450nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6451{
6452 int ret;
6453 struct cb_notify_lock_args *cbnl = key;
6454 struct nfs4_lock_waiter *waiter = wait->private;
6455 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6456 *wowner = waiter->owner;
6457
6458 /* Only wake if the callback was for the same owner */
6459 if (lowner->clientid != wowner->clientid ||
6460 lowner->id != wowner->id ||
6461 lowner->s_dev != wowner->s_dev)
6462 return 0;
6463
6464 /* Make sure it's for the right inode */
6465 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6466 return 0;
6467
6468 waiter->notified = true;
6469
6470 /* override "private" so we can use default_wake_function */
6471 wait->private = waiter->task;
6472 ret = autoremove_wake_function(wait, mode, flags, key);
6473 wait->private = waiter;
6474 return ret;
6475}
6476
6477static int
6478nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6479{
6480 int status = -ERESTARTSYS;
6481 unsigned long flags;
6482 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6483 struct nfs_server *server = NFS_SERVER(state->inode);
6484 struct nfs_client *clp = server->nfs_client;
6485 wait_queue_head_t *q = &clp->cl_lock_waitq;
6486 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6487 .id = lsp->ls_seqid.owner_id,
6488 .s_dev = server->s_dev };
6489 struct nfs4_lock_waiter waiter = { .task = current,
6490 .inode = state->inode,
6491 .owner = &owner,
6492 .notified = false };
6493 wait_queue_t wait;
6494
6495 /* Don't bother with waitqueue if we don't expect a callback */
6496 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6497 return nfs4_retry_setlk_simple(state, cmd, request);
6498
6499 init_wait(&wait);
6500 wait.private = &waiter;
6501 wait.func = nfs4_wake_lock_waiter;
6502 add_wait_queue(q, &wait);
6503
6504 while(!signalled()) {
6505 status = nfs4_proc_setlk(state, cmd, request);
6506 if ((status != -EAGAIN) || IS_SETLK(cmd))
6507 break;
6508
6509 status = -ERESTARTSYS;
6510 spin_lock_irqsave(&q->lock, flags);
6511 if (waiter.notified) {
6512 spin_unlock_irqrestore(&q->lock, flags);
6513 continue;
6514 }
6515 set_current_state(TASK_INTERRUPTIBLE);
6516 spin_unlock_irqrestore(&q->lock, flags);
6517
6518 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6519 }
6520
6521 finish_wait(q, &wait);
6522 return status;
6523}
6524#else /* !CONFIG_NFS_V4_1 */
6525static inline int
6526nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6527{
6528 return nfs4_retry_setlk_simple(state, cmd, request);
6529}
6530#endif
6531
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532static int
6533nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6534{
6535 struct nfs_open_context *ctx;
6536 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 int status;
6538
6539 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006540 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541 state = ctx->state;
6542
6543 if (request->fl_start < 0 || request->fl_end < 0)
6544 return -EINVAL;
6545
Trond Myklebustd9531262009-07-21 19:22:38 -04006546 if (IS_GETLK(cmd)) {
6547 if (state != NULL)
6548 return nfs4_proc_getlk(state, F_GETLK, request);
6549 return 0;
6550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6553 return -EINVAL;
6554
Trond Myklebustd9531262009-07-21 19:22:38 -04006555 if (request->fl_type == F_UNLCK) {
6556 if (state != NULL)
6557 return nfs4_proc_unlck(state, cmd, request);
6558 return 0;
6559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560
Trond Myklebustd9531262009-07-21 19:22:38 -04006561 if (state == NULL)
6562 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006563
6564 if ((request->fl_flags & FL_POSIX) &&
6565 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6566 return -ENOLCK;
6567
Trond Myklebust55725512012-04-18 12:48:35 -04006568 /*
6569 * Don't rely on the VFS having checked the file open mode,
6570 * since it won't do this for flock() locks.
6571 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006572 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006573 case F_RDLCK:
6574 if (!(filp->f_mode & FMODE_READ))
6575 return -EBADF;
6576 break;
6577 case F_WRLCK:
6578 if (!(filp->f_mode & FMODE_WRITE))
6579 return -EBADF;
6580 }
6581
Jeff Layton1ea67db2016-09-17 18:17:37 -04006582 status = nfs4_set_lock_state(state, request);
6583 if (status != 0)
6584 return status;
6585
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006586 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587}
6588
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006589int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006590{
6591 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006592 int err;
6593
6594 err = nfs4_set_lock_state(state, fl);
6595 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006596 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006597 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006598 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006599}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006600
Trond Myklebustcf470c32012-03-07 13:49:12 -05006601struct nfs_release_lockowner_data {
6602 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006603 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006604 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006605 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006606 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006607};
6608
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006609static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6610{
6611 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006612 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006613 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6614 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006615 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006616 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006617}
6618
6619static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6620{
6621 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006622 struct nfs_server *server = data->server;
6623
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006624 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006625
6626 switch (task->tk_status) {
6627 case 0:
6628 renew_lease(server, data->timestamp);
6629 break;
6630 case -NFS4ERR_STALE_CLIENTID:
6631 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006632 nfs4_schedule_lease_recovery(server->nfs_client);
6633 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006634 case -NFS4ERR_LEASE_MOVED:
6635 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006636 if (nfs4_async_handle_error(task, server,
6637 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006638 rpc_restart_call_prepare(task);
6639 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006640}
6641
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006642static void nfs4_release_lockowner_release(void *calldata)
6643{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006644 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006645 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006646 kfree(calldata);
6647}
6648
Trond Myklebust17280172012-03-11 13:11:00 -04006649static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006650 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6651 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006652 .rpc_release = nfs4_release_lockowner_release,
6653};
6654
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006655static void
6656nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006657{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006658 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006659 struct rpc_message msg = {
6660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6661 };
6662
6663 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006664 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006665
Trond Myklebustcf470c32012-03-07 13:49:12 -05006666 data = kmalloc(sizeof(*data), GFP_NOFS);
6667 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006668 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006669 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006670 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006671 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6672 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6673 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006674
Trond Myklebustcf470c32012-03-07 13:49:12 -05006675 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006676 msg.rpc_resp = &data->res;
6677 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006678 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006679}
6680
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006681#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6682
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006683static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006684 struct dentry *unused, struct inode *inode,
6685 const char *key, const void *buf,
6686 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006687{
Al Viro59301222016-05-27 10:19:30 -04006688 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006689}
6690
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006691static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006692 struct dentry *unused, struct inode *inode,
6693 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006694{
Al Virob2968212016-04-10 20:48:24 -04006695 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006696}
6697
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006698static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006699{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006700 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006701}
6702
David Quigleyc9bccef2013-05-22 12:50:45 -04006703#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006704
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006705static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006706 struct dentry *unused, struct inode *inode,
6707 const char *key, const void *buf,
6708 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006709{
6710 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006711 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006712
6713 return -EOPNOTSUPP;
6714}
6715
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006716static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006717 struct dentry *unused, struct inode *inode,
6718 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006719{
6720 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006721 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006722 return -EOPNOTSUPP;
6723}
6724
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006725static ssize_t
6726nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006727{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006728 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006729
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006730 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6731 len = security_inode_listsecurity(inode, list, list_len);
6732 if (list_len && len > list_len)
6733 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006734 }
6735 return len;
6736}
6737
6738static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6739 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006740 .get = nfs4_xattr_get_nfs4_label,
6741 .set = nfs4_xattr_set_nfs4_label,
6742};
David Quigleyc9bccef2013-05-22 12:50:45 -04006743
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006744#else
6745
6746static ssize_t
6747nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6748{
6749 return 0;
6750}
6751
6752#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006753
Andy Adamson533eb462011-06-13 18:25:56 -04006754/*
6755 * nfs_fhget will use either the mounted_on_fileid or the fileid
6756 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006757static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6758{
Andy Adamson533eb462011-06-13 18:25:56 -04006759 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6760 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6761 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006762 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006763 return;
6764
6765 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006766 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006767 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6768 fattr->nlink = 2;
6769}
6770
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006771static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6772 const struct qstr *name,
6773 struct nfs4_fs_locations *fs_locations,
6774 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006775{
6776 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006777 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006778 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006779 };
6780 struct nfs4_fs_locations_arg args = {
6781 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006782 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006783 .page = page,
6784 .bitmask = bitmask,
6785 };
Benny Halevy22958462009-04-01 09:22:02 -04006786 struct nfs4_fs_locations_res res = {
6787 .fs_locations = fs_locations,
6788 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006789 struct rpc_message msg = {
6790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6791 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006792 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006793 };
6794 int status;
6795
Harvey Harrison3110ff82008-05-02 13:42:44 -07006796 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006797
6798 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6799 * is not supported */
6800 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6801 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6802 else
6803 bitmask[0] |= FATTR4_WORD0_FILEID;
6804
Trond Myklebustc228fd32007-01-13 02:28:11 -05006805 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006806 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006807 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006808 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006809 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006810 return status;
6811}
6812
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006813int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6814 const struct qstr *name,
6815 struct nfs4_fs_locations *fs_locations,
6816 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006817{
6818 struct nfs4_exception exception = { };
6819 int err;
6820 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006821 err = _nfs4_proc_fs_locations(client, dir, name,
6822 fs_locations, page);
6823 trace_nfs4_get_fs_locations(dir, name, err);
6824 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006825 &exception);
6826 } while (exception.retry);
6827 return err;
6828}
6829
Chuck Leverb03d7352013-10-17 14:12:50 -04006830/*
6831 * This operation also signals the server that this client is
6832 * performing migration recovery. The server can stop returning
6833 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6834 * appended to this compound to identify the client ID which is
6835 * performing recovery.
6836 */
6837static int _nfs40_proc_get_locations(struct inode *inode,
6838 struct nfs4_fs_locations *locations,
6839 struct page *page, struct rpc_cred *cred)
6840{
6841 struct nfs_server *server = NFS_SERVER(inode);
6842 struct rpc_clnt *clnt = server->client;
6843 u32 bitmask[2] = {
6844 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6845 };
6846 struct nfs4_fs_locations_arg args = {
6847 .clientid = server->nfs_client->cl_clientid,
6848 .fh = NFS_FH(inode),
6849 .page = page,
6850 .bitmask = bitmask,
6851 .migration = 1, /* skip LOOKUP */
6852 .renew = 1, /* append RENEW */
6853 };
6854 struct nfs4_fs_locations_res res = {
6855 .fs_locations = locations,
6856 .migration = 1,
6857 .renew = 1,
6858 };
6859 struct rpc_message msg = {
6860 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6861 .rpc_argp = &args,
6862 .rpc_resp = &res,
6863 .rpc_cred = cred,
6864 };
6865 unsigned long now = jiffies;
6866 int status;
6867
6868 nfs_fattr_init(&locations->fattr);
6869 locations->server = server;
6870 locations->nlocations = 0;
6871
6872 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6873 nfs4_set_sequence_privileged(&args.seq_args);
6874 status = nfs4_call_sync_sequence(clnt, server, &msg,
6875 &args.seq_args, &res.seq_res);
6876 if (status)
6877 return status;
6878
6879 renew_lease(server, now);
6880 return 0;
6881}
6882
6883#ifdef CONFIG_NFS_V4_1
6884
6885/*
6886 * This operation also signals the server that this client is
6887 * performing migration recovery. The server can stop asserting
6888 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6889 * performing this operation is identified in the SEQUENCE
6890 * operation in this compound.
6891 *
6892 * When the client supports GETATTR(fs_locations_info), it can
6893 * be plumbed in here.
6894 */
6895static int _nfs41_proc_get_locations(struct inode *inode,
6896 struct nfs4_fs_locations *locations,
6897 struct page *page, struct rpc_cred *cred)
6898{
6899 struct nfs_server *server = NFS_SERVER(inode);
6900 struct rpc_clnt *clnt = server->client;
6901 u32 bitmask[2] = {
6902 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6903 };
6904 struct nfs4_fs_locations_arg args = {
6905 .fh = NFS_FH(inode),
6906 .page = page,
6907 .bitmask = bitmask,
6908 .migration = 1, /* skip LOOKUP */
6909 };
6910 struct nfs4_fs_locations_res res = {
6911 .fs_locations = locations,
6912 .migration = 1,
6913 };
6914 struct rpc_message msg = {
6915 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6916 .rpc_argp = &args,
6917 .rpc_resp = &res,
6918 .rpc_cred = cred,
6919 };
6920 int status;
6921
6922 nfs_fattr_init(&locations->fattr);
6923 locations->server = server;
6924 locations->nlocations = 0;
6925
6926 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6927 nfs4_set_sequence_privileged(&args.seq_args);
6928 status = nfs4_call_sync_sequence(clnt, server, &msg,
6929 &args.seq_args, &res.seq_res);
6930 if (status == NFS4_OK &&
6931 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6932 status = -NFS4ERR_LEASE_MOVED;
6933 return status;
6934}
6935
6936#endif /* CONFIG_NFS_V4_1 */
6937
6938/**
6939 * nfs4_proc_get_locations - discover locations for a migrated FSID
6940 * @inode: inode on FSID that is migrating
6941 * @locations: result of query
6942 * @page: buffer
6943 * @cred: credential to use for this operation
6944 *
6945 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6946 * operation failed, or a negative errno if a local error occurred.
6947 *
6948 * On success, "locations" is filled in, but if the server has
6949 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6950 * asserted.
6951 *
6952 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6953 * from this client that require migration recovery.
6954 */
6955int nfs4_proc_get_locations(struct inode *inode,
6956 struct nfs4_fs_locations *locations,
6957 struct page *page, struct rpc_cred *cred)
6958{
6959 struct nfs_server *server = NFS_SERVER(inode);
6960 struct nfs_client *clp = server->nfs_client;
6961 const struct nfs4_mig_recovery_ops *ops =
6962 clp->cl_mvops->mig_recovery_ops;
6963 struct nfs4_exception exception = { };
6964 int status;
6965
6966 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6967 (unsigned long long)server->fsid.major,
6968 (unsigned long long)server->fsid.minor,
6969 clp->cl_hostname);
6970 nfs_display_fhandle(NFS_FH(inode), __func__);
6971
6972 do {
6973 status = ops->get_locations(inode, locations, page, cred);
6974 if (status != -NFS4ERR_DELAY)
6975 break;
6976 nfs4_handle_exception(server, status, &exception);
6977 } while (exception.retry);
6978 return status;
6979}
6980
Chuck Lever44c99932013-10-17 14:13:30 -04006981/*
6982 * This operation also signals the server that this client is
6983 * performing "lease moved" recovery. The server can stop
6984 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6985 * is appended to this compound to identify the client ID which is
6986 * performing recovery.
6987 */
6988static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6989{
6990 struct nfs_server *server = NFS_SERVER(inode);
6991 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6992 struct rpc_clnt *clnt = server->client;
6993 struct nfs4_fsid_present_arg args = {
6994 .fh = NFS_FH(inode),
6995 .clientid = clp->cl_clientid,
6996 .renew = 1, /* append RENEW */
6997 };
6998 struct nfs4_fsid_present_res res = {
6999 .renew = 1,
7000 };
7001 struct rpc_message msg = {
7002 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7003 .rpc_argp = &args,
7004 .rpc_resp = &res,
7005 .rpc_cred = cred,
7006 };
7007 unsigned long now = jiffies;
7008 int status;
7009
7010 res.fh = nfs_alloc_fhandle();
7011 if (res.fh == NULL)
7012 return -ENOMEM;
7013
7014 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7015 nfs4_set_sequence_privileged(&args.seq_args);
7016 status = nfs4_call_sync_sequence(clnt, server, &msg,
7017 &args.seq_args, &res.seq_res);
7018 nfs_free_fhandle(res.fh);
7019 if (status)
7020 return status;
7021
7022 do_renew_lease(clp, now);
7023 return 0;
7024}
7025
7026#ifdef CONFIG_NFS_V4_1
7027
7028/*
7029 * This operation also signals the server that this client is
7030 * performing "lease moved" recovery. The server can stop asserting
7031 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7032 * this operation is identified in the SEQUENCE operation in this
7033 * compound.
7034 */
7035static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7036{
7037 struct nfs_server *server = NFS_SERVER(inode);
7038 struct rpc_clnt *clnt = server->client;
7039 struct nfs4_fsid_present_arg args = {
7040 .fh = NFS_FH(inode),
7041 };
7042 struct nfs4_fsid_present_res res = {
7043 };
7044 struct rpc_message msg = {
7045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7046 .rpc_argp = &args,
7047 .rpc_resp = &res,
7048 .rpc_cred = cred,
7049 };
7050 int status;
7051
7052 res.fh = nfs_alloc_fhandle();
7053 if (res.fh == NULL)
7054 return -ENOMEM;
7055
7056 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7057 nfs4_set_sequence_privileged(&args.seq_args);
7058 status = nfs4_call_sync_sequence(clnt, server, &msg,
7059 &args.seq_args, &res.seq_res);
7060 nfs_free_fhandle(res.fh);
7061 if (status == NFS4_OK &&
7062 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7063 status = -NFS4ERR_LEASE_MOVED;
7064 return status;
7065}
7066
7067#endif /* CONFIG_NFS_V4_1 */
7068
7069/**
7070 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7071 * @inode: inode on FSID to check
7072 * @cred: credential to use for this operation
7073 *
7074 * Server indicates whether the FSID is present, moved, or not
7075 * recognized. This operation is necessary to clear a LEASE_MOVED
7076 * condition for this client ID.
7077 *
7078 * Returns NFS4_OK if the FSID is present on this server,
7079 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7080 * NFS4ERR code if some error occurred on the server, or a
7081 * negative errno if a local failure occurred.
7082 */
7083int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7084{
7085 struct nfs_server *server = NFS_SERVER(inode);
7086 struct nfs_client *clp = server->nfs_client;
7087 const struct nfs4_mig_recovery_ops *ops =
7088 clp->cl_mvops->mig_recovery_ops;
7089 struct nfs4_exception exception = { };
7090 int status;
7091
7092 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7093 (unsigned long long)server->fsid.major,
7094 (unsigned long long)server->fsid.minor,
7095 clp->cl_hostname);
7096 nfs_display_fhandle(NFS_FH(inode), __func__);
7097
7098 do {
7099 status = ops->fsid_present(inode, cred);
7100 if (status != -NFS4ERR_DELAY)
7101 break;
7102 nfs4_handle_exception(server, status, &exception);
7103 } while (exception.retry);
7104 return status;
7105}
7106
Andy Adamson5ec16a82013-08-08 10:57:55 -04007107/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007108 * If 'use_integrity' is true and the state managment nfs_client
7109 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7110 * and the machine credential as per RFC3530bis and RFC5661 Security
7111 * Considerations sections. Otherwise, just use the user cred with the
7112 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007113 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007114static 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 +00007115{
7116 int status;
7117 struct nfs4_secinfo_arg args = {
7118 .dir_fh = NFS_FH(dir),
7119 .name = name,
7120 };
7121 struct nfs4_secinfo_res res = {
7122 .flavors = flavors,
7123 };
7124 struct rpc_message msg = {
7125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7126 .rpc_argp = &args,
7127 .rpc_resp = &res,
7128 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007129 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007130 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007131
7132 if (use_integrity) {
7133 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007134 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7135 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007136 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007137
7138 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007139
7140 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7141 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7142
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007143 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7144 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007145 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007146
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007147 if (cred)
7148 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007149
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007150 return status;
7151}
7152
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007153int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7154 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007155{
7156 struct nfs4_exception exception = { };
7157 int err;
7158 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007159 err = -NFS4ERR_WRONGSEC;
7160
7161 /* try to use integrity protection with machine cred */
7162 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7163 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7164
7165 /*
7166 * if unable to use integrity protection, or SECINFO with
7167 * integrity protection returns NFS4ERR_WRONGSEC (which is
7168 * disallowed by spec, but exists in deployed servers) use
7169 * the current filesystem's rpc_client and the user cred.
7170 */
7171 if (err == -NFS4ERR_WRONGSEC)
7172 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7173
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007174 trace_nfs4_secinfo(dir, name, err);
7175 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007176 &exception);
7177 } while (exception.retry);
7178 return err;
7179}
7180
Andy Adamson557134a2009-04-01 09:21:53 -04007181#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007182/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007183 * Check the exchange flags returned by the server for invalid flags, having
7184 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7185 * DS flags set.
7186 */
7187static int nfs4_check_cl_exchange_flags(u32 flags)
7188{
7189 if (flags & ~EXCHGID4_FLAG_MASK_R)
7190 goto out_inval;
7191 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7192 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7193 goto out_inval;
7194 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7195 goto out_inval;
7196 return NFS_OK;
7197out_inval:
7198 return -NFS4ERR_INVAL;
7199}
7200
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007201static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007202nfs41_same_server_scope(struct nfs41_server_scope *a,
7203 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007204{
7205 if (a->server_scope_sz == b->server_scope_sz &&
7206 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7207 return true;
7208
7209 return false;
7210}
7211
Andy Adamson02a95de2016-02-05 16:08:37 -05007212static void
7213nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7214{
7215}
7216
7217static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7218 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7219};
7220
Andy Adamson357f54d2010-12-14 10:11:57 -05007221/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007222 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007223 *
7224 * The 4.1 client currently uses the same TCP connection for the
7225 * fore and backchannel.
7226 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007227static
7228int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7229 struct rpc_xprt *xprt,
7230 struct nfs_client *clp,
7231 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007232{
7233 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007234 struct nfs41_bind_conn_to_session_args args = {
7235 .client = clp,
7236 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7237 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007238 struct nfs41_bind_conn_to_session_res res;
7239 struct rpc_message msg = {
7240 .rpc_proc =
7241 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007242 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007244 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007245 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007246 struct rpc_task_setup task_setup_data = {
7247 .rpc_client = clnt,
7248 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007249 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007250 .rpc_message = &msg,
7251 .flags = RPC_TASK_TIMEOUT,
7252 };
7253 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007254
7255 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007256
Trond Myklebust71a097c2015-02-18 09:27:18 -08007257 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7258 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7259 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007260
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007261 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7262 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7263 args.dir = NFS4_CDFC4_FORE;
7264
7265 task = rpc_run_task(&task_setup_data);
7266 if (!IS_ERR(task)) {
7267 status = task->tk_status;
7268 rpc_put_task(task);
7269 } else
7270 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007271 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007272 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007273 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007274 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7275 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7276 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007277 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007278 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007279 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007280 dprintk("NFS: %s: Unexpected direction from server\n",
7281 __func__);
7282 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007283 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007284 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007285 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007286 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7287 __func__);
7288 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007289 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007290 }
7291 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007292out:
7293 dprintk("<-- %s status= %d\n", __func__, status);
7294 return status;
7295}
7296
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007297struct rpc_bind_conn_calldata {
7298 struct nfs_client *clp;
7299 struct rpc_cred *cred;
7300};
7301
7302static int
7303nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7304 struct rpc_xprt *xprt,
7305 void *calldata)
7306{
7307 struct rpc_bind_conn_calldata *p = calldata;
7308
7309 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7310}
7311
7312int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7313{
7314 struct rpc_bind_conn_calldata data = {
7315 .clp = clp,
7316 .cred = cred,
7317 };
7318 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7319 nfs4_proc_bind_conn_to_session_callback, &data);
7320}
7321
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007322/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007323 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7324 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007325 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007326static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7327 .how = SP4_MACH_CRED,
7328 .enforce.u.words = {
7329 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7330 1 << (OP_EXCHANGE_ID - 32) |
7331 1 << (OP_CREATE_SESSION - 32) |
7332 1 << (OP_DESTROY_SESSION - 32) |
7333 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007334 },
7335 .allow.u.words = {
7336 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007337 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007338 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007339 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007340 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007341 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007342 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007343 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007344 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007345 1 << (OP_FREE_STATEID - 32) |
7346 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007347 }
7348};
7349
7350/*
7351 * Select the state protection mode for client `clp' given the server results
7352 * from exchange_id in `sp'.
7353 *
7354 * Returns 0 on success, negative errno otherwise.
7355 */
7356static int nfs4_sp4_select_mode(struct nfs_client *clp,
7357 struct nfs41_state_protection *sp)
7358{
7359 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7360 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7361 1 << (OP_EXCHANGE_ID - 32) |
7362 1 << (OP_CREATE_SESSION - 32) |
7363 1 << (OP_DESTROY_SESSION - 32) |
7364 1 << (OP_DESTROY_CLIENTID - 32)
7365 };
7366 unsigned int i;
7367
7368 if (sp->how == SP4_MACH_CRED) {
7369 /* Print state protect result */
7370 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7371 for (i = 0; i <= LAST_NFS4_OP; i++) {
7372 if (test_bit(i, sp->enforce.u.longs))
7373 dfprintk(MOUNT, " enforce op %d\n", i);
7374 if (test_bit(i, sp->allow.u.longs))
7375 dfprintk(MOUNT, " allow op %d\n", i);
7376 }
7377
7378 /* make sure nothing is on enforce list that isn't supported */
7379 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7380 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7381 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7382 return -EINVAL;
7383 }
7384 }
7385
7386 /*
7387 * Minimal mode - state operations are allowed to use machine
7388 * credential. Note this already happens by default, so the
7389 * client doesn't have to do anything more than the negotiation.
7390 *
7391 * NOTE: we don't care if EXCHANGE_ID is in the list -
7392 * we're already using the machine cred for exchange_id
7393 * and will never use a different cred.
7394 */
7395 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7396 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7397 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7398 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7399 dfprintk(MOUNT, "sp4_mach_cred:\n");
7400 dfprintk(MOUNT, " minimal mode enabled\n");
7401 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7402 } else {
7403 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7404 return -EINVAL;
7405 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007406
7407 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007408 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7409 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007410 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7411 dfprintk(MOUNT, " cleanup mode enabled\n");
7412 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7413 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007414
Andrew Elble99ade3c2015-12-02 09:39:51 -05007415 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7416 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7417 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7418 &clp->cl_sp4_flags);
7419 }
7420
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007421 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7422 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7423 dfprintk(MOUNT, " secinfo mode enabled\n");
7424 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7425 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007426
7427 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7428 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7429 dfprintk(MOUNT, " stateid mode enabled\n");
7430 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7431 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007432
7433 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7434 dfprintk(MOUNT, " write mode enabled\n");
7435 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7436 }
7437
7438 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7439 dfprintk(MOUNT, " commit mode enabled\n");
7440 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7441 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007442 }
7443
7444 return 0;
7445}
7446
Andy Adamson8d89bd72016-09-09 09:22:18 -04007447struct nfs41_exchange_id_data {
7448 struct nfs41_exchange_id_res res;
7449 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007450 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451 int rpc_status;
7452};
7453
7454static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007456 struct nfs41_exchange_id_data *cdata =
7457 (struct nfs41_exchange_id_data *)data;
7458 struct nfs_client *clp = cdata->args.client;
7459 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007460
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007461 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007462
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007463 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007465
7466 if (cdata->xprt && status == 0) {
7467 status = nfs4_detect_session_trunking(clp, &cdata->res,
7468 cdata->xprt);
7469 goto out;
7470 }
7471
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 if (status == 0)
7473 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007474
Chuck Lever177313f2012-05-21 22:44:58 -04007475 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476 clp->cl_clientid = cdata->res.clientid;
7477 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007478 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007480 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 &clp->cl_session->session_state);
7482 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007483 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007484
Chuck Leveracdeb692012-05-21 22:46:16 -04007485 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 clp->cl_serverowner = cdata->res.server_owner;
7487 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007488
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007489 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007490 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007491 clp->cl_implid = cdata->res.impl_id;
7492 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007493
Chuck Lever177313f2012-05-21 22:44:58 -04007494 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007495 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007497 dprintk("%s: server_scope mismatch detected\n",
7498 __func__);
7499 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007500 kfree(clp->cl_serverscope);
7501 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007502 }
7503
Chuck Lever177313f2012-05-21 22:44:58 -04007504 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007505 clp->cl_serverscope = cdata->res.server_scope;
7506 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007507 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007508 /* Save the EXCHANGE_ID verifier session trunk tests */
7509 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7510 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007511 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007512out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007513 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007514 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007515}
7516
7517static void nfs4_exchange_id_release(void *data)
7518{
7519 struct nfs41_exchange_id_data *cdata =
7520 (struct nfs41_exchange_id_data *)data;
7521
7522 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007523 if (cdata->xprt) {
7524 xprt_put(cdata->xprt);
7525 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7526 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007527 kfree(cdata->res.impl_id);
7528 kfree(cdata->res.server_scope);
7529 kfree(cdata->res.server_owner);
7530 kfree(cdata);
7531}
7532
7533static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7534 .rpc_call_done = nfs4_exchange_id_done,
7535 .rpc_release = nfs4_exchange_id_release,
7536};
7537
Benny Halevy99fe60d2009-04-01 09:22:29 -04007538/*
7539 * _nfs4_proc_exchange_id()
7540 *
7541 * Wrapper for EXCHANGE_ID operation.
7542 */
7543static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007544 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007545{
7546 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007547 struct rpc_message msg = {
7548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549 .rpc_cred = cred,
7550 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007551 struct rpc_task_setup task_setup_data = {
7552 .rpc_client = clp->cl_rpcclient,
7553 .callback_ops = &nfs4_exchange_id_call_ops,
7554 .rpc_message = &msg,
7555 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7556 };
7557 struct nfs41_exchange_id_data *calldata;
7558 struct rpc_task *task;
7559 int status = -EIO;
7560
7561 if (!atomic_inc_not_zero(&clp->cl_count))
7562 goto out;
7563
7564 status = -ENOMEM;
7565 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7566 if (!calldata)
7567 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568
Andy Adamsonad0849a2016-09-09 09:22:28 -04007569 if (!xprt)
7570 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007571
7572 status = nfs4_init_uniform_client_string(clp);
7573 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007574 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007575
7576 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7577 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7578 clp->cl_owner_id);
7579
Andy Adamson8d89bd72016-09-09 09:22:18 -04007580 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7581 GFP_NOFS);
7582 status = -ENOMEM;
7583 if (unlikely(calldata->res.server_owner == NULL))
7584 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007585
Andy Adamson8d89bd72016-09-09 09:22:18 -04007586 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007587 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007588 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007590
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7592 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007593 goto out_server_scope;
7594
7595 switch (sp4_how) {
7596 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007597 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007598 break;
7599
7600 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007601 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007602 break;
7603
7604 default:
7605 /* unsupported! */
7606 WARN_ON_ONCE(1);
7607 status = -EINVAL;
7608 goto out_impl_id;
7609 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007610 if (xprt) {
7611 calldata->xprt = xprt;
7612 task_setup_data.rpc_xprt = xprt;
7613 task_setup_data.flags =
7614 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7615 calldata->args.verifier = &clp->cl_confirm;
7616 } else {
7617 calldata->args.verifier = &verifier;
7618 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007619 calldata->args.client = clp;
7620#ifdef CONFIG_NFS_V4_1_MIGRATION
7621 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7622 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7623 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7624#else
7625 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7626 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7627#endif
7628 msg.rpc_argp = &calldata->args;
7629 msg.rpc_resp = &calldata->res;
7630 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007631
Andy Adamson8d89bd72016-09-09 09:22:18 -04007632 task = rpc_run_task(&task_setup_data);
7633 if (IS_ERR(task)) {
7634 status = PTR_ERR(task);
7635 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007636 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007637
Andy Adamsonad0849a2016-09-09 09:22:28 -04007638 if (!xprt) {
7639 status = rpc_wait_for_completion_task(task);
7640 if (!status)
7641 status = calldata->rpc_status;
7642 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007643 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007644
Andy Adamson8d89bd72016-09-09 09:22:18 -04007645 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007646out:
Chuck Lever177313f2012-05-21 22:44:58 -04007647 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007648 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007649 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007650 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007651 clp->cl_implid->date.seconds,
7652 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007653 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007654 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655
7656out_impl_id:
7657 kfree(calldata->res.impl_id);
7658out_server_scope:
7659 kfree(calldata->res.server_scope);
7660out_server_owner:
7661 kfree(calldata->res.server_owner);
7662out_calldata:
7663 kfree(calldata);
7664 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007665}
7666
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007667/*
7668 * nfs4_proc_exchange_id()
7669 *
7670 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7671 *
7672 * Since the clientid has expired, all compounds using sessions
7673 * associated with the stale clientid will be returning
7674 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7675 * be in some phase of session reset.
7676 *
7677 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7678 */
7679int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7680{
7681 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7682 int status;
7683
7684 /* try SP4_MACH_CRED if krb5i/p */
7685 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7686 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007687 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007688 if (!status)
7689 return 0;
7690 }
7691
7692 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007693 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007694}
7695
Andy Adamson04fa2c62016-09-09 09:22:29 -04007696/**
7697 * nfs4_test_session_trunk
7698 *
7699 * This is an add_xprt_test() test function called from
7700 * rpc_clnt_setup_test_and_add_xprt.
7701 *
7702 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7703 * and is dereferrenced in nfs4_exchange_id_release
7704 *
7705 * Upon success, add the new transport to the rpc_clnt
7706 *
7707 * @clnt: struct rpc_clnt to get new transport
7708 * @xprt: the rpc_xprt to test
7709 * @data: call data for _nfs4_proc_exchange_id.
7710 */
7711int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7712 void *data)
7713{
7714 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7715 u32 sp4_how;
7716
7717 dprintk("--> %s try %s\n", __func__,
7718 xprt->address_strings[RPC_DISPLAY_ADDR]);
7719
7720 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7721
7722 /* Test connection for session trunking. Async exchange_id call */
7723 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7724}
7725EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7726
Trond Myklebust66245532012-05-25 17:18:09 -04007727static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7728 struct rpc_cred *cred)
7729{
7730 struct rpc_message msg = {
7731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7732 .rpc_argp = clp,
7733 .rpc_cred = cred,
7734 };
7735 int status;
7736
7737 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007738 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007739 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007740 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007741 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7742 return status;
7743}
7744
7745static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7746 struct rpc_cred *cred)
7747{
7748 unsigned int loop;
7749 int ret;
7750
7751 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7752 ret = _nfs4_proc_destroy_clientid(clp, cred);
7753 switch (ret) {
7754 case -NFS4ERR_DELAY:
7755 case -NFS4ERR_CLIENTID_BUSY:
7756 ssleep(1);
7757 break;
7758 default:
7759 return ret;
7760 }
7761 }
7762 return 0;
7763}
7764
7765int nfs4_destroy_clientid(struct nfs_client *clp)
7766{
7767 struct rpc_cred *cred;
7768 int ret = 0;
7769
7770 if (clp->cl_mvops->minor_version < 1)
7771 goto out;
7772 if (clp->cl_exchange_flags == 0)
7773 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007774 if (clp->cl_preserve_clid)
7775 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007776 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007777 ret = nfs4_proc_destroy_clientid(clp, cred);
7778 if (cred)
7779 put_rpccred(cred);
7780 switch (ret) {
7781 case 0:
7782 case -NFS4ERR_STALE_CLIENTID:
7783 clp->cl_exchange_flags = 0;
7784 }
7785out:
7786 return ret;
7787}
7788
Andy Adamson2050f0c2009-04-01 09:22:30 -04007789struct nfs4_get_lease_time_data {
7790 struct nfs4_get_lease_time_args *args;
7791 struct nfs4_get_lease_time_res *res;
7792 struct nfs_client *clp;
7793};
7794
7795static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7796 void *calldata)
7797{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007798 struct nfs4_get_lease_time_data *data =
7799 (struct nfs4_get_lease_time_data *)calldata;
7800
7801 dprintk("--> %s\n", __func__);
7802 /* just setup sequence, do not trigger session recovery
7803 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007804 nfs41_setup_sequence(data->clp->cl_session,
7805 &data->args->la_seq_args,
7806 &data->res->lr_seq_res,
7807 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007808 dprintk("<-- %s\n", __func__);
7809}
7810
7811/*
7812 * Called from nfs4_state_manager thread for session setup, so don't recover
7813 * from sequence operation or clientid errors.
7814 */
7815static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7816{
7817 struct nfs4_get_lease_time_data *data =
7818 (struct nfs4_get_lease_time_data *)calldata;
7819
7820 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007821 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7822 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007823 switch (task->tk_status) {
7824 case -NFS4ERR_DELAY:
7825 case -NFS4ERR_GRACE:
7826 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7827 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7828 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007829 /* fall through */
7830 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007831 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007832 return;
7833 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007834 dprintk("<-- %s\n", __func__);
7835}
7836
Trond Myklebust17280172012-03-11 13:11:00 -04007837static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007838 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7839 .rpc_call_done = nfs4_get_lease_time_done,
7840};
7841
7842int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7843{
7844 struct rpc_task *task;
7845 struct nfs4_get_lease_time_args args;
7846 struct nfs4_get_lease_time_res res = {
7847 .lr_fsinfo = fsinfo,
7848 };
7849 struct nfs4_get_lease_time_data data = {
7850 .args = &args,
7851 .res = &res,
7852 .clp = clp,
7853 };
7854 struct rpc_message msg = {
7855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7856 .rpc_argp = &args,
7857 .rpc_resp = &res,
7858 };
7859 struct rpc_task_setup task_setup = {
7860 .rpc_client = clp->cl_rpcclient,
7861 .rpc_message = &msg,
7862 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007863 .callback_data = &data,
7864 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007865 };
7866 int status;
7867
Chuck Levera9c92d62013-08-09 12:48:18 -04007868 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007869 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007870 dprintk("--> %s\n", __func__);
7871 task = rpc_run_task(&task_setup);
7872
7873 if (IS_ERR(task))
7874 status = PTR_ERR(task);
7875 else {
7876 status = task->tk_status;
7877 rpc_put_task(task);
7878 }
7879 dprintk("<-- %s return %d\n", __func__, status);
7880
7881 return status;
7882}
7883
Andy Adamsonfc931582009-04-01 09:22:31 -04007884/*
7885 * Initialize the values to be used by the client in CREATE_SESSION
7886 * If nfs4_init_session set the fore channel request and response sizes,
7887 * use them.
7888 *
7889 * Set the back channel max_resp_sz_cached to zero to force the client to
7890 * always set csa_cachethis to FALSE because the current implementation
7891 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7892 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007893static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7894 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007895{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007896 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007897 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007898
Andy Adamson18aad3d2013-06-26 12:21:49 -04007899 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7900 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7901
Andy Adamsonfc931582009-04-01 09:22:31 -04007902 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007903 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7904 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007905 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007906 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007907
7908 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007909 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007910 __func__,
7911 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007912 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007913
7914 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007915 args->bc_attrs.max_rqst_sz = max_bc_payload;
7916 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007917 args->bc_attrs.max_resp_sz_cached = 0;
7918 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007919 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007920
7921 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7922 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7923 __func__,
7924 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7925 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7926 args->bc_attrs.max_reqs);
7927}
7928
Trond Myklebust79969dd2015-02-18 11:30:18 -08007929static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7930 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007931{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007932 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007934
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007935 if (rcvd->max_resp_sz > sent->max_resp_sz)
7936 return -EINVAL;
7937 /*
7938 * Our requested max_ops is the minimum we need; we're not
7939 * prepared to break up compounds into smaller pieces than that.
7940 * So, no point even trying to continue if the server won't
7941 * cooperate:
7942 */
7943 if (rcvd->max_ops < sent->max_ops)
7944 return -EINVAL;
7945 if (rcvd->max_reqs == 0)
7946 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007947 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7948 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007949 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007950}
7951
Trond Myklebust79969dd2015-02-18 11:30:18 -08007952static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7953 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007954{
7955 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007957
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007958 if (!(res->flags & SESSION4_BACK_CHAN))
7959 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007960 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7961 return -EINVAL;
7962 if (rcvd->max_resp_sz < sent->max_resp_sz)
7963 return -EINVAL;
7964 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7965 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007966 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007967 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007968 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007969 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007970out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007971 return 0;
7972}
Andy Adamson8d353012009-04-01 09:22:32 -04007973
Andy Adamson8d353012009-04-01 09:22:32 -04007974static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007975 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007976{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007977 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007978
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007980 if (ret)
7981 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007982 return nfs4_verify_back_channel_attrs(args, res);
7983}
7984
7985static void nfs4_update_session(struct nfs4_session *session,
7986 struct nfs41_create_session_res *res)
7987{
7988 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007989 /* Mark client id and session as being confirmed */
7990 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7991 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007992 session->flags = res->flags;
7993 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007994 if (res->flags & SESSION4_BACK_CHAN)
7995 memcpy(&session->bc_attrs, &res->bc_attrs,
7996 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007997}
7998
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007999static int _nfs4_proc_create_session(struct nfs_client *clp,
8000 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008001{
8002 struct nfs4_session *session = clp->cl_session;
8003 struct nfs41_create_session_args args = {
8004 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008005 .clientid = clp->cl_clientid,
8006 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008007 .cb_program = NFS4_CALLBACK,
8008 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008009 struct nfs41_create_session_res res;
8010
Andy Adamsonfc931582009-04-01 09:22:31 -04008011 struct rpc_message msg = {
8012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8013 .rpc_argp = &args,
8014 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008015 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008016 };
8017 int status;
8018
Chuck Lever6b26cc82016-05-02 14:40:40 -04008019 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008020 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008021
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008022 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008023 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008024
Trond Myklebustb519d402016-09-11 14:50:01 -04008025 switch (status) {
8026 case -NFS4ERR_STALE_CLIENTID:
8027 case -NFS4ERR_DELAY:
8028 case -ETIMEDOUT:
8029 case -EACCES:
8030 case -EAGAIN:
8031 goto out;
8032 };
8033
8034 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008035 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008036 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008037 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008038 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008039 if (status)
8040 goto out;
8041 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008042 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008043out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008044 return status;
8045}
8046
8047/*
8048 * Issues a CREATE_SESSION operation to the server.
8049 * It is the responsibility of the caller to verify the session is
8050 * expired before calling this routine.
8051 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008052int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008053{
8054 int status;
8055 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008056 struct nfs4_session *session = clp->cl_session;
8057
8058 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8059
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008060 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008061 if (status)
8062 goto out;
8063
Andy Adamsonaacd5532011-11-09 13:58:21 -05008064 /* Init or reset the session slot tables */
8065 status = nfs4_setup_session_slot_tables(session);
8066 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008067 if (status)
8068 goto out;
8069
8070 ptr = (unsigned *)&session->sess_id.data[0];
8071 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8072 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008073out:
8074 dprintk("<-- %s\n", __func__);
8075 return status;
8076}
8077
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008078/*
8079 * Issue the over-the-wire RPC DESTROY_SESSION.
8080 * The caller must serialize access to this routine.
8081 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008082int nfs4_proc_destroy_session(struct nfs4_session *session,
8083 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008084{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008085 struct rpc_message msg = {
8086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8087 .rpc_argp = session,
8088 .rpc_cred = cred,
8089 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008090 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008091
8092 dprintk("--> nfs4_proc_destroy_session\n");
8093
8094 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008095 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8096 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008097
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008098 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008099 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008100
8101 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008102 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008103 "Session has been destroyed regardless...\n", status);
8104
8105 dprintk("<-- nfs4_proc_destroy_session\n");
8106 return status;
8107}
8108
Trond Myklebust7b38c362012-05-23 13:23:31 -04008109/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008110 * Renew the cl_session lease.
8111 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008112struct nfs4_sequence_data {
8113 struct nfs_client *clp;
8114 struct nfs4_sequence_args args;
8115 struct nfs4_sequence_res res;
8116};
8117
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008118static void nfs41_sequence_release(void *data)
8119{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008120 struct nfs4_sequence_data *calldata = data;
8121 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008122
Alexandros Batsakis71358402010-02-05 03:45:05 -08008123 if (atomic_read(&clp->cl_count) > 1)
8124 nfs4_schedule_state_renewal(clp);
8125 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008126 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008127}
8128
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008129static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8130{
8131 switch(task->tk_status) {
8132 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008133 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8134 return -EAGAIN;
8135 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008136 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008137 }
8138 return 0;
8139}
8140
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008141static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008142{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008143 struct nfs4_sequence_data *calldata = data;
8144 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008145
Trond Myklebust14516c32010-07-31 14:29:06 -04008146 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8147 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008148
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008149 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008150 if (task->tk_status < 0) {
8151 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008152 if (atomic_read(&clp->cl_count) == 1)
8153 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008154
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008155 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8156 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008157 return;
8158 }
8159 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008160 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008161out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008162 dprintk("<-- %s\n", __func__);
8163}
8164
8165static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8166{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008167 struct nfs4_sequence_data *calldata = data;
8168 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008169 struct nfs4_sequence_args *args;
8170 struct nfs4_sequence_res *res;
8171
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008172 args = task->tk_msg.rpc_argp;
8173 res = task->tk_msg.rpc_resp;
8174
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008175 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008176}
8177
8178static const struct rpc_call_ops nfs41_sequence_ops = {
8179 .rpc_call_done = nfs41_sequence_call_done,
8180 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008181 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008182};
8183
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008184static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8185 struct rpc_cred *cred,
8186 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008188 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008189 struct rpc_message msg = {
8190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8191 .rpc_cred = cred,
8192 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008193 struct rpc_task_setup task_setup_data = {
8194 .rpc_client = clp->cl_rpcclient,
8195 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008196 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008197 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008198 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008199
Alexandros Batsakis71358402010-02-05 03:45:05 -08008200 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008201 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008202 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008203 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008204 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008205 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008206 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008207 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008208 if (is_privileged)
8209 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008210 msg.rpc_argp = &calldata->args;
8211 msg.rpc_resp = &calldata->res;
8212 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008213 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008214
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008215 return rpc_run_task(&task_setup_data);
8216}
8217
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008218static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008219{
8220 struct rpc_task *task;
8221 int ret = 0;
8222
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008223 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008224 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008225 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008226 if (IS_ERR(task))
8227 ret = PTR_ERR(task);
8228 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008229 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008230 dprintk("<-- %s status=%d\n", __func__, ret);
8231 return ret;
8232}
8233
8234static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8235{
8236 struct rpc_task *task;
8237 int ret;
8238
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008239 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008240 if (IS_ERR(task)) {
8241 ret = PTR_ERR(task);
8242 goto out;
8243 }
8244 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008245 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008246 ret = task->tk_status;
8247 rpc_put_task(task);
8248out:
8249 dprintk("<-- %s status=%d\n", __func__, ret);
8250 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008251}
8252
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008253struct nfs4_reclaim_complete_data {
8254 struct nfs_client *clp;
8255 struct nfs41_reclaim_complete_args arg;
8256 struct nfs41_reclaim_complete_res res;
8257};
8258
8259static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8260{
8261 struct nfs4_reclaim_complete_data *calldata = data;
8262
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008263 nfs41_setup_sequence(calldata->clp->cl_session,
8264 &calldata->arg.seq_args,
8265 &calldata->res.seq_res,
8266 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008267}
8268
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008269static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8270{
8271 switch(task->tk_status) {
8272 case 0:
8273 case -NFS4ERR_COMPLETE_ALREADY:
8274 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8275 break;
8276 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008277 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008278 /* fall through */
8279 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008280 return -EAGAIN;
8281 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008282 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008283 }
8284 return 0;
8285}
8286
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008287static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8288{
8289 struct nfs4_reclaim_complete_data *calldata = data;
8290 struct nfs_client *clp = calldata->clp;
8291 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8292
8293 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008294 if (!nfs41_sequence_done(task, res))
8295 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008296
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008297 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008298 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8299 rpc_restart_call_prepare(task);
8300 return;
8301 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008302 dprintk("<-- %s\n", __func__);
8303}
8304
8305static void nfs4_free_reclaim_complete_data(void *data)
8306{
8307 struct nfs4_reclaim_complete_data *calldata = data;
8308
8309 kfree(calldata);
8310}
8311
8312static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8313 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8314 .rpc_call_done = nfs4_reclaim_complete_done,
8315 .rpc_release = nfs4_free_reclaim_complete_data,
8316};
8317
8318/*
8319 * Issue a global reclaim complete.
8320 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008321static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8322 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008323{
8324 struct nfs4_reclaim_complete_data *calldata;
8325 struct rpc_task *task;
8326 struct rpc_message msg = {
8327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008328 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008329 };
8330 struct rpc_task_setup task_setup_data = {
8331 .rpc_client = clp->cl_rpcclient,
8332 .rpc_message = &msg,
8333 .callback_ops = &nfs4_reclaim_complete_call_ops,
8334 .flags = RPC_TASK_ASYNC,
8335 };
8336 int status = -ENOMEM;
8337
8338 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008339 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008340 if (calldata == NULL)
8341 goto out;
8342 calldata->clp = clp;
8343 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008344
Chuck Levera9c92d62013-08-09 12:48:18 -04008345 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008346 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008347 msg.rpc_argp = &calldata->arg;
8348 msg.rpc_resp = &calldata->res;
8349 task_setup_data.callback_data = calldata;
8350 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008351 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008352 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008353 goto out;
8354 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008355 status = nfs4_wait_for_completion_rpc_task(task);
8356 if (status == 0)
8357 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008358 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008359 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008360out:
8361 dprintk("<-- %s status=%d\n", __func__, status);
8362 return status;
8363}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008364
8365static void
8366nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8367{
8368 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008369 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008370 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008371
8372 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008373 nfs41_setup_sequence(session, &lgp->args.seq_args,
8374 &lgp->res.seq_res, task);
8375 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008376}
8377
8378static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8379{
8380 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008381
8382 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008383 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008384 dprintk("<-- %s\n", __func__);
8385}
8386
8387static int
8388nfs4_layoutget_handle_exception(struct rpc_task *task,
8389 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8390{
Trond Myklebustee314c22012-10-01 17:25:48 -07008391 struct inode *inode = lgp->args.inode;
8392 struct nfs_server *server = NFS_SERVER(inode);
8393 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008394 int nfs4err = task->tk_status;
8395 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008396 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008397
Boaz Harroshed7e5422014-01-22 20:34:54 +02008398 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008399
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008400 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008401 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008402 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008403
8404 /*
8405 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8406 * on the file. set tk_status to -ENODATA to tell upper layer to
8407 * retry go inband.
8408 */
8409 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008410 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008411 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008412 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008413 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8414 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8415 */
8416 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008417 status = -EOVERFLOW;
8418 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008419 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008420 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008421 * (or clients) writing to the same RAID stripe except when
8422 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008423 *
8424 * Treat it like we would RECALLCONFLICT -- we retry for a little
8425 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008426 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008427 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008428 if (lgp->args.minlength == 0) {
8429 status = -EOVERFLOW;
8430 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008431 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008432 status = -EBUSY;
8433 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008434 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008435 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008436 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008437 case -NFS4ERR_DELEG_REVOKED:
8438 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008439 case -NFS4ERR_EXPIRED:
8440 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008441 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008442 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008443 lo = NFS_I(inode)->layout;
8444 /* If the open stateid was bad, then recover it. */
8445 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8446 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008447 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008448 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008449 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008450 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008451 break;
8452 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008453
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008454 /*
8455 * Mark the bad layout state as invalid, then retry
8456 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008457 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008458 spin_unlock(&inode->i_lock);
8459 pnfs_free_lseg_list(&head);
8460 status = -EAGAIN;
8461 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008462 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008463
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008464 err = nfs4_handle_exception(server, nfs4err, exception);
8465 if (!status) {
8466 if (exception->retry)
8467 status = -EAGAIN;
8468 else
8469 status = err;
8470 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008471out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008473 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008474}
8475
Idan Kedar85541162012-08-02 11:47:10 +03008476static size_t max_response_pages(struct nfs_server *server)
8477{
8478 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8479 return nfs_page_array_len(0, max_resp_sz);
8480}
8481
8482static void nfs4_free_pages(struct page **pages, size_t size)
8483{
8484 int i;
8485
8486 if (!pages)
8487 return;
8488
8489 for (i = 0; i < size; i++) {
8490 if (!pages[i])
8491 break;
8492 __free_page(pages[i]);
8493 }
8494 kfree(pages);
8495}
8496
8497static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8498{
8499 struct page **pages;
8500 int i;
8501
8502 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8503 if (!pages) {
8504 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8505 return NULL;
8506 }
8507
8508 for (i = 0; i < size; i++) {
8509 pages[i] = alloc_page(gfp_flags);
8510 if (!pages[i]) {
8511 dprintk("%s: failed to allocate page\n", __func__);
8512 nfs4_free_pages(pages, size);
8513 return NULL;
8514 }
8515 }
8516
8517 return pages;
8518}
8519
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008520static void nfs4_layoutget_release(void *calldata)
8521{
8522 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008523 struct inode *inode = lgp->args.inode;
8524 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008525 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008526
8527 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008528 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008529 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008530 put_nfs_open_context(lgp->args.ctx);
8531 kfree(calldata);
8532 dprintk("<-- %s\n", __func__);
8533}
8534
8535static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8536 .rpc_call_prepare = nfs4_layoutget_prepare,
8537 .rpc_call_done = nfs4_layoutget_done,
8538 .rpc_release = nfs4_layoutget_release,
8539};
8540
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008541struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008542nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008543{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008544 struct inode *inode = lgp->args.inode;
8545 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008546 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008547 struct rpc_task *task;
8548 struct rpc_message msg = {
8549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8550 .rpc_argp = &lgp->args,
8551 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008552 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553 };
8554 struct rpc_task_setup task_setup_data = {
8555 .rpc_client = server->client,
8556 .rpc_message = &msg,
8557 .callback_ops = &nfs4_layoutget_call_ops,
8558 .callback_data = lgp,
8559 .flags = RPC_TASK_ASYNC,
8560 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008561 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008562 struct nfs4_exception exception = {
8563 .inode = inode,
8564 .timeout = *timeout,
8565 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566 int status = 0;
8567
8568 dprintk("--> %s\n", __func__);
8569
Peng Tao4bd5a982014-11-17 11:05:17 +08008570 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8571 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8572
Idan Kedar85541162012-08-02 11:47:10 +03008573 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8574 if (!lgp->args.layout.pages) {
8575 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008576 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008577 }
8578 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8579
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008580 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008581 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008582 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008583
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008584 task = rpc_run_task(&task_setup_data);
8585 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008586 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008587 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008588 if (status == 0) {
8589 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8590 *timeout = exception.timeout;
8591 }
8592
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008593 trace_nfs4_layoutget(lgp->args.ctx,
8594 &lgp->args.range,
8595 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008596 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008597 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008598
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008599 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8600 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008601 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008602 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008603 rpc_put_task(task);
8604 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008605 if (status)
8606 return ERR_PTR(status);
8607 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008608}
8609
Benny Halevycbe82602011-05-22 19:52:37 +03008610static void
8611nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8612{
8613 struct nfs4_layoutreturn *lrp = calldata;
8614
8615 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008616 nfs41_setup_sequence(lrp->clp->cl_session,
8617 &lrp->args.seq_args,
8618 &lrp->res.seq_res,
8619 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008620}
8621
8622static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8623{
8624 struct nfs4_layoutreturn *lrp = calldata;
8625 struct nfs_server *server;
8626
8627 dprintk("--> %s\n", __func__);
8628
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008629 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008630 return;
8631
8632 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008633 switch (task->tk_status) {
8634 default:
8635 task->tk_status = 0;
8636 case 0:
8637 break;
8638 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008639 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008640 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008641 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008642 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008643 return;
8644 }
Benny Halevycbe82602011-05-22 19:52:37 +03008645 dprintk("<-- %s\n", __func__);
8646}
8647
8648static void nfs4_layoutreturn_release(void *calldata)
8649{
8650 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008651 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008652
8653 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008654 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008655 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008656 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008657 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8658 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008659 pnfs_put_layout_hdr(lrp->args.layout);
8660 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008661 kfree(calldata);
8662 dprintk("<-- %s\n", __func__);
8663}
8664
8665static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8666 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8667 .rpc_call_done = nfs4_layoutreturn_done,
8668 .rpc_release = nfs4_layoutreturn_release,
8669};
8670
Peng Tao6c166052014-11-17 09:30:40 +08008671int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008672{
8673 struct rpc_task *task;
8674 struct rpc_message msg = {
8675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8676 .rpc_argp = &lrp->args,
8677 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008678 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008679 };
8680 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008681 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008682 .rpc_message = &msg,
8683 .callback_ops = &nfs4_layoutreturn_call_ops,
8684 .callback_data = lrp,
8685 };
Peng Tao6c166052014-11-17 09:30:40 +08008686 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008687
Andrew Elble99ade3c2015-12-02 09:39:51 -05008688 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8689 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8690 &task_setup_data.rpc_client, &msg);
8691
Benny Halevycbe82602011-05-22 19:52:37 +03008692 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008693 if (!sync) {
8694 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8695 if (!lrp->inode) {
8696 nfs4_layoutreturn_release(lrp);
8697 return -EAGAIN;
8698 }
8699 task_setup_data.flags |= RPC_TASK_ASYNC;
8700 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008701 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008702 task = rpc_run_task(&task_setup_data);
8703 if (IS_ERR(task))
8704 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008705 if (sync)
8706 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008707 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008708 dprintk("<-- %s status=%d\n", __func__, status);
8709 rpc_put_task(task);
8710 return status;
8711}
8712
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008713static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008714_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8715 struct pnfs_device *pdev,
8716 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008717{
8718 struct nfs4_getdeviceinfo_args args = {
8719 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008720 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8721 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008722 };
8723 struct nfs4_getdeviceinfo_res res = {
8724 .pdev = pdev,
8725 };
8726 struct rpc_message msg = {
8727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8728 .rpc_argp = &args,
8729 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008730 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008731 };
8732 int status;
8733
8734 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008735 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008736 if (res.notification & ~args.notify_types)
8737 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008738 if (res.notification != args.notify_types)
8739 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008740
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008741 dprintk("<-- %s status=%d\n", __func__, status);
8742
8743 return status;
8744}
8745
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008746int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8747 struct pnfs_device *pdev,
8748 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008749{
8750 struct nfs4_exception exception = { };
8751 int err;
8752
8753 do {
8754 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008755 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008756 &exception);
8757 } while (exception.retry);
8758 return err;
8759}
8760EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8761
Andy Adamson863a3c62011-03-23 13:27:54 +00008762static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8763{
8764 struct nfs4_layoutcommit_data *data = calldata;
8765 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008766 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008767
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008768 nfs41_setup_sequence(session,
8769 &data->args.seq_args,
8770 &data->res.seq_res,
8771 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008772}
8773
8774static void
8775nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8776{
8777 struct nfs4_layoutcommit_data *data = calldata;
8778 struct nfs_server *server = NFS_SERVER(data->args.inode);
8779
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008780 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008781 return;
8782
8783 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008784 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8785 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8786 case -NFS4ERR_BADLAYOUT: /* no layout */
8787 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008788 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008789 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008790 break;
8791 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008792 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008793 rpc_restart_call_prepare(task);
8794 return;
8795 }
8796 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008797}
8798
8799static void nfs4_layoutcommit_release(void *calldata)
8800{
8801 struct nfs4_layoutcommit_data *data = calldata;
8802
Andy Adamsondb29c082011-07-30 20:52:38 -04008803 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008804 nfs_post_op_update_inode_force_wcc(data->args.inode,
8805 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008806 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008807 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008808 kfree(data);
8809}
8810
8811static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8812 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8813 .rpc_call_done = nfs4_layoutcommit_done,
8814 .rpc_release = nfs4_layoutcommit_release,
8815};
8816
8817int
Andy Adamsonef311532011-03-12 02:58:10 -05008818nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008819{
8820 struct rpc_message msg = {
8821 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8822 .rpc_argp = &data->args,
8823 .rpc_resp = &data->res,
8824 .rpc_cred = data->cred,
8825 };
8826 struct rpc_task_setup task_setup_data = {
8827 .task = &data->task,
8828 .rpc_client = NFS_CLIENT(data->args.inode),
8829 .rpc_message = &msg,
8830 .callback_ops = &nfs4_layoutcommit_ops,
8831 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008832 };
8833 struct rpc_task *task;
8834 int status = 0;
8835
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008836 dprintk("NFS: initiating layoutcommit call. sync %d "
8837 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008838 data->args.lastbytewritten,
8839 data->args.inode->i_ino);
8840
Trond Myklebust472e2592015-02-05 16:50:30 -05008841 if (!sync) {
8842 data->inode = nfs_igrab_and_active(data->args.inode);
8843 if (data->inode == NULL) {
8844 nfs4_layoutcommit_release(data);
8845 return -EAGAIN;
8846 }
8847 task_setup_data.flags = RPC_TASK_ASYNC;
8848 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008849 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008850 task = rpc_run_task(&task_setup_data);
8851 if (IS_ERR(task))
8852 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008853 if (sync)
8854 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008855 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008856 dprintk("%s: status %d\n", __func__, status);
8857 rpc_put_task(task);
8858 return status;
8859}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860
Andy Adamson97431202013-08-08 10:57:56 -04008861/**
8862 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8863 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8864 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008865static int
8866_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008867 struct nfs_fsinfo *info,
8868 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008869{
8870 struct nfs41_secinfo_no_name_args args = {
8871 .style = SECINFO_STYLE_CURRENT_FH,
8872 };
8873 struct nfs4_secinfo_res res = {
8874 .flavors = flavors,
8875 };
8876 struct rpc_message msg = {
8877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8878 .rpc_argp = &args,
8879 .rpc_resp = &res,
8880 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008881 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008882 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008883 int status;
8884
8885 if (use_integrity) {
8886 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008887 cred = nfs4_get_clid_cred(server->nfs_client);
8888 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008889 }
8890
8891 dprintk("--> %s\n", __func__);
8892 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8893 &res.seq_res, 0);
8894 dprintk("<-- %s status=%d\n", __func__, status);
8895
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008896 if (cred)
8897 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008898
8899 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008900}
8901
8902static int
8903nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8904 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8905{
8906 struct nfs4_exception exception = { };
8907 int err;
8908 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008909 /* first try using integrity protection */
8910 err = -NFS4ERR_WRONGSEC;
8911
8912 /* try to use integrity protection with machine cred */
8913 if (_nfs4_is_integrity_protected(server->nfs_client))
8914 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8915 flavors, true);
8916
8917 /*
8918 * if unable to use integrity protection, or SECINFO with
8919 * integrity protection returns NFS4ERR_WRONGSEC (which is
8920 * disallowed by spec, but exists in deployed servers) use
8921 * the current filesystem's rpc_client and the user cred.
8922 */
8923 if (err == -NFS4ERR_WRONGSEC)
8924 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8925 flavors, false);
8926
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008927 switch (err) {
8928 case 0:
8929 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008930 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008931 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008932 default:
8933 err = nfs4_handle_exception(server, err, &exception);
8934 }
8935 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008936out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008937 return err;
8938}
8939
8940static int
8941nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8942 struct nfs_fsinfo *info)
8943{
8944 int err;
8945 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008946 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008947 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008948 struct nfs4_secinfo4 *secinfo;
8949 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008950
8951 page = alloc_page(GFP_KERNEL);
8952 if (!page) {
8953 err = -ENOMEM;
8954 goto out;
8955 }
8956
8957 flavors = page_address(page);
8958 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8959
8960 /*
8961 * Fall back on "guess and check" method if
8962 * the server doesn't support SECINFO_NO_NAME
8963 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008964 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008965 err = nfs4_find_root_sec(server, fhandle, info);
8966 goto out_freepage;
8967 }
8968 if (err)
8969 goto out_freepage;
8970
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008971 for (i = 0; i < flavors->num_flavors; i++) {
8972 secinfo = &flavors->flavors[i];
8973
8974 switch (secinfo->flavor) {
8975 case RPC_AUTH_NULL:
8976 case RPC_AUTH_UNIX:
8977 case RPC_AUTH_GSS:
8978 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8979 &secinfo->flavor_info);
8980 break;
8981 default:
8982 flavor = RPC_AUTH_MAXFLAVOR;
8983 break;
8984 }
8985
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008986 if (!nfs_auth_info_match(&server->auth_info, flavor))
8987 flavor = RPC_AUTH_MAXFLAVOR;
8988
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008989 if (flavor != RPC_AUTH_MAXFLAVOR) {
8990 err = nfs4_lookup_root_sec(server, fhandle,
8991 info, flavor);
8992 if (!err)
8993 break;
8994 }
8995 }
8996
8997 if (flavor == RPC_AUTH_MAXFLAVOR)
8998 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008999
9000out_freepage:
9001 put_page(page);
9002 if (err == -EACCES)
9003 return -EPERM;
9004out:
9005 return err;
9006}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009007
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009008static int _nfs41_test_stateid(struct nfs_server *server,
9009 nfs4_stateid *stateid,
9010 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009011{
9012 int status;
9013 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009014 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009015 };
9016 struct nfs41_test_stateid_res res;
9017 struct rpc_message msg = {
9018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9019 .rpc_argp = &args,
9020 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009021 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009022 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009023 struct rpc_clnt *rpc_client = server->client;
9024
9025 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9026 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009027
Chuck Lever38527b12012-07-11 16:30:23 -04009028 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009029 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009030 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009031 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009032 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009033 if (status != NFS_OK) {
9034 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009035 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009036 }
9037 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009038 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009039}
9040
Trond Myklebust43912bb2016-09-22 13:38:56 -04009041static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9042 int err, struct nfs4_exception *exception)
9043{
9044 exception->retry = 0;
9045 switch(err) {
9046 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009047 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009048 nfs4_handle_exception(server, err, exception);
9049 break;
9050 case -NFS4ERR_BADSESSION:
9051 case -NFS4ERR_BADSLOT:
9052 case -NFS4ERR_BAD_HIGH_SLOT:
9053 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9054 case -NFS4ERR_DEADSESSION:
9055 nfs4_do_handle_exception(server, err, exception);
9056 }
9057}
9058
Chuck Lever38527b12012-07-11 16:30:23 -04009059/**
9060 * nfs41_test_stateid - perform a TEST_STATEID operation
9061 *
9062 * @server: server / transport on which to perform the operation
9063 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009064 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009065 *
9066 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9067 * Otherwise a negative NFS4ERR value is returned if the operation
9068 * failed or the state ID is not currently valid.
9069 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009070static int nfs41_test_stateid(struct nfs_server *server,
9071 nfs4_stateid *stateid,
9072 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009073{
9074 struct nfs4_exception exception = { };
9075 int err;
9076 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009077 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009078 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009079 } while (exception.retry);
9080 return err;
9081}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009082
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009083struct nfs_free_stateid_data {
9084 struct nfs_server *server;
9085 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009086 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009087};
9088
9089static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9090{
9091 struct nfs_free_stateid_data *data = calldata;
9092 nfs41_setup_sequence(nfs4_get_session(data->server),
9093 &data->args.seq_args,
9094 &data->res.seq_res,
9095 task);
9096}
9097
9098static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9099{
9100 struct nfs_free_stateid_data *data = calldata;
9101
9102 nfs41_sequence_done(task, &data->res.seq_res);
9103
9104 switch (task->tk_status) {
9105 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009106 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009107 rpc_restart_call_prepare(task);
9108 }
9109}
9110
9111static void nfs41_free_stateid_release(void *calldata)
9112{
9113 kfree(calldata);
9114}
9115
Trond Myklebust17f26b12013-08-21 15:48:42 -04009116static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117 .rpc_call_prepare = nfs41_free_stateid_prepare,
9118 .rpc_call_done = nfs41_free_stateid_done,
9119 .rpc_release = nfs41_free_stateid_release,
9120};
9121
9122static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009123 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009124 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009125 bool privileged)
9126{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009127 struct rpc_message msg = {
9128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009129 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009130 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009131 struct rpc_task_setup task_setup = {
9132 .rpc_client = server->client,
9133 .rpc_message = &msg,
9134 .callback_ops = &nfs41_free_stateid_ops,
9135 .flags = RPC_TASK_ASYNC,
9136 };
9137 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009138
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009139 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9140 &task_setup.rpc_client, &msg);
9141
Chuck Lever38527b12012-07-11 16:30:23 -04009142 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009143 data = kmalloc(sizeof(*data), GFP_NOFS);
9144 if (!data)
9145 return ERR_PTR(-ENOMEM);
9146 data->server = server;
9147 nfs4_stateid_copy(&data->args.stateid, stateid);
9148
9149 task_setup.callback_data = data;
9150
9151 msg.rpc_argp = &data->args;
9152 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009153 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009154 if (privileged)
9155 nfs4_set_sequence_privileged(&data->args.seq_args);
9156
9157 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009158}
9159
Chuck Lever38527b12012-07-11 16:30:23 -04009160/**
9161 * nfs41_free_stateid - perform a FREE_STATEID operation
9162 *
9163 * @server: server / transport on which to perform the operation
9164 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009165 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009166 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009167 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009168 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009169 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009170static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009171 const nfs4_stateid *stateid,
9172 struct rpc_cred *cred,
9173 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009174{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009175 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009176
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009177 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009178 if (IS_ERR(task))
9179 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009180 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009181 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009182}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009183
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009184static void
9185nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009186{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009187 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009188
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009189 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009190 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009191}
9192
Trond Myklebust36281ca2012-03-04 18:13:56 -05009193static bool nfs41_match_stateid(const nfs4_stateid *s1,
9194 const nfs4_stateid *s2)
9195{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009196 if (s1->type != s2->type)
9197 return false;
9198
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009199 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009200 return false;
9201
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009202 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009203 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009204 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009205 return true;
9206
9207 return false;
9208}
9209
Andy Adamson557134a2009-04-01 09:21:53 -04009210#endif /* CONFIG_NFS_V4_1 */
9211
Trond Myklebust36281ca2012-03-04 18:13:56 -05009212static bool nfs4_match_stateid(const nfs4_stateid *s1,
9213 const nfs4_stateid *s2)
9214{
Trond Myklebustf597c532012-03-04 18:13:56 -05009215 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009216}
9217
9218
Trond Myklebust17280172012-03-11 13:11:00 -04009219static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009220 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009221 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009222 .recover_open = nfs4_open_reclaim,
9223 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009224 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009225 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009226};
9227
Andy Adamson591d71c2009-04-01 09:22:47 -04009228#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009229static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009230 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9231 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9232 .recover_open = nfs4_open_reclaim,
9233 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009234 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009235 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009236 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009237};
9238#endif /* CONFIG_NFS_V4_1 */
9239
Trond Myklebust17280172012-03-11 13:11:00 -04009240static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009241 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009242 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009243 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009244 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009245 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009246};
9247
Andy Adamson591d71c2009-04-01 09:22:47 -04009248#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009249static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009250 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9251 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009252 .recover_open = nfs41_open_expired,
9253 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009254 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009255};
9256#endif /* CONFIG_NFS_V4_1 */
9257
Trond Myklebust17280172012-03-11 13:11:00 -04009258static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009259 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009260 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009261 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009262};
9263
9264#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009265static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009266 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009267 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009268 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009269};
9270#endif
9271
Chuck Leverec011fe2013-10-17 14:12:39 -04009272static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009273 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009274 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009275};
9276
9277#if defined(CONFIG_NFS_V4_1)
9278static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009279 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009280 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009281};
9282#endif /* CONFIG_NFS_V4_1 */
9283
Trond Myklebust97dc1352010-06-16 09:52:26 -04009284static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9285 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009286 .init_caps = NFS_CAP_READDIRPLUS
9287 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009288 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009289 .init_client = nfs40_init_client,
9290 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009291 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009292 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009293 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009294 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009295 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009296 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009297 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9298 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9299 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009300 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009301};
9302
9303#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009304static struct nfs_seqid *
9305nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9306{
9307 return NULL;
9308}
9309
Trond Myklebust97dc1352010-06-16 09:52:26 -04009310static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9311 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009312 .init_caps = NFS_CAP_READDIRPLUS
9313 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009314 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009315 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009316 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009317 .init_client = nfs41_init_client,
9318 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009319 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009320 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009321 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009322 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009323 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009324 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009325 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009326 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9327 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9328 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009329 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009330};
9331#endif
9332
Steve Dickson42c2c422013-05-22 12:50:38 -04009333#if defined(CONFIG_NFS_V4_2)
9334static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9335 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009336 .init_caps = NFS_CAP_READDIRPLUS
9337 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009338 | NFS_CAP_POSIX_LOCK
9339 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009340 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009341 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009342 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009343 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009344 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009345 | NFS_CAP_LAYOUTSTATS
9346 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009347 .init_client = nfs41_init_client,
9348 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009349 .match_stateid = nfs41_match_stateid,
9350 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009351 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009352 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009353 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009354 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009355 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009356 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9357 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9358 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009359 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009360};
9361#endif
9362
Trond Myklebust97dc1352010-06-16 09:52:26 -04009363const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9364 [0] = &nfs_v4_0_minor_ops,
9365#if defined(CONFIG_NFS_V4_1)
9366 [1] = &nfs_v4_1_minor_ops,
9367#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009368#if defined(CONFIG_NFS_V4_2)
9369 [2] = &nfs_v4_2_minor_ops,
9370#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009371};
9372
Trond Myklebust13997822016-07-24 17:10:52 -04009373static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009374{
9375 ssize_t error, error2;
9376
9377 error = generic_listxattr(dentry, list, size);
9378 if (error < 0)
9379 return error;
9380 if (list) {
9381 list += error;
9382 size -= error;
9383 }
9384
9385 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9386 if (error2 < 0)
9387 return error2;
9388 return error + error2;
9389}
9390
Trond Myklebust17f26b12013-08-21 15:48:42 -04009391static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009392 .create = nfs_create,
9393 .lookup = nfs_lookup,
9394 .atomic_open = nfs_atomic_open,
9395 .link = nfs_link,
9396 .unlink = nfs_unlink,
9397 .symlink = nfs_symlink,
9398 .mkdir = nfs_mkdir,
9399 .rmdir = nfs_rmdir,
9400 .mknod = nfs_mknod,
9401 .rename = nfs_rename,
9402 .permission = nfs_permission,
9403 .getattr = nfs_getattr,
9404 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009405 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009406};
9407
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009408static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009409 .permission = nfs_permission,
9410 .getattr = nfs_getattr,
9411 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009412 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009413};
9414
David Howells509de812006-08-22 20:06:11 -04009415const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 .version = 4, /* protocol version */
9417 .dentry_ops = &nfs4_dentry_operations,
9418 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009419 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009420 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009422 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009423 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .getattr = nfs4_proc_getattr,
9425 .setattr = nfs4_proc_setattr,
9426 .lookup = nfs4_proc_lookup,
9427 .access = nfs4_proc_access,
9428 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009429 .create = nfs4_proc_create,
9430 .remove = nfs4_proc_remove,
9431 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009432 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009433 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009434 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009435 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009436 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437 .link = nfs4_proc_link,
9438 .symlink = nfs4_proc_symlink,
9439 .mkdir = nfs4_proc_mkdir,
9440 .rmdir = nfs4_proc_remove,
9441 .readdir = nfs4_proc_readdir,
9442 .mknod = nfs4_proc_mknod,
9443 .statfs = nfs4_proc_statfs,
9444 .fsinfo = nfs4_proc_fsinfo,
9445 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009446 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009448 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009449 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009450 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009452 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009453 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009454 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009455 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009456 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009457 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009458 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009459 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009460 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009461 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009462 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009463 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009464 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009465 .create_server = nfs4_create_server,
9466 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009467};
9468
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009469static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009470 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009471 .list = nfs4_xattr_list_nfs4_acl,
9472 .get = nfs4_xattr_get_nfs4_acl,
9473 .set = nfs4_xattr_set_nfs4_acl,
9474};
9475
9476const struct xattr_handler *nfs4_xattr_handlers[] = {
9477 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009478#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9479 &nfs4_xattr_nfs4_label_handler,
9480#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009481 NULL
9482};
9483
Linus Torvalds1da177e2005-04-16 15:20:36 -07009484/*
9485 * Local variables:
9486 * c-basic-offset: 8
9487 * End:
9488 */