blob: 4fd0e2b7b08e1d1c52916bfa791c95e774ca1253 [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 Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001124static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1125 int err, struct nfs4_exception *exception)
1126{
1127 if (err != -EINVAL)
1128 return false;
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1130 return false;
1131 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1132 exception->retry = 1;
1133 return true;
1134}
1135
Trond Myklebust6ae37332015-01-30 14:21:14 -05001136static u32
1137nfs4_map_atomic_open_share(struct nfs_server *server,
1138 fmode_t fmode, int openflags)
1139{
1140 u32 res = 0;
1141
1142 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1143 case FMODE_READ:
1144 res = NFS4_SHARE_ACCESS_READ;
1145 break;
1146 case FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_WRITE;
1148 break;
1149 case FMODE_READ|FMODE_WRITE:
1150 res = NFS4_SHARE_ACCESS_BOTH;
1151 }
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1153 goto out;
1154 /* Want no delegation if we're using O_DIRECT */
1155 if (openflags & O_DIRECT)
1156 res |= NFS4_SHARE_WANT_NO_DELEG;
1157out:
1158 return res;
1159}
1160
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001161static enum open_claim_type4
1162nfs4_map_atomic_open_claim(struct nfs_server *server,
1163 enum open_claim_type4 claim)
1164{
1165 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1166 return claim;
1167 switch (claim) {
1168 default:
1169 return claim;
1170 case NFS4_OPEN_CLAIM_FH:
1171 return NFS4_OPEN_CLAIM_NULL;
1172 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1173 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1174 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1175 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1176 }
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180{
1181 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001182 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 p->o_res.seqid = p->o_arg.seqid;
1184 p->c_res.seqid = p->c_arg.seqid;
1185 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001186 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001188 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189}
1190
Al Viro82a2c1b2011-06-22 18:30:55 -04001191static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001194 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001195 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001196 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197{
Al Viro82a2c1b2011-06-22 18:30:55 -04001198 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001199 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 struct nfs4_opendata *p;
1203
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001204 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 if (p == NULL)
1206 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208 p->f_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->f_label))
1210 goto err_free_p;
1211
Kinglong Meea49c2692015-07-27 15:31:38 +08001212 p->a_label = nfs4_label_alloc(server, gfp_mask);
1213 if (IS_ERR(p->a_label))
1214 goto err_free_f;
1215
Trond Myklebust63f5f792015-01-23 19:19:25 -05001216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1217 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001218 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001219 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 nfs_sb_active(dentry->d_sb);
1221 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->dir = parent;
1223 p->owner = sp;
1224 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 p->o_arg.open_flags = flags;
1226 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001227 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001228 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1229 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001230 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1231 * will return permission denied for all bits until close */
1232 if (!(flags & O_EXCL)) {
1233 /* ask server to check for all possible rights as results
1234 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001235 switch (p->o_arg.claim) {
1236 default:
1237 break;
1238 case NFS4_OPEN_CLAIM_NULL:
1239 case NFS4_OPEN_CLAIM_FH:
1240 p->o_arg.access = NFS4_ACCESS_READ |
1241 NFS4_ACCESS_MODIFY |
1242 NFS4_ACCESS_EXTEND |
1243 NFS4_ACCESS_EXECUTE;
1244 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001245 }
David Howells7539bba2006-08-22 20:06:09 -04001246 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001247 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1248 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001249 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001251 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001252 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001253 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001254 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 case NFS4_OPEN_CLAIM_NULL:
1256 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1257 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1258 p->o_arg.fh = NFS_FH(dir);
1259 break;
1260 case NFS4_OPEN_CLAIM_PREVIOUS:
1261 case NFS4_OPEN_CLAIM_FH:
1262 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1263 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001264 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001265 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001266 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001267 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001268
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001269 p->o_arg.u.attrs = &p->attrs;
1270 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001271
1272 verf[0] = jiffies;
1273 verf[1] = current->pid;
1274 memcpy(p->o_arg.u.verifier.data, verf,
1275 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277 p->c_arg.fh = &p->o_res.fh;
1278 p->c_arg.stateid = &p->o_res.stateid;
1279 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001280 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001282 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001283
1284err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001285 nfs4_label_free(p->a_label);
1286err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001287 nfs4_label_free(p->f_label);
1288err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289 kfree(p);
1290err:
1291 dput(parent);
1292 return NULL;
1293}
1294
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 struct nfs4_opendata *p = container_of(kref,
1298 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001299 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300
1301 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001302 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303 if (p->state != NULL)
1304 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001306
Kinglong Meea49c2692015-07-27 15:31:38 +08001307 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001308 nfs4_label_free(p->f_label);
1309
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001310 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 dput(p->dentry);
1312 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001313 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001314 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001315 kfree(p);
1316}
1317
1318static void nfs4_opendata_put(struct nfs4_opendata *p)
1319{
1320 if (p != NULL)
1321 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001322}
1323
Trond Myklebust06f814a2006-01-03 09:55:07 +01001324static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1325{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001326 int ret;
1327
Trond Myklebust06f814a2006-01-03 09:55:07 +01001328 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001329 return ret;
1330}
1331
Trond Myklebust24311f82015-09-20 10:50:17 -04001332static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1333 fmode_t fmode)
1334{
1335 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1336 case FMODE_READ|FMODE_WRITE:
1337 return state->n_rdwr != 0;
1338 case FMODE_WRITE:
1339 return state->n_wronly != 0;
1340 case FMODE_READ:
1341 return state->n_rdonly != 0;
1342 }
1343 WARN_ON_ONCE(1);
1344 return false;
1345}
1346
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348{
1349 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350
Trond Myklebust536e43d2012-01-17 22:04:26 -05001351 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352 goto out;
1353 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001355 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1356 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 break;
1358 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001359 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1360 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 break;
1362 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1364 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 return ret;
1368}
1369
Trond Myklebust2a606182015-08-19 22:30:00 -05001370static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1371 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001373 if (delegation == NULL)
1374 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001377 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1378 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001379 switch (claim) {
1380 case NFS4_OPEN_CLAIM_NULL:
1381 case NFS4_OPEN_CLAIM_FH:
1382 break;
1383 case NFS4_OPEN_CLAIM_PREVIOUS:
1384 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1385 break;
1386 default:
1387 return 0;
1388 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001389 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390 return 1;
1391}
1392
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001393static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001394{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001396 case FMODE_WRITE:
1397 state->n_wronly++;
1398 break;
1399 case FMODE_READ:
1400 state->n_rdonly++;
1401 break;
1402 case FMODE_READ|FMODE_WRITE:
1403 state->n_rdwr++;
1404 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001405 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001406}
1407
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001408#ifdef CONFIG_NFS_V4_1
1409static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1410{
1411 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1412 return true;
1413 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1414 return true;
1415 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1416 return true;
1417 return false;
1418}
1419#endif /* CONFIG_NFS_V4_1 */
1420
Trond Myklebust4f14c192014-02-12 19:15:06 -05001421static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001422{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001423 struct nfs_client *clp = state->owner->so_server->nfs_client;
1424 bool need_recover = false;
1425
1426 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1427 need_recover = true;
1428 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1429 need_recover = true;
1430 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1431 need_recover = true;
1432 if (need_recover)
1433 nfs4_state_mark_reclaim_nograce(clp, state);
1434}
1435
Trond Myklebuste999e802014-02-10 18:20:47 -05001436static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001437 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001438{
1439 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1440 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001441 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001442 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001443 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001444 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001445 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001446 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1447 return true;
1448 return false;
1449}
1450
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1452{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1454 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455 if (state->n_wronly)
1456 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 if (state->n_rdonly)
1458 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1459 if (state->n_rdwr)
1460 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001461 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001462}
1463
Trond Myklebust226056c2014-02-11 10:41:07 -05001464static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1465 nfs4_stateid *stateid, fmode_t fmode)
1466{
1467 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1468 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1469 case FMODE_WRITE:
1470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1471 break;
1472 case FMODE_READ:
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1474 break;
1475 case 0:
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 clear_bit(NFS_OPEN_STATE, &state->flags);
1479 }
1480 if (stateid == NULL)
1481 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001482 /* Handle OPEN+OPEN_DOWNGRADE races */
1483 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1484 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001485 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001487 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 nfs4_stateid_copy(&state->stateid, stateid);
1490 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001491}
1492
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001493static void nfs_clear_open_stateid(struct nfs4_state *state,
1494 nfs4_stateid *arg_stateid,
1495 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001496{
1497 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001498 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1499 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1500 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001501 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001502 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1503 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001504}
1505
Trond Myklebust1393d962016-09-22 13:39:13 -04001506static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1507 const nfs4_stateid *stateid, fmode_t fmode,
1508 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001509{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001510 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 case FMODE_READ:
1512 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1513 break;
1514 case FMODE_WRITE:
1515 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1516 break;
1517 case FMODE_READ|FMODE_WRITE:
1518 set_bit(NFS_O_RDWR_STATE, &state->flags);
1519 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001520 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001521 return;
1522 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1523 nfs4_stateid_copy(&state->stateid, stateid);
1524 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001525}
1526
Trond Myklebust1393d962016-09-22 13:39:13 -04001527static void __update_open_stateid(struct nfs4_state *state,
1528 const nfs4_stateid *open_stateid,
1529 const nfs4_stateid *deleg_stateid,
1530 fmode_t fmode,
1531 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001533 /*
1534 * Protect the call to nfs4_state_set_mode_locked and
1535 * serialise the stateid update
1536 */
Andrew Elble361cad32015-12-02 09:20:57 -05001537 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001538 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001539 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001540 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001541 set_bit(NFS_DELEGATED_STATE, &state->flags);
1542 }
1543 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001544 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001545 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001546 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001547 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Trond Myklebust1393d962016-09-22 13:39:13 -04001550static int update_open_stateid(struct nfs4_state *state,
1551 const nfs4_stateid *open_stateid,
1552 const nfs4_stateid *delegation,
1553 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001554{
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 struct nfs_server *server = NFS_SERVER(state->inode);
1556 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001557 struct nfs_inode *nfsi = NFS_I(state->inode);
1558 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001559 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001560 int ret = 0;
1561
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001562 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001563
1564 rcu_read_lock();
1565 deleg_cur = rcu_dereference(nfsi->delegation);
1566 if (deleg_cur == NULL)
1567 goto no_delegation;
1568
1569 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001570 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001571 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001572 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001573 goto no_delegation_unlock;
1574
1575 if (delegation == NULL)
1576 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001577 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001578 goto no_delegation_unlock;
1579
Trond Myklebustb7391f42008-12-23 15:21:52 -05001580 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001581 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1582 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001583 ret = 1;
1584no_delegation_unlock:
1585 spin_unlock(&deleg_cur->lock);
1586no_delegation:
1587 rcu_read_unlock();
1588
1589 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001590 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001591 ret = 1;
1592 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001593 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001594 nfs4_schedule_state_manager(clp);
1595 if (freeme.type != 0)
1596 nfs4_test_and_free_stateid(server, &freeme,
1597 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001598
1599 return ret;
1600}
1601
Trond Myklebust39071e62015-01-24 15:07:56 -05001602static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1603 const nfs4_stateid *stateid)
1604{
1605 struct nfs4_state *state = lsp->ls_state;
1606 bool ret = false;
1607
1608 spin_lock(&state->state_lock);
1609 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1610 goto out_noupdate;
1611 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1612 goto out_noupdate;
1613 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1614 ret = true;
1615out_noupdate:
1616 spin_unlock(&state->state_lock);
1617 return ret;
1618}
Trond Myklebust34310432008-12-23 15:21:38 -05001619
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001620static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621{
1622 struct nfs_delegation *delegation;
1623
1624 rcu_read_lock();
1625 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001626 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 rcu_read_unlock();
1628 return;
1629 }
1630 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001631 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001632}
1633
Trond Myklebust6ee41262007-07-08 14:11:36 -04001634static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635{
1636 struct nfs4_state *state = opendata->state;
1637 struct nfs_inode *nfsi = NFS_I(state->inode);
1638 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001639 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001640 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001641 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 nfs4_stateid stateid;
1643 int ret = -EAGAIN;
1644
Trond Myklebustaac00a82007-07-05 19:02:21 -04001645 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001646 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001648 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001649 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001650 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001651 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001652 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001653 rcu_read_lock();
1654 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001655 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001656 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001657 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001658 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001660 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001661 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001662 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001663 if (!opendata->is_recover) {
1664 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1665 if (ret != 0)
1666 goto out;
1667 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001669
1670 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001671 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001672 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674out:
1675 return ERR_PTR(ret);
1676out_return_state:
1677 atomic_inc(&state->count);
1678 return state;
1679}
1680
Andy Adamsone23008e2012-10-02 21:07:32 -04001681static void
1682nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1683{
1684 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1685 struct nfs_delegation *delegation;
1686 int delegation_flags = 0;
1687
1688 rcu_read_lock();
1689 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1690 if (delegation)
1691 delegation_flags = delegation->flags;
1692 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001693 switch (data->o_arg.claim) {
1694 default:
1695 break;
1696 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1697 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001698 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1699 "returning a delegation for "
1700 "OPEN(CLAIM_DELEGATE_CUR)\n",
1701 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001702 return;
1703 }
1704 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 nfs_inode_set_delegation(state->inode,
1706 data->owner->so_cred,
1707 &data->o_res);
1708 else
1709 nfs_inode_reclaim_delegation(state->inode,
1710 data->owner->so_cred,
1711 &data->o_res);
1712}
1713
1714/*
1715 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1716 * and update the nfs4_state.
1717 */
1718static struct nfs4_state *
1719_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1720{
1721 struct inode *inode = data->state->inode;
1722 struct nfs4_state *state = data->state;
1723 int ret;
1724
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001725 if (!data->rpc_done) {
1726 if (data->rpc_status) {
1727 ret = data->rpc_status;
1728 goto err;
1729 }
1730 /* cached opens have already been processed */
1731 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001732 }
1733
Andy Adamsone23008e2012-10-02 21:07:32 -04001734 ret = nfs_refresh_inode(inode, &data->f_attr);
1735 if (ret)
1736 goto err;
1737
1738 if (data->o_res.delegation_type != 0)
1739 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001740update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001741 update_open_stateid(state, &data->o_res.stateid, NULL,
1742 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001743 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001744
1745 return state;
1746err:
1747 return ERR_PTR(ret);
1748
1749}
1750
1751static struct nfs4_state *
1752_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753{
1754 struct inode *inode;
1755 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757
Trond Myklebustaac00a82007-07-05 19:02:21 -04001758 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001759 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001760 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001761 goto out;
1762 }
1763
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001767 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001769 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001770 goto err;
1771 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 state = nfs4_get_open_state(inode, data->owner);
1773 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001775 if (data->o_res.delegation_type != 0)
1776 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001777 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001778 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001780out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001781 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001783err_put_inode:
1784 iput(inode);
1785err:
1786 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787}
1788
Andy Adamsone23008e2012-10-02 21:07:32 -04001789static struct nfs4_state *
1790nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1791{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001792 struct nfs4_state *ret;
1793
Andy Adamsone23008e2012-10-02 21:07:32 -04001794 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001795 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1796 else
1797 ret = _nfs4_opendata_to_nfs4_state(data);
1798 nfs4_sequence_free_slot(&data->o_res.seq_res);
1799 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001800}
1801
Trond Myklebust864472e2006-01-03 09:55:15 +01001802static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1803{
1804 struct nfs_inode *nfsi = NFS_I(state->inode);
1805 struct nfs_open_context *ctx;
1806
1807 spin_lock(&state->inode->i_lock);
1808 list_for_each_entry(ctx, &nfsi->open_files, list) {
1809 if (ctx->state != state)
1810 continue;
1811 get_nfs_open_context(ctx);
1812 spin_unlock(&state->inode->i_lock);
1813 return ctx;
1814 }
1815 spin_unlock(&state->inode->i_lock);
1816 return ERR_PTR(-ENOENT);
1817}
1818
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001819static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1820 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001821{
1822 struct nfs4_opendata *opendata;
1823
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001824 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001825 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001826 if (opendata == NULL)
1827 return ERR_PTR(-ENOMEM);
1828 opendata->state = state;
1829 atomic_inc(&state->count);
1830 return opendata;
1831}
1832
Trond Myklebust24311f82015-09-20 10:50:17 -04001833static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1834 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001835{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001836 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 int ret;
1838
Trond Myklebust24311f82015-09-20 10:50:17 -04001839 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001840 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001841 opendata->o_arg.open_flags = 0;
1842 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001843 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1844 NFS_SB(opendata->dentry->d_sb),
1845 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001846 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1847 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1848 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001849 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001850 if (ret != 0)
1851 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001852 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001853 if (IS_ERR(newstate))
1854 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001855 if (newstate != opendata->state)
1856 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001857 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001858 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001859}
1860
1861static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1862{
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 int ret;
1864
Trond Myklebust4f14c192014-02-12 19:15:06 -05001865 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1866 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1867 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1868 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001869 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001870 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001871 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001872 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001873 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1874 if (ret != 0)
1875 return ret;
1876 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1877 if (ret != 0)
1878 return ret;
1879 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1880 if (ret != 0)
1881 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001882 /*
1883 * We may have performed cached opens for all three recoveries.
1884 * Check if we need to update the current stateid.
1885 */
1886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001887 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001888 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001889 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001890 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001891 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001892 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001893 return 0;
1894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
1897 * OPEN_RECLAIM:
1898 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001900static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001902 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001903 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001904 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int status;
1906
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001907 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1908 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001909 if (IS_ERR(opendata))
1910 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001911 rcu_read_lock();
1912 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001913 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001914 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001915 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001916 opendata->o_arg.u.delegation_type = delegation_type;
1917 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001918 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return status;
1920}
1921
Trond Myklebust539cd032007-06-05 11:46:42 -04001922static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 struct nfs_server *server = NFS_SERVER(state->inode);
1925 struct nfs4_exception exception = { };
1926 int err;
1927 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001928 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001929 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001930 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1931 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001932 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001933 break;
1934 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } while (exception.retry);
1936 return err;
1937}
1938
Trond Myklebust864472e2006-01-03 09:55:15 +01001939static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1940{
1941 struct nfs_open_context *ctx;
1942 int ret;
1943
1944 ctx = nfs4_state_find_open_context(state);
1945 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001946 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001947 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001948 put_nfs_open_context(ctx);
1949 return ret;
1950}
1951
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001952static 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 -07001953{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001954 switch (err) {
1955 default:
1956 printk(KERN_ERR "NFS: %s: unhandled error "
1957 "%d.\n", __func__, err);
1958 case 0:
1959 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001960 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 case -ESTALE:
1962 break;
1963 case -NFS4ERR_BADSESSION:
1964 case -NFS4ERR_BADSLOT:
1965 case -NFS4ERR_BAD_HIGH_SLOT:
1966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1967 case -NFS4ERR_DEADSESSION:
1968 set_bit(NFS_DELEGATED_STATE, &state->flags);
1969 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1970 return -EAGAIN;
1971 case -NFS4ERR_STALE_CLIENTID:
1972 case -NFS4ERR_STALE_STATEID:
1973 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001974 /* Don't recall a delegation if it was lost */
1975 nfs4_schedule_lease_recovery(server->nfs_client);
1976 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001977 case -NFS4ERR_MOVED:
1978 nfs4_schedule_migration_recovery(server);
1979 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001980 case -NFS4ERR_LEASE_MOVED:
1981 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1982 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 case -NFS4ERR_DELEG_REVOKED:
1984 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001985 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001986 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001987 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001988 nfs_inode_find_state_and_recover(state->inode,
1989 stateid);
1990 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001991 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001992 case -NFS4ERR_DELAY:
1993 case -NFS4ERR_GRACE:
1994 set_bit(NFS_DELEGATED_STATE, &state->flags);
1995 ssleep(1);
1996 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001997 case -ENOMEM:
1998 case -NFS4ERR_DENIED:
1999 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2000 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return err;
2003}
2004
Trond Myklebust24311f82015-09-20 10:50:17 -04002005int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2006 struct nfs4_state *state, const nfs4_stateid *stateid,
2007 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002008{
2009 struct nfs_server *server = NFS_SERVER(state->inode);
2010 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002011 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002012
2013 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2014 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2015 if (IS_ERR(opendata))
2016 return PTR_ERR(opendata);
2017 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002018 write_seqlock(&state->seqlock);
2019 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2020 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002021 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2022 switch (type & (FMODE_READ|FMODE_WRITE)) {
2023 case FMODE_READ|FMODE_WRITE:
2024 case FMODE_WRITE:
2025 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2026 if (err)
2027 break;
2028 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2029 if (err)
2030 break;
2031 case FMODE_READ:
2032 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2033 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002034 nfs4_opendata_put(opendata);
2035 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2036}
2037
Chuck Leverbe05c862013-08-09 12:49:47 -04002038static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2039{
2040 struct nfs4_opendata *data = calldata;
2041
Peng Taocb04ad22014-06-11 05:24:15 +08002042 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2043 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002044}
2045
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2047{
2048 struct nfs4_opendata *data = calldata;
2049
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002050 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002051
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002052 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002053 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002054 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002055 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002057 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002058 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059}
2060
2061static void nfs4_open_confirm_release(void *calldata)
2062{
2063 struct nfs4_opendata *data = calldata;
2064 struct nfs4_state *state = NULL;
2065
2066 /* If this request hasn't been cancelled, do nothing */
2067 if (data->cancelled == 0)
2068 goto out_free;
2069 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002070 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002072 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002073 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002074 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002075out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002076 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002077}
2078
2079static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002080 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002081 .rpc_call_done = nfs4_open_confirm_done,
2082 .rpc_release = nfs4_open_confirm_release,
2083};
2084
2085/*
2086 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2087 */
2088static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2089{
David Howells2b0143b2015-03-17 22:25:59 +00002090 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002091 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002092 struct rpc_message msg = {
2093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2094 .rpc_argp = &data->c_arg,
2095 .rpc_resp = &data->c_res,
2096 .rpc_cred = data->owner->so_cred,
2097 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002098 struct rpc_task_setup task_setup_data = {
2099 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002100 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002101 .callback_ops = &nfs4_open_confirm_ops,
2102 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002103 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002104 .flags = RPC_TASK_ASYNC,
2105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int status;
2107
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002108 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002109 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002110 data->rpc_done = 0;
2111 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002112 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002113 if (data->is_recover)
2114 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002115 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002116 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002117 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002118 status = nfs4_wait_for_completion_rpc_task(task);
2119 if (status != 0) {
2120 data->cancelled = 1;
2121 smp_wmb();
2122 } else
2123 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002124 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return status;
2126}
2127
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 struct nfs4_opendata *data = calldata;
2131 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002132 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002133 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002134
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002135 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002136 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002137 /*
2138 * Check if we still need to send an OPEN call, or if we can use
2139 * a delegation instead.
2140 */
2141 if (data->state != NULL) {
2142 struct nfs_delegation *delegation;
2143
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002144 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002145 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 rcu_read_lock();
2147 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002148 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002149 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002150 rcu_read_unlock();
2151 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002152 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002153 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002154 switch (claim) {
2155 default:
2156 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002157 case NFS4_OPEN_CLAIM_PREVIOUS:
2158 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2159 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002160 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002161 case NFS4_OPEN_CLAIM_FH:
2162 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002163 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2164 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002165 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002166 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002167 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002168 &data->o_res.seq_res,
2169 task) != 0)
2170 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002171
2172 /* Set the create mode (note dependency on the session type) */
2173 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2174 if (data->o_arg.open_flags & O_EXCL) {
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2176 if (nfs4_has_persistent_session(clp))
2177 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2178 else if (clp->cl_mvops->minor_version > 0)
2179 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2180 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002181 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002182unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002183 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002184 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002185out_no_action:
2186 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002187out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002188 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002189}
2190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191static void nfs4_open_done(struct rpc_task *task, void *calldata)
2192{
2193 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002196
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002197 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002198 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002201 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2202 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 case S_IFREG:
2204 break;
2205 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002207 break;
2208 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002209 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 break;
2211 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002213 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002215 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002216 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2217 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002218 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002219 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002221
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002222static void nfs4_open_release(void *calldata)
2223{
2224 struct nfs4_opendata *data = calldata;
2225 struct nfs4_state *state = NULL;
2226
2227 /* If this request hasn't been cancelled, do nothing */
2228 if (data->cancelled == 0)
2229 goto out_free;
2230 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002231 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232 goto out_free;
2233 /* In case we need an open_confirm, no cleanup! */
2234 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2235 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002236 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002237 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002238 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002240 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002241}
2242
2243static const struct rpc_call_ops nfs4_open_ops = {
2244 .rpc_call_prepare = nfs4_open_prepare,
2245 .rpc_call_done = nfs4_open_done,
2246 .rpc_release = nfs4_open_release,
2247};
2248
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002249static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002250{
David Howells2b0143b2015-03-17 22:25:59 +00002251 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002252 struct nfs_server *server = NFS_SERVER(dir);
2253 struct nfs_openargs *o_arg = &data->o_arg;
2254 struct nfs_openres *o_res = &data->o_res;
2255 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2258 .rpc_argp = o_arg,
2259 .rpc_resp = o_res,
2260 .rpc_cred = data->owner->so_cred,
2261 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002262 struct rpc_task_setup task_setup_data = {
2263 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002264 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002265 .callback_ops = &nfs4_open_ops,
2266 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002267 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002268 .flags = RPC_TASK_ASYNC,
2269 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002270 int status;
2271
Chuck Levera9c92d62013-08-09 12:48:18 -04002272 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002273 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002274 data->rpc_done = 0;
2275 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002276 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002277 data->is_recover = 0;
2278 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002279 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002280 data->is_recover = 1;
2281 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002282 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002283 if (IS_ERR(task))
2284 return PTR_ERR(task);
2285 status = nfs4_wait_for_completion_rpc_task(task);
2286 if (status != 0) {
2287 data->cancelled = 1;
2288 smp_wmb();
2289 } else
2290 status = data->rpc_status;
2291 rpc_put_task(task);
2292
2293 return status;
2294}
2295
2296static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2297{
David Howells2b0143b2015-03-17 22:25:59 +00002298 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002299 struct nfs_openres *o_res = &data->o_res;
2300 int status;
2301
2302 status = nfs4_run_open_task(data, 1);
2303 if (status != 0 || !data->rpc_done)
2304 return status;
2305
Trond Myklebust6926afd12012-01-07 13:22:46 -05002306 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2307
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002308 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2309 status = _nfs4_proc_open_confirm(data);
2310 if (status != 0)
2311 return status;
2312 }
2313
2314 return status;
2315}
2316
Trond Myklebustf3792d62014-07-10 08:54:32 -04002317/*
2318 * Additional permission checks in order to distinguish between an
2319 * open for read, and an open for execute. This works around the
2320 * fact that NFSv4 OPEN treats read and execute permissions as being
2321 * the same.
2322 * Note that in the non-execute case, we want to turn off permission
2323 * checking if we just created a new file (POSIX open() semantics).
2324 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325static int nfs4_opendata_access(struct rpc_cred *cred,
2326 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002327 struct nfs4_state *state, fmode_t fmode,
2328 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002329{
2330 struct nfs_access_entry cache;
2331 u32 mask;
2332
2333 /* access call failed or for some reason the server doesn't
2334 * support any access modes -- defer access call until later */
2335 if (opendata->o_res.access_supported == 0)
2336 return 0;
2337
2338 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002339 /*
2340 * Use openflags to check for exec, because fmode won't
2341 * always have FMODE_EXEC set when file open for exec.
2342 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002343 if (openflags & __FMODE_EXEC) {
2344 /* ONLY check for exec rights */
2345 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002346 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002347 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002348
2349 cache.cred = cred;
2350 cache.jiffies = jiffies;
2351 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2352 nfs_access_add_cache(state->inode, &cache);
2353
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002354 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002355 return 0;
2356
2357 /* even though OPEN succeeded, access is denied. Close the file */
2358 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002359 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002360}
2361
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002362/*
2363 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2364 */
2365static int _nfs4_proc_open(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002368 struct nfs_server *server = NFS_SERVER(dir);
2369 struct nfs_openargs *o_arg = &data->o_arg;
2370 struct nfs_openres *o_res = &data->o_res;
2371 int status;
2372
2373 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002374 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002375 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002376 if (status != 0) {
2377 if (status == -NFS4ERR_BADNAME &&
2378 !(o_arg->open_flags & O_CREAT))
2379 return -ENOENT;
2380 return status;
2381 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382
Trond Myklebust6926afd12012-01-07 13:22:46 -05002383 nfs_fattr_map_and_free_names(server, &data->f_attr);
2384
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002385 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002386 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002387 if (o_arg->open_flags & O_EXCL)
2388 data->file_created = 1;
2389 else if (o_res->cinfo.before != o_res->cinfo.after)
2390 data->file_created = 1;
2391 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002392 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2393 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002395 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002400 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Andy Adamsond83217c2011-03-01 01:34:17 +00002404static int nfs4_recover_expired_lease(struct nfs_server *server)
2405{
2406 return nfs4_client_recover_expired_lease(server->nfs_client);
2407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/*
2410 * OPEN_EXPIRED:
2411 * reclaim state on the server after a network partition.
2412 * Assumes caller holds the appropriate lock
2413 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002414static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002416 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002417 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002419 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002420 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002421 if (IS_ERR(opendata))
2422 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002423 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002424 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002425 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002426 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002427 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002430static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431{
Trond Myklebust539cd032007-06-05 11:46:42 -04002432 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002433 struct nfs4_exception exception = { };
2434 int err;
2435
2436 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002437 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002438 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002439 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2440 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002441 switch (err) {
2442 default:
2443 goto out;
2444 case -NFS4ERR_GRACE:
2445 case -NFS4ERR_DELAY:
2446 nfs4_handle_exception(server, err, &exception);
2447 err = 0;
2448 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002449 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002450out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002451 return err;
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002457 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Trond Myklebust864472e2006-01-03 09:55:15 +01002459 ctx = nfs4_state_find_open_context(state);
2460 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002461 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002462 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002463 put_nfs_open_context(ctx);
2464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Trond Myklebust41020b62016-09-22 13:38:58 -04002467static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2468 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002469{
Trond Myklebust41020b62016-09-22 13:38:58 -04002470 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002471 write_seqlock(&state->seqlock);
2472 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2473 write_sequnlock(&state->seqlock);
2474 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2475}
2476
2477static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2478{
2479 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002480 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002481}
2482
2483static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2484{
2485 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2486 nfs40_clear_delegation_stateid(state);
2487 return nfs4_open_expired(sp, state);
2488}
2489
Trond Myklebust45870d62016-09-22 13:38:59 -04002490static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2491 nfs4_stateid *stateid,
2492 struct rpc_cred *cred)
2493{
2494 return -NFS4ERR_BAD_STATEID;
2495}
2496
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002497#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002498static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2501{
2502 int status;
2503
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002504 switch (stateid->type) {
2505 default:
2506 break;
2507 case NFS4_INVALID_STATEID_TYPE:
2508 case NFS4_SPECIAL_STATEID_TYPE:
2509 return -NFS4ERR_BAD_STATEID;
2510 case NFS4_REVOKED_STATEID_TYPE:
2511 goto out_free;
2512 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002513
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002514 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002515 switch (status) {
2516 case -NFS4ERR_EXPIRED:
2517 case -NFS4ERR_ADMIN_REVOKED:
2518 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002519 break;
2520 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002521 return status;
2522 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002523out_free:
2524 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002525 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002526 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002527}
2528
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002530{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002531 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002532 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002534 struct rpc_cred *cred;
2535 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002536
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 /* Get the delegation credential for use by test/free_stateid */
2538 rcu_read_lock();
2539 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002541 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 return;
2543 }
2544
2545 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002546 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2547 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002548 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002549 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002551
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002552 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2553 rcu_read_unlock();
2554 return;
2555 }
2556
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002557 cred = get_rpccred(delegation->cred);
2558 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002559 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002560 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002561 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002562 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002563
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002564 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565}
2566
2567/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002568 * nfs41_check_expired_locks - possibly free a lock stateid
2569 *
2570 * @state: NFSv4 state for an inode
2571 *
2572 * Returns NFS_OK if recovery for this stateid is now finished.
2573 * Otherwise a negative NFS4ERR value is returned.
2574 */
2575static int nfs41_check_expired_locks(struct nfs4_state *state)
2576{
2577 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002578 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002579 struct nfs_server *server = NFS_SERVER(state->inode);
2580
2581 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2582 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002583
2584 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002585 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2586 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2587 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2588
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002589 atomic_inc(&lsp->ls_count);
2590 spin_unlock(&state->state_lock);
2591
2592 nfs4_put_lock_state(prev);
2593 prev = lsp;
2594
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002595 status = nfs41_test_and_free_expired_stateid(server,
2596 &lsp->ls_stateid,
2597 cred);
2598 trace_nfs4_test_lock_stateid(state, lsp, status);
2599 if (status == -NFS4ERR_EXPIRED ||
2600 status == -NFS4ERR_BAD_STATEID) {
2601 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002602 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603 if (!recover_lost_locks)
2604 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2605 } else if (status != NFS_OK) {
2606 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002607 nfs4_put_lock_state(prev);
2608 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002609 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002610 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002612 }
2613 spin_unlock(&state->state_lock);
2614 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002615out:
2616 return ret;
2617}
2618
2619/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 * nfs41_check_open_stateid - possibly free an open stateid
2621 *
2622 * @state: NFSv4 state for an inode
2623 *
2624 * Returns NFS_OK if recovery for this stateid is now finished.
2625 * Otherwise a negative NFS4ERR value is returned.
2626 */
2627static int nfs41_check_open_stateid(struct nfs4_state *state)
2628{
2629 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002630 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002631 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002632 int status;
2633
Trond Myklebustb134fc42016-09-22 13:39:16 -04002634 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2636 if (nfs4_have_delegation(state->inode, state->state))
2637 return NFS_OK;
2638 return -NFS4ERR_OPENMODE;
2639 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002640 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002641 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002642 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002643 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002644 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002645 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2647 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002648 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002649 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002650 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002651 if (status != NFS_OK)
2652 return status;
2653 if (nfs_open_stateid_recover_openmode(state))
2654 return -NFS4ERR_OPENMODE;
2655 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002656}
2657
2658static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2659{
Chuck Levereb64cf92012-07-11 16:30:05 -04002660 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002661
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002662 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002663 status = nfs41_check_expired_locks(state);
2664 if (status != NFS_OK)
2665 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002666 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002667 if (status != NFS_OK)
2668 status = nfs4_open_expired(sp, state);
2669 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002670}
2671#endif
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002674 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2675 * fields corresponding to attributes that were used to store the verifier.
2676 * Make sure we clobber those fields in the later setattr call
2677 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002678static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2679 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002681 const u32 *attrset = opendata->o_res.attrset;
2682
2683 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684 !(sattr->ia_valid & ATTR_ATIME_SET))
2685 sattr->ia_valid |= ATTR_ATIME;
2686
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002687 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002688 !(sattr->ia_valid & ATTR_MTIME_SET))
2689 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002690
2691 /* Except MODE, it seems harmless of setting twice. */
2692 if ((attrset[1] & FATTR4_WORD1_MODE))
2693 sattr->ia_valid &= ~ATTR_MODE;
2694
2695 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2696 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002697}
2698
Trond Myklebustc21443c2013-02-07 14:26:21 -05002699static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2700 fmode_t fmode,
2701 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002702 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002703{
2704 struct nfs4_state_owner *sp = opendata->owner;
2705 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002706 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707 struct nfs4_state *state;
2708 unsigned int seq;
2709 int ret;
2710
2711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2712
2713 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002714 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 goto out;
2716
2717 state = nfs4_opendata_to_nfs4_state(opendata);
2718 ret = PTR_ERR(state);
2719 if (IS_ERR(state))
2720 goto out;
2721 if (server->caps & NFS_CAP_POSIX_LOCK)
2722 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002723 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2724 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002725
Trond Myklebust275bb302013-05-29 13:11:28 -04002726 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002727 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002728 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002729 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002730 alias = d_exact_alias(dentry, state->inode);
2731 if (!alias)
2732 alias = d_splice_alias(igrab(state->inode), dentry);
2733 /* d_splice_alias() can't fail here - it's a non-directory */
2734 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002735 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002736 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 }
2738 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002739 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002740 }
2741
Trond Myklebustc21443c2013-02-07 14:26:21 -05002742 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2743 if (ret != 0)
2744 goto out;
2745
Trond Myklebust3efb9722013-05-29 13:17:04 -04002746 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002748 nfs_inode_attach_open_context(ctx);
2749 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2750 nfs4_schedule_stateid_recovery(server, state);
2751 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752out:
2753 return ret;
2754}
2755
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002756/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002757 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Andy Adamson82be4172012-05-23 05:02:35 -04002759static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002760 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002761 int flags,
2762 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002763 struct nfs4_label *label,
2764 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct nfs4_state_owner *sp;
2767 struct nfs4_state *state = NULL;
2768 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002769 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002770 struct dentry *dentry = ctx->dentry;
2771 struct rpc_cred *cred = ctx->cred;
2772 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2773 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002774 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002775 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002776 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002780 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2781 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2783 goto out_err;
2784 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002785 status = nfs4_recover_expired_lease(server);
2786 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002787 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002788 if (d_really_is_positive(dentry))
2789 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002790 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002792 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002793 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002794 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002795 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002796 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
David Quigley14c43f72013-05-22 12:50:43 -04002798 if (label) {
2799 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2800 if (IS_ERR(olabel)) {
2801 status = PTR_ERR(olabel);
2802 goto err_opendata_put;
2803 }
2804 }
2805
Trond Myklebuste911b812014-03-26 13:24:37 -07002806 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2807 if (!opendata->f_attr.mdsthreshold) {
2808 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2809 if (!opendata->f_attr.mdsthreshold)
2810 goto err_free_label;
2811 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002812 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002813 }
David Howells2b0143b2015-03-17 22:25:59 +00002814 if (d_really_is_positive(dentry))
2815 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002816
Trond Myklebust3efb9722013-05-29 13:17:04 -04002817 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002818 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002819 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002820 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002821
NeilBrownefcbc042015-07-30 13:00:56 +10002822 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002823 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002824 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002825 /*
2826 * send create attributes which was not set by open
2827 * with an extra setattr.
2828 */
2829 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2830 nfs_fattr_init(opendata->o_res.f_attr);
2831 status = nfs4_do_setattr(state->inode, cred,
2832 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002833 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002834 if (status == 0) {
2835 nfs_setattr_update_inode(state->inode, sattr,
2836 opendata->o_res.f_attr);
2837 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2838 }
David Quigley1775fd32013-05-22 12:50:42 -04002839 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002840 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002841 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002842 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002843
Trond Myklebuste911b812014-03-26 13:24:37 -07002844 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002845 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002846 opendata->f_attr.mdsthreshold = NULL;
2847 }
Andy Adamson82be4172012-05-23 05:02:35 -04002848
David Quigley14c43f72013-05-22 12:50:43 -04002849 nfs4_label_free(olabel);
2850
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002851 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002854err_free_label:
2855 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002856err_opendata_put:
2857 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002858err_put_state_owner:
2859 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
2864
Andy Adamson82be4172012-05-23 05:02:35 -04002865static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002866 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002867 int flags,
2868 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002869 struct nfs4_label *label,
2870 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002872 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct nfs4_exception exception = { };
2874 struct nfs4_state *res;
2875 int status;
2876
2877 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002878 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002879 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002880 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (status == 0)
2882 break;
2883 /* NOTE: BAD_SEQID means the server and client disagree about the
2884 * book-keeping w.r.t. state-changing operations
2885 * (OPEN/CLOSE/LOCK/LOCKU...)
2886 * It is actually a sign of a bug on the client or on the server.
2887 *
2888 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002889 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 * have unhashed the old state_owner for us, and that we can
2891 * therefore safely retry using a new one. We should still warn
2892 * the user though...
2893 */
2894 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002895 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002896 " returned a bad sequence-id error!\n",
2897 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 exception.retry = 1;
2899 continue;
2900 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002901 /*
2902 * BAD_STATEID on OPEN means that the server cancelled our
2903 * state before it received the OPEN_CONFIRM.
2904 * Recover by retrying the request as per the discussion
2905 * on Page 181 of RFC3530.
2906 */
2907 if (status == -NFS4ERR_BAD_STATEID) {
2908 exception.retry = 1;
2909 continue;
2910 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002911 if (status == -EAGAIN) {
2912 /* We must have found a delegation */
2913 exception.retry = 1;
2914 continue;
2915 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002916 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2917 continue;
2918 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 status, &exception));
2920 } while (exception.retry);
2921 return res;
2922}
2923
Trond Myklebust8487c472016-06-26 08:44:35 -04002924static int _nfs4_do_setattr(struct inode *inode,
2925 struct nfs_setattrargs *arg,
2926 struct nfs_setattrres *res,
2927 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002928 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002929{
2930 struct nfs_server *server = NFS_SERVER(inode);
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2933 .rpc_argp = arg,
2934 .rpc_resp = res,
2935 .rpc_cred = cred,
2936 };
2937 struct rpc_cred *delegation_cred = NULL;
2938 unsigned long timestamp = jiffies;
2939 fmode_t fmode;
2940 bool truncate;
2941 int status;
2942
2943 nfs_fattr_init(res->fattr);
2944
2945 /* Servers should only apply open mode checks for file size changes */
2946 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2947 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2948
2949 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2950 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002951 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002952 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002953 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002954 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002955 l_ctx = nfs_get_lock_context(ctx);
2956 if (IS_ERR(l_ctx))
2957 return PTR_ERR(l_ctx);
2958 if (nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
Trond Myklebust8487c472016-06-26 08:44:35 -04002959 &arg->stateid, &delegation_cred) == -EIO)
2960 return -EBADF;
2961 } else
2962 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2963 if (delegation_cred)
2964 msg.rpc_cred = delegation_cred;
2965
2966 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2967
2968 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002969 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002970 renew_lease(server, timestamp);
2971 trace_nfs4_setattr(inode, &arg->stateid, status);
2972 return status;
2973}
2974
2975static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2976 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002977 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002978 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002980 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002981 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002983 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 .iap = sattr,
2985 .server = server,
2986 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002987 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 };
2989 struct nfs_setattrres res = {
2990 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002991 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .server = server,
2993 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002994 struct nfs4_exception exception = {
2995 .state = state,
2996 .inode = inode,
2997 .stateid = &arg.stateid,
2998 };
2999 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
David Quigleyaa9c2662013-05-22 12:50:44 -04003001 arg.bitmask = nfs4_bitmask(server, ilabel);
3002 if (ilabel)
3003 arg.bitmask = nfs4_bitmask(server, olabel);
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003006 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003007 switch (err) {
3008 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003009 if (!(sattr->ia_valid & ATTR_SIZE)) {
3010 pr_warn_once("NFSv4: server %s is incorrectly "
3011 "applying open mode checks to "
3012 "a SETATTR that is not "
3013 "changing file size.\n",
3014 server->nfs_client->cl_hostname);
3015 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003016 if (state && !(state->state & FMODE_WRITE)) {
3017 err = -EBADF;
3018 if (sattr->ia_valid & ATTR_OPEN)
3019 err = -EACCES;
3020 goto out;
3021 }
3022 }
3023 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003025out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return err;
3027}
3028
Peng Tao500d7012015-09-22 11:35:22 +08003029static bool
3030nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3031{
3032 if (inode == NULL || !nfs_have_layout(inode))
3033 return false;
3034
3035 return pnfs_wait_on_layoutreturn(inode, task);
3036}
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038struct nfs4_closedata {
3039 struct inode *inode;
3040 struct nfs4_state *state;
3041 struct nfs_closeargs arg;
3042 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003043 struct {
3044 struct nfs4_layoutreturn_args arg;
3045 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003046 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003047 u32 roc_barrier;
3048 bool roc;
3049 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003050 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003051 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052};
3053
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003054static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003055{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003056 struct nfs4_closedata *calldata = data;
3057 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003058 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003059
Trond Myklebustcf805162016-11-15 14:56:07 -05003060 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003061 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3062 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003063 nfs4_put_open_state(calldata->state);
3064 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003065 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003066 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003067 kfree(calldata);
3068}
3069
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003070static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003072 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003075 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Chuck Levera3ca5652012-03-01 17:00:40 -05003077 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003078 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3079 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003080 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003081
3082 /* Handle Layoutreturn errors */
3083 if (calldata->arg.lr_args && task->tk_status != 0) {
3084 switch (calldata->res.lr_ret) {
3085 default:
3086 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3087 break;
3088 case 0:
3089 calldata->arg.lr_args = NULL;
3090 calldata->res.lr_res = NULL;
3091 break;
3092 case -NFS4ERR_ADMIN_REVOKED:
3093 case -NFS4ERR_DELEG_REVOKED:
3094 case -NFS4ERR_EXPIRED:
3095 case -NFS4ERR_BAD_STATEID:
3096 case -NFS4ERR_OLD_STATEID:
3097 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3098 case -NFS4ERR_WRONG_CRED:
3099 calldata->arg.lr_args = NULL;
3100 calldata->res.lr_res = NULL;
3101 calldata->res.lr_ret = 0;
3102 rpc_restart_call_prepare(task);
3103 return;
3104 }
3105 }
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /* hmm. we are done with the inode, and in the process of freeing
3108 * the state_owner. we keep this around to process errors
3109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 switch (task->tk_status) {
3111 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003112 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003113 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003114 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003115 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003117 case -NFS4ERR_EXPIRED:
3118 nfs4_free_revoked_stateid(server,
3119 &calldata->arg.stateid,
3120 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003121 case -NFS4ERR_OLD_STATEID:
3122 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003123 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003124 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003125 rpc_restart_call_prepare(task);
3126 goto out_release;
3127 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003128 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003131 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003132 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003133 goto out_release;
3134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003136 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3137 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003138out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003139 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003140 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003141 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003144static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003146 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003147 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003148 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003149 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003150 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003151
Chuck Levera3ca5652012-03-01 17:00:40 -05003152 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003153 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003154 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003155
Trond Myklebust88069f72009-12-08 08:33:16 -05003156 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003157 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003158 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3159 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3160 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003161 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003162 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003163 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003164 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003165 if (state->n_rdonly == 0)
3166 call_close |= is_rdonly;
3167 else if (is_rdonly)
3168 calldata->arg.fmode |= FMODE_READ;
3169 if (state->n_wronly == 0)
3170 call_close |= is_wronly;
3171 else if (is_wronly)
3172 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003173 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3174 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003175 } else if (is_rdwr)
3176 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3177
Trond Myklebust5cc78612016-11-14 11:19:56 -05003178 if (!nfs4_valid_open_stateid(state) ||
3179 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003180 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003181 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003182
3183 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003184 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003185 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003186 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003187
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003188 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003189 nfs_release_seqid(calldata->arg.seqid);
3190 goto out_wait;
3191 }
3192
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003193 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05003194 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003195
3196 /* Close-to-open cache consistency revalidation */
3197 if (!nfs4_have_delegation(inode, FMODE_READ))
3198 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3199 else
3200 calldata->arg.bitmask = NULL;
3201 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003202
Trond Myklebust6ae37332015-01-30 14:21:14 -05003203 calldata->arg.share_access =
3204 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3205 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003206
Trond Myklebust516a6af2005-10-27 22:12:41 -04003207 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003208 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003209 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003210 &calldata->arg.seq_args,
3211 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003212 task) != 0)
3213 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003214 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003215 return;
3216out_no_action:
3217 task->tk_action = NULL;
3218out_wait:
3219 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003222static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003223 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224 .rpc_call_done = nfs4_close_done,
3225 .rpc_release = nfs4_free_closedata,
3226};
3227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228/*
3229 * It is possible for data to be read/written from a mem-mapped file
3230 * after the sys_close call (which hits the vfs layer as a flush).
3231 * This means that we can't safely call nfsv4 close on a file until
3232 * the inode is cleared. This in turn means that we are not good
3233 * NFSv4 citizens - we do not indicate to the server to update the file's
3234 * share state even when we are done with one of the three share
3235 * stateid's in the inode.
3236 *
3237 * NOTE: Caller must be holding the sp->so_owner semaphore!
3238 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003239int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003241 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003242 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003244 struct nfs4_state_owner *sp = state->owner;
3245 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003246 struct rpc_message msg = {
3247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3248 .rpc_cred = state->owner->so_cred,
3249 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003250 struct rpc_task_setup task_setup_data = {
3251 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003252 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003253 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003254 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003255 .flags = RPC_TASK_ASYNC,
3256 };
Trond Myklebust95121352005-10-18 14:20:12 -07003257 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003259 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3260 &task_setup_data.rpc_client, &msg);
3261
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003262 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003264 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003265 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003266 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003268 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003270 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3271 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003272 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003273 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003274 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003275 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003276 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003277 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003278 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003279 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003280 calldata->lr.roc = pnfs_roc(state->inode,
3281 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3282 if (calldata->lr.roc) {
3283 calldata->arg.lr_args = &calldata->lr.arg;
3284 calldata->res.lr_res = &calldata->lr.res;
3285 }
Al Viro643168c2011-06-22 18:20:23 -04003286 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003287
Trond Myklebust1174dd12010-12-21 10:52:24 -05003288 msg.rpc_argp = &calldata->arg;
3289 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003290 task_setup_data.callback_data = calldata;
3291 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003292 if (IS_ERR(task))
3293 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003294 status = 0;
3295 if (wait)
3296 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003297 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003298 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003299out_free_calldata:
3300 kfree(calldata);
3301out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003302 nfs4_put_open_state(state);
3303 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003304 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Trond Myklebust2b484292010-09-17 10:56:51 -04003307static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003308nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3309 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003312 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3313
3314 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Trond Myklebust565277f2007-10-15 18:17:53 -04003316 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003317 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003318
3319 nfs4_label_release_security(label);
3320
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003321 if (IS_ERR(state))
3322 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003323 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Trond Myklebust1185a552009-12-03 15:54:02 -05003326static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003327{
3328 if (ctx->state == NULL)
3329 return;
3330 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003331 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003332 else
Al Viro643168c2011-06-22 18:20:23 -04003333 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
Trond Myklebustb944dba2013-11-04 15:20:20 -05003336#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3337#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003338#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3341{
Kinglong Mee8c612822015-08-26 21:12:58 +08003342 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003343 struct nfs4_server_caps_arg args = {
3344 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003345 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003346 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 struct nfs4_server_caps_res res = {};
3348 struct rpc_message msg = {
3349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003350 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 .rpc_resp = &res,
3352 };
3353 int status;
3354
Kinglong Mee8c612822015-08-26 21:12:58 +08003355 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3356 FATTR4_WORD0_FH_EXPIRE_TYPE |
3357 FATTR4_WORD0_LINK_SUPPORT |
3358 FATTR4_WORD0_SYMLINK_SUPPORT |
3359 FATTR4_WORD0_ACLSUPPORT;
3360 if (minorversion)
3361 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3362
Bryan Schumaker7c513052011-03-24 17:12:24 +00003363 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003365 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003366 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003367 case 0:
3368 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3369 res.attr_bitmask[2] = 0;
3370 break;
3371 case 1:
3372 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3373 break;
3374 case 2:
3375 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003378 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3379 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3380 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3381 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003382 NFS_CAP_CTIME|NFS_CAP_MTIME|
3383 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003384 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3385 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 server->caps |= NFS_CAP_ACLS;
3387 if (res.has_links != 0)
3388 server->caps |= NFS_CAP_HARDLINKS;
3389 if (res.has_symlinks != 0)
3390 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003391 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3392 server->caps |= NFS_CAP_FILEID;
3393 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3394 server->caps |= NFS_CAP_MODE;
3395 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3396 server->caps |= NFS_CAP_NLINK;
3397 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3398 server->caps |= NFS_CAP_OWNER;
3399 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3400 server->caps |= NFS_CAP_OWNER_GROUP;
3401 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3402 server->caps |= NFS_CAP_ATIME;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3404 server->caps |= NFS_CAP_CTIME;
3405 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3406 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003407#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3408 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3409 server->caps |= NFS_CAP_SECURITY_LABEL;
3410#endif
3411 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3412 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003413 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003414
Trond Myklebusta65318b2009-03-11 14:10:28 -04003415 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3416 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3417 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003418 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003419 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3420 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003422 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003424
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return status;
3426}
3427
Trond Myklebust55a97592006-06-09 09:34:19 -04003428int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 struct nfs4_exception exception = { };
3431 int err;
3432 do {
3433 err = nfs4_handle_exception(server,
3434 _nfs4_server_capabilities(server, fhandle),
3435 &exception);
3436 } while (exception.retry);
3437 return err;
3438}
3439
3440static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3441 struct nfs_fsinfo *info)
3442{
David Quigleyaa9c2662013-05-22 12:50:44 -04003443 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003445 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 };
3447 struct nfs4_lookup_res res = {
3448 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003449 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .fh = fhandle,
3451 };
3452 struct rpc_message msg = {
3453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3454 .rpc_argp = &args,
3455 .rpc_resp = &res,
3456 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003457
David Quigleyaa9c2662013-05-22 12:50:44 -04003458 bitmask[0] = nfs4_fattr_bitmap[0];
3459 bitmask[1] = nfs4_fattr_bitmap[1];
3460 /*
3461 * Process the label in the upcoming getfattr
3462 */
3463 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3464
Trond Myklebust0e574af2005-10-27 22:12:38 -04003465 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003466 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467}
3468
3469static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3470 struct nfs_fsinfo *info)
3471{
3472 struct nfs4_exception exception = { };
3473 int err;
3474 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003475 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003476 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003477 switch (err) {
3478 case 0:
3479 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003480 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003481 default:
3482 err = nfs4_handle_exception(server, err, &exception);
3483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003485out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 return err;
3487}
3488
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003489static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3490 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3491{
Trond Myklebustc2190662013-08-26 19:23:04 -04003492 struct rpc_auth_create_args auth_args = {
3493 .pseudoflavor = flavor,
3494 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003495 struct rpc_auth *auth;
3496 int ret;
3497
Trond Myklebustc2190662013-08-26 19:23:04 -04003498 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003499 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003500 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003501 goto out;
3502 }
3503 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003504out:
3505 return ret;
3506}
3507
Chuck Lever9a744ba2013-03-16 15:56:02 -04003508/*
3509 * Retry pseudoroot lookup with various security flavors. We do this when:
3510 *
3511 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3512 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3513 *
3514 * Returns zero on success, or a negative NFS4ERR value, or a
3515 * negative errno value.
3516 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003517static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003518 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003520 /* Per 3530bis 15.33.5 */
3521 static const rpc_authflavor_t flav_array[] = {
3522 RPC_AUTH_GSS_KRB5P,
3523 RPC_AUTH_GSS_KRB5I,
3524 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003525 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003526 RPC_AUTH_NULL,
3527 };
3528 int status = -EPERM;
3529 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003531 if (server->auth_info.flavor_len > 0) {
3532 /* try each flavor specified by user */
3533 for (i = 0; i < server->auth_info.flavor_len; i++) {
3534 status = nfs4_lookup_root_sec(server, fhandle, info,
3535 server->auth_info.flavors[i]);
3536 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3537 continue;
3538 break;
3539 }
3540 } else {
3541 /* no flavors specified by user, try default list */
3542 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3543 status = nfs4_lookup_root_sec(server, fhandle, info,
3544 flav_array[i]);
3545 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3546 continue;
3547 break;
3548 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003549 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003550
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003551 /*
3552 * -EACCESS could mean that the user doesn't have correct permissions
3553 * to access the mount. It could also mean that we tried to mount
3554 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3555 * existing mount programs don't handle -EACCES very well so it should
3556 * be mapped to -EPERM instead.
3557 */
3558 if (status == -EACCES)
3559 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003560 return status;
3561}
3562
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003563/**
3564 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3565 * @server: initialized nfs_server handle
3566 * @fhandle: we fill in the pseudo-fs root file handle
3567 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003568 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003569 *
3570 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003571 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003572int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003573 struct nfs_fsinfo *info,
3574 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003575{
Andre Przywarac7757072015-04-23 17:17:40 +01003576 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003577
Andre Przywarac7757072015-04-23 17:17:40 +01003578 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003579 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003580
3581 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003582 status = server->nfs_client->cl_mvops->find_root_sec(server,
3583 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003584
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 if (status == 0)
3586 status = nfs4_server_capabilities(server, fhandle);
3587 if (status == 0)
3588 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003589
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003590 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
Bryan Schumakerbae36242012-05-10 15:07:31 -04003593static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3594 struct nfs_fsinfo *info)
3595{
3596 int error;
3597 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003598 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003599
3600 error = nfs4_server_capabilities(server, mntfh);
3601 if (error < 0) {
3602 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3603 return error;
3604 }
3605
David Quigley14c43f72013-05-22 12:50:43 -04003606 label = nfs4_label_alloc(server, GFP_KERNEL);
3607 if (IS_ERR(label))
3608 return PTR_ERR(label);
3609
David Quigley1775fd32013-05-22 12:50:42 -04003610 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003611 if (error < 0) {
3612 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003613 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003614 }
3615
3616 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3617 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3618 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3619
David Quigley14c43f72013-05-22 12:50:43 -04003620err_free_label:
3621 nfs4_label_free(label);
3622
Bryan Schumakerbae36242012-05-10 15:07:31 -04003623 return error;
3624}
3625
Manoj Naik6b97fd32006-06-09 09:34:29 -04003626/*
3627 * Get locations and (maybe) other attributes of a referral.
3628 * Note that we'll actually follow the referral later when
3629 * we detect fsid mismatch in inode revalidation
3630 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003631static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3632 const struct qstr *name, struct nfs_fattr *fattr,
3633 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003634{
3635 int status = -ENOMEM;
3636 struct page *page = NULL;
3637 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003638
3639 page = alloc_page(GFP_KERNEL);
3640 if (page == NULL)
3641 goto out;
3642 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3643 if (locations == NULL)
3644 goto out;
3645
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003646 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003647 if (status != 0)
3648 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003649
3650 /*
3651 * If the fsid didn't change, this is a migration event, not a
3652 * referral. Cause us to drop into the exception handler, which
3653 * will kick off migration recovery.
3654 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003655 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003656 dprintk("%s: server did not return a different fsid for"
3657 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003658 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003659 goto out;
3660 }
Andy Adamson533eb462011-06-13 18:25:56 -04003661 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3662 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003663
Andy Adamson533eb462011-06-13 18:25:56 -04003664 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003665 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003666 memset(fhandle, 0, sizeof(struct nfs_fh));
3667out:
3668 if (page)
3669 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003670 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003671 return status;
3672}
3673
David Quigley1775fd32013-05-22 12:50:42 -04003674static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3675 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676{
3677 struct nfs4_getattr_arg args = {
3678 .fh = fhandle,
3679 .bitmask = server->attr_bitmask,
3680 };
3681 struct nfs4_getattr_res res = {
3682 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003683 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 .server = server,
3685 };
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3688 .rpc_argp = &args,
3689 .rpc_resp = &res,
3690 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003691
3692 args.bitmask = nfs4_bitmask(server, label);
3693
Trond Myklebust0e574af2005-10-27 22:12:38 -04003694 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003695 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696}
3697
David Quigley1775fd32013-05-22 12:50:42 -04003698static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3699 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
3701 struct nfs4_exception exception = { };
3702 int err;
3703 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003704 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3705 trace_nfs4_getattr(server, fhandle, fattr, err);
3706 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 &exception);
3708 } while (exception.retry);
3709 return err;
3710}
3711
3712/*
3713 * The file is not closed if it is opened due to the a request to change
3714 * the size of the file. The open call will not be needed once the
3715 * VFS layer lookup-intents are implemented.
3716 *
3717 * Close is called when the inode is destroyed.
3718 * If we haven't opened the file for O_WRONLY, we
3719 * need to in the size_change case to obtain a stateid.
3720 *
3721 * Got race?
3722 * Because OPEN is always done by name in nfsv4, it is
3723 * possible that we opened a different file by the same
3724 * name. We can recognize this race condition, but we
3725 * can't do anything about it besides returning an error.
3726 *
3727 * This will be fixed with VFS changes (lookup-intent).
3728 */
3729static int
3730nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3731 struct iattr *sattr)
3732{
David Howells2b0143b2015-03-17 22:25:59 +00003733 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003734 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003735 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003736 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 int status;
3738
Peng Tao88ac8152014-09-12 11:04:10 +08003739 if (pnfs_ld_layoutret_on_setattr(inode) &&
3740 sattr->ia_valid & ATTR_SIZE &&
3741 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003742 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003743
Trond Myklebust0e574af2005-10-27 22:12:38 -04003744 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Andy Adamson26699402012-05-30 16:12:24 -04003746 /* Deal with open(O_TRUNC) */
3747 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003748 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003749
3750 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003751 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003752 return 0;
3753
Trond Myklebustd5308382005-11-04 15:33:38 -05003754 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003755 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003756
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003757 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003758 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003759 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003760 }
3761
David Quigley14c43f72013-05-22 12:50:43 -04003762 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3763 if (IS_ERR(label))
3764 return PTR_ERR(label);
3765
NeilBrown29b59f92016-10-13 15:26:47 +11003766 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003767 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003768 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003769 nfs_setsecurity(inode, fattr, label);
3770 }
David Quigley14c43f72013-05-22 12:50:43 -04003771 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return status;
3773}
3774
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003775static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3776 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003777 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003778{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003779 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003780 int status;
3781 struct nfs4_lookup_arg args = {
3782 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003783 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003784 .name = name,
3785 };
3786 struct nfs4_lookup_res res = {
3787 .server = server,
3788 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003789 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003790 .fh = fhandle,
3791 };
3792 struct rpc_message msg = {
3793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3794 .rpc_argp = &args,
3795 .rpc_resp = &res,
3796 };
3797
David Quigleyaa9c2662013-05-22 12:50:44 -04003798 args.bitmask = nfs4_bitmask(server, label);
3799
David Howells2b3de442006-08-22 20:06:09 -04003800 nfs_fattr_init(fattr);
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003803 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 dprintk("NFS reply lookup: %d\n", status);
3805 return status;
3806}
3807
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003808static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003809{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003810 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003811 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003812 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3813 fattr->nlink = 2;
3814}
3815
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003816static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003817 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003818 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003819{
3820 struct nfs4_exception exception = { };
3821 struct rpc_clnt *client = *clnt;
3822 int err;
3823 do {
David Quigley1775fd32013-05-22 12:50:42 -04003824 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003825 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003826 switch (err) {
3827 case -NFS4ERR_BADNAME:
3828 err = -ENOENT;
3829 goto out;
3830 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003831 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003832 if (err == -NFS4ERR_MOVED)
3833 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003834 goto out;
3835 case -NFS4ERR_WRONGSEC:
3836 err = -EPERM;
3837 if (client != *clnt)
3838 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003839 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003840 if (IS_ERR(client))
3841 return PTR_ERR(client);
3842
3843 exception.retry = 1;
3844 break;
3845 default:
3846 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3847 }
3848 } while (exception.retry);
3849
3850out:
3851 if (err == 0)
3852 *clnt = client;
3853 else if (client != *clnt)
3854 rpc_shutdown_client(client);
3855
3856 return err;
3857}
3858
Al Virobeffb8f2016-07-20 16:34:42 -04003859static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003860 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3861 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003863 int status;
3864 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003865
David Quigley1775fd32013-05-22 12:50:42 -04003866 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003867 if (client != NFS_CLIENT(dir)) {
3868 rpc_shutdown_client(client);
3869 nfs_fixup_secinfo_attributes(fattr);
3870 }
3871 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003874struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003875nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003876 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3877{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003878 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003879 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003880
David Quigley1775fd32013-05-22 12:50:42 -04003881 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003882 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003883 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003884 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003885}
3886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3888{
Trond Myklebust76b32992007-08-10 17:45:11 -04003889 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 struct nfs4_accessargs args = {
3891 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003892 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003894 struct nfs4_accessres res = {
3895 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003896 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 struct rpc_message msg = {
3898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3899 .rpc_argp = &args,
3900 .rpc_resp = &res,
3901 .rpc_cred = entry->cred,
3902 };
3903 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003904 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 /*
3907 * Determine which access bits we want to ask for...
3908 */
3909 if (mode & MAY_READ)
3910 args.access |= NFS4_ACCESS_READ;
3911 if (S_ISDIR(inode->i_mode)) {
3912 if (mode & MAY_WRITE)
3913 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3914 if (mode & MAY_EXEC)
3915 args.access |= NFS4_ACCESS_LOOKUP;
3916 } else {
3917 if (mode & MAY_WRITE)
3918 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3919 if (mode & MAY_EXEC)
3920 args.access |= NFS4_ACCESS_EXECUTE;
3921 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003922
3923 res.fattr = nfs_alloc_fattr();
3924 if (res.fattr == NULL)
3925 return -ENOMEM;
3926
Bryan Schumaker7c513052011-03-24 17:12:24 +00003927 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003929 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003930 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003932 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return status;
3934}
3935
3936static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3937{
3938 struct nfs4_exception exception = { };
3939 int err;
3940 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003941 err = _nfs4_proc_access(inode, entry);
3942 trace_nfs4_access(inode, err);
3943 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 &exception);
3945 } while (exception.retry);
3946 return err;
3947}
3948
3949/*
3950 * TODO: For the time being, we don't try to get any attributes
3951 * along with any of the zero-copy operations READ, READDIR,
3952 * READLINK, WRITE.
3953 *
3954 * In the case of the first three, we want to put the GETATTR
3955 * after the read-type operation -- this is because it is hard
3956 * to predict the length of a GETATTR response in v4, and thus
3957 * align the READ data correctly. This means that the GETATTR
3958 * may end up partially falling into the page cache, and we should
3959 * shift it into the 'tail' of the xdr_buf before processing.
3960 * To do this efficiently, we need to know the total length
3961 * of data received, which doesn't seem to be available outside
3962 * of the RPC layer.
3963 *
3964 * In the case of WRITE, we also want to put the GETATTR after
3965 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003966 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 *
3968 * Both of these changes to the XDR layer would in fact be quite
3969 * minor, but I decided to leave them for a subsequent patch.
3970 */
3971static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3972 unsigned int pgbase, unsigned int pglen)
3973{
3974 struct nfs4_readlink args = {
3975 .fh = NFS_FH(inode),
3976 .pgbase = pgbase,
3977 .pglen = pglen,
3978 .pages = &page,
3979 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003980 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 struct rpc_message msg = {
3982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3983 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003984 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 };
3986
Bryan Schumaker7c513052011-03-24 17:12:24 +00003987 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 -07003988}
3989
3990static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3991 unsigned int pgbase, unsigned int pglen)
3992{
3993 struct nfs4_exception exception = { };
3994 int err;
3995 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003996 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3997 trace_nfs4_readlink(inode, err);
3998 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 &exception);
4000 } while (exception.retry);
4001 return err;
4002}
4003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004005 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007static int
4008nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004009 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
David Quigleyaa9c2662013-05-22 12:50:44 -04004011 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004012 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 int status = 0;
4015
NeilBrown532d4de2016-10-13 15:26:47 +11004016 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004017 if (IS_ERR(ctx))
4018 return PTR_ERR(ctx);
4019
David Quigleyaa9c2662013-05-22 12:50:44 -04004020 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4021
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004022 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004023 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (IS_ERR(state)) {
4025 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004026 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004029 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004030 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return status;
4032}
4033
Al Virobeffb8f2016-07-20 16:34:42 -04004034static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Trond Myklebust16e42952005-10-27 22:12:44 -04004036 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004037 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004039 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004041 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004042 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4046 .rpc_argp = &args,
4047 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004049 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004050
Bryan Schumaker7c513052011-03-24 17:12:24 +00004051 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004052 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004053 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 return status;
4055}
4056
Al Virobeffb8f2016-07-20 16:34:42 -04004057static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
4059 struct nfs4_exception exception = { };
4060 int err;
4061 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004062 err = _nfs4_proc_remove(dir, name);
4063 trace_nfs4_remove(dir, name, err);
4064 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 &exception);
4066 } while (exception.retry);
4067 return err;
4068}
4069
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004070static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072 struct nfs_server *server = NFS_SERVER(dir);
4073 struct nfs_removeargs *args = msg->rpc_argp;
4074 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004076 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004078 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004079
4080 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081}
4082
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004083static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4084{
Al Viro884be172016-04-28 23:56:31 -04004085 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004086 &data->args.seq_args,
4087 &data->res.seq_res,
4088 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004091static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004093 struct nfs_unlinkdata *data = task->tk_calldata;
4094 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004095
Trond Myklebust14516c32010-07-31 14:29:06 -04004096 if (!nfs4_sequence_done(task, &res->seq_res))
4097 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004098 if (nfs4_async_handle_error(task, res->server, NULL,
4099 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004100 return 0;
4101 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004102 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103}
4104
Jeff Laytond3d41522010-09-17 17:31:57 -04004105static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4106{
4107 struct nfs_server *server = NFS_SERVER(dir);
4108 struct nfs_renameargs *arg = msg->rpc_argp;
4109 struct nfs_renameres *res = msg->rpc_resp;
4110
4111 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004112 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004113 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004114}
4115
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004116static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4117{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004118 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4119 &data->args.seq_args,
4120 &data->res.seq_res,
4121 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004122}
4123
4124static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4125 struct inode *new_dir)
4126{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004127 struct nfs_renamedata *data = task->tk_calldata;
4128 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004129
4130 if (!nfs4_sequence_done(task, &res->seq_res))
4131 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004132 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004133 return 0;
4134
4135 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004136 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004137 return 1;
4138}
4139
Al Virobeffb8f2016-07-20 16:34:42 -04004140static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004142 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 struct nfs4_link_arg arg = {
4144 .fh = NFS_FH(inode),
4145 .dir_fh = NFS_FH(dir),
4146 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004147 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004149 struct nfs4_link_res res = {
4150 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004151 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 struct rpc_message msg = {
4154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4155 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004156 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004158 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Trond Myklebust136f2622010-04-16 16:22:49 -04004160 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004161 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004162 goto out;
4163
David Quigley14c43f72013-05-22 12:50:43 -04004164 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4165 if (IS_ERR(res.label)) {
4166 status = PTR_ERR(res.label);
4167 goto out;
4168 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004169 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004170
Bryan Schumaker7c513052011-03-24 17:12:24 +00004171 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004172 if (!status) {
4173 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004174 status = nfs_post_op_update_inode(inode, res.fattr);
4175 if (!status)
4176 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004177 }
David Quigley14c43f72013-05-22 12:50:43 -04004178
4179
4180 nfs4_label_free(res.label);
4181
Trond Myklebust136f2622010-04-16 16:22:49 -04004182out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004183 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 return status;
4185}
4186
Al Virobeffb8f2016-07-20 16:34:42 -04004187static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188{
4189 struct nfs4_exception exception = { };
4190 int err;
4191 do {
4192 err = nfs4_handle_exception(NFS_SERVER(inode),
4193 _nfs4_proc_link(inode, dir, name),
4194 &exception);
4195 } while (exception.retry);
4196 return err;
4197}
4198
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004199struct nfs4_createdata {
4200 struct rpc_message msg;
4201 struct nfs4_create_arg arg;
4202 struct nfs4_create_res res;
4203 struct nfs_fh fh;
4204 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004205 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004206};
4207
4208static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004209 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004210{
4211 struct nfs4_createdata *data;
4212
4213 data = kzalloc(sizeof(*data), GFP_KERNEL);
4214 if (data != NULL) {
4215 struct nfs_server *server = NFS_SERVER(dir);
4216
David Quigley14c43f72013-05-22 12:50:43 -04004217 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4218 if (IS_ERR(data->label))
4219 goto out_free;
4220
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004221 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4222 data->msg.rpc_argp = &data->arg;
4223 data->msg.rpc_resp = &data->res;
4224 data->arg.dir_fh = NFS_FH(dir);
4225 data->arg.server = server;
4226 data->arg.name = name;
4227 data->arg.attrs = sattr;
4228 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004229 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 data->res.server = server;
4231 data->res.fh = &data->fh;
4232 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004233 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004234 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004235 }
4236 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004237out_free:
4238 kfree(data);
4239 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004240}
4241
4242static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4243{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004244 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004245 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246 if (status == 0) {
4247 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004248 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004249 }
4250 return status;
4251}
4252
4253static void nfs4_free_createdata(struct nfs4_createdata *data)
4254{
David Quigley14c43f72013-05-22 12:50:43 -04004255 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004256 kfree(data);
4257}
4258
Chuck Lever4f390c12006-08-22 20:06:22 -04004259static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004260 struct page *page, unsigned int len, struct iattr *sattr,
4261 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004263 struct nfs4_createdata *data;
4264 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Chuck Lever94a6d752006-08-22 20:06:23 -04004266 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004267 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004268
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004269 status = -ENOMEM;
4270 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4271 if (data == NULL)
4272 goto out;
4273
4274 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4275 data->arg.u.symlink.pages = &page;
4276 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004277 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004279 status = nfs4_do_create(dir, dentry, data);
4280
4281 nfs4_free_createdata(data);
4282out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return status;
4284}
4285
Chuck Lever4f390c12006-08-22 20:06:22 -04004286static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004287 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
4289 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004290 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004292
4293 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004296 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4297 trace_nfs4_symlink(dir, &dentry->d_name, err);
4298 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 &exception);
4300 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004301
4302 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return err;
4304}
4305
4306static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004307 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004309 struct nfs4_createdata *data;
4310 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004312 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4313 if (data == NULL)
4314 goto out;
4315
David Quigley1775fd32013-05-22 12:50:42 -04004316 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004317 status = nfs4_do_create(dir, dentry, data);
4318
4319 nfs4_free_createdata(data);
4320out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return status;
4322}
4323
4324static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4325 struct iattr *sattr)
4326{
4327 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004328 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004330
David Quigleyaa9c2662013-05-22 12:50:44 -04004331 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4332
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004333 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004335 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4336 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4337 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 &exception);
4339 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004340 nfs4_label_release_security(label);
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 return err;
4343}
4344
4345static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004346 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347{
David Howells2b0143b2015-03-17 22:25:59 +00004348 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 struct nfs4_readdir_arg args = {
4350 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004351 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 .pgbase = 0,
4353 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004354 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004355 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 };
4357 struct nfs4_readdir_res res;
4358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4360 .rpc_argp = &args,
4361 .rpc_resp = &res,
4362 .rpc_cred = cred,
4363 };
4364 int status;
4365
Al Viro6de14722013-09-16 10:53:17 -04004366 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4367 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004368 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004369 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004371 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 -05004372 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004373 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004374 status += args.pgbase;
4375 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004376
4377 nfs_invalidate_atime(dir);
4378
Harvey Harrison3110ff82008-05-02 13:42:44 -07004379 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 return status;
4381}
4382
4383static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004384 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
4386 struct nfs4_exception exception = { };
4387 int err;
4388 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004389 err = _nfs4_proc_readdir(dentry, cred, cookie,
4390 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004391 trace_nfs4_readdir(d_inode(dentry), err);
4392 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 &exception);
4394 } while (exception.retry);
4395 return err;
4396}
4397
4398static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004399 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 struct nfs4_createdata *data;
4402 int mode = sattr->ia_mode;
4403 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4406 if (data == NULL)
4407 goto out;
4408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004410 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 data->arg.ftype = NF4BLK;
4413 data->arg.u.device.specdata1 = MAJOR(rdev);
4414 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 }
4416 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004417 data->arg.ftype = NF4CHR;
4418 data->arg.u.device.specdata1 = MAJOR(rdev);
4419 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004420 } else if (!S_ISSOCK(mode)) {
4421 status = -EINVAL;
4422 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
David Quigley1775fd32013-05-22 12:50:42 -04004424
David Quigleyaa9c2662013-05-22 12:50:44 -04004425 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004427out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004428 nfs4_free_createdata(data);
4429out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return status;
4431}
4432
4433static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4434 struct iattr *sattr, dev_t rdev)
4435{
4436 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004437 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004439
David Quigleyaa9c2662013-05-22 12:50:44 -04004440 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4441
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004442 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004444 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4445 trace_nfs4_mknod(dir, &dentry->d_name, err);
4446 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 &exception);
4448 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004449
4450 nfs4_label_release_security(label);
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return err;
4453}
4454
4455static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4456 struct nfs_fsstat *fsstat)
4457{
4458 struct nfs4_statfs_arg args = {
4459 .fh = fhandle,
4460 .bitmask = server->attr_bitmask,
4461 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004462 struct nfs4_statfs_res res = {
4463 .fsstat = fsstat,
4464 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 struct rpc_message msg = {
4466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4467 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004468 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 };
4470
Trond Myklebust0e574af2005-10-27 22:12:38 -04004471 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004472 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473}
4474
4475static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4476{
4477 struct nfs4_exception exception = { };
4478 int err;
4479 do {
4480 err = nfs4_handle_exception(server,
4481 _nfs4_proc_statfs(server, fhandle, fsstat),
4482 &exception);
4483 } while (exception.retry);
4484 return err;
4485}
4486
4487static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4488 struct nfs_fsinfo *fsinfo)
4489{
4490 struct nfs4_fsinfo_arg args = {
4491 .fh = fhandle,
4492 .bitmask = server->attr_bitmask,
4493 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004494 struct nfs4_fsinfo_res res = {
4495 .fsinfo = fsinfo,
4496 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 struct rpc_message msg = {
4498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4499 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004500 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 };
4502
Bryan Schumaker7c513052011-03-24 17:12:24 +00004503 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}
4505
4506static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4507{
4508 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004509 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 int err;
4511
4512 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004513 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004514 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004515 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004516 nfs4_set_lease_period(server->nfs_client,
4517 fsinfo->lease_time * HZ,
4518 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004519 break;
4520 }
4521 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 } while (exception.retry);
4523 return err;
4524}
4525
4526static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4527{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004528 int error;
4529
Trond Myklebust0e574af2005-10-27 22:12:38 -04004530 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004531 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004532 if (error == 0) {
4533 /* block layout checks this! */
4534 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004535 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004536 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004537
4538 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539}
4540
4541static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4542 struct nfs_pathconf *pathconf)
4543{
4544 struct nfs4_pathconf_arg args = {
4545 .fh = fhandle,
4546 .bitmask = server->attr_bitmask,
4547 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004548 struct nfs4_pathconf_res res = {
4549 .pathconf = pathconf,
4550 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 struct rpc_message msg = {
4552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4553 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004554 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 };
4556
4557 /* None of the pathconf attributes are mandatory to implement */
4558 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4559 memset(pathconf, 0, sizeof(*pathconf));
4560 return 0;
4561 }
4562
Trond Myklebust0e574af2005-10-27 22:12:38 -04004563 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004564 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
4567static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4568 struct nfs_pathconf *pathconf)
4569{
4570 struct nfs4_exception exception = { };
4571 int err;
4572
4573 do {
4574 err = nfs4_handle_exception(server,
4575 _nfs4_proc_pathconf(server, fhandle, pathconf),
4576 &exception);
4577 } while (exception.retry);
4578 return err;
4579}
4580
Trond Myklebust5521abf2013-03-16 20:54:34 -04004581int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004582 const struct nfs_open_context *ctx,
4583 const struct nfs_lock_context *l_ctx,
4584 fmode_t fmode)
4585{
NeilBrown17393472016-10-13 15:26:47 +11004586 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004587}
4588EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4589
Trond Myklebust5521abf2013-03-16 20:54:34 -04004590static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4591 const struct nfs_open_context *ctx,
4592 const struct nfs_lock_context *l_ctx,
4593 fmode_t fmode)
4594{
4595 nfs4_stateid current_stateid;
4596
Trond Myklebuste1253be2014-03-05 08:44:23 -05004597 /* If the current stateid represents a lost lock, then exit */
4598 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4599 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004600 return nfs4_stateid_match(stateid, &current_stateid);
4601}
4602
4603static bool nfs4_error_stateid_expired(int err)
4604{
4605 switch (err) {
4606 case -NFS4ERR_DELEG_REVOKED:
4607 case -NFS4ERR_ADMIN_REVOKED:
4608 case -NFS4ERR_BAD_STATEID:
4609 case -NFS4ERR_STALE_STATEID:
4610 case -NFS4ERR_OLD_STATEID:
4611 case -NFS4ERR_OPENMODE:
4612 case -NFS4ERR_EXPIRED:
4613 return true;
4614 }
4615 return false;
4616}
4617
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004618static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004622 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004623 if (task->tk_status < 0) {
4624 struct nfs4_exception exception = {
4625 .inode = hdr->inode,
4626 .state = hdr->args.context->state,
4627 .stateid = &hdr->args.stateid,
4628 };
4629 task->tk_status = nfs4_async_handle_exception(task,
4630 server, task->tk_status, &exception);
4631 if (exception.retry) {
4632 rpc_restart_call_prepare(task);
4633 return -EAGAIN;
4634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004638 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
Trond Myklebust5521abf2013-03-16 20:54:34 -04004642static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004643 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004644{
4645
4646 if (!nfs4_error_stateid_expired(task->tk_status) ||
4647 nfs4_stateid_is_current(&args->stateid,
4648 args->context,
4649 args->lock_context,
4650 FMODE_READ))
4651 return false;
4652 rpc_restart_call_prepare(task);
4653 return true;
4654}
4655
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004656static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004657{
4658
4659 dprintk("--> %s\n", __func__);
4660
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004662 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004664 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004665 if (task->tk_status > 0)
4666 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004667 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4668 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004669}
4670
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4672 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004674 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004675 if (!hdr->pgio_done_cb)
4676 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004677 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004678 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679}
4680
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4682 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004683{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4685 &hdr->args.seq_args,
4686 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004687 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004688 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4690 hdr->args.lock_context,
4691 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004692 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004693 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004694 return -EIO;
4695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696}
4697
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004698static int nfs4_write_done_cb(struct rpc_task *task,
4699 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004702
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004704 if (task->tk_status < 0) {
4705 struct nfs4_exception exception = {
4706 .inode = hdr->inode,
4707 .state = hdr->args.context->state,
4708 .stateid = &hdr->args.stateid,
4709 };
4710 task->tk_status = nfs4_async_handle_exception(task,
4711 NFS_SERVER(inode), task->tk_status,
4712 &exception);
4713 if (exception.retry) {
4714 rpc_restart_call_prepare(task);
4715 return -EAGAIN;
4716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004718 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004719 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004720 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004721 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723}
4724
Trond Myklebust5521abf2013-03-16 20:54:34 -04004725static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004726 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004727{
4728
4729 if (!nfs4_error_stateid_expired(task->tk_status) ||
4730 nfs4_stateid_is_current(&args->stateid,
4731 args->context,
4732 args->lock_context,
4733 FMODE_WRITE))
4734 return false;
4735 rpc_restart_call_prepare(task);
4736 return true;
4737}
4738
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004739static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004740{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004741 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004742 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004744 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4746 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004747}
4748
Trond Myklebust5a37f852012-04-28 14:55:16 -04004749static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004750bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004751{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004752 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004754 return false;
4755 /* Otherwise, request attributes if and only if we don't hold
4756 * a delegation
4757 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004758 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004759}
Fred Isamana69aef12011-03-03 15:13:47 +00004760
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004761static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4762 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004765
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4767 hdr->args.bitmask = NULL;
4768 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004769 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004770 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004771
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004772 if (!hdr->pgio_done_cb)
4773 hdr->pgio_done_cb = nfs4_write_done_cb;
4774 hdr->res.server = server;
4775 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004777 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004778 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779}
4780
Fred Isaman0b7c0152012-04-20 14:47:39 -04004781static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4782{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004783 nfs4_setup_sequence(NFS_SERVER(data->inode),
4784 &data->args.seq_args,
4785 &data->res.seq_res,
4786 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787}
4788
Fred Isaman0b7c0152012-04-20 14:47:39 -04004789static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004792
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004793 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004794 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4795 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004796 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004797 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800}
4801
Fred Isaman0b7c0152012-04-20 14:47:39 -04004802static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004803{
4804 if (!nfs4_sequence_done(task, &data->res.seq_res))
4805 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004806 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004807}
4808
Fred Isaman0b7c0152012-04-20 14:47:39 -04004809static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004811 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004812
Fred Isaman0b7c0152012-04-20 14:47:39 -04004813 if (data->commit_done_cb == NULL)
4814 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004815 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004816 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004817 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004820struct nfs4_renewdata {
4821 struct nfs_client *client;
4822 unsigned long timestamp;
4823};
4824
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825/*
4826 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4827 * standalone procedure for queueing an asynchronous RENEW.
4828 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004829static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004830{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004831 struct nfs4_renewdata *data = calldata;
4832 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004833
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004834 if (atomic_read(&clp->cl_count) > 1)
4835 nfs4_schedule_state_renewal(clp);
4836 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004837 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004838}
4839
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004840static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004842 struct nfs4_renewdata *data = calldata;
4843 struct nfs_client *clp = data->client;
4844 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004846 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004847 switch (task->tk_status) {
4848 case 0:
4849 break;
4850 case -NFS4ERR_LEASE_MOVED:
4851 nfs4_schedule_lease_moved_recovery(clp);
4852 break;
4853 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004854 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004855 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4856 return;
4857 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004858 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004859 return;
4860 }
4861 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004863 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864}
4865
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004866static const struct rpc_call_ops nfs4_renew_ops = {
4867 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004868 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004869};
4870
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004871static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872{
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4875 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004876 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004878 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004880 if (renew_flags == 0)
4881 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004882 if (!atomic_inc_not_zero(&clp->cl_count))
4883 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004884 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004885 if (data == NULL)
4886 return -ENOMEM;
4887 data->client = clp;
4888 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004889 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004890 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004893static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894{
4895 struct rpc_message msg = {
4896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4897 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004898 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 };
4900 unsigned long now = jiffies;
4901 int status;
4902
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004903 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 if (status < 0)
4905 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004906 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return 0;
4908}
4909
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004910static inline int nfs4_server_supports_acls(struct nfs_server *server)
4911{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004912 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004913}
4914
Trond Myklebust21f498c2012-08-24 10:59:25 -04004915/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4916 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004917 * the stack.
4918 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004919#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004920
Neil Hormane9e3d722011-03-04 19:26:03 -05004921static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004922 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004923{
4924 struct page *newpage, **spages;
4925 int rc = 0;
4926 size_t len;
4927 spages = pages;
4928
4929 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004930 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004931 newpage = alloc_page(GFP_KERNEL);
4932
4933 if (newpage == NULL)
4934 goto unwind;
4935 memcpy(page_address(newpage), buf, len);
4936 buf += len;
4937 buflen -= len;
4938 *pages++ = newpage;
4939 rc++;
4940 } while (buflen != 0);
4941
4942 return rc;
4943
4944unwind:
4945 for(; rc > 0; rc--)
4946 __free_page(spages[rc-1]);
4947 return -ENOMEM;
4948}
4949
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004950struct nfs4_cached_acl {
4951 int cached;
4952 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004953 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004954};
4955
4956static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004957{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004958 struct nfs_inode *nfsi = NFS_I(inode);
4959
4960 spin_lock(&inode->i_lock);
4961 kfree(nfsi->nfs4_acl);
4962 nfsi->nfs4_acl = acl;
4963 spin_unlock(&inode->i_lock);
4964}
4965
4966static void nfs4_zap_acl_attr(struct inode *inode)
4967{
4968 nfs4_set_cached_acl(inode, NULL);
4969}
4970
4971static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4972{
4973 struct nfs_inode *nfsi = NFS_I(inode);
4974 struct nfs4_cached_acl *acl;
4975 int ret = -ENOENT;
4976
4977 spin_lock(&inode->i_lock);
4978 acl = nfsi->nfs4_acl;
4979 if (acl == NULL)
4980 goto out;
4981 if (buf == NULL) /* user is just asking for length */
4982 goto out_len;
4983 if (acl->cached == 0)
4984 goto out;
4985 ret = -ERANGE; /* see getxattr(2) man page */
4986 if (acl->len > buflen)
4987 goto out;
4988 memcpy(buf, acl->data, acl->len);
4989out_len:
4990 ret = acl->len;
4991out:
4992 spin_unlock(&inode->i_lock);
4993 return ret;
4994}
4995
Sachin Prabhu5794d212012-04-17 14:36:40 +01004996static 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 +00004997{
4998 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004999 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005000
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005001 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005002 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005003 if (acl == NULL)
5004 goto out;
5005 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005006 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005007 } else {
5008 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5009 if (acl == NULL)
5010 goto out;
5011 acl->cached = 0;
5012 }
5013 acl->len = acl_len;
5014out:
5015 nfs4_set_cached_acl(inode, acl);
5016}
5017
Andy Adamsonbf118a32011-12-07 11:55:27 -05005018/*
5019 * The getxattr API returns the required buffer length when called with a
5020 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5021 * the required buf. On a NULL buf, we send a page of data to the server
5022 * guessing that the ACL request can be serviced by a page. If so, we cache
5023 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5024 * the cache. If not so, we throw away the page, and cache the required
5025 * length. The next getxattr call will then produce another round trip to
5026 * the server, this time with the input buf of the required size.
5027 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005028static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005029{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005030 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005031 struct nfs_getaclargs args = {
5032 .fh = NFS_FH(inode),
5033 .acl_pages = pages,
5034 .acl_len = buflen,
5035 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005036 struct nfs_getaclres res = {
5037 .acl_len = buflen,
5038 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005039 struct rpc_message msg = {
5040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5041 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005042 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005043 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005044 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5045 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005046
Andy Adamsonbf118a32011-12-07 11:55:27 -05005047 /* As long as we're doing a round trip to the server anyway,
5048 * let's be prepared for a page of acl data. */
5049 if (npages == 0)
5050 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005051 if (npages > ARRAY_SIZE(pages))
5052 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005053
Andy Adamsonbf118a32011-12-07 11:55:27 -05005054 for (i = 0; i < npages; i++) {
5055 pages[i] = alloc_page(GFP_KERNEL);
5056 if (!pages[i])
5057 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005058 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005059
5060 /* for decoding across pages */
5061 res.acl_scratch = alloc_page(GFP_KERNEL);
5062 if (!res.acl_scratch)
5063 goto out_free;
5064
Andy Adamsonbf118a32011-12-07 11:55:27 -05005065 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005066
Peng Taode040be2012-01-10 22:42:47 +08005067 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005068 __func__, buf, buflen, npages, args.acl_len);
5069 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5070 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005071 if (ret)
5072 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005073
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005074 /* Handle the case where the passed-in buffer is too short */
5075 if (res.acl_flags & NFS4_ACL_TRUNC) {
5076 /* Did the user only issue a request for the acl length? */
5077 if (buf == NULL)
5078 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005079 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005080 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005082 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005083 if (buf) {
5084 if (res.acl_len > buflen) {
5085 ret = -ERANGE;
5086 goto out_free;
5087 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005088 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005089 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005090out_ok:
5091 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005092out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005093 for (i = 0; i < npages; i++)
5094 if (pages[i])
5095 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005096 if (res.acl_scratch)
5097 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005098 return ret;
5099}
5100
Trond Myklebust16b42892006-08-24 12:27:15 -04005101static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5102{
5103 struct nfs4_exception exception = { };
5104 ssize_t ret;
5105 do {
5106 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005107 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005108 if (ret >= 0)
5109 break;
5110 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5111 } while (exception.retry);
5112 return ret;
5113}
5114
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5116{
5117 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005118 int ret;
5119
5120 if (!nfs4_server_supports_acls(server))
5121 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005122 ret = nfs_revalidate_inode(server, inode);
5123 if (ret < 0)
5124 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005125 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5126 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005127 ret = nfs4_read_cached_acl(inode, buf, buflen);
5128 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005129 /* -ENOENT is returned if there is no ACL or if there is an ACL
5130 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005131 return ret;
5132 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005133}
5134
Trond Myklebust16b42892006-08-24 12:27:15 -04005135static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005136{
5137 struct nfs_server *server = NFS_SERVER(inode);
5138 struct page *pages[NFS4ACL_MAXPAGES];
5139 struct nfs_setaclargs arg = {
5140 .fh = NFS_FH(inode),
5141 .acl_pages = pages,
5142 .acl_len = buflen,
5143 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005144 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005145 struct rpc_message msg = {
5146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5147 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005148 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005149 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005150 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005151 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005152
5153 if (!nfs4_server_supports_acls(server))
5154 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005155 if (npages > ARRAY_SIZE(pages))
5156 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005157 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005158 if (i < 0)
5159 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005160 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005161 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005162
5163 /*
5164 * Free each page after tx, so the only ref left is
5165 * held by the network stack
5166 */
5167 for (; i > 0; i--)
5168 put_page(pages[i-1]);
5169
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005170 /*
5171 * Acl update can result in inode attribute update.
5172 * so mark the attribute cache invalid.
5173 */
5174 spin_lock(&inode->i_lock);
5175 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5176 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005177 nfs_access_zap_cache(inode);
5178 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005179 return ret;
5180}
5181
Trond Myklebust16b42892006-08-24 12:27:15 -04005182static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5183{
5184 struct nfs4_exception exception = { };
5185 int err;
5186 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005187 err = __nfs4_proc_set_acl(inode, buf, buflen);
5188 trace_nfs4_set_acl(inode, err);
5189 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005190 &exception);
5191 } while (exception.retry);
5192 return err;
5193}
5194
David Quigleyaa9c2662013-05-22 12:50:44 -04005195#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5196static int _nfs4_get_security_label(struct inode *inode, void *buf,
5197 size_t buflen)
5198{
5199 struct nfs_server *server = NFS_SERVER(inode);
5200 struct nfs_fattr fattr;
5201 struct nfs4_label label = {0, 0, buflen, buf};
5202
5203 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005204 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005205 .fh = NFS_FH(inode),
5206 .bitmask = bitmask,
5207 };
5208 struct nfs4_getattr_res res = {
5209 .fattr = &fattr,
5210 .label = &label,
5211 .server = server,
5212 };
5213 struct rpc_message msg = {
5214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005215 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005216 .rpc_resp = &res,
5217 };
5218 int ret;
5219
5220 nfs_fattr_init(&fattr);
5221
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005222 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 if (ret)
5224 return ret;
5225 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5226 return -ENOENT;
5227 if (buflen < label.len)
5228 return -ERANGE;
5229 return 0;
5230}
5231
5232static int nfs4_get_security_label(struct inode *inode, void *buf,
5233 size_t buflen)
5234{
5235 struct nfs4_exception exception = { };
5236 int err;
5237
5238 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5239 return -EOPNOTSUPP;
5240
5241 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005242 err = _nfs4_get_security_label(inode, buf, buflen);
5243 trace_nfs4_get_security_label(inode, err);
5244 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005245 &exception);
5246 } while (exception.retry);
5247 return err;
5248}
5249
5250static int _nfs4_do_set_security_label(struct inode *inode,
5251 struct nfs4_label *ilabel,
5252 struct nfs_fattr *fattr,
5253 struct nfs4_label *olabel)
5254{
5255
5256 struct iattr sattr = {0};
5257 struct nfs_server *server = NFS_SERVER(inode);
5258 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005259 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005260 .fh = NFS_FH(inode),
5261 .iap = &sattr,
5262 .server = server,
5263 .bitmask = bitmask,
5264 .label = ilabel,
5265 };
5266 struct nfs_setattrres res = {
5267 .fattr = fattr,
5268 .label = olabel,
5269 .server = server,
5270 };
5271 struct rpc_message msg = {
5272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005273 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005274 .rpc_resp = &res,
5275 };
5276 int status;
5277
Jeff Layton12207f62013-11-01 10:49:32 -04005278 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005279
Jeff Layton12207f62013-11-01 10:49:32 -04005280 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005281 if (status)
5282 dprintk("%s failed: %d\n", __func__, status);
5283
5284 return status;
5285}
5286
5287static int nfs4_do_set_security_label(struct inode *inode,
5288 struct nfs4_label *ilabel,
5289 struct nfs_fattr *fattr,
5290 struct nfs4_label *olabel)
5291{
5292 struct nfs4_exception exception = { };
5293 int err;
5294
5295 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005296 err = _nfs4_do_set_security_label(inode, ilabel,
5297 fattr, olabel);
5298 trace_nfs4_set_security_label(inode, err);
5299 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005300 &exception);
5301 } while (exception.retry);
5302 return err;
5303}
5304
5305static int
Al Viro59301222016-05-27 10:19:30 -04005306nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005307{
5308 struct nfs4_label ilabel, *olabel = NULL;
5309 struct nfs_fattr fattr;
5310 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005311 int status;
5312
5313 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5314 return -EOPNOTSUPP;
5315
5316 nfs_fattr_init(&fattr);
5317
5318 ilabel.pi = 0;
5319 ilabel.lfs = 0;
5320 ilabel.label = (char *)buf;
5321 ilabel.len = buflen;
5322
5323 cred = rpc_lookup_cred();
5324 if (IS_ERR(cred))
5325 return PTR_ERR(cred);
5326
5327 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5328 if (IS_ERR(olabel)) {
5329 status = -PTR_ERR(olabel);
5330 goto out;
5331 }
5332
5333 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5334 if (status == 0)
5335 nfs_setsecurity(inode, &fattr, olabel);
5336
5337 nfs4_label_free(olabel);
5338out:
5339 put_rpccred(cred);
5340 return status;
5341}
5342#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5343
5344
Chuck Leverf0920752012-05-21 22:45:41 -04005345static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5346 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005347{
5348 __be32 verf[2];
5349
Chuck Lever2c820d92012-05-21 22:45:33 -04005350 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5351 /* An impossible timestamp guarantees this value
5352 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005353 verf[0] = cpu_to_be32(U32_MAX);
5354 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005355 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005356 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005357 u64 ns = ktime_to_ns(nn->boot_time);
5358
5359 verf[0] = cpu_to_be32(ns >> 32);
5360 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005361 }
Chuck Levercd937102012-03-02 17:14:31 -05005362 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5363}
5364
Jeff Laytona3192682015-06-09 19:43:59 -04005365static int
5366nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005367{
Jeff Laytona3192682015-06-09 19:43:59 -04005368 size_t len;
5369 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005370
Trond Myklebustceb3a162015-01-03 15:16:04 -05005371 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005372 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005373
Jeff Laytona3192682015-06-09 19:43:59 -04005374 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005375 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005376 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5377 1 +
5378 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5379 1;
5380 rcu_read_unlock();
5381
5382 if (len > NFS4_OPAQUE_LIMIT + 1)
5383 return -EINVAL;
5384
5385 /*
5386 * Since this string is allocated at mount time, and held until the
5387 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5388 * about a memory-reclaim deadlock.
5389 */
5390 str = kmalloc(len, GFP_KERNEL);
5391 if (!str)
5392 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005393
Chuck Levere984a552012-09-14 17:24:21 -04005394 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005395 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005396 clp->cl_ipaddr,
5397 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5398 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005399 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005400
Jeff Laytona3192682015-06-09 19:43:59 -04005401 clp->cl_owner_id = str;
5402 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005403}
5404
Jeff Layton873e3852015-06-09 19:44:00 -04005405static int
5406nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005407{
Jeff Layton873e3852015-06-09 19:44:00 -04005408 size_t len;
5409 char *str;
5410
5411 len = 10 + 10 + 1 + 10 + 1 +
5412 strlen(nfs4_client_id_uniquifier) + 1 +
5413 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5414
5415 if (len > NFS4_OPAQUE_LIMIT + 1)
5416 return -EINVAL;
5417
5418 /*
5419 * Since this string is allocated at mount time, and held until the
5420 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5421 * about a memory-reclaim deadlock.
5422 */
5423 str = kmalloc(len, GFP_KERNEL);
5424 if (!str)
5425 return -ENOMEM;
5426
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005427 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005428 clp->rpc_ops->version, clp->cl_minorversion,
5429 nfs4_client_id_uniquifier,
5430 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005431 clp->cl_owner_id = str;
5432 return 0;
5433}
5434
5435static int
5436nfs4_init_uniform_client_string(struct nfs_client *clp)
5437{
Jeff Layton873e3852015-06-09 19:44:00 -04005438 size_t len;
5439 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005440
5441 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005442 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005443
5444 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005445 return nfs4_init_uniquifier_client_string(clp);
5446
5447 len = 10 + 10 + 1 + 10 + 1 +
5448 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5449
5450 if (len > NFS4_OPAQUE_LIMIT + 1)
5451 return -EINVAL;
5452
5453 /*
5454 * Since this string is allocated at mount time, and held until the
5455 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5456 * about a memory-reclaim deadlock.
5457 */
5458 str = kmalloc(len, GFP_KERNEL);
5459 if (!str)
5460 return -ENOMEM;
5461
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005462 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005463 clp->rpc_ops->version, clp->cl_minorversion,
5464 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005465 clp->cl_owner_id = str;
5466 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005467}
5468
Chuck Lever706cb8d2014-03-12 12:51:47 -04005469/*
5470 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5471 * services. Advertise one based on the address family of the
5472 * clientaddr.
5473 */
5474static unsigned int
5475nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5476{
5477 if (strchr(clp->cl_ipaddr, ':') != NULL)
5478 return scnprintf(buf, len, "tcp6");
5479 else
5480 return scnprintf(buf, len, "tcp");
5481}
5482
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005483static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5484{
5485 struct nfs4_setclientid *sc = calldata;
5486
5487 if (task->tk_status == 0)
5488 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5489}
5490
5491static const struct rpc_call_ops nfs4_setclientid_ops = {
5492 .rpc_call_done = nfs4_setclientid_done,
5493};
5494
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005495/**
5496 * nfs4_proc_setclientid - Negotiate client ID
5497 * @clp: state data structure
5498 * @program: RPC program for NFSv4 callback service
5499 * @port: IP port number for NFS4 callback service
5500 * @cred: RPC credential to use for this call
5501 * @res: where to place the result
5502 *
5503 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5504 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005505int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5506 unsigned short port, struct rpc_cred *cred,
5507 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
5509 nfs4_verifier sc_verifier;
5510 struct nfs4_setclientid setclientid = {
5511 .sc_verifier = &sc_verifier,
5512 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005513 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 };
5515 struct rpc_message msg = {
5516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5517 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005518 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005519 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005521 struct rpc_task *task;
5522 struct rpc_task_setup task_setup_data = {
5523 .rpc_client = clp->cl_rpcclient,
5524 .rpc_message = &msg,
5525 .callback_ops = &nfs4_setclientid_ops,
5526 .callback_data = &setclientid,
5527 .flags = RPC_TASK_TIMEOUT,
5528 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005529 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
Chuck Leverde734832012-07-11 16:30:50 -04005531 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005532 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005533
5534 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5535 status = nfs4_init_uniform_client_string(clp);
5536 else
Jeff Laytona3192682015-06-09 19:43:59 -04005537 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005538
5539 if (status)
5540 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005541
Chuck Leverde734832012-07-11 16:30:50 -04005542 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005543 setclientid.sc_netid_len =
5544 nfs4_init_callback_netid(clp,
5545 setclientid.sc_netid,
5546 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005547 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005548 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 clp->cl_ipaddr, port >> 8, port & 255);
5550
Jeff Layton3a6bb732015-06-09 19:43:57 -04005551 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005553 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005554 task = rpc_run_task(&task_setup_data);
5555 if (IS_ERR(task)) {
5556 status = PTR_ERR(task);
5557 goto out;
5558 }
5559 status = task->tk_status;
5560 if (setclientid.sc_cred) {
5561 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5562 put_rpccred(setclientid.sc_cred);
5563 }
5564 rpc_put_task(task);
5565out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005566 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005567 dprintk("NFS reply setclientid: %d\n", status);
5568 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569}
5570
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005571/**
5572 * nfs4_proc_setclientid_confirm - Confirm client ID
5573 * @clp: state data structure
5574 * @res: result of a previous SETCLIENTID
5575 * @cred: RPC credential to use for this call
5576 *
5577 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5578 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005579int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005580 struct nfs4_setclientid_res *arg,
5581 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 struct rpc_message msg = {
5584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005585 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005586 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 int status;
5589
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005590 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5591 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5592 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005593 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005594 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005595 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 return status;
5597}
5598
Trond Myklebustfe650402006-01-03 09:55:18 +01005599struct nfs4_delegreturndata {
5600 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005601 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005602 struct nfs_fh fh;
5603 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005604 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005605 struct {
5606 struct nfs4_layoutreturn_args arg;
5607 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005608 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005609 u32 roc_barrier;
5610 bool roc;
5611 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005612 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005613 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005614 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005615};
5616
Trond Myklebustfe650402006-01-03 09:55:18 +01005617static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5618{
5619 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005620
Trond Myklebust14516c32010-07-31 14:29:06 -04005621 if (!nfs4_sequence_done(task, &data->res.seq_res))
5622 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005623
Trond Myklebustca8acf82013-08-13 10:36:56 -04005624 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005625
5626 /* Handle Layoutreturn errors */
5627 if (data->args.lr_args && task->tk_status != 0) {
5628 switch(data->res.lr_ret) {
5629 default:
5630 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5631 break;
5632 case 0:
5633 data->args.lr_args = NULL;
5634 data->res.lr_res = NULL;
5635 break;
5636 case -NFS4ERR_ADMIN_REVOKED:
5637 case -NFS4ERR_DELEG_REVOKED:
5638 case -NFS4ERR_EXPIRED:
5639 case -NFS4ERR_BAD_STATEID:
5640 case -NFS4ERR_OLD_STATEID:
5641 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5642 case -NFS4ERR_WRONG_CRED:
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5645 data->res.lr_ret = 0;
5646 rpc_restart_call_prepare(task);
5647 return;
5648 }
5649 }
5650
Ricardo Labiaga79708862009-12-07 09:23:21 -05005651 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005652 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005653 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005654 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005655 case -NFS4ERR_ADMIN_REVOKED:
5656 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005657 case -NFS4ERR_EXPIRED:
5658 nfs4_free_revoked_stateid(data->res.server,
5659 data->args.stateid,
5660 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005661 case -NFS4ERR_BAD_STATEID:
5662 case -NFS4ERR_OLD_STATEID:
5663 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005664 task->tk_status = 0;
5665 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005666 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005667 if (nfs4_async_handle_error(task, data->res.server,
5668 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005669 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005670 return;
5671 }
5672 }
5673 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005674}
5675
5676static void nfs4_delegreturn_release(void *calldata)
5677{
Peng Tao039b7562014-07-03 13:05:02 +08005678 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005679 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005680
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005681 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005682 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005683 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5684 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005685 nfs_iput_and_deactive(inode);
5686 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005687 kfree(calldata);
5688}
5689
Andy Adamson938e1012009-04-01 09:22:28 -04005690static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5691{
5692 struct nfs4_delegreturndata *d_data;
5693
5694 d_data = (struct nfs4_delegreturndata *)data;
5695
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005696 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005697 return;
5698
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005699 nfs4_setup_sequence(d_data->res.server,
5700 &d_data->args.seq_args,
5701 &d_data->res.seq_res,
5702 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005703}
Andy Adamson938e1012009-04-01 09:22:28 -04005704
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005705static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005706 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 .rpc_call_done = nfs4_delegreturn_done,
5708 .rpc_release = nfs4_delegreturn_release,
5709};
5710
Trond Myklebuste6f81072008-01-24 18:14:34 -05005711static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005712{
5713 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005714 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005715 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005716 struct rpc_message msg = {
5717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5718 .rpc_cred = cred,
5719 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005720 struct rpc_task_setup task_setup_data = {
5721 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005722 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005723 .callback_ops = &nfs4_delegreturn_ops,
5724 .flags = RPC_TASK_ASYNC,
5725 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005726 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005727
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005728 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005729 if (data == NULL)
5730 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005731 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005732
5733 nfs4_state_protect(server->nfs_client,
5734 NFS_SP4_MACH_CRED_CLEANUP,
5735 &task_setup_data.rpc_client, &msg);
5736
Trond Myklebustfe650402006-01-03 09:55:18 +01005737 data->args.fhandle = &data->fh;
5738 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005739 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005740 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005741 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005742 data->res.fattr = &data->fattr;
5743 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005744 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005745 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005746 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005747 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005748 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005749 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005750 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005751 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005752 if (data->lr.roc) {
5753 data->args.lr_args = &data->lr.arg;
5754 data->res.lr_res = &data->lr.res;
5755 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005756 } else if (data->lr.roc) {
5757 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5758 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005759 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005760
Trond Myklebustc970aa82007-07-14 15:39:59 -04005761 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005762 msg.rpc_argp = &data->args;
5763 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005764 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005765 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005766 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005767 if (!issync)
5768 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005769 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005770 if (status != 0)
5771 goto out;
5772 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005773 if (status == 0)
5774 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5775 else
5776 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005777out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005778 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005779 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780}
5781
Trond Myklebuste6f81072008-01-24 18:14:34 -05005782int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783{
5784 struct nfs_server *server = NFS_SERVER(inode);
5785 struct nfs4_exception exception = { };
5786 int err;
5787 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005788 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005789 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 switch (err) {
5791 case -NFS4ERR_STALE_STATEID:
5792 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 case 0:
5794 return 0;
5795 }
5796 err = nfs4_handle_exception(server, err, &exception);
5797 } while (exception.retry);
5798 return err;
5799}
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5802{
5803 struct inode *inode = state->inode;
5804 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005805 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005806 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005810 struct nfs_lockt_res res = {
5811 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 };
5813 struct rpc_message msg = {
5814 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5815 .rpc_argp = &arg,
5816 .rpc_resp = &res,
5817 .rpc_cred = state->owner->so_cred,
5818 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 struct nfs4_lock_state *lsp;
5820 int status;
5821
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005822 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005823 status = nfs4_set_lock_state(state, request);
5824 if (status != 0)
5825 goto out;
5826 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005827 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005828 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005829 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005830 switch (status) {
5831 case 0:
5832 request->fl_type = F_UNLCK;
5833 break;
5834 case -NFS4ERR_DENIED:
5835 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005837 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005838 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005839out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 return status;
5841}
5842
5843static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5844{
5845 struct nfs4_exception exception = { };
5846 int err;
5847
5848 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005849 err = _nfs4_proc_getlk(state, cmd, request);
5850 trace_nfs4_get_lock(request, state, cmd, err);
5851 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 &exception);
5853 } while (exception.retry);
5854 return err;
5855}
5856
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005857struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858 struct nfs_locku_args arg;
5859 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005860 struct nfs4_lock_state *lsp;
5861 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005862 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005863 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005864 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005865};
5866
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005867static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5868 struct nfs_open_context *ctx,
5869 struct nfs4_lock_state *lsp,
5870 struct nfs_seqid *seqid)
5871{
5872 struct nfs4_unlockdata *p;
5873 struct inode *inode = lsp->ls_state->inode;
5874
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005875 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005876 if (p == NULL)
5877 return NULL;
5878 p->arg.fh = NFS_FH(inode);
5879 p->arg.fl = &p->fl;
5880 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005881 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005882 p->lsp = lsp;
5883 atomic_inc(&lsp->ls_count);
5884 /* Ensure we don't close file until we're done freeing locks! */
5885 p->ctx = get_nfs_open_context(ctx);
5886 memcpy(&p->fl, fl, sizeof(p->fl));
5887 p->server = NFS_SERVER(inode);
5888 return p;
5889}
5890
Trond Myklebust06f814a2006-01-03 09:55:07 +01005891static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005892{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005893 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005894 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005895 nfs4_put_lock_state(calldata->lsp);
5896 put_nfs_open_context(calldata->ctx);
5897 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005898}
5899
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005900static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005901{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005902 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005903
Trond Myklebust14516c32010-07-31 14:29:06 -04005904 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5905 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005906 switch (task->tk_status) {
5907 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005908 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005909 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005910 if (nfs4_update_lock_stateid(calldata->lsp,
5911 &calldata->res.stateid))
5912 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005913 case -NFS4ERR_ADMIN_REVOKED:
5914 case -NFS4ERR_EXPIRED:
5915 nfs4_free_revoked_stateid(calldata->server,
5916 &calldata->arg.stateid,
5917 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005918 case -NFS4ERR_BAD_STATEID:
5919 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005920 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005921 if (!nfs4_stateid_match(&calldata->arg.stateid,
5922 &calldata->lsp->ls_stateid))
5923 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005924 break;
5925 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005926 if (nfs4_async_handle_error(task, calldata->server,
5927 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005928 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005929 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005930 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005931}
5932
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005933static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005934{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005935 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005937 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005938 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005939 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005940 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005941 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005942 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005943 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005944 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005945 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005946 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005947 &calldata->res.seq_res,
5948 task) != 0)
5949 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005950 return;
5951out_no_action:
5952 task->tk_action = NULL;
5953out_wait:
5954 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955}
5956
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005957static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005958 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005959 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005960 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005961};
5962
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005963static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5964 struct nfs_open_context *ctx,
5965 struct nfs4_lock_state *lsp,
5966 struct nfs_seqid *seqid)
5967{
5968 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5971 .rpc_cred = ctx->cred,
5972 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005973 struct rpc_task_setup task_setup_data = {
5974 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005975 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005976 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005977 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005978 .flags = RPC_TASK_ASYNC,
5979 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005980
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005981 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5982 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5983
Frank Filz137d6ac2007-07-09 15:32:29 -07005984 /* Ensure this is an unlock - when canceling a lock, the
5985 * canceled lock is passed in, and it won't be an unlock.
5986 */
5987 fl->fl_type = F_UNLCK;
5988
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005989 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5990 if (data == NULL) {
5991 nfs_free_seqid(seqid);
5992 return ERR_PTR(-ENOMEM);
5993 }
5994
Chuck Levera9c92d62013-08-09 12:48:18 -04005995 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005996 msg.rpc_argp = &data->arg;
5997 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005998 task_setup_data.callback_data = data;
5999 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006000}
6001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6003{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006004 struct inode *inode = state->inode;
6005 struct nfs4_state_owner *sp = state->owner;
6006 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006007 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006008 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006009 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006010 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006011 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006012 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013
Trond Myklebust9b073572006-06-29 16:38:34 -04006014 status = nfs4_set_lock_state(state, request);
6015 /* Unlock _before_ we do the RPC call */
6016 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006017 /* Exclude nfs_delegation_claim_locks() */
6018 mutex_lock(&sp->so_delegreturn_mutex);
6019 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006020 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006021 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006022 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006023 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006024 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006025 }
6026 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006027 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006028 if (status != 0)
6029 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006030 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006031 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006032 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6033 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006034 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6035 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006036 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006037 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006038 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006039 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006040 status = PTR_ERR(task);
6041 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006042 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006043 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006044 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006045out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006046 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006047 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006048 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049}
6050
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006051struct nfs4_lockdata {
6052 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006053 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006054 struct nfs4_lock_state *lsp;
6055 struct nfs_open_context *ctx;
6056 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006057 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006058 int rpc_status;
6059 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006060 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006061};
6062
6063static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006064 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6065 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006066{
6067 struct nfs4_lockdata *p;
6068 struct inode *inode = lsp->ls_state->inode;
6069 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006070 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006072 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006073 if (p == NULL)
6074 return NULL;
6075
6076 p->arg.fh = NFS_FH(inode);
6077 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006078 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006079 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006080 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006081 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6082 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006083 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006084 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006085 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006086 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006087 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006088 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006090 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091 atomic_inc(&lsp->ls_count);
6092 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006093 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094 memcpy(&p->fl, fl, sizeof(p->fl));
6095 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006096out_free_seqid:
6097 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006098out_free:
6099 kfree(p);
6100 return NULL;
6101}
6102
6103static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6104{
6105 struct nfs4_lockdata *data = calldata;
6106 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107
Harvey Harrison3110ff82008-05-02 13:42:44 -07006108 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006109 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006110 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006111 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006112 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006113 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006114 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006115 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006116 nfs4_stateid_copy(&data->arg.open_stateid,
6117 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006118 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006119 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006120 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006121 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006122 nfs4_stateid_copy(&data->arg.lock_stateid,
6123 &data->lsp->ls_stateid);
6124 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006125 if (!nfs4_valid_open_stateid(state)) {
6126 data->rpc_status = -EBADF;
6127 task->tk_action = NULL;
6128 goto out_release_open_seqid;
6129 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006130 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006131 if (nfs4_setup_sequence(data->server,
6132 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006133 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006134 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006135 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006136out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006137 nfs_release_seqid(data->arg.open_seqid);
6138out_release_lock_seqid:
6139 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006140out_wait:
6141 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006142 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006143}
6144
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6146{
6147 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006148 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006149
Harvey Harrison3110ff82008-05-02 13:42:44 -07006150 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151
Trond Myklebust14516c32010-07-31 14:29:06 -04006152 if (!nfs4_sequence_done(task, &data->res.seq_res))
6153 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006154
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006156 switch (task->tk_status) {
6157 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006158 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006159 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006160 if (data->arg.new_lock) {
6161 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006162 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006163 rpc_restart_call_prepare(task);
6164 break;
6165 }
6166 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006167 if (data->arg.new_lock_owner != 0) {
6168 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6169 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6170 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6171 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6172 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006173 break;
6174 case -NFS4ERR_BAD_STATEID:
6175 case -NFS4ERR_OLD_STATEID:
6176 case -NFS4ERR_STALE_STATEID:
6177 case -NFS4ERR_EXPIRED:
6178 if (data->arg.new_lock_owner != 0) {
6179 if (!nfs4_stateid_match(&data->arg.open_stateid,
6180 &lsp->ls_state->open_stateid))
6181 rpc_restart_call_prepare(task);
6182 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6183 &lsp->ls_stateid))
6184 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006186 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006187}
6188
6189static void nfs4_lock_release(void *calldata)
6190{
6191 struct nfs4_lockdata *data = calldata;
6192
Harvey Harrison3110ff82008-05-02 13:42:44 -07006193 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006194 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006195 if (data->cancelled != 0) {
6196 struct rpc_task *task;
6197 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6198 data->arg.lock_seqid);
6199 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006200 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006201 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202 } else
6203 nfs_free_seqid(data->arg.lock_seqid);
6204 nfs4_put_lock_state(data->lsp);
6205 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006206 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006207 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006208 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209}
6210
6211static const struct rpc_call_ops nfs4_lock_ops = {
6212 .rpc_call_prepare = nfs4_lock_prepare,
6213 .rpc_call_done = nfs4_lock_done,
6214 .rpc_release = nfs4_lock_release,
6215};
6216
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006217static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6218{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006219 switch (error) {
6220 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006221 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006222 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006223 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006224 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006225 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006226 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006227 break;
6228 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006229 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006230 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006231 };
6232}
6233
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006234static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235{
6236 struct nfs4_lockdata *data;
6237 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006238 struct rpc_message msg = {
6239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6240 .rpc_cred = state->owner->so_cred,
6241 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006242 struct rpc_task_setup task_setup_data = {
6243 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006244 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006245 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006246 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006247 .flags = RPC_TASK_ASYNC,
6248 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006249 int ret;
6250
Harvey Harrison3110ff82008-05-02 13:42:44 -07006251 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006252 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006253 fl->fl_u.nfs4_fl.owner,
6254 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006255 if (data == NULL)
6256 return -ENOMEM;
6257 if (IS_SETLKW(cmd))
6258 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006259 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006260 msg.rpc_argp = &data->arg;
6261 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006262 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006263 if (recovery_type > NFS_LOCK_NEW) {
6264 if (recovery_type == NFS_LOCK_RECLAIM)
6265 data->arg.reclaim = NFS_LOCK_RECLAIM;
6266 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006267 } else
6268 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006269 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006270 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006272 ret = nfs4_wait_for_completion_rpc_task(task);
6273 if (ret == 0) {
6274 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006275 if (ret)
6276 nfs4_handle_setlk_error(data->server, data->lsp,
6277 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006278 } else
6279 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006280 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006281 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006282 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284}
6285
6286static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6287{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006288 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006289 struct nfs4_exception exception = {
6290 .inode = state->inode,
6291 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006292 int err;
6293
6294 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006295 /* Cache the lock if possible... */
6296 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6297 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006298 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006299 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006300 break;
6301 nfs4_handle_exception(server, err, &exception);
6302 } while (exception.retry);
6303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304}
6305
6306static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6307{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006308 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006309 struct nfs4_exception exception = {
6310 .inode = state->inode,
6311 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006312 int err;
6313
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006314 err = nfs4_set_lock_state(state, request);
6315 if (err != 0)
6316 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006317 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006318 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6319 return 0;
6320 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006321 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6323 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006324 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006325 switch (err) {
6326 default:
6327 goto out;
6328 case -NFS4ERR_GRACE:
6329 case -NFS4ERR_DELAY:
6330 nfs4_handle_exception(server, err, &exception);
6331 err = 0;
6332 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006333 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006334out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006335 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336}
6337
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006338#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006339static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6340{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006341 struct nfs4_lock_state *lsp;
6342 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006343
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006344 status = nfs4_set_lock_state(state, request);
6345 if (status != 0)
6346 return status;
6347 lsp = request->fl_u.nfs4_fl.owner;
6348 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6349 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6350 return 0;
6351 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006352 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006353}
6354#endif
6355
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6357{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006358 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006359 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006360 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006361 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
Trond Myklebust01c3b862006-06-29 16:38:39 -04006363 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006364 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006365 if (status < 0)
6366 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006367 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006368 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006369 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006370 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006371 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006372 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006373 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006374 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006375 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006376 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006377 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006378 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006379 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006380 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006381out:
6382 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 return status;
6384}
6385
6386static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6387{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006388 struct nfs4_exception exception = {
6389 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006390 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006391 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 int err;
6393
6394 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006395 err = _nfs4_proc_setlk(state, cmd, request);
6396 if (err == -NFS4ERR_DENIED)
6397 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006399 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 } while (exception.retry);
6401 return err;
6402}
6403
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006404#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6405#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6406
6407static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006408nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6409 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006410{
6411 int status = -ERESTARTSYS;
6412 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6413
6414 while(!signalled()) {
6415 status = nfs4_proc_setlk(state, cmd, request);
6416 if ((status != -EAGAIN) || IS_SETLK(cmd))
6417 break;
6418 freezable_schedule_timeout_interruptible(timeout);
6419 timeout *= 2;
6420 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6421 status = -ERESTARTSYS;
6422 }
6423 return status;
6424}
6425
Jeff Laytona1d617d82016-09-17 18:17:39 -04006426#ifdef CONFIG_NFS_V4_1
6427struct nfs4_lock_waiter {
6428 struct task_struct *task;
6429 struct inode *inode;
6430 struct nfs_lowner *owner;
6431 bool notified;
6432};
6433
6434static int
6435nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6436{
6437 int ret;
6438 struct cb_notify_lock_args *cbnl = key;
6439 struct nfs4_lock_waiter *waiter = wait->private;
6440 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6441 *wowner = waiter->owner;
6442
6443 /* Only wake if the callback was for the same owner */
6444 if (lowner->clientid != wowner->clientid ||
6445 lowner->id != wowner->id ||
6446 lowner->s_dev != wowner->s_dev)
6447 return 0;
6448
6449 /* Make sure it's for the right inode */
6450 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6451 return 0;
6452
6453 waiter->notified = true;
6454
6455 /* override "private" so we can use default_wake_function */
6456 wait->private = waiter->task;
6457 ret = autoremove_wake_function(wait, mode, flags, key);
6458 wait->private = waiter;
6459 return ret;
6460}
6461
6462static int
6463nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6464{
6465 int status = -ERESTARTSYS;
6466 unsigned long flags;
6467 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6468 struct nfs_server *server = NFS_SERVER(state->inode);
6469 struct nfs_client *clp = server->nfs_client;
6470 wait_queue_head_t *q = &clp->cl_lock_waitq;
6471 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6472 .id = lsp->ls_seqid.owner_id,
6473 .s_dev = server->s_dev };
6474 struct nfs4_lock_waiter waiter = { .task = current,
6475 .inode = state->inode,
6476 .owner = &owner,
6477 .notified = false };
6478 wait_queue_t wait;
6479
6480 /* Don't bother with waitqueue if we don't expect a callback */
6481 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6482 return nfs4_retry_setlk_simple(state, cmd, request);
6483
6484 init_wait(&wait);
6485 wait.private = &waiter;
6486 wait.func = nfs4_wake_lock_waiter;
6487 add_wait_queue(q, &wait);
6488
6489 while(!signalled()) {
6490 status = nfs4_proc_setlk(state, cmd, request);
6491 if ((status != -EAGAIN) || IS_SETLK(cmd))
6492 break;
6493
6494 status = -ERESTARTSYS;
6495 spin_lock_irqsave(&q->lock, flags);
6496 if (waiter.notified) {
6497 spin_unlock_irqrestore(&q->lock, flags);
6498 continue;
6499 }
6500 set_current_state(TASK_INTERRUPTIBLE);
6501 spin_unlock_irqrestore(&q->lock, flags);
6502
6503 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6504 }
6505
6506 finish_wait(q, &wait);
6507 return status;
6508}
6509#else /* !CONFIG_NFS_V4_1 */
6510static inline int
6511nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6512{
6513 return nfs4_retry_setlk_simple(state, cmd, request);
6514}
6515#endif
6516
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517static int
6518nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6519{
6520 struct nfs_open_context *ctx;
6521 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 int status;
6523
6524 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006525 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 state = ctx->state;
6527
6528 if (request->fl_start < 0 || request->fl_end < 0)
6529 return -EINVAL;
6530
Trond Myklebustd9531262009-07-21 19:22:38 -04006531 if (IS_GETLK(cmd)) {
6532 if (state != NULL)
6533 return nfs4_proc_getlk(state, F_GETLK, request);
6534 return 0;
6535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536
6537 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6538 return -EINVAL;
6539
Trond Myklebustd9531262009-07-21 19:22:38 -04006540 if (request->fl_type == F_UNLCK) {
6541 if (state != NULL)
6542 return nfs4_proc_unlck(state, cmd, request);
6543 return 0;
6544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
Trond Myklebustd9531262009-07-21 19:22:38 -04006546 if (state == NULL)
6547 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006548
6549 if ((request->fl_flags & FL_POSIX) &&
6550 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6551 return -ENOLCK;
6552
Trond Myklebust55725512012-04-18 12:48:35 -04006553 /*
6554 * Don't rely on the VFS having checked the file open mode,
6555 * since it won't do this for flock() locks.
6556 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006557 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006558 case F_RDLCK:
6559 if (!(filp->f_mode & FMODE_READ))
6560 return -EBADF;
6561 break;
6562 case F_WRLCK:
6563 if (!(filp->f_mode & FMODE_WRITE))
6564 return -EBADF;
6565 }
6566
Jeff Layton1ea67db2016-09-17 18:17:37 -04006567 status = nfs4_set_lock_state(state, request);
6568 if (status != 0)
6569 return status;
6570
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006571 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572}
6573
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006574int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006575{
6576 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006577 int err;
6578
6579 err = nfs4_set_lock_state(state, fl);
6580 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006581 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006582 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006583 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006584}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006585
Trond Myklebustcf470c32012-03-07 13:49:12 -05006586struct nfs_release_lockowner_data {
6587 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006588 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006589 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006590 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006591 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006592};
6593
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006594static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6595{
6596 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006597 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006598 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6599 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006600 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006601 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006602}
6603
6604static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6605{
6606 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006607 struct nfs_server *server = data->server;
6608
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006609 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006610
6611 switch (task->tk_status) {
6612 case 0:
6613 renew_lease(server, data->timestamp);
6614 break;
6615 case -NFS4ERR_STALE_CLIENTID:
6616 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006617 nfs4_schedule_lease_recovery(server->nfs_client);
6618 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006619 case -NFS4ERR_LEASE_MOVED:
6620 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006621 if (nfs4_async_handle_error(task, server,
6622 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006623 rpc_restart_call_prepare(task);
6624 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006625}
6626
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006627static void nfs4_release_lockowner_release(void *calldata)
6628{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006629 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006630 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006631 kfree(calldata);
6632}
6633
Trond Myklebust17280172012-03-11 13:11:00 -04006634static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006635 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6636 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006637 .rpc_release = nfs4_release_lockowner_release,
6638};
6639
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006640static void
6641nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006642{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006643 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006644 struct rpc_message msg = {
6645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6646 };
6647
6648 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006649 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006650
Trond Myklebustcf470c32012-03-07 13:49:12 -05006651 data = kmalloc(sizeof(*data), GFP_NOFS);
6652 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006653 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006654 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006655 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006656 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6657 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6658 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006659
Trond Myklebustcf470c32012-03-07 13:49:12 -05006660 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006661 msg.rpc_resp = &data->res;
6662 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006663 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006664}
6665
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006666#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6667
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006668static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006669 struct dentry *unused, struct inode *inode,
6670 const char *key, const void *buf,
6671 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006672{
Al Viro59301222016-05-27 10:19:30 -04006673 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006674}
6675
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006676static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006677 struct dentry *unused, struct inode *inode,
6678 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006679{
Al Virob2968212016-04-10 20:48:24 -04006680 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006681}
6682
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006683static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006684{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006685 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006686}
6687
David Quigleyc9bccef2013-05-22 12:50:45 -04006688#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006689
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006690static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006691 struct dentry *unused, struct inode *inode,
6692 const char *key, const void *buf,
6693 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006694{
6695 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006696 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006697
6698 return -EOPNOTSUPP;
6699}
6700
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006701static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006702 struct dentry *unused, struct inode *inode,
6703 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006704{
6705 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006706 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006707 return -EOPNOTSUPP;
6708}
6709
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006710static ssize_t
6711nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006712{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006713 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006714
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006715 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6716 len = security_inode_listsecurity(inode, list, list_len);
6717 if (list_len && len > list_len)
6718 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006719 }
6720 return len;
6721}
6722
6723static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6724 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006725 .get = nfs4_xattr_get_nfs4_label,
6726 .set = nfs4_xattr_set_nfs4_label,
6727};
David Quigleyc9bccef2013-05-22 12:50:45 -04006728
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006729#else
6730
6731static ssize_t
6732nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6733{
6734 return 0;
6735}
6736
6737#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006738
Andy Adamson533eb462011-06-13 18:25:56 -04006739/*
6740 * nfs_fhget will use either the mounted_on_fileid or the fileid
6741 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006742static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6743{
Andy Adamson533eb462011-06-13 18:25:56 -04006744 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6745 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6746 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006747 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006748 return;
6749
6750 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006751 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006752 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6753 fattr->nlink = 2;
6754}
6755
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006756static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6757 const struct qstr *name,
6758 struct nfs4_fs_locations *fs_locations,
6759 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006760{
6761 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006762 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006763 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006764 };
6765 struct nfs4_fs_locations_arg args = {
6766 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006767 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006768 .page = page,
6769 .bitmask = bitmask,
6770 };
Benny Halevy22958462009-04-01 09:22:02 -04006771 struct nfs4_fs_locations_res res = {
6772 .fs_locations = fs_locations,
6773 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006774 struct rpc_message msg = {
6775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6776 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006777 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006778 };
6779 int status;
6780
Harvey Harrison3110ff82008-05-02 13:42:44 -07006781 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006782
6783 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6784 * is not supported */
6785 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6786 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6787 else
6788 bitmask[0] |= FATTR4_WORD0_FILEID;
6789
Trond Myklebustc228fd32007-01-13 02:28:11 -05006790 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006791 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006792 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006793 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006794 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006795 return status;
6796}
6797
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006798int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6799 const struct qstr *name,
6800 struct nfs4_fs_locations *fs_locations,
6801 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006802{
6803 struct nfs4_exception exception = { };
6804 int err;
6805 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006806 err = _nfs4_proc_fs_locations(client, dir, name,
6807 fs_locations, page);
6808 trace_nfs4_get_fs_locations(dir, name, err);
6809 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006810 &exception);
6811 } while (exception.retry);
6812 return err;
6813}
6814
Chuck Leverb03d7352013-10-17 14:12:50 -04006815/*
6816 * This operation also signals the server that this client is
6817 * performing migration recovery. The server can stop returning
6818 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6819 * appended to this compound to identify the client ID which is
6820 * performing recovery.
6821 */
6822static int _nfs40_proc_get_locations(struct inode *inode,
6823 struct nfs4_fs_locations *locations,
6824 struct page *page, struct rpc_cred *cred)
6825{
6826 struct nfs_server *server = NFS_SERVER(inode);
6827 struct rpc_clnt *clnt = server->client;
6828 u32 bitmask[2] = {
6829 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6830 };
6831 struct nfs4_fs_locations_arg args = {
6832 .clientid = server->nfs_client->cl_clientid,
6833 .fh = NFS_FH(inode),
6834 .page = page,
6835 .bitmask = bitmask,
6836 .migration = 1, /* skip LOOKUP */
6837 .renew = 1, /* append RENEW */
6838 };
6839 struct nfs4_fs_locations_res res = {
6840 .fs_locations = locations,
6841 .migration = 1,
6842 .renew = 1,
6843 };
6844 struct rpc_message msg = {
6845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6846 .rpc_argp = &args,
6847 .rpc_resp = &res,
6848 .rpc_cred = cred,
6849 };
6850 unsigned long now = jiffies;
6851 int status;
6852
6853 nfs_fattr_init(&locations->fattr);
6854 locations->server = server;
6855 locations->nlocations = 0;
6856
6857 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6858 nfs4_set_sequence_privileged(&args.seq_args);
6859 status = nfs4_call_sync_sequence(clnt, server, &msg,
6860 &args.seq_args, &res.seq_res);
6861 if (status)
6862 return status;
6863
6864 renew_lease(server, now);
6865 return 0;
6866}
6867
6868#ifdef CONFIG_NFS_V4_1
6869
6870/*
6871 * This operation also signals the server that this client is
6872 * performing migration recovery. The server can stop asserting
6873 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6874 * performing this operation is identified in the SEQUENCE
6875 * operation in this compound.
6876 *
6877 * When the client supports GETATTR(fs_locations_info), it can
6878 * be plumbed in here.
6879 */
6880static int _nfs41_proc_get_locations(struct inode *inode,
6881 struct nfs4_fs_locations *locations,
6882 struct page *page, struct rpc_cred *cred)
6883{
6884 struct nfs_server *server = NFS_SERVER(inode);
6885 struct rpc_clnt *clnt = server->client;
6886 u32 bitmask[2] = {
6887 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6888 };
6889 struct nfs4_fs_locations_arg args = {
6890 .fh = NFS_FH(inode),
6891 .page = page,
6892 .bitmask = bitmask,
6893 .migration = 1, /* skip LOOKUP */
6894 };
6895 struct nfs4_fs_locations_res res = {
6896 .fs_locations = locations,
6897 .migration = 1,
6898 };
6899 struct rpc_message msg = {
6900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6901 .rpc_argp = &args,
6902 .rpc_resp = &res,
6903 .rpc_cred = cred,
6904 };
6905 int status;
6906
6907 nfs_fattr_init(&locations->fattr);
6908 locations->server = server;
6909 locations->nlocations = 0;
6910
6911 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6912 nfs4_set_sequence_privileged(&args.seq_args);
6913 status = nfs4_call_sync_sequence(clnt, server, &msg,
6914 &args.seq_args, &res.seq_res);
6915 if (status == NFS4_OK &&
6916 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6917 status = -NFS4ERR_LEASE_MOVED;
6918 return status;
6919}
6920
6921#endif /* CONFIG_NFS_V4_1 */
6922
6923/**
6924 * nfs4_proc_get_locations - discover locations for a migrated FSID
6925 * @inode: inode on FSID that is migrating
6926 * @locations: result of query
6927 * @page: buffer
6928 * @cred: credential to use for this operation
6929 *
6930 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6931 * operation failed, or a negative errno if a local error occurred.
6932 *
6933 * On success, "locations" is filled in, but if the server has
6934 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6935 * asserted.
6936 *
6937 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6938 * from this client that require migration recovery.
6939 */
6940int nfs4_proc_get_locations(struct inode *inode,
6941 struct nfs4_fs_locations *locations,
6942 struct page *page, struct rpc_cred *cred)
6943{
6944 struct nfs_server *server = NFS_SERVER(inode);
6945 struct nfs_client *clp = server->nfs_client;
6946 const struct nfs4_mig_recovery_ops *ops =
6947 clp->cl_mvops->mig_recovery_ops;
6948 struct nfs4_exception exception = { };
6949 int status;
6950
6951 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6952 (unsigned long long)server->fsid.major,
6953 (unsigned long long)server->fsid.minor,
6954 clp->cl_hostname);
6955 nfs_display_fhandle(NFS_FH(inode), __func__);
6956
6957 do {
6958 status = ops->get_locations(inode, locations, page, cred);
6959 if (status != -NFS4ERR_DELAY)
6960 break;
6961 nfs4_handle_exception(server, status, &exception);
6962 } while (exception.retry);
6963 return status;
6964}
6965
Chuck Lever44c99932013-10-17 14:13:30 -04006966/*
6967 * This operation also signals the server that this client is
6968 * performing "lease moved" recovery. The server can stop
6969 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6970 * is appended to this compound to identify the client ID which is
6971 * performing recovery.
6972 */
6973static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6974{
6975 struct nfs_server *server = NFS_SERVER(inode);
6976 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6977 struct rpc_clnt *clnt = server->client;
6978 struct nfs4_fsid_present_arg args = {
6979 .fh = NFS_FH(inode),
6980 .clientid = clp->cl_clientid,
6981 .renew = 1, /* append RENEW */
6982 };
6983 struct nfs4_fsid_present_res res = {
6984 .renew = 1,
6985 };
6986 struct rpc_message msg = {
6987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6988 .rpc_argp = &args,
6989 .rpc_resp = &res,
6990 .rpc_cred = cred,
6991 };
6992 unsigned long now = jiffies;
6993 int status;
6994
6995 res.fh = nfs_alloc_fhandle();
6996 if (res.fh == NULL)
6997 return -ENOMEM;
6998
6999 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7000 nfs4_set_sequence_privileged(&args.seq_args);
7001 status = nfs4_call_sync_sequence(clnt, server, &msg,
7002 &args.seq_args, &res.seq_res);
7003 nfs_free_fhandle(res.fh);
7004 if (status)
7005 return status;
7006
7007 do_renew_lease(clp, now);
7008 return 0;
7009}
7010
7011#ifdef CONFIG_NFS_V4_1
7012
7013/*
7014 * This operation also signals the server that this client is
7015 * performing "lease moved" recovery. The server can stop asserting
7016 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7017 * this operation is identified in the SEQUENCE operation in this
7018 * compound.
7019 */
7020static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7021{
7022 struct nfs_server *server = NFS_SERVER(inode);
7023 struct rpc_clnt *clnt = server->client;
7024 struct nfs4_fsid_present_arg args = {
7025 .fh = NFS_FH(inode),
7026 };
7027 struct nfs4_fsid_present_res res = {
7028 };
7029 struct rpc_message msg = {
7030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7031 .rpc_argp = &args,
7032 .rpc_resp = &res,
7033 .rpc_cred = cred,
7034 };
7035 int status;
7036
7037 res.fh = nfs_alloc_fhandle();
7038 if (res.fh == NULL)
7039 return -ENOMEM;
7040
7041 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7042 nfs4_set_sequence_privileged(&args.seq_args);
7043 status = nfs4_call_sync_sequence(clnt, server, &msg,
7044 &args.seq_args, &res.seq_res);
7045 nfs_free_fhandle(res.fh);
7046 if (status == NFS4_OK &&
7047 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7048 status = -NFS4ERR_LEASE_MOVED;
7049 return status;
7050}
7051
7052#endif /* CONFIG_NFS_V4_1 */
7053
7054/**
7055 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7056 * @inode: inode on FSID to check
7057 * @cred: credential to use for this operation
7058 *
7059 * Server indicates whether the FSID is present, moved, or not
7060 * recognized. This operation is necessary to clear a LEASE_MOVED
7061 * condition for this client ID.
7062 *
7063 * Returns NFS4_OK if the FSID is present on this server,
7064 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7065 * NFS4ERR code if some error occurred on the server, or a
7066 * negative errno if a local failure occurred.
7067 */
7068int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7069{
7070 struct nfs_server *server = NFS_SERVER(inode);
7071 struct nfs_client *clp = server->nfs_client;
7072 const struct nfs4_mig_recovery_ops *ops =
7073 clp->cl_mvops->mig_recovery_ops;
7074 struct nfs4_exception exception = { };
7075 int status;
7076
7077 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7078 (unsigned long long)server->fsid.major,
7079 (unsigned long long)server->fsid.minor,
7080 clp->cl_hostname);
7081 nfs_display_fhandle(NFS_FH(inode), __func__);
7082
7083 do {
7084 status = ops->fsid_present(inode, cred);
7085 if (status != -NFS4ERR_DELAY)
7086 break;
7087 nfs4_handle_exception(server, status, &exception);
7088 } while (exception.retry);
7089 return status;
7090}
7091
Andy Adamson5ec16a82013-08-08 10:57:55 -04007092/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007093 * If 'use_integrity' is true and the state managment nfs_client
7094 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7095 * and the machine credential as per RFC3530bis and RFC5661 Security
7096 * Considerations sections. Otherwise, just use the user cred with the
7097 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007098 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007099static 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 +00007100{
7101 int status;
7102 struct nfs4_secinfo_arg args = {
7103 .dir_fh = NFS_FH(dir),
7104 .name = name,
7105 };
7106 struct nfs4_secinfo_res res = {
7107 .flavors = flavors,
7108 };
7109 struct rpc_message msg = {
7110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7111 .rpc_argp = &args,
7112 .rpc_resp = &res,
7113 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007114 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007115 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007116
7117 if (use_integrity) {
7118 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007119 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7120 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007121 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007122
7123 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007124
7125 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7126 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7127
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007128 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7129 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007130 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007131
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007132 if (cred)
7133 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007134
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007135 return status;
7136}
7137
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007138int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7139 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007140{
7141 struct nfs4_exception exception = { };
7142 int err;
7143 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007144 err = -NFS4ERR_WRONGSEC;
7145
7146 /* try to use integrity protection with machine cred */
7147 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7148 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7149
7150 /*
7151 * if unable to use integrity protection, or SECINFO with
7152 * integrity protection returns NFS4ERR_WRONGSEC (which is
7153 * disallowed by spec, but exists in deployed servers) use
7154 * the current filesystem's rpc_client and the user cred.
7155 */
7156 if (err == -NFS4ERR_WRONGSEC)
7157 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7158
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007159 trace_nfs4_secinfo(dir, name, err);
7160 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007161 &exception);
7162 } while (exception.retry);
7163 return err;
7164}
7165
Andy Adamson557134a2009-04-01 09:21:53 -04007166#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007167/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007168 * Check the exchange flags returned by the server for invalid flags, having
7169 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7170 * DS flags set.
7171 */
7172static int nfs4_check_cl_exchange_flags(u32 flags)
7173{
7174 if (flags & ~EXCHGID4_FLAG_MASK_R)
7175 goto out_inval;
7176 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7177 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7178 goto out_inval;
7179 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7180 goto out_inval;
7181 return NFS_OK;
7182out_inval:
7183 return -NFS4ERR_INVAL;
7184}
7185
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007186static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007187nfs41_same_server_scope(struct nfs41_server_scope *a,
7188 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007189{
7190 if (a->server_scope_sz == b->server_scope_sz &&
7191 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7192 return true;
7193
7194 return false;
7195}
7196
Andy Adamson02a95de2016-02-05 16:08:37 -05007197static void
7198nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7199{
7200}
7201
7202static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7203 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7204};
7205
Andy Adamson357f54d2010-12-14 10:11:57 -05007206/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007207 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007208 *
7209 * The 4.1 client currently uses the same TCP connection for the
7210 * fore and backchannel.
7211 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007212static
7213int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7214 struct rpc_xprt *xprt,
7215 struct nfs_client *clp,
7216 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007217{
7218 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007219 struct nfs41_bind_conn_to_session_args args = {
7220 .client = clp,
7221 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7222 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007223 struct nfs41_bind_conn_to_session_res res;
7224 struct rpc_message msg = {
7225 .rpc_proc =
7226 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007227 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007228 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007229 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007231 struct rpc_task_setup task_setup_data = {
7232 .rpc_client = clnt,
7233 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007234 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007235 .rpc_message = &msg,
7236 .flags = RPC_TASK_TIMEOUT,
7237 };
7238 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007239
7240 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007241
Trond Myklebust71a097c2015-02-18 09:27:18 -08007242 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7243 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7244 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007245
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007246 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7247 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7248 args.dir = NFS4_CDFC4_FORE;
7249
7250 task = rpc_run_task(&task_setup_data);
7251 if (!IS_ERR(task)) {
7252 status = task->tk_status;
7253 rpc_put_task(task);
7254 } else
7255 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007256 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007257 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007258 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7260 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7261 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007262 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007263 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007264 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007265 dprintk("NFS: %s: Unexpected direction from server\n",
7266 __func__);
7267 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007268 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007269 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007270 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007271 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7272 __func__);
7273 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007274 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007275 }
7276 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007277out:
7278 dprintk("<-- %s status= %d\n", __func__, status);
7279 return status;
7280}
7281
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007282struct rpc_bind_conn_calldata {
7283 struct nfs_client *clp;
7284 struct rpc_cred *cred;
7285};
7286
7287static int
7288nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7289 struct rpc_xprt *xprt,
7290 void *calldata)
7291{
7292 struct rpc_bind_conn_calldata *p = calldata;
7293
7294 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7295}
7296
7297int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7298{
7299 struct rpc_bind_conn_calldata data = {
7300 .clp = clp,
7301 .cred = cred,
7302 };
7303 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7304 nfs4_proc_bind_conn_to_session_callback, &data);
7305}
7306
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007307/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007308 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7309 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007310 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007311static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7312 .how = SP4_MACH_CRED,
7313 .enforce.u.words = {
7314 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7315 1 << (OP_EXCHANGE_ID - 32) |
7316 1 << (OP_CREATE_SESSION - 32) |
7317 1 << (OP_DESTROY_SESSION - 32) |
7318 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007319 },
7320 .allow.u.words = {
7321 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007322 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007323 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007324 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007325 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007326 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007327 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007328 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007329 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007330 1 << (OP_FREE_STATEID - 32) |
7331 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007332 }
7333};
7334
7335/*
7336 * Select the state protection mode for client `clp' given the server results
7337 * from exchange_id in `sp'.
7338 *
7339 * Returns 0 on success, negative errno otherwise.
7340 */
7341static int nfs4_sp4_select_mode(struct nfs_client *clp,
7342 struct nfs41_state_protection *sp)
7343{
7344 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7345 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7346 1 << (OP_EXCHANGE_ID - 32) |
7347 1 << (OP_CREATE_SESSION - 32) |
7348 1 << (OP_DESTROY_SESSION - 32) |
7349 1 << (OP_DESTROY_CLIENTID - 32)
7350 };
7351 unsigned int i;
7352
7353 if (sp->how == SP4_MACH_CRED) {
7354 /* Print state protect result */
7355 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7356 for (i = 0; i <= LAST_NFS4_OP; i++) {
7357 if (test_bit(i, sp->enforce.u.longs))
7358 dfprintk(MOUNT, " enforce op %d\n", i);
7359 if (test_bit(i, sp->allow.u.longs))
7360 dfprintk(MOUNT, " allow op %d\n", i);
7361 }
7362
7363 /* make sure nothing is on enforce list that isn't supported */
7364 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7365 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7366 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7367 return -EINVAL;
7368 }
7369 }
7370
7371 /*
7372 * Minimal mode - state operations are allowed to use machine
7373 * credential. Note this already happens by default, so the
7374 * client doesn't have to do anything more than the negotiation.
7375 *
7376 * NOTE: we don't care if EXCHANGE_ID is in the list -
7377 * we're already using the machine cred for exchange_id
7378 * and will never use a different cred.
7379 */
7380 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7381 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7382 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7383 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7384 dfprintk(MOUNT, "sp4_mach_cred:\n");
7385 dfprintk(MOUNT, " minimal mode enabled\n");
7386 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7387 } else {
7388 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7389 return -EINVAL;
7390 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007391
7392 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007393 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7394 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007395 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7396 dfprintk(MOUNT, " cleanup mode enabled\n");
7397 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7398 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007399
Andrew Elble99ade3c2015-12-02 09:39:51 -05007400 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7401 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7402 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7403 &clp->cl_sp4_flags);
7404 }
7405
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007406 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7407 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7408 dfprintk(MOUNT, " secinfo mode enabled\n");
7409 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7410 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007411
7412 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7413 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7414 dfprintk(MOUNT, " stateid mode enabled\n");
7415 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7416 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007417
7418 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7419 dfprintk(MOUNT, " write mode enabled\n");
7420 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7421 }
7422
7423 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7424 dfprintk(MOUNT, " commit mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7426 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007427 }
7428
7429 return 0;
7430}
7431
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432struct nfs41_exchange_id_data {
7433 struct nfs41_exchange_id_res res;
7434 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007435 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007436 int rpc_status;
7437};
7438
7439static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007440{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007441 struct nfs41_exchange_id_data *cdata =
7442 (struct nfs41_exchange_id_data *)data;
7443 struct nfs_client *clp = cdata->args.client;
7444 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007445
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007446 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007447
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007448 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007449 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007450
7451 if (cdata->xprt && status == 0) {
7452 status = nfs4_detect_session_trunking(clp, &cdata->res,
7453 cdata->xprt);
7454 goto out;
7455 }
7456
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 if (status == 0)
7458 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007459
Chuck Lever177313f2012-05-21 22:44:58 -04007460 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007461 clp->cl_clientid = cdata->res.clientid;
7462 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007463 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007464 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007465 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 &clp->cl_session->session_state);
7467 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007468 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007469
Chuck Leveracdeb692012-05-21 22:46:16 -04007470 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 clp->cl_serverowner = cdata->res.server_owner;
7472 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007473
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007474 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007475 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476 clp->cl_implid = cdata->res.impl_id;
7477 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007478
Chuck Lever177313f2012-05-21 22:44:58 -04007479 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007480 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007482 dprintk("%s: server_scope mismatch detected\n",
7483 __func__);
7484 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007485 kfree(clp->cl_serverscope);
7486 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007487 }
7488
Chuck Lever177313f2012-05-21 22:44:58 -04007489 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007490 clp->cl_serverscope = cdata->res.server_scope;
7491 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007492 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007493 /* Save the EXCHANGE_ID verifier session trunk tests */
7494 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7495 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007497out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007499 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500}
7501
7502static void nfs4_exchange_id_release(void *data)
7503{
7504 struct nfs41_exchange_id_data *cdata =
7505 (struct nfs41_exchange_id_data *)data;
7506
7507 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007508 if (cdata->xprt) {
7509 xprt_put(cdata->xprt);
7510 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7511 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007512 kfree(cdata->res.impl_id);
7513 kfree(cdata->res.server_scope);
7514 kfree(cdata->res.server_owner);
7515 kfree(cdata);
7516}
7517
7518static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7519 .rpc_call_done = nfs4_exchange_id_done,
7520 .rpc_release = nfs4_exchange_id_release,
7521};
7522
Benny Halevy99fe60d2009-04-01 09:22:29 -04007523/*
7524 * _nfs4_proc_exchange_id()
7525 *
7526 * Wrapper for EXCHANGE_ID operation.
7527 */
7528static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007529 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007530{
7531 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007532 struct rpc_message msg = {
7533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007534 .rpc_cred = cred,
7535 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007536 struct rpc_task_setup task_setup_data = {
7537 .rpc_client = clp->cl_rpcclient,
7538 .callback_ops = &nfs4_exchange_id_call_ops,
7539 .rpc_message = &msg,
7540 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7541 };
7542 struct nfs41_exchange_id_data *calldata;
7543 struct rpc_task *task;
7544 int status = -EIO;
7545
7546 if (!atomic_inc_not_zero(&clp->cl_count))
7547 goto out;
7548
7549 status = -ENOMEM;
7550 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7551 if (!calldata)
7552 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007553
Andy Adamsonad0849a2016-09-09 09:22:28 -04007554 if (!xprt)
7555 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007556
7557 status = nfs4_init_uniform_client_string(clp);
7558 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007559 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007560
7561 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7563 clp->cl_owner_id);
7564
Andy Adamson8d89bd72016-09-09 09:22:18 -04007565 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7566 GFP_NOFS);
7567 status = -ENOMEM;
7568 if (unlikely(calldata->res.server_owner == NULL))
7569 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570
Andy Adamson8d89bd72016-09-09 09:22:18 -04007571 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007572 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007573 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007574 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007575
Andy Adamson8d89bd72016-09-09 09:22:18 -04007576 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7577 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007578 goto out_server_scope;
7579
7580 switch (sp4_how) {
7581 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007583 break;
7584
7585 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007586 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007587 break;
7588
7589 default:
7590 /* unsupported! */
7591 WARN_ON_ONCE(1);
7592 status = -EINVAL;
7593 goto out_impl_id;
7594 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007595 if (xprt) {
7596 calldata->xprt = xprt;
7597 task_setup_data.rpc_xprt = xprt;
7598 task_setup_data.flags =
7599 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7600 calldata->args.verifier = &clp->cl_confirm;
7601 } else {
7602 calldata->args.verifier = &verifier;
7603 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007604 calldata->args.client = clp;
7605#ifdef CONFIG_NFS_V4_1_MIGRATION
7606 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7607 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7608 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7609#else
7610 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7611 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7612#endif
7613 msg.rpc_argp = &calldata->args;
7614 msg.rpc_resp = &calldata->res;
7615 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007616
Andy Adamson8d89bd72016-09-09 09:22:18 -04007617 task = rpc_run_task(&task_setup_data);
7618 if (IS_ERR(task)) {
7619 status = PTR_ERR(task);
7620 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007621 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007622
Andy Adamsonad0849a2016-09-09 09:22:28 -04007623 if (!xprt) {
7624 status = rpc_wait_for_completion_task(task);
7625 if (!status)
7626 status = calldata->rpc_status;
7627 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007628 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007629
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007631out:
Chuck Lever177313f2012-05-21 22:44:58 -04007632 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007633 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007634 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007635 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007636 clp->cl_implid->date.seconds,
7637 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007638 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007639 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007640
7641out_impl_id:
7642 kfree(calldata->res.impl_id);
7643out_server_scope:
7644 kfree(calldata->res.server_scope);
7645out_server_owner:
7646 kfree(calldata->res.server_owner);
7647out_calldata:
7648 kfree(calldata);
7649 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007650}
7651
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007652/*
7653 * nfs4_proc_exchange_id()
7654 *
7655 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7656 *
7657 * Since the clientid has expired, all compounds using sessions
7658 * associated with the stale clientid will be returning
7659 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7660 * be in some phase of session reset.
7661 *
7662 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7663 */
7664int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7665{
7666 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7667 int status;
7668
7669 /* try SP4_MACH_CRED if krb5i/p */
7670 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7671 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007672 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007673 if (!status)
7674 return 0;
7675 }
7676
7677 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007678 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007679}
7680
Andy Adamson04fa2c62016-09-09 09:22:29 -04007681/**
7682 * nfs4_test_session_trunk
7683 *
7684 * This is an add_xprt_test() test function called from
7685 * rpc_clnt_setup_test_and_add_xprt.
7686 *
7687 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7688 * and is dereferrenced in nfs4_exchange_id_release
7689 *
7690 * Upon success, add the new transport to the rpc_clnt
7691 *
7692 * @clnt: struct rpc_clnt to get new transport
7693 * @xprt: the rpc_xprt to test
7694 * @data: call data for _nfs4_proc_exchange_id.
7695 */
7696int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7697 void *data)
7698{
7699 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7700 u32 sp4_how;
7701
7702 dprintk("--> %s try %s\n", __func__,
7703 xprt->address_strings[RPC_DISPLAY_ADDR]);
7704
7705 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7706
7707 /* Test connection for session trunking. Async exchange_id call */
7708 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7709}
7710EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7711
Trond Myklebust66245532012-05-25 17:18:09 -04007712static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7713 struct rpc_cred *cred)
7714{
7715 struct rpc_message msg = {
7716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7717 .rpc_argp = clp,
7718 .rpc_cred = cred,
7719 };
7720 int status;
7721
7722 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007723 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007724 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007725 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007726 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7727 return status;
7728}
7729
7730static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7731 struct rpc_cred *cred)
7732{
7733 unsigned int loop;
7734 int ret;
7735
7736 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7737 ret = _nfs4_proc_destroy_clientid(clp, cred);
7738 switch (ret) {
7739 case -NFS4ERR_DELAY:
7740 case -NFS4ERR_CLIENTID_BUSY:
7741 ssleep(1);
7742 break;
7743 default:
7744 return ret;
7745 }
7746 }
7747 return 0;
7748}
7749
7750int nfs4_destroy_clientid(struct nfs_client *clp)
7751{
7752 struct rpc_cred *cred;
7753 int ret = 0;
7754
7755 if (clp->cl_mvops->minor_version < 1)
7756 goto out;
7757 if (clp->cl_exchange_flags == 0)
7758 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007759 if (clp->cl_preserve_clid)
7760 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007761 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007762 ret = nfs4_proc_destroy_clientid(clp, cred);
7763 if (cred)
7764 put_rpccred(cred);
7765 switch (ret) {
7766 case 0:
7767 case -NFS4ERR_STALE_CLIENTID:
7768 clp->cl_exchange_flags = 0;
7769 }
7770out:
7771 return ret;
7772}
7773
Andy Adamson2050f0c2009-04-01 09:22:30 -04007774struct nfs4_get_lease_time_data {
7775 struct nfs4_get_lease_time_args *args;
7776 struct nfs4_get_lease_time_res *res;
7777 struct nfs_client *clp;
7778};
7779
7780static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7781 void *calldata)
7782{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007783 struct nfs4_get_lease_time_data *data =
7784 (struct nfs4_get_lease_time_data *)calldata;
7785
7786 dprintk("--> %s\n", __func__);
7787 /* just setup sequence, do not trigger session recovery
7788 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007789 nfs41_setup_sequence(data->clp->cl_session,
7790 &data->args->la_seq_args,
7791 &data->res->lr_seq_res,
7792 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007793 dprintk("<-- %s\n", __func__);
7794}
7795
7796/*
7797 * Called from nfs4_state_manager thread for session setup, so don't recover
7798 * from sequence operation or clientid errors.
7799 */
7800static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7801{
7802 struct nfs4_get_lease_time_data *data =
7803 (struct nfs4_get_lease_time_data *)calldata;
7804
7805 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007806 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7807 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007808 switch (task->tk_status) {
7809 case -NFS4ERR_DELAY:
7810 case -NFS4ERR_GRACE:
7811 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7812 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7813 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007814 /* fall through */
7815 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007816 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007817 return;
7818 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007819 dprintk("<-- %s\n", __func__);
7820}
7821
Trond Myklebust17280172012-03-11 13:11:00 -04007822static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007823 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7824 .rpc_call_done = nfs4_get_lease_time_done,
7825};
7826
7827int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7828{
7829 struct rpc_task *task;
7830 struct nfs4_get_lease_time_args args;
7831 struct nfs4_get_lease_time_res res = {
7832 .lr_fsinfo = fsinfo,
7833 };
7834 struct nfs4_get_lease_time_data data = {
7835 .args = &args,
7836 .res = &res,
7837 .clp = clp,
7838 };
7839 struct rpc_message msg = {
7840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7841 .rpc_argp = &args,
7842 .rpc_resp = &res,
7843 };
7844 struct rpc_task_setup task_setup = {
7845 .rpc_client = clp->cl_rpcclient,
7846 .rpc_message = &msg,
7847 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007848 .callback_data = &data,
7849 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007850 };
7851 int status;
7852
Chuck Levera9c92d62013-08-09 12:48:18 -04007853 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007854 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007855 dprintk("--> %s\n", __func__);
7856 task = rpc_run_task(&task_setup);
7857
7858 if (IS_ERR(task))
7859 status = PTR_ERR(task);
7860 else {
7861 status = task->tk_status;
7862 rpc_put_task(task);
7863 }
7864 dprintk("<-- %s return %d\n", __func__, status);
7865
7866 return status;
7867}
7868
Andy Adamsonfc931582009-04-01 09:22:31 -04007869/*
7870 * Initialize the values to be used by the client in CREATE_SESSION
7871 * If nfs4_init_session set the fore channel request and response sizes,
7872 * use them.
7873 *
7874 * Set the back channel max_resp_sz_cached to zero to force the client to
7875 * always set csa_cachethis to FALSE because the current implementation
7876 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7877 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007878static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7879 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007880{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007881 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007882 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007883
Andy Adamson18aad3d2013-06-26 12:21:49 -04007884 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7885 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7886
Andy Adamsonfc931582009-04-01 09:22:31 -04007887 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007888 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7889 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007890 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007891 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007892
7893 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007894 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007895 __func__,
7896 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007897 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007898
7899 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007900 args->bc_attrs.max_rqst_sz = max_bc_payload;
7901 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007902 args->bc_attrs.max_resp_sz_cached = 0;
7903 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007904 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007905
7906 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7907 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7908 __func__,
7909 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7910 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7911 args->bc_attrs.max_reqs);
7912}
7913
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7915 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007916{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007917 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007919
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007920 if (rcvd->max_resp_sz > sent->max_resp_sz)
7921 return -EINVAL;
7922 /*
7923 * Our requested max_ops is the minimum we need; we're not
7924 * prepared to break up compounds into smaller pieces than that.
7925 * So, no point even trying to continue if the server won't
7926 * cooperate:
7927 */
7928 if (rcvd->max_ops < sent->max_ops)
7929 return -EINVAL;
7930 if (rcvd->max_reqs == 0)
7931 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007932 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7933 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007934 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007935}
7936
Trond Myklebust79969dd2015-02-18 11:30:18 -08007937static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7938 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007939{
7940 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007941 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007942
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007943 if (!(res->flags & SESSION4_BACK_CHAN))
7944 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007945 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7946 return -EINVAL;
7947 if (rcvd->max_resp_sz < sent->max_resp_sz)
7948 return -EINVAL;
7949 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7950 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007951 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007952 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007953 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007954 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007955out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007956 return 0;
7957}
Andy Adamson8d353012009-04-01 09:22:32 -04007958
Andy Adamson8d353012009-04-01 09:22:32 -04007959static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007960 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007961{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007962 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007963
Trond Myklebust79969dd2015-02-18 11:30:18 -08007964 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007965 if (ret)
7966 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007967 return nfs4_verify_back_channel_attrs(args, res);
7968}
7969
7970static void nfs4_update_session(struct nfs4_session *session,
7971 struct nfs41_create_session_res *res)
7972{
7973 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007974 /* Mark client id and session as being confirmed */
7975 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7976 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007977 session->flags = res->flags;
7978 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007979 if (res->flags & SESSION4_BACK_CHAN)
7980 memcpy(&session->bc_attrs, &res->bc_attrs,
7981 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007982}
7983
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007984static int _nfs4_proc_create_session(struct nfs_client *clp,
7985 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007986{
7987 struct nfs4_session *session = clp->cl_session;
7988 struct nfs41_create_session_args args = {
7989 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007990 .clientid = clp->cl_clientid,
7991 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007992 .cb_program = NFS4_CALLBACK,
7993 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007994 struct nfs41_create_session_res res;
7995
Andy Adamsonfc931582009-04-01 09:22:31 -04007996 struct rpc_message msg = {
7997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7998 .rpc_argp = &args,
7999 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008000 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008001 };
8002 int status;
8003
Chuck Lever6b26cc82016-05-02 14:40:40 -04008004 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008005 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008006
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008007 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008008 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008009
Trond Myklebustb519d402016-09-11 14:50:01 -04008010 switch (status) {
8011 case -NFS4ERR_STALE_CLIENTID:
8012 case -NFS4ERR_DELAY:
8013 case -ETIMEDOUT:
8014 case -EACCES:
8015 case -EAGAIN:
8016 goto out;
8017 };
8018
8019 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008020 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008021 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008022 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008023 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008024 if (status)
8025 goto out;
8026 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008027 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008028out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008029 return status;
8030}
8031
8032/*
8033 * Issues a CREATE_SESSION operation to the server.
8034 * It is the responsibility of the caller to verify the session is
8035 * expired before calling this routine.
8036 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008037int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008038{
8039 int status;
8040 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008041 struct nfs4_session *session = clp->cl_session;
8042
8043 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8044
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008045 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008046 if (status)
8047 goto out;
8048
Andy Adamsonaacd5532011-11-09 13:58:21 -05008049 /* Init or reset the session slot tables */
8050 status = nfs4_setup_session_slot_tables(session);
8051 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008052 if (status)
8053 goto out;
8054
8055 ptr = (unsigned *)&session->sess_id.data[0];
8056 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8057 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008058out:
8059 dprintk("<-- %s\n", __func__);
8060 return status;
8061}
8062
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008063/*
8064 * Issue the over-the-wire RPC DESTROY_SESSION.
8065 * The caller must serialize access to this routine.
8066 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008067int nfs4_proc_destroy_session(struct nfs4_session *session,
8068 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008069{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008070 struct rpc_message msg = {
8071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8072 .rpc_argp = session,
8073 .rpc_cred = cred,
8074 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008075 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008076
8077 dprintk("--> nfs4_proc_destroy_session\n");
8078
8079 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008080 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8081 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008082
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008083 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008084 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008085
8086 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008087 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008088 "Session has been destroyed regardless...\n", status);
8089
8090 dprintk("<-- nfs4_proc_destroy_session\n");
8091 return status;
8092}
8093
Trond Myklebust7b38c362012-05-23 13:23:31 -04008094/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008095 * Renew the cl_session lease.
8096 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008097struct nfs4_sequence_data {
8098 struct nfs_client *clp;
8099 struct nfs4_sequence_args args;
8100 struct nfs4_sequence_res res;
8101};
8102
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008103static void nfs41_sequence_release(void *data)
8104{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008105 struct nfs4_sequence_data *calldata = data;
8106 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008107
Alexandros Batsakis71358402010-02-05 03:45:05 -08008108 if (atomic_read(&clp->cl_count) > 1)
8109 nfs4_schedule_state_renewal(clp);
8110 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008111 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008112}
8113
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008114static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8115{
8116 switch(task->tk_status) {
8117 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008118 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8119 return -EAGAIN;
8120 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008121 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008122 }
8123 return 0;
8124}
8125
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008126static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008127{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008128 struct nfs4_sequence_data *calldata = data;
8129 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008130
Trond Myklebust14516c32010-07-31 14:29:06 -04008131 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8132 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008133
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008134 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135 if (task->tk_status < 0) {
8136 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008137 if (atomic_read(&clp->cl_count) == 1)
8138 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008139
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008140 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8141 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008142 return;
8143 }
8144 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008145 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008146out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008147 dprintk("<-- %s\n", __func__);
8148}
8149
8150static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8151{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008152 struct nfs4_sequence_data *calldata = data;
8153 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008154 struct nfs4_sequence_args *args;
8155 struct nfs4_sequence_res *res;
8156
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008157 args = task->tk_msg.rpc_argp;
8158 res = task->tk_msg.rpc_resp;
8159
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008160 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008161}
8162
8163static const struct rpc_call_ops nfs41_sequence_ops = {
8164 .rpc_call_done = nfs41_sequence_call_done,
8165 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008166 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008167};
8168
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008169static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8170 struct rpc_cred *cred,
8171 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008172{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008173 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008174 struct rpc_message msg = {
8175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8176 .rpc_cred = cred,
8177 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008178 struct rpc_task_setup task_setup_data = {
8179 .rpc_client = clp->cl_rpcclient,
8180 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008181 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008182 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008183 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008184
Alexandros Batsakis71358402010-02-05 03:45:05 -08008185 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008186 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008187 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008188 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008189 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008190 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008191 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008192 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008193 if (is_privileged)
8194 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008195 msg.rpc_argp = &calldata->args;
8196 msg.rpc_resp = &calldata->res;
8197 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008198 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008199
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008200 return rpc_run_task(&task_setup_data);
8201}
8202
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008203static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008204{
8205 struct rpc_task *task;
8206 int ret = 0;
8207
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008208 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008209 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008210 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008211 if (IS_ERR(task))
8212 ret = PTR_ERR(task);
8213 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008214 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008215 dprintk("<-- %s status=%d\n", __func__, ret);
8216 return ret;
8217}
8218
8219static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8220{
8221 struct rpc_task *task;
8222 int ret;
8223
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008224 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008225 if (IS_ERR(task)) {
8226 ret = PTR_ERR(task);
8227 goto out;
8228 }
8229 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008230 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008231 ret = task->tk_status;
8232 rpc_put_task(task);
8233out:
8234 dprintk("<-- %s status=%d\n", __func__, ret);
8235 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008236}
8237
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008238struct nfs4_reclaim_complete_data {
8239 struct nfs_client *clp;
8240 struct nfs41_reclaim_complete_args arg;
8241 struct nfs41_reclaim_complete_res res;
8242};
8243
8244static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8245{
8246 struct nfs4_reclaim_complete_data *calldata = data;
8247
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008248 nfs41_setup_sequence(calldata->clp->cl_session,
8249 &calldata->arg.seq_args,
8250 &calldata->res.seq_res,
8251 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008252}
8253
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008254static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8255{
8256 switch(task->tk_status) {
8257 case 0:
8258 case -NFS4ERR_COMPLETE_ALREADY:
8259 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8260 break;
8261 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008262 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008263 /* fall through */
8264 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008265 return -EAGAIN;
8266 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008267 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008268 }
8269 return 0;
8270}
8271
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008272static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8273{
8274 struct nfs4_reclaim_complete_data *calldata = data;
8275 struct nfs_client *clp = calldata->clp;
8276 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8277
8278 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008279 if (!nfs41_sequence_done(task, res))
8280 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008281
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008282 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008283 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8284 rpc_restart_call_prepare(task);
8285 return;
8286 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008287 dprintk("<-- %s\n", __func__);
8288}
8289
8290static void nfs4_free_reclaim_complete_data(void *data)
8291{
8292 struct nfs4_reclaim_complete_data *calldata = data;
8293
8294 kfree(calldata);
8295}
8296
8297static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8298 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8299 .rpc_call_done = nfs4_reclaim_complete_done,
8300 .rpc_release = nfs4_free_reclaim_complete_data,
8301};
8302
8303/*
8304 * Issue a global reclaim complete.
8305 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008306static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8307 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008308{
8309 struct nfs4_reclaim_complete_data *calldata;
8310 struct rpc_task *task;
8311 struct rpc_message msg = {
8312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008313 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008314 };
8315 struct rpc_task_setup task_setup_data = {
8316 .rpc_client = clp->cl_rpcclient,
8317 .rpc_message = &msg,
8318 .callback_ops = &nfs4_reclaim_complete_call_ops,
8319 .flags = RPC_TASK_ASYNC,
8320 };
8321 int status = -ENOMEM;
8322
8323 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008324 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008325 if (calldata == NULL)
8326 goto out;
8327 calldata->clp = clp;
8328 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008329
Chuck Levera9c92d62013-08-09 12:48:18 -04008330 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008331 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008332 msg.rpc_argp = &calldata->arg;
8333 msg.rpc_resp = &calldata->res;
8334 task_setup_data.callback_data = calldata;
8335 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008336 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008337 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008338 goto out;
8339 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008340 status = nfs4_wait_for_completion_rpc_task(task);
8341 if (status == 0)
8342 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008343 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008344 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008345out:
8346 dprintk("<-- %s status=%d\n", __func__, status);
8347 return status;
8348}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008349
8350static void
8351nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8352{
8353 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008354 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008355 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008356
8357 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008358 nfs41_setup_sequence(session, &lgp->args.seq_args,
8359 &lgp->res.seq_res, task);
8360 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008361}
8362
8363static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8364{
8365 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008366
8367 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008368 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008369 dprintk("<-- %s\n", __func__);
8370}
8371
8372static int
8373nfs4_layoutget_handle_exception(struct rpc_task *task,
8374 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8375{
Trond Myklebustee314c22012-10-01 17:25:48 -07008376 struct inode *inode = lgp->args.inode;
8377 struct nfs_server *server = NFS_SERVER(inode);
8378 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008379 int nfs4err = task->tk_status;
8380 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008381 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008382
Boaz Harroshed7e5422014-01-22 20:34:54 +02008383 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008384
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008385 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008386 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008387 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008388
8389 /*
8390 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8391 * on the file. set tk_status to -ENODATA to tell upper layer to
8392 * retry go inband.
8393 */
8394 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008395 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008396 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008397 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008398 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8399 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8400 */
8401 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008402 status = -EOVERFLOW;
8403 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008404 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008405 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008406 * (or clients) writing to the same RAID stripe except when
8407 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008408 *
8409 * Treat it like we would RECALLCONFLICT -- we retry for a little
8410 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008411 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008412 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008413 if (lgp->args.minlength == 0) {
8414 status = -EOVERFLOW;
8415 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008416 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008417 status = -EBUSY;
8418 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008419 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008420 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008421 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008422 case -NFS4ERR_DELEG_REVOKED:
8423 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008424 case -NFS4ERR_EXPIRED:
8425 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008426 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008427 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008428 lo = NFS_I(inode)->layout;
8429 /* If the open stateid was bad, then recover it. */
8430 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8431 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008432 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008433 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008434 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008435 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008436 break;
8437 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008438
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008439 /*
8440 * Mark the bad layout state as invalid, then retry
8441 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008442 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008443 spin_unlock(&inode->i_lock);
8444 pnfs_free_lseg_list(&head);
8445 status = -EAGAIN;
8446 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008447 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008448
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008449 err = nfs4_handle_exception(server, nfs4err, exception);
8450 if (!status) {
8451 if (exception->retry)
8452 status = -EAGAIN;
8453 else
8454 status = err;
8455 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008456out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008458 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459}
8460
Idan Kedar85541162012-08-02 11:47:10 +03008461static size_t max_response_pages(struct nfs_server *server)
8462{
8463 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8464 return nfs_page_array_len(0, max_resp_sz);
8465}
8466
8467static void nfs4_free_pages(struct page **pages, size_t size)
8468{
8469 int i;
8470
8471 if (!pages)
8472 return;
8473
8474 for (i = 0; i < size; i++) {
8475 if (!pages[i])
8476 break;
8477 __free_page(pages[i]);
8478 }
8479 kfree(pages);
8480}
8481
8482static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8483{
8484 struct page **pages;
8485 int i;
8486
8487 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8488 if (!pages) {
8489 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8490 return NULL;
8491 }
8492
8493 for (i = 0; i < size; i++) {
8494 pages[i] = alloc_page(gfp_flags);
8495 if (!pages[i]) {
8496 dprintk("%s: failed to allocate page\n", __func__);
8497 nfs4_free_pages(pages, size);
8498 return NULL;
8499 }
8500 }
8501
8502 return pages;
8503}
8504
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008505static void nfs4_layoutget_release(void *calldata)
8506{
8507 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008508 struct inode *inode = lgp->args.inode;
8509 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008510 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511
8512 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008513 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008514 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008515 put_nfs_open_context(lgp->args.ctx);
8516 kfree(calldata);
8517 dprintk("<-- %s\n", __func__);
8518}
8519
8520static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8521 .rpc_call_prepare = nfs4_layoutget_prepare,
8522 .rpc_call_done = nfs4_layoutget_done,
8523 .rpc_release = nfs4_layoutget_release,
8524};
8525
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008526struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008527nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008528{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008529 struct inode *inode = lgp->args.inode;
8530 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008531 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008532 struct rpc_task *task;
8533 struct rpc_message msg = {
8534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8535 .rpc_argp = &lgp->args,
8536 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008537 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008538 };
8539 struct rpc_task_setup task_setup_data = {
8540 .rpc_client = server->client,
8541 .rpc_message = &msg,
8542 .callback_ops = &nfs4_layoutget_call_ops,
8543 .callback_data = lgp,
8544 .flags = RPC_TASK_ASYNC,
8545 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008546 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008547 struct nfs4_exception exception = {
8548 .inode = inode,
8549 .timeout = *timeout,
8550 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008551 int status = 0;
8552
8553 dprintk("--> %s\n", __func__);
8554
Peng Tao4bd5a982014-11-17 11:05:17 +08008555 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8556 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8557
Idan Kedar85541162012-08-02 11:47:10 +03008558 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8559 if (!lgp->args.layout.pages) {
8560 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008561 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008562 }
8563 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8564
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008565 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008567 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008568
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008569 task = rpc_run_task(&task_setup_data);
8570 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008571 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008572 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008573 if (status == 0) {
8574 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8575 *timeout = exception.timeout;
8576 }
8577
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008578 trace_nfs4_layoutget(lgp->args.ctx,
8579 &lgp->args.range,
8580 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008581 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008582 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008583
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008584 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8585 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008586 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008587 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588 rpc_put_task(task);
8589 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008590 if (status)
8591 return ERR_PTR(status);
8592 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008593}
8594
Benny Halevycbe82602011-05-22 19:52:37 +03008595static void
8596nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8597{
8598 struct nfs4_layoutreturn *lrp = calldata;
8599
8600 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008601 nfs41_setup_sequence(lrp->clp->cl_session,
8602 &lrp->args.seq_args,
8603 &lrp->res.seq_res,
8604 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008605}
8606
8607static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8608{
8609 struct nfs4_layoutreturn *lrp = calldata;
8610 struct nfs_server *server;
8611
8612 dprintk("--> %s\n", __func__);
8613
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008614 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008615 return;
8616
8617 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008618 switch (task->tk_status) {
8619 default:
8620 task->tk_status = 0;
8621 case 0:
8622 break;
8623 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008624 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008625 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008626 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008627 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008628 return;
8629 }
Benny Halevycbe82602011-05-22 19:52:37 +03008630 dprintk("<-- %s\n", __func__);
8631}
8632
8633static void nfs4_layoutreturn_release(void *calldata)
8634{
8635 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008636 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008637
8638 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008639 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008640 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008641 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008642 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008643 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008644 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8645 lrp->ld_private.ops->free(&lrp->ld_private);
Benny Halevycbe82602011-05-22 19:52:37 +03008646 kfree(calldata);
8647 dprintk("<-- %s\n", __func__);
8648}
8649
8650static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8651 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8652 .rpc_call_done = nfs4_layoutreturn_done,
8653 .rpc_release = nfs4_layoutreturn_release,
8654};
8655
Peng Tao6c166052014-11-17 09:30:40 +08008656int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008657{
8658 struct rpc_task *task;
8659 struct rpc_message msg = {
8660 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8661 .rpc_argp = &lrp->args,
8662 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008663 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008664 };
8665 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008666 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008667 .rpc_message = &msg,
8668 .callback_ops = &nfs4_layoutreturn_call_ops,
8669 .callback_data = lrp,
8670 };
Peng Tao6c166052014-11-17 09:30:40 +08008671 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008672
Andrew Elble99ade3c2015-12-02 09:39:51 -05008673 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8674 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8675 &task_setup_data.rpc_client, &msg);
8676
Benny Halevycbe82602011-05-22 19:52:37 +03008677 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008678 if (!sync) {
8679 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8680 if (!lrp->inode) {
8681 nfs4_layoutreturn_release(lrp);
8682 return -EAGAIN;
8683 }
8684 task_setup_data.flags |= RPC_TASK_ASYNC;
8685 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008686 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008687 task = rpc_run_task(&task_setup_data);
8688 if (IS_ERR(task))
8689 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008690 if (sync)
8691 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008692 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008693 dprintk("<-- %s status=%d\n", __func__, status);
8694 rpc_put_task(task);
8695 return status;
8696}
8697
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008698static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008699_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8700 struct pnfs_device *pdev,
8701 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008702{
8703 struct nfs4_getdeviceinfo_args args = {
8704 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008705 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8706 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008707 };
8708 struct nfs4_getdeviceinfo_res res = {
8709 .pdev = pdev,
8710 };
8711 struct rpc_message msg = {
8712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8713 .rpc_argp = &args,
8714 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008715 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716 };
8717 int status;
8718
8719 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008720 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008721 if (res.notification & ~args.notify_types)
8722 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008723 if (res.notification != args.notify_types)
8724 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008725
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008726 dprintk("<-- %s status=%d\n", __func__, status);
8727
8728 return status;
8729}
8730
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008731int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8732 struct pnfs_device *pdev,
8733 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008734{
8735 struct nfs4_exception exception = { };
8736 int err;
8737
8738 do {
8739 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008740 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008741 &exception);
8742 } while (exception.retry);
8743 return err;
8744}
8745EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8746
Andy Adamson863a3c62011-03-23 13:27:54 +00008747static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8748{
8749 struct nfs4_layoutcommit_data *data = calldata;
8750 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008751 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008752
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008753 nfs41_setup_sequence(session,
8754 &data->args.seq_args,
8755 &data->res.seq_res,
8756 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008757}
8758
8759static void
8760nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8761{
8762 struct nfs4_layoutcommit_data *data = calldata;
8763 struct nfs_server *server = NFS_SERVER(data->args.inode);
8764
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008765 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008766 return;
8767
8768 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008769 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8770 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8771 case -NFS4ERR_BADLAYOUT: /* no layout */
8772 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008773 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008774 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008775 break;
8776 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008777 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008778 rpc_restart_call_prepare(task);
8779 return;
8780 }
8781 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008782}
8783
8784static void nfs4_layoutcommit_release(void *calldata)
8785{
8786 struct nfs4_layoutcommit_data *data = calldata;
8787
Andy Adamsondb29c082011-07-30 20:52:38 -04008788 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008789 nfs_post_op_update_inode_force_wcc(data->args.inode,
8790 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008792 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008793 kfree(data);
8794}
8795
8796static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8797 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8798 .rpc_call_done = nfs4_layoutcommit_done,
8799 .rpc_release = nfs4_layoutcommit_release,
8800};
8801
8802int
Andy Adamsonef311532011-03-12 02:58:10 -05008803nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008804{
8805 struct rpc_message msg = {
8806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8807 .rpc_argp = &data->args,
8808 .rpc_resp = &data->res,
8809 .rpc_cred = data->cred,
8810 };
8811 struct rpc_task_setup task_setup_data = {
8812 .task = &data->task,
8813 .rpc_client = NFS_CLIENT(data->args.inode),
8814 .rpc_message = &msg,
8815 .callback_ops = &nfs4_layoutcommit_ops,
8816 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008817 };
8818 struct rpc_task *task;
8819 int status = 0;
8820
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008821 dprintk("NFS: initiating layoutcommit call. sync %d "
8822 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008823 data->args.lastbytewritten,
8824 data->args.inode->i_ino);
8825
Trond Myklebust472e2592015-02-05 16:50:30 -05008826 if (!sync) {
8827 data->inode = nfs_igrab_and_active(data->args.inode);
8828 if (data->inode == NULL) {
8829 nfs4_layoutcommit_release(data);
8830 return -EAGAIN;
8831 }
8832 task_setup_data.flags = RPC_TASK_ASYNC;
8833 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008834 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008835 task = rpc_run_task(&task_setup_data);
8836 if (IS_ERR(task))
8837 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008838 if (sync)
8839 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008840 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008841 dprintk("%s: status %d\n", __func__, status);
8842 rpc_put_task(task);
8843 return status;
8844}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008845
Andy Adamson97431202013-08-08 10:57:56 -04008846/**
8847 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8848 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8849 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008850static int
8851_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008852 struct nfs_fsinfo *info,
8853 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008854{
8855 struct nfs41_secinfo_no_name_args args = {
8856 .style = SECINFO_STYLE_CURRENT_FH,
8857 };
8858 struct nfs4_secinfo_res res = {
8859 .flavors = flavors,
8860 };
8861 struct rpc_message msg = {
8862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8863 .rpc_argp = &args,
8864 .rpc_resp = &res,
8865 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008866 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008867 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008868 int status;
8869
8870 if (use_integrity) {
8871 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008872 cred = nfs4_get_clid_cred(server->nfs_client);
8873 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008874 }
8875
8876 dprintk("--> %s\n", __func__);
8877 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8878 &res.seq_res, 0);
8879 dprintk("<-- %s status=%d\n", __func__, status);
8880
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008881 if (cred)
8882 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008883
8884 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008885}
8886
8887static int
8888nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8889 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8890{
8891 struct nfs4_exception exception = { };
8892 int err;
8893 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008894 /* first try using integrity protection */
8895 err = -NFS4ERR_WRONGSEC;
8896
8897 /* try to use integrity protection with machine cred */
8898 if (_nfs4_is_integrity_protected(server->nfs_client))
8899 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8900 flavors, true);
8901
8902 /*
8903 * if unable to use integrity protection, or SECINFO with
8904 * integrity protection returns NFS4ERR_WRONGSEC (which is
8905 * disallowed by spec, but exists in deployed servers) use
8906 * the current filesystem's rpc_client and the user cred.
8907 */
8908 if (err == -NFS4ERR_WRONGSEC)
8909 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8910 flavors, false);
8911
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008912 switch (err) {
8913 case 0:
8914 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008915 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008916 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008917 default:
8918 err = nfs4_handle_exception(server, err, &exception);
8919 }
8920 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008921out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008922 return err;
8923}
8924
8925static int
8926nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8927 struct nfs_fsinfo *info)
8928{
8929 int err;
8930 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008931 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008932 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008933 struct nfs4_secinfo4 *secinfo;
8934 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008935
8936 page = alloc_page(GFP_KERNEL);
8937 if (!page) {
8938 err = -ENOMEM;
8939 goto out;
8940 }
8941
8942 flavors = page_address(page);
8943 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8944
8945 /*
8946 * Fall back on "guess and check" method if
8947 * the server doesn't support SECINFO_NO_NAME
8948 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008949 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008950 err = nfs4_find_root_sec(server, fhandle, info);
8951 goto out_freepage;
8952 }
8953 if (err)
8954 goto out_freepage;
8955
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008956 for (i = 0; i < flavors->num_flavors; i++) {
8957 secinfo = &flavors->flavors[i];
8958
8959 switch (secinfo->flavor) {
8960 case RPC_AUTH_NULL:
8961 case RPC_AUTH_UNIX:
8962 case RPC_AUTH_GSS:
8963 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8964 &secinfo->flavor_info);
8965 break;
8966 default:
8967 flavor = RPC_AUTH_MAXFLAVOR;
8968 break;
8969 }
8970
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008971 if (!nfs_auth_info_match(&server->auth_info, flavor))
8972 flavor = RPC_AUTH_MAXFLAVOR;
8973
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008974 if (flavor != RPC_AUTH_MAXFLAVOR) {
8975 err = nfs4_lookup_root_sec(server, fhandle,
8976 info, flavor);
8977 if (!err)
8978 break;
8979 }
8980 }
8981
8982 if (flavor == RPC_AUTH_MAXFLAVOR)
8983 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008984
8985out_freepage:
8986 put_page(page);
8987 if (err == -EACCES)
8988 return -EPERM;
8989out:
8990 return err;
8991}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008992
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008993static int _nfs41_test_stateid(struct nfs_server *server,
8994 nfs4_stateid *stateid,
8995 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008996{
8997 int status;
8998 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008999 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009000 };
9001 struct nfs41_test_stateid_res res;
9002 struct rpc_message msg = {
9003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9004 .rpc_argp = &args,
9005 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009006 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009007 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009008 struct rpc_clnt *rpc_client = server->client;
9009
9010 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9011 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009012
Chuck Lever38527b12012-07-11 16:30:23 -04009013 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009014 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009015 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009016 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009017 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009018 if (status != NFS_OK) {
9019 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009020 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009021 }
9022 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009023 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009024}
9025
Trond Myklebust43912bb2016-09-22 13:38:56 -04009026static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9027 int err, struct nfs4_exception *exception)
9028{
9029 exception->retry = 0;
9030 switch(err) {
9031 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009032 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009033 nfs4_handle_exception(server, err, exception);
9034 break;
9035 case -NFS4ERR_BADSESSION:
9036 case -NFS4ERR_BADSLOT:
9037 case -NFS4ERR_BAD_HIGH_SLOT:
9038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9039 case -NFS4ERR_DEADSESSION:
9040 nfs4_do_handle_exception(server, err, exception);
9041 }
9042}
9043
Chuck Lever38527b12012-07-11 16:30:23 -04009044/**
9045 * nfs41_test_stateid - perform a TEST_STATEID operation
9046 *
9047 * @server: server / transport on which to perform the operation
9048 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009049 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009050 *
9051 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9052 * Otherwise a negative NFS4ERR value is returned if the operation
9053 * failed or the state ID is not currently valid.
9054 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009055static int nfs41_test_stateid(struct nfs_server *server,
9056 nfs4_stateid *stateid,
9057 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009058{
9059 struct nfs4_exception exception = { };
9060 int err;
9061 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009062 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009063 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009064 } while (exception.retry);
9065 return err;
9066}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009067
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009068struct nfs_free_stateid_data {
9069 struct nfs_server *server;
9070 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009071 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009072};
9073
9074static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9075{
9076 struct nfs_free_stateid_data *data = calldata;
9077 nfs41_setup_sequence(nfs4_get_session(data->server),
9078 &data->args.seq_args,
9079 &data->res.seq_res,
9080 task);
9081}
9082
9083static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9084{
9085 struct nfs_free_stateid_data *data = calldata;
9086
9087 nfs41_sequence_done(task, &data->res.seq_res);
9088
9089 switch (task->tk_status) {
9090 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009091 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009092 rpc_restart_call_prepare(task);
9093 }
9094}
9095
9096static void nfs41_free_stateid_release(void *calldata)
9097{
9098 kfree(calldata);
9099}
9100
Trond Myklebust17f26b12013-08-21 15:48:42 -04009101static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009102 .rpc_call_prepare = nfs41_free_stateid_prepare,
9103 .rpc_call_done = nfs41_free_stateid_done,
9104 .rpc_release = nfs41_free_stateid_release,
9105};
9106
9107static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009108 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009109 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 bool privileged)
9111{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009112 struct rpc_message msg = {
9113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009114 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009115 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009116 struct rpc_task_setup task_setup = {
9117 .rpc_client = server->client,
9118 .rpc_message = &msg,
9119 .callback_ops = &nfs41_free_stateid_ops,
9120 .flags = RPC_TASK_ASYNC,
9121 };
9122 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009123
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009124 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9125 &task_setup.rpc_client, &msg);
9126
Chuck Lever38527b12012-07-11 16:30:23 -04009127 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009128 data = kmalloc(sizeof(*data), GFP_NOFS);
9129 if (!data)
9130 return ERR_PTR(-ENOMEM);
9131 data->server = server;
9132 nfs4_stateid_copy(&data->args.stateid, stateid);
9133
9134 task_setup.callback_data = data;
9135
9136 msg.rpc_argp = &data->args;
9137 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009138 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009139 if (privileged)
9140 nfs4_set_sequence_privileged(&data->args.seq_args);
9141
9142 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009143}
9144
Chuck Lever38527b12012-07-11 16:30:23 -04009145/**
9146 * nfs41_free_stateid - perform a FREE_STATEID operation
9147 *
9148 * @server: server / transport on which to perform the operation
9149 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009150 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009151 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009152 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009153 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009154 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009155static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009156 const nfs4_stateid *stateid,
9157 struct rpc_cred *cred,
9158 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009159{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009160 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009161
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009162 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009163 if (IS_ERR(task))
9164 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009165 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009166 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009167}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009168
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009169static void
9170nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009171{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009172 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009173
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009174 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009175 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009176}
9177
Trond Myklebust36281ca2012-03-04 18:13:56 -05009178static bool nfs41_match_stateid(const nfs4_stateid *s1,
9179 const nfs4_stateid *s2)
9180{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009181 if (s1->type != s2->type)
9182 return false;
9183
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009184 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009185 return false;
9186
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009187 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009188 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009189 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009190 return true;
9191
9192 return false;
9193}
9194
Andy Adamson557134a2009-04-01 09:21:53 -04009195#endif /* CONFIG_NFS_V4_1 */
9196
Trond Myklebust36281ca2012-03-04 18:13:56 -05009197static bool nfs4_match_stateid(const nfs4_stateid *s1,
9198 const nfs4_stateid *s2)
9199{
Trond Myklebustf597c532012-03-04 18:13:56 -05009200 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009201}
9202
9203
Trond Myklebust17280172012-03-11 13:11:00 -04009204static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009205 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009206 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009207 .recover_open = nfs4_open_reclaim,
9208 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009209 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009210 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009211};
9212
Andy Adamson591d71c2009-04-01 09:22:47 -04009213#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009214static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009215 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9216 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9217 .recover_open = nfs4_open_reclaim,
9218 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009219 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009220 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009221 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009222};
9223#endif /* CONFIG_NFS_V4_1 */
9224
Trond Myklebust17280172012-03-11 13:11:00 -04009225static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009226 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009227 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009228 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009229 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009230 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231};
9232
Andy Adamson591d71c2009-04-01 09:22:47 -04009233#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009234static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009235 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9236 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009237 .recover_open = nfs41_open_expired,
9238 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009239 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009240};
9241#endif /* CONFIG_NFS_V4_1 */
9242
Trond Myklebust17280172012-03-11 13:11:00 -04009243static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009244 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009245 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009246 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009247};
9248
9249#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009250static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009251 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009252 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009253 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009254};
9255#endif
9256
Chuck Leverec011fe2013-10-17 14:12:39 -04009257static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009258 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009259 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009260};
9261
9262#if defined(CONFIG_NFS_V4_1)
9263static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009264 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009265 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009266};
9267#endif /* CONFIG_NFS_V4_1 */
9268
Trond Myklebust97dc1352010-06-16 09:52:26 -04009269static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9270 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009271 .init_caps = NFS_CAP_READDIRPLUS
9272 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009273 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009274 .init_client = nfs40_init_client,
9275 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009276 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009277 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009278 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009279 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009280 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009281 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009282 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9283 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9284 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009285 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009286};
9287
9288#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009289static struct nfs_seqid *
9290nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9291{
9292 return NULL;
9293}
9294
Trond Myklebust97dc1352010-06-16 09:52:26 -04009295static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9296 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009297 .init_caps = NFS_CAP_READDIRPLUS
9298 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009299 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009300 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009301 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009302 .init_client = nfs41_init_client,
9303 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009304 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009305 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009306 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009307 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009308 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009309 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009310 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009311 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9312 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9313 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009314 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009315};
9316#endif
9317
Steve Dickson42c2c422013-05-22 12:50:38 -04009318#if defined(CONFIG_NFS_V4_2)
9319static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9320 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009321 .init_caps = NFS_CAP_READDIRPLUS
9322 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009323 | NFS_CAP_POSIX_LOCK
9324 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009325 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009326 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009327 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009328 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009329 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009330 | NFS_CAP_LAYOUTSTATS
9331 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009332 .init_client = nfs41_init_client,
9333 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009334 .match_stateid = nfs41_match_stateid,
9335 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009336 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009337 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009338 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009339 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009340 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009341 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9342 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9343 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009344 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009345};
9346#endif
9347
Trond Myklebust97dc1352010-06-16 09:52:26 -04009348const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9349 [0] = &nfs_v4_0_minor_ops,
9350#if defined(CONFIG_NFS_V4_1)
9351 [1] = &nfs_v4_1_minor_ops,
9352#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009353#if defined(CONFIG_NFS_V4_2)
9354 [2] = &nfs_v4_2_minor_ops,
9355#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009356};
9357
Trond Myklebust13997822016-07-24 17:10:52 -04009358static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009359{
9360 ssize_t error, error2;
9361
9362 error = generic_listxattr(dentry, list, size);
9363 if (error < 0)
9364 return error;
9365 if (list) {
9366 list += error;
9367 size -= error;
9368 }
9369
9370 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9371 if (error2 < 0)
9372 return error2;
9373 return error + error2;
9374}
9375
Trond Myklebust17f26b12013-08-21 15:48:42 -04009376static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009377 .create = nfs_create,
9378 .lookup = nfs_lookup,
9379 .atomic_open = nfs_atomic_open,
9380 .link = nfs_link,
9381 .unlink = nfs_unlink,
9382 .symlink = nfs_symlink,
9383 .mkdir = nfs_mkdir,
9384 .rmdir = nfs_rmdir,
9385 .mknod = nfs_mknod,
9386 .rename = nfs_rename,
9387 .permission = nfs_permission,
9388 .getattr = nfs_getattr,
9389 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009390 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009391};
9392
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009393static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009394 .permission = nfs_permission,
9395 .getattr = nfs_getattr,
9396 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009397 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009398};
9399
David Howells509de812006-08-22 20:06:11 -04009400const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009401 .version = 4, /* protocol version */
9402 .dentry_ops = &nfs4_dentry_operations,
9403 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009404 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009405 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009406 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009407 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009408 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009409 .getattr = nfs4_proc_getattr,
9410 .setattr = nfs4_proc_setattr,
9411 .lookup = nfs4_proc_lookup,
9412 .access = nfs4_proc_access,
9413 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009414 .create = nfs4_proc_create,
9415 .remove = nfs4_proc_remove,
9416 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009417 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009418 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009419 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009420 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009421 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009422 .link = nfs4_proc_link,
9423 .symlink = nfs4_proc_symlink,
9424 .mkdir = nfs4_proc_mkdir,
9425 .rmdir = nfs4_proc_remove,
9426 .readdir = nfs4_proc_readdir,
9427 .mknod = nfs4_proc_mknod,
9428 .statfs = nfs4_proc_statfs,
9429 .fsinfo = nfs4_proc_fsinfo,
9430 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009431 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009433 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009435 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009437 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009439 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009440 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009442 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009443 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009444 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009445 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009446 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009447 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009448 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009449 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009450 .create_server = nfs4_create_server,
9451 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009452};
9453
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009454static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009455 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009456 .list = nfs4_xattr_list_nfs4_acl,
9457 .get = nfs4_xattr_get_nfs4_acl,
9458 .set = nfs4_xattr_set_nfs4_acl,
9459};
9460
9461const struct xattr_handler *nfs4_xattr_handlers[] = {
9462 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009463#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9464 &nfs4_xattr_nfs4_label_handler,
9465#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009466 NULL
9467};
9468
Linus Torvalds1da177e2005-04-16 15:20:36 -07009469/*
9470 * Local variables:
9471 * c-basic-offset: 8
9472 * End:
9473 */