blob: 221d97de0e2c2f2a88d0af5d569f9275b539de02 [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,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 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 Myklebust14516c32010-07-31 14:29:06 -0400819 default:
820 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 }
823out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500826out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500827 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500828retry_nowait:
829 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400830 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500831 task->tk_status = 0;
832 ret = 0;
833 }
834 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400835out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400836 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 goto out;
838 rpc_delay(task, NFS4_POLL_RETRY_MAX);
839 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400840}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400841
842int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
843{
844 if (!nfs41_sequence_process(task, res))
845 return 0;
846 if (res->sr_slot != NULL)
847 nfs41_sequence_free_slot(res);
848 return 1;
849
850}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500851EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400852
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400853static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
854{
855 if (res->sr_slot == NULL)
856 return 1;
857 if (res->sr_slot->table->session != NULL)
858 return nfs41_sequence_process(task, res);
859 return nfs40_sequence_done(task, res);
860}
861
862static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
863{
864 if (res->sr_slot != NULL) {
865 if (res->sr_slot->table->session != NULL)
866 nfs41_sequence_free_slot(res);
867 else
868 nfs40_sequence_free_slot(res);
869 }
870}
871
Peng Tao2c4b1312014-06-11 05:24:16 +0800872int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400873{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500874 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400875 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400876 if (!res->sr_slot->table->session)
877 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400878 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400879}
Peng Tao2c4b1312014-06-11 05:24:16 +0800880EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400881
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000882int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400883 struct nfs4_sequence_args *args,
884 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400885 struct rpc_task *task)
886{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400887 struct nfs4_slot *slot;
888 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400889
890 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400891 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400892 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400893 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400894
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400895 tbl = &session->fc_slot_table;
896
Trond Myklebust69d206b2012-11-22 13:21:02 -0500897 task->tk_timeout = 0;
898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400900 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400901 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500902 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500903 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400904 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400905 }
906
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500907 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500908 if (IS_ERR(slot)) {
909 /* If out of memory, try again in 1/4 second */
910 if (slot == ERR_PTR(-ENOMEM))
911 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400912 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400913 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400914 }
915 spin_unlock(&tbl->slot_tbl_lock);
916
Trond Myklebust0a014a42016-09-22 13:38:51 -0400917 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500918 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400919
Chuck Levere8d92382013-08-09 12:47:51 -0400920 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500921 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400922
Benny Halevydfb4f3092010-09-24 09:17:01 -0400923 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500924 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400925 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926 /*
927 * sr_status is only set in decode_sequence, and so will remain
928 * set to 1 if an rpc level failure occurs.
929 */
930 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400931 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400932out_success:
933 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400934 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400935out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400936 /* Privileged tasks are queued with top priority */
937 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400938 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
939 NULL, RPC_PRIORITY_PRIVILEGED);
940 else
941 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400942 spin_unlock(&tbl->slot_tbl_lock);
943 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400944}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000945EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400946
Chuck Lever5a580e02013-08-09 12:48:09 -0400947static int nfs4_setup_sequence(const struct nfs_server *server,
948 struct nfs4_sequence_args *args,
949 struct nfs4_sequence_res *res,
950 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400951{
Trond Myklebust035168a2010-06-16 09:52:26 -0400952 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400953 int ret = 0;
954
Chuck Lever3bd23842013-08-09 12:49:19 -0400955 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800956 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
957 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400958
Chuck Levere8d92382013-08-09 12:47:51 -0400959 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400960 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400961 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Trond Myklebust9d12b212012-01-17 22:04:25 -0500963 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400964
Andy Adamsonce5039c2009-04-01 09:22:13 -0400965 dprintk("<-- %s status=%d\n", __func__, ret);
966 return ret;
967}
968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
970{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400971 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400972 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973
Trond Myklebust035168a2010-06-16 09:52:26 -0400974 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
975
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400976 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977}
978
Andy Adamson69ab40c2009-04-01 09:22:19 -0400979static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
980{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400981 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400982
Trond Myklebust14516c32010-07-31 14:29:06 -0400983 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400984}
985
Trond Myklebust17280172012-03-11 13:11:00 -0400986static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400987 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400989};
990
Chuck Lever3bd23842013-08-09 12:49:19 -0400991#else /* !CONFIG_NFS_V4_1 */
992
Chuck Lever5a580e02013-08-09 12:48:09 -0400993static int nfs4_setup_sequence(const struct nfs_server *server,
994 struct nfs4_sequence_args *args,
995 struct nfs4_sequence_res *res,
996 struct rpc_task *task)
997{
Peng Taocb04ad22014-06-11 05:24:15 +0800998 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
999 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001000}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001001
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001002static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1003{
1004 return nfs40_sequence_done(task, res);
1005}
1006
1007static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1008{
1009 if (res->sr_slot != NULL)
1010 nfs40_sequence_free_slot(res);
1011}
1012
Peng Tao2c4b1312014-06-11 05:24:16 +08001013int nfs4_sequence_done(struct rpc_task *task,
1014 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001015{
Chuck Lever3bd23842013-08-09 12:49:19 -04001016 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001017}
Peng Tao2c4b1312014-06-11 05:24:16 +08001018EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001019
1020#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001021
Chuck Lever9915ea72013-08-09 12:48:27 -04001022static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1023{
1024 struct nfs4_call_sync_data *data = calldata;
1025 nfs4_setup_sequence(data->seq_server,
1026 data->seq_args, data->seq_res, task);
1027}
1028
1029static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1030{
1031 struct nfs4_call_sync_data *data = calldata;
1032 nfs4_sequence_done(task, data->seq_res);
1033}
1034
1035static const struct rpc_call_ops nfs40_call_sync_ops = {
1036 .rpc_call_prepare = nfs40_call_sync_prepare,
1037 .rpc_call_done = nfs40_call_sync_done,
1038};
1039
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001040static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001041 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001043 struct nfs4_sequence_args *args,
1044 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001045{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001046 int ret;
1047 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001048 struct nfs_client *clp = server->nfs_client;
1049 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 .seq_server = server,
1051 .seq_args = args,
1052 .seq_res = res,
1053 };
1054 struct rpc_task_setup task_setup = {
1055 .rpc_client = clnt,
1056 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001057 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001058 .callback_data = &data
1059 };
1060
1061 task = rpc_run_task(&task_setup);
1062 if (IS_ERR(task))
1063 ret = PTR_ERR(task);
1064 else {
1065 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001066 rpc_put_task(task);
1067 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001068 return ret;
1069}
1070
Bryan Schumaker7c513052011-03-24 17:12:24 +00001071int nfs4_call_sync(struct rpc_clnt *clnt,
1072 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001073 struct rpc_message *msg,
1074 struct nfs4_sequence_args *args,
1075 struct nfs4_sequence_res *res,
1076 int cache_reply)
1077{
Chuck Levera9c92d62013-08-09 12:48:18 -04001078 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001079 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1083{
1084 struct nfs_inode *nfsi = NFS_I(dir);
1085
1086 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001087 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001088 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001090 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001091 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001092 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 spin_unlock(&dir->i_lock);
1094}
1095
1096struct nfs4_opendata {
1097 struct kref kref;
1098 struct nfs_openargs o_arg;
1099 struct nfs_openres o_res;
1100 struct nfs_open_confirmargs c_arg;
1101 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001102 struct nfs4_string owner_name;
1103 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001104 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001106 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001108 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs4_state_owner *owner;
1110 struct nfs4_state *state;
1111 struct iattr attrs;
1112 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001113 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001114 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001115 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001116 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int cancelled;
1118};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001119
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001120static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1121 int err, struct nfs4_exception *exception)
1122{
1123 if (err != -EINVAL)
1124 return false;
1125 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1126 return false;
1127 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1128 exception->retry = 1;
1129 return true;
1130}
1131
Trond Myklebust6ae37332015-01-30 14:21:14 -05001132static u32
1133nfs4_map_atomic_open_share(struct nfs_server *server,
1134 fmode_t fmode, int openflags)
1135{
1136 u32 res = 0;
1137
1138 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1139 case FMODE_READ:
1140 res = NFS4_SHARE_ACCESS_READ;
1141 break;
1142 case FMODE_WRITE:
1143 res = NFS4_SHARE_ACCESS_WRITE;
1144 break;
1145 case FMODE_READ|FMODE_WRITE:
1146 res = NFS4_SHARE_ACCESS_BOTH;
1147 }
1148 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1149 goto out;
1150 /* Want no delegation if we're using O_DIRECT */
1151 if (openflags & O_DIRECT)
1152 res |= NFS4_SHARE_WANT_NO_DELEG;
1153out:
1154 return res;
1155}
1156
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001157static enum open_claim_type4
1158nfs4_map_atomic_open_claim(struct nfs_server *server,
1159 enum open_claim_type4 claim)
1160{
1161 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1162 return claim;
1163 switch (claim) {
1164 default:
1165 return claim;
1166 case NFS4_OPEN_CLAIM_FH:
1167 return NFS4_OPEN_CLAIM_NULL;
1168 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1169 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1170 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1171 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1172 }
1173}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176{
1177 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001178 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001179 p->o_res.seqid = p->o_arg.seqid;
1180 p->c_res.seqid = p->c_arg.seqid;
1181 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001182 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001184 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001185}
1186
Al Viro82a2c1b2011-06-22 18:30:55 -04001187static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001189 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001190 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001191 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001192 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193{
Al Viro82a2c1b2011-06-22 18:30:55 -04001194 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001195 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001196 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001197 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 struct nfs4_opendata *p;
1199
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001200 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001201 if (p == NULL)
1202 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001203
1204 p->f_label = nfs4_label_alloc(server, gfp_mask);
1205 if (IS_ERR(p->f_label))
1206 goto err_free_p;
1207
Kinglong Meea49c2692015-07-27 15:31:38 +08001208 p->a_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->a_label))
1210 goto err_free_f;
1211
Trond Myklebust63f5f792015-01-23 19:19:25 -05001212 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1213 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001214 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001215 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001216 nfs_sb_active(dentry->d_sb);
1217 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001218 p->dir = parent;
1219 p->owner = sp;
1220 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001221 p->o_arg.open_flags = flags;
1222 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001223 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001224 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1225 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001226 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1227 * will return permission denied for all bits until close */
1228 if (!(flags & O_EXCL)) {
1229 /* ask server to check for all possible rights as results
1230 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001231 switch (p->o_arg.claim) {
1232 default:
1233 break;
1234 case NFS4_OPEN_CLAIM_NULL:
1235 case NFS4_OPEN_CLAIM_FH:
1236 p->o_arg.access = NFS4_ACCESS_READ |
1237 NFS4_ACCESS_MODIFY |
1238 NFS4_ACCESS_EXTEND |
1239 NFS4_ACCESS_EXECUTE;
1240 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001241 }
David Howells7539bba2006-08-22 20:06:09 -04001242 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001243 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1244 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001245 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001247 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001248 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001249 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001250 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001251 case NFS4_OPEN_CLAIM_NULL:
1252 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1253 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1254 p->o_arg.fh = NFS_FH(dir);
1255 break;
1256 case NFS4_OPEN_CLAIM_PREVIOUS:
1257 case NFS4_OPEN_CLAIM_FH:
1258 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1259 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001260 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001261 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001262 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001263 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001264
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 p->o_arg.u.attrs = &p->attrs;
1266 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001267
1268 verf[0] = jiffies;
1269 verf[1] = current->pid;
1270 memcpy(p->o_arg.u.verifier.data, verf,
1271 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273 p->c_arg.fh = &p->o_res.fh;
1274 p->c_arg.stateid = &p->o_res.stateid;
1275 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001276 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001279
1280err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001281 nfs4_label_free(p->a_label);
1282err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001283 nfs4_label_free(p->f_label);
1284err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001285 kfree(p);
1286err:
1287 dput(parent);
1288 return NULL;
1289}
1290
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001291static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 struct nfs4_opendata *p = container_of(kref,
1294 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001295 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001296
1297 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001298 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001299 if (p->state != NULL)
1300 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001301 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001302
Kinglong Meea49c2692015-07-27 15:31:38 +08001303 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001304 nfs4_label_free(p->f_label);
1305
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001307 dput(p->dentry);
1308 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001309 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001310 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 kfree(p);
1312}
1313
1314static void nfs4_opendata_put(struct nfs4_opendata *p)
1315{
1316 if (p != NULL)
1317 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001318}
1319
Trond Myklebust06f814a2006-01-03 09:55:07 +01001320static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1321{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001322 int ret;
1323
Trond Myklebust06f814a2006-01-03 09:55:07 +01001324 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001325 return ret;
1326}
1327
Trond Myklebust24311f82015-09-20 10:50:17 -04001328static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1329 fmode_t fmode)
1330{
1331 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1332 case FMODE_READ|FMODE_WRITE:
1333 return state->n_rdwr != 0;
1334 case FMODE_WRITE:
1335 return state->n_wronly != 0;
1336 case FMODE_READ:
1337 return state->n_rdonly != 0;
1338 }
1339 WARN_ON_ONCE(1);
1340 return false;
1341}
1342
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001344{
1345 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346
Trond Myklebust536e43d2012-01-17 22:04:26 -05001347 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348 goto out;
1349 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001350 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001351 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1352 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353 break;
1354 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001355 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1356 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 break;
1358 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001359 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1360 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001362out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001363 return ret;
1364}
1365
Trond Myklebust2a606182015-08-19 22:30:00 -05001366static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1367 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001369 if (delegation == NULL)
1370 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001373 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1374 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001375 switch (claim) {
1376 case NFS4_OPEN_CLAIM_NULL:
1377 case NFS4_OPEN_CLAIM_FH:
1378 break;
1379 case NFS4_OPEN_CLAIM_PREVIOUS:
1380 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1381 break;
1382 default:
1383 return 0;
1384 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001385 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001386 return 1;
1387}
1388
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001389static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001390{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001391 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001392 case FMODE_WRITE:
1393 state->n_wronly++;
1394 break;
1395 case FMODE_READ:
1396 state->n_rdonly++;
1397 break;
1398 case FMODE_READ|FMODE_WRITE:
1399 state->n_rdwr++;
1400 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001402}
1403
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001404#ifdef CONFIG_NFS_V4_1
1405static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1406{
1407 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1408 return true;
1409 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1410 return true;
1411 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1412 return true;
1413 return false;
1414}
1415#endif /* CONFIG_NFS_V4_1 */
1416
Trond Myklebust4f14c192014-02-12 19:15:06 -05001417static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001418{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001419 struct nfs_client *clp = state->owner->so_server->nfs_client;
1420 bool need_recover = false;
1421
1422 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1423 need_recover = true;
1424 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1425 need_recover = true;
1426 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1427 need_recover = true;
1428 if (need_recover)
1429 nfs4_state_mark_reclaim_nograce(clp, state);
1430}
1431
Trond Myklebuste999e802014-02-10 18:20:47 -05001432static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001433 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001434{
1435 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001438 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001439 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001440 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001441 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001442 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1443 return true;
1444 return false;
1445}
1446
Trond Myklebustf95549c2015-01-23 18:06:09 -05001447static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1448{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001449 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1450 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451 if (state->n_wronly)
1452 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1453 if (state->n_rdonly)
1454 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1455 if (state->n_rdwr)
1456 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001457 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001458}
1459
Trond Myklebust226056c2014-02-11 10:41:07 -05001460static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1461 nfs4_stateid *stateid, fmode_t fmode)
1462{
1463 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1464 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1465 case FMODE_WRITE:
1466 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 break;
1468 case FMODE_READ:
1469 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1470 break;
1471 case 0:
1472 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1474 clear_bit(NFS_OPEN_STATE, &state->flags);
1475 }
1476 if (stateid == NULL)
1477 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001478 /* Handle OPEN+OPEN_DOWNGRADE races */
1479 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1480 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001481 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001482 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001483 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001484 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001485 nfs4_stateid_copy(&state->stateid, stateid);
1486 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001487}
1488
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001489static void nfs_clear_open_stateid(struct nfs4_state *state,
1490 nfs4_stateid *arg_stateid,
1491 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001492{
1493 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001494 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1495 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1496 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001497 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001498 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1499 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001500}
1501
Trond Myklebust1393d962016-09-22 13:39:13 -04001502static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1503 const nfs4_stateid *stateid, fmode_t fmode,
1504 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001505{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001506 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001507 case FMODE_READ:
1508 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1509 break;
1510 case FMODE_WRITE:
1511 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1512 break;
1513 case FMODE_READ|FMODE_WRITE:
1514 set_bit(NFS_O_RDWR_STATE, &state->flags);
1515 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001516 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001517 return;
1518 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1519 nfs4_stateid_copy(&state->stateid, stateid);
1520 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001521}
1522
Trond Myklebust1393d962016-09-22 13:39:13 -04001523static void __update_open_stateid(struct nfs4_state *state,
1524 const nfs4_stateid *open_stateid,
1525 const nfs4_stateid *deleg_stateid,
1526 fmode_t fmode,
1527 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001529 /*
1530 * Protect the call to nfs4_state_set_mode_locked and
1531 * serialise the stateid update
1532 */
Andrew Elble361cad32015-12-02 09:20:57 -05001533 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001534 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001535 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001536 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001537 set_bit(NFS_DELEGATED_STATE, &state->flags);
1538 }
1539 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001540 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001541 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001542 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001543 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Trond Myklebust1393d962016-09-22 13:39:13 -04001546static int update_open_stateid(struct nfs4_state *state,
1547 const nfs4_stateid *open_stateid,
1548 const nfs4_stateid *delegation,
1549 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001550{
Trond Myklebust1393d962016-09-22 13:39:13 -04001551 struct nfs_server *server = NFS_SERVER(state->inode);
1552 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001553 struct nfs_inode *nfsi = NFS_I(state->inode);
1554 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001555 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001556 int ret = 0;
1557
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001558 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001559
1560 rcu_read_lock();
1561 deleg_cur = rcu_dereference(nfsi->delegation);
1562 if (deleg_cur == NULL)
1563 goto no_delegation;
1564
1565 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001566 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001567 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001568 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001569 goto no_delegation_unlock;
1570
1571 if (delegation == NULL)
1572 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001573 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001574 goto no_delegation_unlock;
1575
Trond Myklebustb7391f42008-12-23 15:21:52 -05001576 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001577 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1578 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001579 ret = 1;
1580no_delegation_unlock:
1581 spin_unlock(&deleg_cur->lock);
1582no_delegation:
1583 rcu_read_unlock();
1584
1585 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001586 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001587 ret = 1;
1588 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001589 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001590 nfs4_schedule_state_manager(clp);
1591 if (freeme.type != 0)
1592 nfs4_test_and_free_stateid(server, &freeme,
1593 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001594
1595 return ret;
1596}
1597
Trond Myklebust39071e62015-01-24 15:07:56 -05001598static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1599 const nfs4_stateid *stateid)
1600{
1601 struct nfs4_state *state = lsp->ls_state;
1602 bool ret = false;
1603
1604 spin_lock(&state->state_lock);
1605 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1606 goto out_noupdate;
1607 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1608 goto out_noupdate;
1609 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1610 ret = true;
1611out_noupdate:
1612 spin_unlock(&state->state_lock);
1613 return ret;
1614}
Trond Myklebust34310432008-12-23 15:21:38 -05001615
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617{
1618 struct nfs_delegation *delegation;
1619
1620 rcu_read_lock();
1621 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001623 rcu_read_unlock();
1624 return;
1625 }
1626 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001627 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628}
1629
Trond Myklebust6ee41262007-07-08 14:11:36 -04001630static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631{
1632 struct nfs4_state *state = opendata->state;
1633 struct nfs_inode *nfsi = NFS_I(state->inode);
1634 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001635 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001636 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001637 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001638 nfs4_stateid stateid;
1639 int ret = -EAGAIN;
1640
Trond Myklebustaac00a82007-07-05 19:02:21 -04001641 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001642 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001643 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001644 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001645 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001646 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001647 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001648 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001649 rcu_read_lock();
1650 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001651 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001652 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001653 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001654 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001656 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001657 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001658 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001659 if (!opendata->is_recover) {
1660 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1661 if (ret != 0)
1662 goto out;
1663 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001664 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001665
1666 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001668 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001670out:
1671 return ERR_PTR(ret);
1672out_return_state:
1673 atomic_inc(&state->count);
1674 return state;
1675}
1676
Andy Adamsone23008e2012-10-02 21:07:32 -04001677static void
1678nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1679{
1680 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1681 struct nfs_delegation *delegation;
1682 int delegation_flags = 0;
1683
1684 rcu_read_lock();
1685 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1686 if (delegation)
1687 delegation_flags = delegation->flags;
1688 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001689 switch (data->o_arg.claim) {
1690 default:
1691 break;
1692 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1693 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001694 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1695 "returning a delegation for "
1696 "OPEN(CLAIM_DELEGATE_CUR)\n",
1697 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001698 return;
1699 }
1700 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001701 nfs_inode_set_delegation(state->inode,
1702 data->owner->so_cred,
1703 &data->o_res);
1704 else
1705 nfs_inode_reclaim_delegation(state->inode,
1706 data->owner->so_cred,
1707 &data->o_res);
1708}
1709
1710/*
1711 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1712 * and update the nfs4_state.
1713 */
1714static struct nfs4_state *
1715_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1716{
1717 struct inode *inode = data->state->inode;
1718 struct nfs4_state *state = data->state;
1719 int ret;
1720
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001721 if (!data->rpc_done) {
1722 if (data->rpc_status) {
1723 ret = data->rpc_status;
1724 goto err;
1725 }
1726 /* cached opens have already been processed */
1727 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001728 }
1729
Andy Adamsone23008e2012-10-02 21:07:32 -04001730 ret = nfs_refresh_inode(inode, &data->f_attr);
1731 if (ret)
1732 goto err;
1733
1734 if (data->o_res.delegation_type != 0)
1735 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001736update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001737 update_open_stateid(state, &data->o_res.stateid, NULL,
1738 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001739 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001740
1741 return state;
1742err:
1743 return ERR_PTR(ret);
1744
1745}
1746
1747static struct nfs4_state *
1748_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001749{
1750 struct inode *inode;
1751 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001752 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753
Trond Myklebustaac00a82007-07-05 19:02:21 -04001754 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001755 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001756 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001757 goto out;
1758 }
1759
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001761 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001762 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001763 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001765 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766 goto err;
1767 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001768 state = nfs4_get_open_state(inode, data->owner);
1769 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001770 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001771 if (data->o_res.delegation_type != 0)
1772 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001773 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001775 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001776out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001777 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001778 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001779err_put_inode:
1780 iput(inode);
1781err:
1782 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783}
1784
Andy Adamsone23008e2012-10-02 21:07:32 -04001785static struct nfs4_state *
1786nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1787{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001788 struct nfs4_state *ret;
1789
Andy Adamsone23008e2012-10-02 21:07:32 -04001790 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001791 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1792 else
1793 ret = _nfs4_opendata_to_nfs4_state(data);
1794 nfs4_sequence_free_slot(&data->o_res.seq_res);
1795 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001796}
1797
Trond Myklebust864472e2006-01-03 09:55:15 +01001798static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1799{
1800 struct nfs_inode *nfsi = NFS_I(state->inode);
1801 struct nfs_open_context *ctx;
1802
1803 spin_lock(&state->inode->i_lock);
1804 list_for_each_entry(ctx, &nfsi->open_files, list) {
1805 if (ctx->state != state)
1806 continue;
1807 get_nfs_open_context(ctx);
1808 spin_unlock(&state->inode->i_lock);
1809 return ctx;
1810 }
1811 spin_unlock(&state->inode->i_lock);
1812 return ERR_PTR(-ENOENT);
1813}
1814
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001815static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1816 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001817{
1818 struct nfs4_opendata *opendata;
1819
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001820 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001821 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001822 if (opendata == NULL)
1823 return ERR_PTR(-ENOMEM);
1824 opendata->state = state;
1825 atomic_inc(&state->count);
1826 return opendata;
1827}
1828
Trond Myklebust24311f82015-09-20 10:50:17 -04001829static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1830 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001831{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001832 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001833 int ret;
1834
Trond Myklebust24311f82015-09-20 10:50:17 -04001835 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001836 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001837 opendata->o_arg.open_flags = 0;
1838 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001839 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1840 NFS_SB(opendata->dentry->d_sb),
1841 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001842 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1843 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1844 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001845 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001846 if (ret != 0)
1847 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001848 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001849 if (IS_ERR(newstate))
1850 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001851 if (newstate != opendata->state)
1852 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001853 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001854 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001855}
1856
1857static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1858{
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 int ret;
1860
Trond Myklebust4f14c192014-02-12 19:15:06 -05001861 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1862 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1863 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1864 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001865 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001866 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001867 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001868 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001869 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1870 if (ret != 0)
1871 return ret;
1872 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1873 if (ret != 0)
1874 return ret;
1875 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1876 if (ret != 0)
1877 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001878 /*
1879 * We may have performed cached opens for all three recoveries.
1880 * Check if we need to update the current stateid.
1881 */
1882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001883 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001884 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001885 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001886 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001887 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001888 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001889 return 0;
1890}
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892/*
1893 * OPEN_RECLAIM:
1894 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001896static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001898 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001899 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001900 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int status;
1902
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001903 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1904 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001905 if (IS_ERR(opendata))
1906 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001907 rcu_read_lock();
1908 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001909 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001910 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001911 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001912 opendata->o_arg.u.delegation_type = delegation_type;
1913 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001914 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return status;
1916}
1917
Trond Myklebust539cd032007-06-05 11:46:42 -04001918static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
1920 struct nfs_server *server = NFS_SERVER(state->inode);
1921 struct nfs4_exception exception = { };
1922 int err;
1923 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001924 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001925 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001926 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1927 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001928 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001929 break;
1930 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 } while (exception.retry);
1932 return err;
1933}
1934
Trond Myklebust864472e2006-01-03 09:55:15 +01001935static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1936{
1937 struct nfs_open_context *ctx;
1938 int ret;
1939
1940 ctx = nfs4_state_find_open_context(state);
1941 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001942 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001943 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 put_nfs_open_context(ctx);
1945 return ret;
1946}
1947
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001948static 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 -07001949{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001950 switch (err) {
1951 default:
1952 printk(KERN_ERR "NFS: %s: unhandled error "
1953 "%d.\n", __func__, err);
1954 case 0:
1955 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001956 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001957 case -ESTALE:
1958 break;
1959 case -NFS4ERR_BADSESSION:
1960 case -NFS4ERR_BADSLOT:
1961 case -NFS4ERR_BAD_HIGH_SLOT:
1962 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1963 case -NFS4ERR_DEADSESSION:
1964 set_bit(NFS_DELEGATED_STATE, &state->flags);
1965 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1966 return -EAGAIN;
1967 case -NFS4ERR_STALE_CLIENTID:
1968 case -NFS4ERR_STALE_STATEID:
1969 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001970 /* Don't recall a delegation if it was lost */
1971 nfs4_schedule_lease_recovery(server->nfs_client);
1972 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001973 case -NFS4ERR_MOVED:
1974 nfs4_schedule_migration_recovery(server);
1975 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001976 case -NFS4ERR_LEASE_MOVED:
1977 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1978 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001979 case -NFS4ERR_DELEG_REVOKED:
1980 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001981 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001983 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001984 nfs_inode_find_state_and_recover(state->inode,
1985 stateid);
1986 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001987 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001988 case -NFS4ERR_DELAY:
1989 case -NFS4ERR_GRACE:
1990 set_bit(NFS_DELEGATED_STATE, &state->flags);
1991 ssleep(1);
1992 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001993 case -ENOMEM:
1994 case -NFS4ERR_DENIED:
1995 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1996 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return err;
1999}
2000
Trond Myklebust24311f82015-09-20 10:50:17 -04002001int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2002 struct nfs4_state *state, const nfs4_stateid *stateid,
2003 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002004{
2005 struct nfs_server *server = NFS_SERVER(state->inode);
2006 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002007 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002008
2009 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2010 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2011 if (IS_ERR(opendata))
2012 return PTR_ERR(opendata);
2013 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002014 write_seqlock(&state->seqlock);
2015 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2016 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002017 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2018 switch (type & (FMODE_READ|FMODE_WRITE)) {
2019 case FMODE_READ|FMODE_WRITE:
2020 case FMODE_WRITE:
2021 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2022 if (err)
2023 break;
2024 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2025 if (err)
2026 break;
2027 case FMODE_READ:
2028 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2029 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002030 nfs4_opendata_put(opendata);
2031 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2032}
2033
Chuck Leverbe05c862013-08-09 12:49:47 -04002034static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2035{
2036 struct nfs4_opendata *data = calldata;
2037
Peng Taocb04ad22014-06-11 05:24:15 +08002038 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2039 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002040}
2041
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2043{
2044 struct nfs4_opendata *data = calldata;
2045
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002046 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002047
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002048 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002049 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002050 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002051 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002052 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002053 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002055}
2056
2057static void nfs4_open_confirm_release(void *calldata)
2058{
2059 struct nfs4_opendata *data = calldata;
2060 struct nfs4_state *state = NULL;
2061
2062 /* If this request hasn't been cancelled, do nothing */
2063 if (data->cancelled == 0)
2064 goto out_free;
2065 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002066 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002068 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002069 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002070 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002072 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073}
2074
2075static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002076 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002077 .rpc_call_done = nfs4_open_confirm_done,
2078 .rpc_release = nfs4_open_confirm_release,
2079};
2080
2081/*
2082 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2083 */
2084static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2085{
David Howells2b0143b2015-03-17 22:25:59 +00002086 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002087 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002088 struct rpc_message msg = {
2089 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2090 .rpc_argp = &data->c_arg,
2091 .rpc_resp = &data->c_res,
2092 .rpc_cred = data->owner->so_cred,
2093 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002094 struct rpc_task_setup task_setup_data = {
2095 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002096 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002097 .callback_ops = &nfs4_open_confirm_ops,
2098 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002099 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002100 .flags = RPC_TASK_ASYNC,
2101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 int status;
2103
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002104 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002105 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002106 data->rpc_done = 0;
2107 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002108 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002109 if (data->is_recover)
2110 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002111 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002112 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002113 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002114 status = nfs4_wait_for_completion_rpc_task(task);
2115 if (status != 0) {
2116 data->cancelled = 1;
2117 smp_wmb();
2118 } else
2119 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002120 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return status;
2122}
2123
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002124static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002126 struct nfs4_opendata *data = calldata;
2127 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002128 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002129 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002130
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002132 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002133 /*
2134 * Check if we still need to send an OPEN call, or if we can use
2135 * a delegation instead.
2136 */
2137 if (data->state != NULL) {
2138 struct nfs_delegation *delegation;
2139
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002140 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002141 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002142 rcu_read_lock();
2143 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002144 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002145 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 rcu_read_unlock();
2147 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002148 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002149 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002150 switch (claim) {
2151 default:
2152 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002153 case NFS4_OPEN_CLAIM_PREVIOUS:
2154 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2155 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002156 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002157 case NFS4_OPEN_CLAIM_FH:
2158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002159 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2160 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002161 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002162 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002163 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002164 &data->o_res.seq_res,
2165 task) != 0)
2166 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002167
2168 /* Set the create mode (note dependency on the session type) */
2169 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2170 if (data->o_arg.open_flags & O_EXCL) {
2171 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2172 if (nfs4_has_persistent_session(clp))
2173 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2174 else if (clp->cl_mvops->minor_version > 0)
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2176 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002177 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002178unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002179 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002180 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002181out_no_action:
2182 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002183out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002184 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002185}
2186
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002187static void nfs4_open_done(struct rpc_task *task, void *calldata)
2188{
2189 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002192
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002193 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002194 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002195
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002196 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002197 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2198 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002199 case S_IFREG:
2200 break;
2201 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 break;
2204 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002206 break;
2207 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002209 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002211 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002212 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2213 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002215 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002216}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002217
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002218static void nfs4_open_release(void *calldata)
2219{
2220 struct nfs4_opendata *data = calldata;
2221 struct nfs4_state *state = NULL;
2222
2223 /* If this request hasn't been cancelled, do nothing */
2224 if (data->cancelled == 0)
2225 goto out_free;
2226 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002227 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002228 goto out_free;
2229 /* In case we need an open_confirm, no cleanup! */
2230 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2231 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002233 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002234 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002235out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002236 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237}
2238
2239static const struct rpc_call_ops nfs4_open_ops = {
2240 .rpc_call_prepare = nfs4_open_prepare,
2241 .rpc_call_done = nfs4_open_done,
2242 .rpc_release = nfs4_open_release,
2243};
2244
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002245static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002246{
David Howells2b0143b2015-03-17 22:25:59 +00002247 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248 struct nfs_server *server = NFS_SERVER(dir);
2249 struct nfs_openargs *o_arg = &data->o_arg;
2250 struct nfs_openres *o_res = &data->o_res;
2251 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2254 .rpc_argp = o_arg,
2255 .rpc_resp = o_res,
2256 .rpc_cred = data->owner->so_cred,
2257 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 struct rpc_task_setup task_setup_data = {
2259 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002260 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002261 .callback_ops = &nfs4_open_ops,
2262 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002263 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002264 .flags = RPC_TASK_ASYNC,
2265 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266 int status;
2267
Chuck Levera9c92d62013-08-09 12:48:18 -04002268 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002269 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002270 data->rpc_done = 0;
2271 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002272 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002273 data->is_recover = 0;
2274 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002275 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002276 data->is_recover = 1;
2277 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002278 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002279 if (IS_ERR(task))
2280 return PTR_ERR(task);
2281 status = nfs4_wait_for_completion_rpc_task(task);
2282 if (status != 0) {
2283 data->cancelled = 1;
2284 smp_wmb();
2285 } else
2286 status = data->rpc_status;
2287 rpc_put_task(task);
2288
2289 return status;
2290}
2291
2292static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2293{
David Howells2b0143b2015-03-17 22:25:59 +00002294 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002295 struct nfs_openres *o_res = &data->o_res;
2296 int status;
2297
2298 status = nfs4_run_open_task(data, 1);
2299 if (status != 0 || !data->rpc_done)
2300 return status;
2301
Trond Myklebust6926afd2012-01-07 13:22:46 -05002302 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2303
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002304 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2305 status = _nfs4_proc_open_confirm(data);
2306 if (status != 0)
2307 return status;
2308 }
2309
2310 return status;
2311}
2312
Trond Myklebustf3792d62014-07-10 08:54:32 -04002313/*
2314 * Additional permission checks in order to distinguish between an
2315 * open for read, and an open for execute. This works around the
2316 * fact that NFSv4 OPEN treats read and execute permissions as being
2317 * the same.
2318 * Note that in the non-execute case, we want to turn off permission
2319 * checking if we just created a new file (POSIX open() semantics).
2320 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002321static int nfs4_opendata_access(struct rpc_cred *cred,
2322 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002323 struct nfs4_state *state, fmode_t fmode,
2324 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325{
2326 struct nfs_access_entry cache;
2327 u32 mask;
2328
2329 /* access call failed or for some reason the server doesn't
2330 * support any access modes -- defer access call until later */
2331 if (opendata->o_res.access_supported == 0)
2332 return 0;
2333
2334 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002335 /*
2336 * Use openflags to check for exec, because fmode won't
2337 * always have FMODE_EXEC set when file open for exec.
2338 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002339 if (openflags & __FMODE_EXEC) {
2340 /* ONLY check for exec rights */
2341 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002342 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002343 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002344
2345 cache.cred = cred;
2346 cache.jiffies = jiffies;
2347 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2348 nfs_access_add_cache(state->inode, &cache);
2349
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002350 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002351 return 0;
2352
2353 /* even though OPEN succeeded, access is denied. Close the file */
2354 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002355 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356}
2357
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358/*
2359 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2360 */
2361static int _nfs4_proc_open(struct nfs4_opendata *data)
2362{
David Howells2b0143b2015-03-17 22:25:59 +00002363 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002364 struct nfs_server *server = NFS_SERVER(dir);
2365 struct nfs_openargs *o_arg = &data->o_arg;
2366 struct nfs_openres *o_res = &data->o_res;
2367 int status;
2368
2369 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002370 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002371 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002372 if (status != 0) {
2373 if (status == -NFS4ERR_BADNAME &&
2374 !(o_arg->open_flags & O_CREAT))
2375 return -ENOENT;
2376 return status;
2377 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002378
Trond Myklebust6926afd2012-01-07 13:22:46 -05002379 nfs_fattr_map_and_free_names(server, &data->f_attr);
2380
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002381 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002382 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002383 if (o_arg->open_flags & O_EXCL)
2384 data->file_created = 1;
2385 else if (o_res->cinfo.before != o_res->cinfo.after)
2386 data->file_created = 1;
2387 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002388 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2389 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002391 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002396 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398}
2399
Andy Adamsond83217c2011-03-01 01:34:17 +00002400static int nfs4_recover_expired_lease(struct nfs_server *server)
2401{
2402 return nfs4_client_recover_expired_lease(server->nfs_client);
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405/*
2406 * OPEN_EXPIRED:
2407 * reclaim state on the server after a network partition.
2408 * Assumes caller holds the appropriate lock
2409 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002410static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002412 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002413 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002415 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002416 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002417 if (IS_ERR(opendata))
2418 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002419 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002420 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002421 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002422 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002426static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002427{
Trond Myklebust539cd032007-06-05 11:46:42 -04002428 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002429 struct nfs4_exception exception = { };
2430 int err;
2431
2432 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002433 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002434 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002435 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2436 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002437 switch (err) {
2438 default:
2439 goto out;
2440 case -NFS4ERR_GRACE:
2441 case -NFS4ERR_DELAY:
2442 nfs4_handle_exception(server, err, &exception);
2443 err = 0;
2444 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002445 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002446out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002447 return err;
2448}
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002453 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 ctx = nfs4_state_find_open_context(state);
2456 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002457 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002458 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002459 put_nfs_open_context(ctx);
2460 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461}
2462
Trond Myklebust41020b62016-09-22 13:38:58 -04002463static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2464 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002465{
Trond Myklebust41020b62016-09-22 13:38:58 -04002466 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002467 write_seqlock(&state->seqlock);
2468 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2469 write_sequnlock(&state->seqlock);
2470 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2471}
2472
2473static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2474{
2475 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002476 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002477}
2478
2479static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2480{
2481 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2482 nfs40_clear_delegation_stateid(state);
2483 return nfs4_open_expired(sp, state);
2484}
2485
Trond Myklebust45870d62016-09-22 13:38:59 -04002486static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2487 nfs4_stateid *stateid,
2488 struct rpc_cred *cred)
2489{
2490 return -NFS4ERR_BAD_STATEID;
2491}
2492
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002493#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002494static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2495 nfs4_stateid *stateid,
2496 struct rpc_cred *cred)
2497{
2498 int status;
2499
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002500 switch (stateid->type) {
2501 default:
2502 break;
2503 case NFS4_INVALID_STATEID_TYPE:
2504 case NFS4_SPECIAL_STATEID_TYPE:
2505 return -NFS4ERR_BAD_STATEID;
2506 case NFS4_REVOKED_STATEID_TYPE:
2507 goto out_free;
2508 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002509
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002510 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002511 switch (status) {
2512 case -NFS4ERR_EXPIRED:
2513 case -NFS4ERR_ADMIN_REVOKED:
2514 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 break;
2516 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002517 return status;
2518 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002519out_free:
2520 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002521 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002522 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002523}
2524
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002525static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002526{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002527 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002528 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002529 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530 struct rpc_cred *cred;
2531 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002532
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 /* Get the delegation credential for use by test/free_stateid */
2534 rcu_read_lock();
2535 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002536 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002538 return;
2539 }
2540
2541 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002542 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2543 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002544 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002545 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002546 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002547
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002548 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2549 rcu_read_unlock();
2550 return;
2551 }
2552
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002553 cred = get_rpccred(delegation->cred);
2554 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002555 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002556 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002557 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002558 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002559
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002560 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002561}
2562
2563/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002564 * nfs41_check_expired_locks - possibly free a lock stateid
2565 *
2566 * @state: NFSv4 state for an inode
2567 *
2568 * Returns NFS_OK if recovery for this stateid is now finished.
2569 * Otherwise a negative NFS4ERR value is returned.
2570 */
2571static int nfs41_check_expired_locks(struct nfs4_state *state)
2572{
2573 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002574 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002575 struct nfs_server *server = NFS_SERVER(state->inode);
2576
2577 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2578 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002579
2580 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002581 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2582 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2583 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2584
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002585 atomic_inc(&lsp->ls_count);
2586 spin_unlock(&state->state_lock);
2587
2588 nfs4_put_lock_state(prev);
2589 prev = lsp;
2590
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002591 status = nfs41_test_and_free_expired_stateid(server,
2592 &lsp->ls_stateid,
2593 cred);
2594 trace_nfs4_test_lock_stateid(state, lsp, status);
2595 if (status == -NFS4ERR_EXPIRED ||
2596 status == -NFS4ERR_BAD_STATEID) {
2597 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002598 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002599 if (!recover_lost_locks)
2600 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2601 } else if (status != NFS_OK) {
2602 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002603 nfs4_put_lock_state(prev);
2604 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002605 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002606 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002607 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002608 }
2609 spin_unlock(&state->state_lock);
2610 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611out:
2612 return ret;
2613}
2614
2615/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002616 * nfs41_check_open_stateid - possibly free an open stateid
2617 *
2618 * @state: NFSv4 state for an inode
2619 *
2620 * Returns NFS_OK if recovery for this stateid is now finished.
2621 * Otherwise a negative NFS4ERR value is returned.
2622 */
2623static int nfs41_check_open_stateid(struct nfs4_state *state)
2624{
2625 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002626 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002627 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002628 int status;
2629
Trond Myklebustb134fc42016-09-22 13:39:16 -04002630 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002631 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2632 if (nfs4_have_delegation(state->inode, state->state))
2633 return NFS_OK;
2634 return -NFS4ERR_OPENMODE;
2635 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002636 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002637 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002638 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002639 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002640 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002641 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2642 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2643 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002644 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002645 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002646 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002647 if (status != NFS_OK)
2648 return status;
2649 if (nfs_open_stateid_recover_openmode(state))
2650 return -NFS4ERR_OPENMODE;
2651 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002652}
2653
2654static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2655{
Chuck Levereb64cf92012-07-11 16:30:05 -04002656 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002657
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002658 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002659 status = nfs41_check_expired_locks(state);
2660 if (status != NFS_OK)
2661 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002662 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002663 if (status != NFS_OK)
2664 status = nfs4_open_expired(sp, state);
2665 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002666}
2667#endif
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002670 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2671 * fields corresponding to attributes that were used to store the verifier.
2672 * Make sure we clobber those fields in the later setattr call
2673 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002674static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2675 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002676{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002677 const u32 *attrset = opendata->o_res.attrset;
2678
2679 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680 !(sattr->ia_valid & ATTR_ATIME_SET))
2681 sattr->ia_valid |= ATTR_ATIME;
2682
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002683 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684 !(sattr->ia_valid & ATTR_MTIME_SET))
2685 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686
2687 /* Except MODE, it seems harmless of setting twice. */
2688 if ((attrset[1] & FATTR4_WORD1_MODE))
2689 sattr->ia_valid &= ~ATTR_MODE;
2690
2691 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2692 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002693}
2694
Trond Myklebustc21443c2013-02-07 14:26:21 -05002695static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2696 fmode_t fmode,
2697 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002698 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002699{
2700 struct nfs4_state_owner *sp = opendata->owner;
2701 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002702 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002703 struct nfs4_state *state;
2704 unsigned int seq;
2705 int ret;
2706
2707 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2708
2709 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002710 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002711 goto out;
2712
2713 state = nfs4_opendata_to_nfs4_state(opendata);
2714 ret = PTR_ERR(state);
2715 if (IS_ERR(state))
2716 goto out;
2717 if (server->caps & NFS_CAP_POSIX_LOCK)
2718 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002719 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2720 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002721
Trond Myklebust275bb302013-05-29 13:11:28 -04002722 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002723 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002724 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002725 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002726 alias = d_exact_alias(dentry, state->inode);
2727 if (!alias)
2728 alias = d_splice_alias(igrab(state->inode), dentry);
2729 /* d_splice_alias() can't fail here - it's a non-directory */
2730 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002731 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002732 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002733 }
2734 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002735 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 }
2737
Trond Myklebustc21443c2013-02-07 14:26:21 -05002738 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2739 if (ret != 0)
2740 goto out;
2741
Trond Myklebust3efb9722013-05-29 13:17:04 -04002742 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002743 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002744 nfs_inode_attach_open_context(ctx);
2745 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2746 nfs4_schedule_stateid_recovery(server, state);
2747 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002748out:
2749 return ret;
2750}
2751
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002752/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002753 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 */
Andy Adamson82be4172012-05-23 05:02:35 -04002755static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002756 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002757 int flags,
2758 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002759 struct nfs4_label *label,
2760 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
2762 struct nfs4_state_owner *sp;
2763 struct nfs4_state *state = NULL;
2764 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002765 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002766 struct dentry *dentry = ctx->dentry;
2767 struct rpc_cred *cred = ctx->cred;
2768 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2769 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002770 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002771 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002772 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002776 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2777 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2779 goto out_err;
2780 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002781 status = nfs4_recover_expired_lease(server);
2782 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002783 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002784 if (d_really_is_positive(dentry))
2785 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002786 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002787 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002788 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002789 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002790 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002791 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002792 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
David Quigley14c43f72013-05-22 12:50:43 -04002794 if (label) {
2795 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2796 if (IS_ERR(olabel)) {
2797 status = PTR_ERR(olabel);
2798 goto err_opendata_put;
2799 }
2800 }
2801
Trond Myklebuste911b812014-03-26 13:24:37 -07002802 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2803 if (!opendata->f_attr.mdsthreshold) {
2804 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2805 if (!opendata->f_attr.mdsthreshold)
2806 goto err_free_label;
2807 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002808 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002809 }
David Howells2b0143b2015-03-17 22:25:59 +00002810 if (d_really_is_positive(dentry))
2811 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002812
Trond Myklebust3efb9722013-05-29 13:17:04 -04002813 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002814 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002815 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002816 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002817
NeilBrownefcbc042015-07-30 13:00:56 +10002818 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002819 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002820 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002821 /*
2822 * send create attributes which was not set by open
2823 * with an extra setattr.
2824 */
2825 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2826 nfs_fattr_init(opendata->o_res.f_attr);
2827 status = nfs4_do_setattr(state->inode, cred,
2828 opendata->o_res.f_attr, sattr,
2829 state, label, olabel);
2830 if (status == 0) {
2831 nfs_setattr_update_inode(state->inode, sattr,
2832 opendata->o_res.f_attr);
2833 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2834 }
David Quigley1775fd32013-05-22 12:50:42 -04002835 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002836 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002837 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002838 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002839
Trond Myklebuste911b812014-03-26 13:24:37 -07002840 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002841 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002842 opendata->f_attr.mdsthreshold = NULL;
2843 }
Andy Adamson82be4172012-05-23 05:02:35 -04002844
David Quigley14c43f72013-05-22 12:50:43 -04002845 nfs4_label_free(olabel);
2846
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002847 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002850err_free_label:
2851 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002852err_opendata_put:
2853 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002854err_put_state_owner:
2855 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return status;
2858}
2859
2860
Andy Adamson82be4172012-05-23 05:02:35 -04002861static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002862 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002863 int flags,
2864 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002865 struct nfs4_label *label,
2866 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002868 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 struct nfs4_exception exception = { };
2870 struct nfs4_state *res;
2871 int status;
2872
2873 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002874 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002875 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002876 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (status == 0)
2878 break;
2879 /* NOTE: BAD_SEQID means the server and client disagree about the
2880 * book-keeping w.r.t. state-changing operations
2881 * (OPEN/CLOSE/LOCK/LOCKU...)
2882 * It is actually a sign of a bug on the client or on the server.
2883 *
2884 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002885 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 * have unhashed the old state_owner for us, and that we can
2887 * therefore safely retry using a new one. We should still warn
2888 * the user though...
2889 */
2890 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002891 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002892 " returned a bad sequence-id error!\n",
2893 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 exception.retry = 1;
2895 continue;
2896 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002897 /*
2898 * BAD_STATEID on OPEN means that the server cancelled our
2899 * state before it received the OPEN_CONFIRM.
2900 * Recover by retrying the request as per the discussion
2901 * on Page 181 of RFC3530.
2902 */
2903 if (status == -NFS4ERR_BAD_STATEID) {
2904 exception.retry = 1;
2905 continue;
2906 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002907 if (status == -EAGAIN) {
2908 /* We must have found a delegation */
2909 exception.retry = 1;
2910 continue;
2911 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002912 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2913 continue;
2914 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 status, &exception));
2916 } while (exception.retry);
2917 return res;
2918}
2919
Trond Myklebust8487c472016-06-26 08:44:35 -04002920static int _nfs4_do_setattr(struct inode *inode,
2921 struct nfs_setattrargs *arg,
2922 struct nfs_setattrres *res,
2923 struct rpc_cred *cred,
2924 struct nfs4_state *state)
2925{
2926 struct nfs_server *server = NFS_SERVER(inode);
2927 struct rpc_message msg = {
2928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2929 .rpc_argp = arg,
2930 .rpc_resp = res,
2931 .rpc_cred = cred,
2932 };
2933 struct rpc_cred *delegation_cred = NULL;
2934 unsigned long timestamp = jiffies;
2935 fmode_t fmode;
2936 bool truncate;
2937 int status;
2938
2939 nfs_fattr_init(res->fattr);
2940
2941 /* Servers should only apply open mode checks for file size changes */
2942 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2943 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2944
2945 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2946 /* Use that stateid */
2947 } else if (truncate && state != NULL) {
2948 struct nfs_lockowner lockowner = {
2949 .l_owner = current->files,
2950 .l_pid = current->tgid,
2951 };
2952 if (!nfs4_valid_open_stateid(state))
2953 return -EBADF;
2954 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2955 &arg->stateid, &delegation_cred) == -EIO)
2956 return -EBADF;
2957 } else
2958 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2959 if (delegation_cred)
2960 msg.rpc_cred = delegation_cred;
2961
2962 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2963
2964 put_rpccred(delegation_cred);
2965 if (status == 0 && state != NULL)
2966 renew_lease(server, timestamp);
2967 trace_nfs4_setattr(inode, &arg->stateid, status);
2968 return status;
2969}
2970
2971static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2972 struct nfs_fattr *fattr, struct iattr *sattr,
2973 struct nfs4_state *state, struct nfs4_label *ilabel,
2974 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002976 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002978 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 .iap = sattr,
2980 .server = server,
2981 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002982 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
2984 struct nfs_setattrres res = {
2985 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002986 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .server = server,
2988 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002989 struct nfs4_exception exception = {
2990 .state = state,
2991 .inode = inode,
2992 .stateid = &arg.stateid,
2993 };
2994 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
David Quigleyaa9c2662013-05-22 12:50:44 -04002996 arg.bitmask = nfs4_bitmask(server, ilabel);
2997 if (ilabel)
2998 arg.bitmask = nfs4_bitmask(server, olabel);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 do {
Trond Myklebust8487c472016-06-26 08:44:35 -04003001 err = _nfs4_do_setattr(inode, &arg, &res, cred, state);
Trond Myklebust451146b2012-04-18 16:29:11 -04003002 switch (err) {
3003 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003004 if (!(sattr->ia_valid & ATTR_SIZE)) {
3005 pr_warn_once("NFSv4: server %s is incorrectly "
3006 "applying open mode checks to "
3007 "a SETATTR that is not "
3008 "changing file size.\n",
3009 server->nfs_client->cl_hostname);
3010 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003011 if (state && !(state->state & FMODE_WRITE)) {
3012 err = -EBADF;
3013 if (sattr->ia_valid & ATTR_OPEN)
3014 err = -EACCES;
3015 goto out;
3016 }
3017 }
3018 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003020out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return err;
3022}
3023
Peng Tao500d7012015-09-22 11:35:22 +08003024static bool
3025nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3026{
3027 if (inode == NULL || !nfs_have_layout(inode))
3028 return false;
3029
3030 return pnfs_wait_on_layoutreturn(inode, task);
3031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033struct nfs4_closedata {
3034 struct inode *inode;
3035 struct nfs4_state *state;
3036 struct nfs_closeargs arg;
3037 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003038 struct {
3039 struct nfs4_layoutreturn_args arg;
3040 struct nfs4_layoutreturn_res res;
3041 u32 roc_barrier;
3042 bool roc;
3043 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003044 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003045 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046};
3047
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003048static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003049{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003050 struct nfs4_closedata *calldata = data;
3051 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003052 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003053
Trond Myklebustcf805162016-11-15 14:56:07 -05003054 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003055 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3056 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003057 nfs4_put_open_state(calldata->state);
3058 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003059 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003060 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003061 kfree(calldata);
3062}
3063
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003064static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003066 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003069 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Chuck Levera3ca5652012-03-01 17:00:40 -05003071 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003072 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3073 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003074 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003075
3076 /* Handle Layoutreturn errors */
3077 if (calldata->arg.lr_args && task->tk_status != 0) {
3078 switch (calldata->res.lr_ret) {
3079 default:
3080 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3081 break;
3082 case 0:
3083 calldata->arg.lr_args = NULL;
3084 calldata->res.lr_res = NULL;
3085 break;
3086 case -NFS4ERR_ADMIN_REVOKED:
3087 case -NFS4ERR_DELEG_REVOKED:
3088 case -NFS4ERR_EXPIRED:
3089 case -NFS4ERR_BAD_STATEID:
3090 case -NFS4ERR_OLD_STATEID:
3091 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3092 case -NFS4ERR_WRONG_CRED:
3093 calldata->arg.lr_args = NULL;
3094 calldata->res.lr_res = NULL;
3095 calldata->res.lr_ret = 0;
3096 rpc_restart_call_prepare(task);
3097 return;
3098 }
3099 }
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 /* hmm. we are done with the inode, and in the process of freeing
3102 * the state_owner. we keep this around to process errors
3103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 switch (task->tk_status) {
3105 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003106 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003107 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003108 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003109 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003111 case -NFS4ERR_EXPIRED:
3112 nfs4_free_revoked_stateid(server,
3113 &calldata->arg.stateid,
3114 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003115 case -NFS4ERR_OLD_STATEID:
3116 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003117 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003118 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003119 rpc_restart_call_prepare(task);
3120 goto out_release;
3121 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003122 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003125 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003126 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003127 goto out_release;
3128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003130 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3131 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003132out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003133 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003134 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003135 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003138static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003140 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003141 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003142 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003143 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003144 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003145
Chuck Levera3ca5652012-03-01 17:00:40 -05003146 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003147 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003148 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003149
Trond Myklebust88069f72009-12-08 08:33:16 -05003150 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003151 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003152 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3153 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3154 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003155 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003156 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003157 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003158 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003159 if (state->n_rdonly == 0)
3160 call_close |= is_rdonly;
3161 else if (is_rdonly)
3162 calldata->arg.fmode |= FMODE_READ;
3163 if (state->n_wronly == 0)
3164 call_close |= is_wronly;
3165 else if (is_wronly)
3166 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003167 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3168 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003169 } else if (is_rdwr)
3170 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3171
Trond Myklebust5cc78612016-11-14 11:19:56 -05003172 if (!nfs4_valid_open_stateid(state) ||
3173 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003174 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003175 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003176
3177 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003178 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003179 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003180 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003181
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003182 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003183 nfs_release_seqid(calldata->arg.seqid);
3184 goto out_wait;
3185 }
3186
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003187 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05003188 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003189
3190 /* Close-to-open cache consistency revalidation */
3191 if (!nfs4_have_delegation(inode, FMODE_READ))
3192 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3193 else
3194 calldata->arg.bitmask = NULL;
3195 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003196
Trond Myklebust6ae37332015-01-30 14:21:14 -05003197 calldata->arg.share_access =
3198 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3199 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003200
Trond Myklebust516a6af2005-10-27 22:12:41 -04003201 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003202 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003203 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003204 &calldata->arg.seq_args,
3205 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003206 task) != 0)
3207 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003208 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003209 return;
3210out_no_action:
3211 task->tk_action = NULL;
3212out_wait:
3213 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003216static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003217 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003218 .rpc_call_done = nfs4_close_done,
3219 .rpc_release = nfs4_free_closedata,
3220};
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222/*
3223 * It is possible for data to be read/written from a mem-mapped file
3224 * after the sys_close call (which hits the vfs layer as a flush).
3225 * This means that we can't safely call nfsv4 close on a file until
3226 * the inode is cleared. This in turn means that we are not good
3227 * NFSv4 citizens - we do not indicate to the server to update the file's
3228 * share state even when we are done with one of the three share
3229 * stateid's in the inode.
3230 *
3231 * NOTE: Caller must be holding the sp->so_owner semaphore!
3232 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003233int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003235 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003236 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003238 struct nfs4_state_owner *sp = state->owner;
3239 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003240 struct rpc_message msg = {
3241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3242 .rpc_cred = state->owner->so_cred,
3243 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003244 struct rpc_task_setup task_setup_data = {
3245 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003246 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003247 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003248 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003249 .flags = RPC_TASK_ASYNC,
3250 };
Trond Myklebust95121352005-10-18 14:20:12 -07003251 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003253 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3254 &task_setup_data.rpc_client, &msg);
3255
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003256 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003258 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003259 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003260 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003262 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003264 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3265 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003266 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003267 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003268 calldata->arg.fmode = 0;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003269 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003270 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003271 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003272 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003273 calldata->lr.roc = pnfs_roc(state->inode,
3274 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3275 if (calldata->lr.roc) {
3276 calldata->arg.lr_args = &calldata->lr.arg;
3277 calldata->res.lr_res = &calldata->lr.res;
3278 }
Al Viro643168c2011-06-22 18:20:23 -04003279 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003280
Trond Myklebust1174dd12010-12-21 10:52:24 -05003281 msg.rpc_argp = &calldata->arg;
3282 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003283 task_setup_data.callback_data = calldata;
3284 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003285 if (IS_ERR(task))
3286 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003287 status = 0;
3288 if (wait)
3289 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003290 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003291 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003292out_free_calldata:
3293 kfree(calldata);
3294out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003295 nfs4_put_open_state(state);
3296 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003297 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Trond Myklebust2b484292010-09-17 10:56:51 -04003300static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003301nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3302 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003305 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3306
3307 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Trond Myklebust565277f2007-10-15 18:17:53 -04003309 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003310 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003311
3312 nfs4_label_release_security(label);
3313
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003314 if (IS_ERR(state))
3315 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003316 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Trond Myklebust1185a552009-12-03 15:54:02 -05003319static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003320{
3321 if (ctx->state == NULL)
3322 return;
3323 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003324 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003325 else
Al Viro643168c2011-06-22 18:20:23 -04003326 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Trond Myklebustb944dba2013-11-04 15:20:20 -05003329#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3330#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003331#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3334{
Kinglong Mee8c612822015-08-26 21:12:58 +08003335 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003336 struct nfs4_server_caps_arg args = {
3337 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003338 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003339 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 struct nfs4_server_caps_res res = {};
3341 struct rpc_message msg = {
3342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003343 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 .rpc_resp = &res,
3345 };
3346 int status;
3347
Kinglong Mee8c612822015-08-26 21:12:58 +08003348 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3349 FATTR4_WORD0_FH_EXPIRE_TYPE |
3350 FATTR4_WORD0_LINK_SUPPORT |
3351 FATTR4_WORD0_SYMLINK_SUPPORT |
3352 FATTR4_WORD0_ACLSUPPORT;
3353 if (minorversion)
3354 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3355
Bryan Schumaker7c513052011-03-24 17:12:24 +00003356 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003358 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003359 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003360 case 0:
3361 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3362 res.attr_bitmask[2] = 0;
3363 break;
3364 case 1:
3365 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3366 break;
3367 case 2:
3368 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003371 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3372 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3373 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3374 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003375 NFS_CAP_CTIME|NFS_CAP_MTIME|
3376 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003377 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3378 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 server->caps |= NFS_CAP_ACLS;
3380 if (res.has_links != 0)
3381 server->caps |= NFS_CAP_HARDLINKS;
3382 if (res.has_symlinks != 0)
3383 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003384 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3385 server->caps |= NFS_CAP_FILEID;
3386 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3387 server->caps |= NFS_CAP_MODE;
3388 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3389 server->caps |= NFS_CAP_NLINK;
3390 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3391 server->caps |= NFS_CAP_OWNER;
3392 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3393 server->caps |= NFS_CAP_OWNER_GROUP;
3394 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3395 server->caps |= NFS_CAP_ATIME;
3396 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3397 server->caps |= NFS_CAP_CTIME;
3398 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3399 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003400#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3401 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3402 server->caps |= NFS_CAP_SECURITY_LABEL;
3403#endif
3404 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3405 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003406 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003407
Trond Myklebusta65318b2009-03-11 14:10:28 -04003408 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3409 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3410 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003411 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003412 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3413 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003415 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return status;
3419}
3420
Trond Myklebust55a97592006-06-09 09:34:19 -04003421int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422{
3423 struct nfs4_exception exception = { };
3424 int err;
3425 do {
3426 err = nfs4_handle_exception(server,
3427 _nfs4_server_capabilities(server, fhandle),
3428 &exception);
3429 } while (exception.retry);
3430 return err;
3431}
3432
3433static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3434 struct nfs_fsinfo *info)
3435{
David Quigleyaa9c2662013-05-22 12:50:44 -04003436 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003438 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 };
3440 struct nfs4_lookup_res res = {
3441 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003442 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 .fh = fhandle,
3444 };
3445 struct rpc_message msg = {
3446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3447 .rpc_argp = &args,
3448 .rpc_resp = &res,
3449 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003450
David Quigleyaa9c2662013-05-22 12:50:44 -04003451 bitmask[0] = nfs4_fattr_bitmap[0];
3452 bitmask[1] = nfs4_fattr_bitmap[1];
3453 /*
3454 * Process the label in the upcoming getfattr
3455 */
3456 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3457
Trond Myklebust0e574af2005-10-27 22:12:38 -04003458 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003459 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460}
3461
3462static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3463 struct nfs_fsinfo *info)
3464{
3465 struct nfs4_exception exception = { };
3466 int err;
3467 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003468 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003469 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003470 switch (err) {
3471 case 0:
3472 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003473 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003474 default:
3475 err = nfs4_handle_exception(server, err, &exception);
3476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003478out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return err;
3480}
3481
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003482static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3483 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3484{
Trond Myklebustc2190662013-08-26 19:23:04 -04003485 struct rpc_auth_create_args auth_args = {
3486 .pseudoflavor = flavor,
3487 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003488 struct rpc_auth *auth;
3489 int ret;
3490
Trond Myklebustc2190662013-08-26 19:23:04 -04003491 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003492 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003493 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003494 goto out;
3495 }
3496 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003497out:
3498 return ret;
3499}
3500
Chuck Lever9a744ba2013-03-16 15:56:02 -04003501/*
3502 * Retry pseudoroot lookup with various security flavors. We do this when:
3503 *
3504 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3505 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3506 *
3507 * Returns zero on success, or a negative NFS4ERR value, or a
3508 * negative errno value.
3509 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003510static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003511 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003513 /* Per 3530bis 15.33.5 */
3514 static const rpc_authflavor_t flav_array[] = {
3515 RPC_AUTH_GSS_KRB5P,
3516 RPC_AUTH_GSS_KRB5I,
3517 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003518 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003519 RPC_AUTH_NULL,
3520 };
3521 int status = -EPERM;
3522 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003524 if (server->auth_info.flavor_len > 0) {
3525 /* try each flavor specified by user */
3526 for (i = 0; i < server->auth_info.flavor_len; i++) {
3527 status = nfs4_lookup_root_sec(server, fhandle, info,
3528 server->auth_info.flavors[i]);
3529 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3530 continue;
3531 break;
3532 }
3533 } else {
3534 /* no flavors specified by user, try default list */
3535 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3536 status = nfs4_lookup_root_sec(server, fhandle, info,
3537 flav_array[i]);
3538 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3539 continue;
3540 break;
3541 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003542 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003543
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003544 /*
3545 * -EACCESS could mean that the user doesn't have correct permissions
3546 * to access the mount. It could also mean that we tried to mount
3547 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3548 * existing mount programs don't handle -EACCES very well so it should
3549 * be mapped to -EPERM instead.
3550 */
3551 if (status == -EACCES)
3552 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003553 return status;
3554}
3555
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003556/**
3557 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3558 * @server: initialized nfs_server handle
3559 * @fhandle: we fill in the pseudo-fs root file handle
3560 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003561 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003562 *
3563 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003564 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003565int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003566 struct nfs_fsinfo *info,
3567 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003568{
Andre Przywarac7757072015-04-23 17:17:40 +01003569 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003570
Andre Przywarac7757072015-04-23 17:17:40 +01003571 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003572 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003573
3574 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003575 status = server->nfs_client->cl_mvops->find_root_sec(server,
3576 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 if (status == 0)
3579 status = nfs4_server_capabilities(server, fhandle);
3580 if (status == 0)
3581 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003582
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003583 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
Bryan Schumakerbae36242012-05-10 15:07:31 -04003586static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3587 struct nfs_fsinfo *info)
3588{
3589 int error;
3590 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003591 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003592
3593 error = nfs4_server_capabilities(server, mntfh);
3594 if (error < 0) {
3595 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3596 return error;
3597 }
3598
David Quigley14c43f72013-05-22 12:50:43 -04003599 label = nfs4_label_alloc(server, GFP_KERNEL);
3600 if (IS_ERR(label))
3601 return PTR_ERR(label);
3602
David Quigley1775fd32013-05-22 12:50:42 -04003603 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003604 if (error < 0) {
3605 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003606 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003607 }
3608
3609 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3610 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3611 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3612
David Quigley14c43f72013-05-22 12:50:43 -04003613err_free_label:
3614 nfs4_label_free(label);
3615
Bryan Schumakerbae36242012-05-10 15:07:31 -04003616 return error;
3617}
3618
Manoj Naik6b97fd32006-06-09 09:34:29 -04003619/*
3620 * Get locations and (maybe) other attributes of a referral.
3621 * Note that we'll actually follow the referral later when
3622 * we detect fsid mismatch in inode revalidation
3623 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003624static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3625 const struct qstr *name, struct nfs_fattr *fattr,
3626 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003627{
3628 int status = -ENOMEM;
3629 struct page *page = NULL;
3630 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003631
3632 page = alloc_page(GFP_KERNEL);
3633 if (page == NULL)
3634 goto out;
3635 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3636 if (locations == NULL)
3637 goto out;
3638
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003639 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003640 if (status != 0)
3641 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003642
3643 /*
3644 * If the fsid didn't change, this is a migration event, not a
3645 * referral. Cause us to drop into the exception handler, which
3646 * will kick off migration recovery.
3647 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003648 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003649 dprintk("%s: server did not return a different fsid for"
3650 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003651 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003652 goto out;
3653 }
Andy Adamson533eb462011-06-13 18:25:56 -04003654 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3655 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003656
Andy Adamson533eb462011-06-13 18:25:56 -04003657 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003658 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003659 memset(fhandle, 0, sizeof(struct nfs_fh));
3660out:
3661 if (page)
3662 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003663 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003664 return status;
3665}
3666
David Quigley1775fd32013-05-22 12:50:42 -04003667static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3668 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669{
3670 struct nfs4_getattr_arg args = {
3671 .fh = fhandle,
3672 .bitmask = server->attr_bitmask,
3673 };
3674 struct nfs4_getattr_res res = {
3675 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003676 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 .server = server,
3678 };
3679 struct rpc_message msg = {
3680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3681 .rpc_argp = &args,
3682 .rpc_resp = &res,
3683 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003684
3685 args.bitmask = nfs4_bitmask(server, label);
3686
Trond Myklebust0e574af2005-10-27 22:12:38 -04003687 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003688 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
David Quigley1775fd32013-05-22 12:50:42 -04003691static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3692 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
3694 struct nfs4_exception exception = { };
3695 int err;
3696 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003697 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3698 trace_nfs4_getattr(server, fhandle, fattr, err);
3699 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 &exception);
3701 } while (exception.retry);
3702 return err;
3703}
3704
3705/*
3706 * The file is not closed if it is opened due to the a request to change
3707 * the size of the file. The open call will not be needed once the
3708 * VFS layer lookup-intents are implemented.
3709 *
3710 * Close is called when the inode is destroyed.
3711 * If we haven't opened the file for O_WRONLY, we
3712 * need to in the size_change case to obtain a stateid.
3713 *
3714 * Got race?
3715 * Because OPEN is always done by name in nfsv4, it is
3716 * possible that we opened a different file by the same
3717 * name. We can recognize this race condition, but we
3718 * can't do anything about it besides returning an error.
3719 *
3720 * This will be fixed with VFS changes (lookup-intent).
3721 */
3722static int
3723nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3724 struct iattr *sattr)
3725{
David Howells2b0143b2015-03-17 22:25:59 +00003726 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003727 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003728 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003729 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 int status;
3731
Peng Tao88ac8152014-09-12 11:04:10 +08003732 if (pnfs_ld_layoutret_on_setattr(inode) &&
3733 sattr->ia_valid & ATTR_SIZE &&
3734 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003735 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003736
Trond Myklebust0e574af2005-10-27 22:12:38 -04003737 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Andy Adamson26699402012-05-30 16:12:24 -04003739 /* Deal with open(O_TRUNC) */
3740 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003741 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003742
3743 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003744 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003745 return 0;
3746
Trond Myklebustd5308382005-11-04 15:33:38 -05003747 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003748 if (sattr->ia_valid & ATTR_FILE) {
3749 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003750
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003751 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003752 if (ctx) {
3753 cred = ctx->cred;
3754 state = ctx->state;
3755 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003756 }
3757
David Quigley14c43f72013-05-22 12:50:43 -04003758 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3759 if (IS_ERR(label))
3760 return PTR_ERR(label);
3761
3762 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003763 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003764 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003765 nfs_setsecurity(inode, fattr, label);
3766 }
David Quigley14c43f72013-05-22 12:50:43 -04003767 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 return status;
3769}
3770
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003771static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3772 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003773 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003774{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003775 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003776 int status;
3777 struct nfs4_lookup_arg args = {
3778 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003779 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003780 .name = name,
3781 };
3782 struct nfs4_lookup_res res = {
3783 .server = server,
3784 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003785 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003786 .fh = fhandle,
3787 };
3788 struct rpc_message msg = {
3789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3790 .rpc_argp = &args,
3791 .rpc_resp = &res,
3792 };
3793
David Quigleyaa9c2662013-05-22 12:50:44 -04003794 args.bitmask = nfs4_bitmask(server, label);
3795
David Howells2b3de442006-08-22 20:06:09 -04003796 nfs_fattr_init(fattr);
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003799 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 dprintk("NFS reply lookup: %d\n", status);
3801 return status;
3802}
3803
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003804static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003805{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003806 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003807 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003808 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3809 fattr->nlink = 2;
3810}
3811
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003812static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003813 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003814 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003815{
3816 struct nfs4_exception exception = { };
3817 struct rpc_clnt *client = *clnt;
3818 int err;
3819 do {
David Quigley1775fd32013-05-22 12:50:42 -04003820 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003821 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003822 switch (err) {
3823 case -NFS4ERR_BADNAME:
3824 err = -ENOENT;
3825 goto out;
3826 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003827 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003828 if (err == -NFS4ERR_MOVED)
3829 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003830 goto out;
3831 case -NFS4ERR_WRONGSEC:
3832 err = -EPERM;
3833 if (client != *clnt)
3834 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003835 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003836 if (IS_ERR(client))
3837 return PTR_ERR(client);
3838
3839 exception.retry = 1;
3840 break;
3841 default:
3842 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3843 }
3844 } while (exception.retry);
3845
3846out:
3847 if (err == 0)
3848 *clnt = client;
3849 else if (client != *clnt)
3850 rpc_shutdown_client(client);
3851
3852 return err;
3853}
3854
Al Virobeffb8f2016-07-20 16:34:42 -04003855static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003856 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3857 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003859 int status;
3860 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003861
David Quigley1775fd32013-05-22 12:50:42 -04003862 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003863 if (client != NFS_CLIENT(dir)) {
3864 rpc_shutdown_client(client);
3865 nfs_fixup_secinfo_attributes(fattr);
3866 }
3867 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868}
3869
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003870struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003871nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003872 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3873{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003874 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003875 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003876
David Quigley1775fd32013-05-22 12:50:42 -04003877 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003878 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003879 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003880 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003881}
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3884{
Trond Myklebust76b32992007-08-10 17:45:11 -04003885 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 struct nfs4_accessargs args = {
3887 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003888 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003890 struct nfs4_accessres res = {
3891 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3895 .rpc_argp = &args,
3896 .rpc_resp = &res,
3897 .rpc_cred = entry->cred,
3898 };
3899 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003900 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901
3902 /*
3903 * Determine which access bits we want to ask for...
3904 */
3905 if (mode & MAY_READ)
3906 args.access |= NFS4_ACCESS_READ;
3907 if (S_ISDIR(inode->i_mode)) {
3908 if (mode & MAY_WRITE)
3909 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3910 if (mode & MAY_EXEC)
3911 args.access |= NFS4_ACCESS_LOOKUP;
3912 } else {
3913 if (mode & MAY_WRITE)
3914 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3915 if (mode & MAY_EXEC)
3916 args.access |= NFS4_ACCESS_EXECUTE;
3917 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003918
3919 res.fattr = nfs_alloc_fattr();
3920 if (res.fattr == NULL)
3921 return -ENOMEM;
3922
Bryan Schumaker7c513052011-03-24 17:12:24 +00003923 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003925 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003926 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003928 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return status;
3930}
3931
3932static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3933{
3934 struct nfs4_exception exception = { };
3935 int err;
3936 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003937 err = _nfs4_proc_access(inode, entry);
3938 trace_nfs4_access(inode, err);
3939 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 &exception);
3941 } while (exception.retry);
3942 return err;
3943}
3944
3945/*
3946 * TODO: For the time being, we don't try to get any attributes
3947 * along with any of the zero-copy operations READ, READDIR,
3948 * READLINK, WRITE.
3949 *
3950 * In the case of the first three, we want to put the GETATTR
3951 * after the read-type operation -- this is because it is hard
3952 * to predict the length of a GETATTR response in v4, and thus
3953 * align the READ data correctly. This means that the GETATTR
3954 * may end up partially falling into the page cache, and we should
3955 * shift it into the 'tail' of the xdr_buf before processing.
3956 * To do this efficiently, we need to know the total length
3957 * of data received, which doesn't seem to be available outside
3958 * of the RPC layer.
3959 *
3960 * In the case of WRITE, we also want to put the GETATTR after
3961 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003962 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 *
3964 * Both of these changes to the XDR layer would in fact be quite
3965 * minor, but I decided to leave them for a subsequent patch.
3966 */
3967static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3968 unsigned int pgbase, unsigned int pglen)
3969{
3970 struct nfs4_readlink args = {
3971 .fh = NFS_FH(inode),
3972 .pgbase = pgbase,
3973 .pglen = pglen,
3974 .pages = &page,
3975 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003976 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 struct rpc_message msg = {
3978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3979 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003980 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 };
3982
Bryan Schumaker7c513052011-03-24 17:12:24 +00003983 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 -07003984}
3985
3986static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3987 unsigned int pgbase, unsigned int pglen)
3988{
3989 struct nfs4_exception exception = { };
3990 int err;
3991 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003992 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3993 trace_nfs4_readlink(inode, err);
3994 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 &exception);
3996 } while (exception.retry);
3997 return err;
3998}
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004001 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003static int
4004nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004005 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
David Quigleyaa9c2662013-05-22 12:50:44 -04004007 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004008 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 int status = 0;
4011
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004012 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
4013 if (IS_ERR(ctx))
4014 return PTR_ERR(ctx);
4015
David Quigleyaa9c2662013-05-22 12:50:44 -04004016 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4017
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004018 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004019 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 if (IS_ERR(state)) {
4021 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004022 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004025 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004026 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return status;
4028}
4029
Al Virobeffb8f2016-07-20 16:34:42 -04004030static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Trond Myklebust16e42952005-10-27 22:12:44 -04004032 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004033 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004035 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004037 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004038 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004039 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4042 .rpc_argp = &args,
4043 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004045 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004046
Bryan Schumaker7c513052011-03-24 17:12:24 +00004047 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004048 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004049 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return status;
4051}
4052
Al Virobeffb8f2016-07-20 16:34:42 -04004053static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054{
4055 struct nfs4_exception exception = { };
4056 int err;
4057 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004058 err = _nfs4_proc_remove(dir, name);
4059 trace_nfs4_remove(dir, name, err);
4060 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 &exception);
4062 } while (exception.retry);
4063 return err;
4064}
4065
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004066static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004068 struct nfs_server *server = NFS_SERVER(dir);
4069 struct nfs_removeargs *args = msg->rpc_argp;
4070 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004074 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004075
4076 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077}
4078
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004079static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4080{
Al Viro884be172016-04-28 23:56:31 -04004081 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004082 &data->args.seq_args,
4083 &data->res.seq_res,
4084 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085}
4086
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004087static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004089 struct nfs_unlinkdata *data = task->tk_calldata;
4090 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004091
Trond Myklebust14516c32010-07-31 14:29:06 -04004092 if (!nfs4_sequence_done(task, &res->seq_res))
4093 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004094 if (nfs4_async_handle_error(task, res->server, NULL,
4095 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004096 return 0;
4097 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004098 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099}
4100
Jeff Laytond3d41522010-09-17 17:31:57 -04004101static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4102{
4103 struct nfs_server *server = NFS_SERVER(dir);
4104 struct nfs_renameargs *arg = msg->rpc_argp;
4105 struct nfs_renameres *res = msg->rpc_resp;
4106
4107 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004108 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004109 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004110}
4111
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004112static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4113{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004114 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4115 &data->args.seq_args,
4116 &data->res.seq_res,
4117 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004118}
4119
4120static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4121 struct inode *new_dir)
4122{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004123 struct nfs_renamedata *data = task->tk_calldata;
4124 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004125
4126 if (!nfs4_sequence_done(task, &res->seq_res))
4127 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004128 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004129 return 0;
4130
4131 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004132 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004133 return 1;
4134}
4135
Al Virobeffb8f2016-07-20 16:34:42 -04004136static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004138 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 struct nfs4_link_arg arg = {
4140 .fh = NFS_FH(inode),
4141 .dir_fh = NFS_FH(dir),
4142 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004143 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004145 struct nfs4_link_res res = {
4146 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004147 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 struct rpc_message msg = {
4150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4151 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004154 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Trond Myklebust136f2622010-04-16 16:22:49 -04004156 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004157 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004158 goto out;
4159
David Quigley14c43f72013-05-22 12:50:43 -04004160 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4161 if (IS_ERR(res.label)) {
4162 status = PTR_ERR(res.label);
4163 goto out;
4164 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004165 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004166
Bryan Schumaker7c513052011-03-24 17:12:24 +00004167 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004168 if (!status) {
4169 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004170 status = nfs_post_op_update_inode(inode, res.fattr);
4171 if (!status)
4172 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004173 }
David Quigley14c43f72013-05-22 12:50:43 -04004174
4175
4176 nfs4_label_free(res.label);
4177
Trond Myklebust136f2622010-04-16 16:22:49 -04004178out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004179 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return status;
4181}
4182
Al Virobeffb8f2016-07-20 16:34:42 -04004183static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
4185 struct nfs4_exception exception = { };
4186 int err;
4187 do {
4188 err = nfs4_handle_exception(NFS_SERVER(inode),
4189 _nfs4_proc_link(inode, dir, name),
4190 &exception);
4191 } while (exception.retry);
4192 return err;
4193}
4194
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004195struct nfs4_createdata {
4196 struct rpc_message msg;
4197 struct nfs4_create_arg arg;
4198 struct nfs4_create_res res;
4199 struct nfs_fh fh;
4200 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004201 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004202};
4203
4204static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004205 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004206{
4207 struct nfs4_createdata *data;
4208
4209 data = kzalloc(sizeof(*data), GFP_KERNEL);
4210 if (data != NULL) {
4211 struct nfs_server *server = NFS_SERVER(dir);
4212
David Quigley14c43f72013-05-22 12:50:43 -04004213 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4214 if (IS_ERR(data->label))
4215 goto out_free;
4216
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004217 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4218 data->msg.rpc_argp = &data->arg;
4219 data->msg.rpc_resp = &data->res;
4220 data->arg.dir_fh = NFS_FH(dir);
4221 data->arg.server = server;
4222 data->arg.name = name;
4223 data->arg.attrs = sattr;
4224 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004225 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226 data->res.server = server;
4227 data->res.fh = &data->fh;
4228 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004229 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004231 }
4232 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004233out_free:
4234 kfree(data);
4235 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236}
4237
4238static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4239{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004240 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004241 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242 if (status == 0) {
4243 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004244 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004245 }
4246 return status;
4247}
4248
4249static void nfs4_free_createdata(struct nfs4_createdata *data)
4250{
David Quigley14c43f72013-05-22 12:50:43 -04004251 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 kfree(data);
4253}
4254
Chuck Lever4f390c12006-08-22 20:06:22 -04004255static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004256 struct page *page, unsigned int len, struct iattr *sattr,
4257 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004259 struct nfs4_createdata *data;
4260 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Chuck Lever94a6d752006-08-22 20:06:23 -04004262 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004263 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004264
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004265 status = -ENOMEM;
4266 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4267 if (data == NULL)
4268 goto out;
4269
4270 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4271 data->arg.u.symlink.pages = &page;
4272 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004273 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 status = nfs4_do_create(dir, dentry, data);
4276
4277 nfs4_free_createdata(data);
4278out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return status;
4280}
4281
Chuck Lever4f390c12006-08-22 20:06:22 -04004282static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004283 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284{
4285 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004286 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004288
4289 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004292 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4293 trace_nfs4_symlink(dir, &dentry->d_name, err);
4294 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 &exception);
4296 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004297
4298 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return err;
4300}
4301
4302static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004303 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004305 struct nfs4_createdata *data;
4306 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004308 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4309 if (data == NULL)
4310 goto out;
4311
David Quigley1775fd32013-05-22 12:50:42 -04004312 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004313 status = nfs4_do_create(dir, dentry, data);
4314
4315 nfs4_free_createdata(data);
4316out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return status;
4318}
4319
4320static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4321 struct iattr *sattr)
4322{
4323 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004324 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004326
David Quigleyaa9c2662013-05-22 12:50:44 -04004327 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4328
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004329 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004331 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4332 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4333 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 &exception);
4335 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004336 nfs4_label_release_security(label);
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return err;
4339}
4340
4341static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004342 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
David Howells2b0143b2015-03-17 22:25:59 +00004344 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 struct nfs4_readdir_arg args = {
4346 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004347 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 .pgbase = 0,
4349 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004350 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004351 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 };
4353 struct nfs4_readdir_res res;
4354 struct rpc_message msg = {
4355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4356 .rpc_argp = &args,
4357 .rpc_resp = &res,
4358 .rpc_cred = cred,
4359 };
4360 int status;
4361
Al Viro6de14722013-09-16 10:53:17 -04004362 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4363 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004364 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004365 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004367 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 -05004368 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004369 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004370 status += args.pgbase;
4371 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004372
4373 nfs_invalidate_atime(dir);
4374
Harvey Harrison3110ff82008-05-02 13:42:44 -07004375 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 return status;
4377}
4378
4379static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004380 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381{
4382 struct nfs4_exception exception = { };
4383 int err;
4384 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004385 err = _nfs4_proc_readdir(dentry, cred, cookie,
4386 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004387 trace_nfs4_readdir(d_inode(dentry), err);
4388 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 &exception);
4390 } while (exception.retry);
4391 return err;
4392}
4393
4394static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004395 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004397 struct nfs4_createdata *data;
4398 int mode = sattr->ia_mode;
4399 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4402 if (data == NULL)
4403 goto out;
4404
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004406 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004408 data->arg.ftype = NF4BLK;
4409 data->arg.u.device.specdata1 = MAJOR(rdev);
4410 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
4412 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004413 data->arg.ftype = NF4CHR;
4414 data->arg.u.device.specdata1 = MAJOR(rdev);
4415 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004416 } else if (!S_ISSOCK(mode)) {
4417 status = -EINVAL;
4418 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
David Quigley1775fd32013-05-22 12:50:42 -04004420
David Quigleyaa9c2662013-05-22 12:50:44 -04004421 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004422 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004423out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004424 nfs4_free_createdata(data);
4425out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return status;
4427}
4428
4429static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4430 struct iattr *sattr, dev_t rdev)
4431{
4432 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004433 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004435
David Quigleyaa9c2662013-05-22 12:50:44 -04004436 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4437
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004438 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004440 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4441 trace_nfs4_mknod(dir, &dentry->d_name, err);
4442 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 &exception);
4444 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004445
4446 nfs4_label_release_security(label);
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 return err;
4449}
4450
4451static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4452 struct nfs_fsstat *fsstat)
4453{
4454 struct nfs4_statfs_arg args = {
4455 .fh = fhandle,
4456 .bitmask = server->attr_bitmask,
4457 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004458 struct nfs4_statfs_res res = {
4459 .fsstat = fsstat,
4460 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 struct rpc_message msg = {
4462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4463 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004464 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 };
4466
Trond Myklebust0e574af2005-10-27 22:12:38 -04004467 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004468 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
4471static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4472{
4473 struct nfs4_exception exception = { };
4474 int err;
4475 do {
4476 err = nfs4_handle_exception(server,
4477 _nfs4_proc_statfs(server, fhandle, fsstat),
4478 &exception);
4479 } while (exception.retry);
4480 return err;
4481}
4482
4483static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4484 struct nfs_fsinfo *fsinfo)
4485{
4486 struct nfs4_fsinfo_arg args = {
4487 .fh = fhandle,
4488 .bitmask = server->attr_bitmask,
4489 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004490 struct nfs4_fsinfo_res res = {
4491 .fsinfo = fsinfo,
4492 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 struct rpc_message msg = {
4494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4495 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004496 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 };
4498
Bryan Schumaker7c513052011-03-24 17:12:24 +00004499 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500}
4501
4502static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4503{
4504 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004505 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 int err;
4507
4508 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004509 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004510 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004511 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004512 nfs4_set_lease_period(server->nfs_client,
4513 fsinfo->lease_time * HZ,
4514 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004515 break;
4516 }
4517 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 } while (exception.retry);
4519 return err;
4520}
4521
4522static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4523{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004524 int error;
4525
Trond Myklebust0e574af2005-10-27 22:12:38 -04004526 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004527 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004528 if (error == 0) {
4529 /* block layout checks this! */
4530 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004531 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004532 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004533
4534 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
4537static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4538 struct nfs_pathconf *pathconf)
4539{
4540 struct nfs4_pathconf_arg args = {
4541 .fh = fhandle,
4542 .bitmask = server->attr_bitmask,
4543 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004544 struct nfs4_pathconf_res res = {
4545 .pathconf = pathconf,
4546 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 struct rpc_message msg = {
4548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4549 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004550 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 };
4552
4553 /* None of the pathconf attributes are mandatory to implement */
4554 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4555 memset(pathconf, 0, sizeof(*pathconf));
4556 return 0;
4557 }
4558
Trond Myklebust0e574af2005-10-27 22:12:38 -04004559 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004560 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
4563static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4564 struct nfs_pathconf *pathconf)
4565{
4566 struct nfs4_exception exception = { };
4567 int err;
4568
4569 do {
4570 err = nfs4_handle_exception(server,
4571 _nfs4_proc_pathconf(server, fhandle, pathconf),
4572 &exception);
4573 } while (exception.retry);
4574 return err;
4575}
4576
Trond Myklebust5521abf2013-03-16 20:54:34 -04004577int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004578 const struct nfs_open_context *ctx,
4579 const struct nfs_lock_context *l_ctx,
4580 fmode_t fmode)
4581{
4582 const struct nfs_lockowner *lockowner = NULL;
4583
4584 if (l_ctx != NULL)
4585 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004586 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, 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;
5608 u32 roc_barrier;
5609 bool roc;
5610 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005611 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005612 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005613 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005614};
5615
Trond Myklebustfe650402006-01-03 09:55:18 +01005616static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5617{
5618 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005619
Trond Myklebust14516c32010-07-31 14:29:06 -04005620 if (!nfs4_sequence_done(task, &data->res.seq_res))
5621 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005622
Trond Myklebustca8acf82013-08-13 10:36:56 -04005623 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005624
5625 /* Handle Layoutreturn errors */
5626 if (data->args.lr_args && task->tk_status != 0) {
5627 switch(data->res.lr_ret) {
5628 default:
5629 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5630 break;
5631 case 0:
5632 data->args.lr_args = NULL;
5633 data->res.lr_res = NULL;
5634 break;
5635 case -NFS4ERR_ADMIN_REVOKED:
5636 case -NFS4ERR_DELEG_REVOKED:
5637 case -NFS4ERR_EXPIRED:
5638 case -NFS4ERR_BAD_STATEID:
5639 case -NFS4ERR_OLD_STATEID:
5640 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5641 case -NFS4ERR_WRONG_CRED:
5642 data->args.lr_args = NULL;
5643 data->res.lr_res = NULL;
5644 data->res.lr_ret = 0;
5645 rpc_restart_call_prepare(task);
5646 return;
5647 }
5648 }
5649
Ricardo Labiaga79708862009-12-07 09:23:21 -05005650 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005651 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005652 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005653 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005654 case -NFS4ERR_ADMIN_REVOKED:
5655 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005656 case -NFS4ERR_EXPIRED:
5657 nfs4_free_revoked_stateid(data->res.server,
5658 data->args.stateid,
5659 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005660 case -NFS4ERR_BAD_STATEID:
5661 case -NFS4ERR_OLD_STATEID:
5662 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005663 task->tk_status = 0;
5664 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005665 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005666 if (nfs4_async_handle_error(task, data->res.server,
5667 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005668 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005669 return;
5670 }
5671 }
5672 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005673}
5674
5675static void nfs4_delegreturn_release(void *calldata)
5676{
Peng Tao039b7562014-07-03 13:05:02 +08005677 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005678 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005679
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005680 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005681 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005682 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5683 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005684 nfs_iput_and_deactive(inode);
5685 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005686 kfree(calldata);
5687}
5688
Andy Adamson938e1012009-04-01 09:22:28 -04005689static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5690{
5691 struct nfs4_delegreturndata *d_data;
5692
5693 d_data = (struct nfs4_delegreturndata *)data;
5694
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005695 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005696 return;
5697
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005698 nfs4_setup_sequence(d_data->res.server,
5699 &d_data->args.seq_args,
5700 &d_data->res.seq_res,
5701 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005702}
Andy Adamson938e1012009-04-01 09:22:28 -04005703
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005704static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005705 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005706 .rpc_call_done = nfs4_delegreturn_done,
5707 .rpc_release = nfs4_delegreturn_release,
5708};
5709
Trond Myklebuste6f81072008-01-24 18:14:34 -05005710static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005711{
5712 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005713 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005714 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005715 struct rpc_message msg = {
5716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5717 .rpc_cred = cred,
5718 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005719 struct rpc_task_setup task_setup_data = {
5720 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005721 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005722 .callback_ops = &nfs4_delegreturn_ops,
5723 .flags = RPC_TASK_ASYNC,
5724 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005725 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005726
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005727 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005728 if (data == NULL)
5729 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005730 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005731
5732 nfs4_state_protect(server->nfs_client,
5733 NFS_SP4_MACH_CRED_CLEANUP,
5734 &task_setup_data.rpc_client, &msg);
5735
Trond Myklebustfe650402006-01-03 09:55:18 +01005736 data->args.fhandle = &data->fh;
5737 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005738 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005739 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005740 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005741 data->res.fattr = &data->fattr;
5742 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005743 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005744 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005745 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005746 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005747 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005748 if (data->inode) {
5749 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res,
5750 cred);
5751 if (data->lr.roc) {
5752 data->args.lr_args = &data->lr.arg;
5753 data->res.lr_res = &data->lr.res;
5754 }
5755 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005756
Trond Myklebustc970aa82007-07-14 15:39:59 -04005757 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005758 msg.rpc_argp = &data->args;
5759 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005760 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005761 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005762 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005763 if (!issync)
5764 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005765 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005766 if (status != 0)
5767 goto out;
5768 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005769 if (status == 0)
5770 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5771 else
5772 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005773out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005774 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005775 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776}
5777
Trond Myklebuste6f81072008-01-24 18:14:34 -05005778int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
5780 struct nfs_server *server = NFS_SERVER(inode);
5781 struct nfs4_exception exception = { };
5782 int err;
5783 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005784 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005785 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 switch (err) {
5787 case -NFS4ERR_STALE_STATEID:
5788 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 case 0:
5790 return 0;
5791 }
5792 err = nfs4_handle_exception(server, err, &exception);
5793 } while (exception.retry);
5794 return err;
5795}
5796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5798{
5799 struct inode *inode = state->inode;
5800 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005801 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005802 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005804 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005806 struct nfs_lockt_res res = {
5807 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 };
5809 struct rpc_message msg = {
5810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5811 .rpc_argp = &arg,
5812 .rpc_resp = &res,
5813 .rpc_cred = state->owner->so_cred,
5814 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 struct nfs4_lock_state *lsp;
5816 int status;
5817
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005818 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005819 status = nfs4_set_lock_state(state, request);
5820 if (status != 0)
5821 goto out;
5822 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005823 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005824 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005825 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005826 switch (status) {
5827 case 0:
5828 request->fl_type = F_UNLCK;
5829 break;
5830 case -NFS4ERR_DENIED:
5831 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005833 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005834 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return status;
5837}
5838
5839static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5840{
5841 struct nfs4_exception exception = { };
5842 int err;
5843
5844 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005845 err = _nfs4_proc_getlk(state, cmd, request);
5846 trace_nfs4_get_lock(request, state, cmd, err);
5847 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 &exception);
5849 } while (exception.retry);
5850 return err;
5851}
5852
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005853struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005854 struct nfs_locku_args arg;
5855 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005856 struct nfs4_lock_state *lsp;
5857 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005859 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005860 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005861};
5862
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005863static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5864 struct nfs_open_context *ctx,
5865 struct nfs4_lock_state *lsp,
5866 struct nfs_seqid *seqid)
5867{
5868 struct nfs4_unlockdata *p;
5869 struct inode *inode = lsp->ls_state->inode;
5870
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005871 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005872 if (p == NULL)
5873 return NULL;
5874 p->arg.fh = NFS_FH(inode);
5875 p->arg.fl = &p->fl;
5876 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005877 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 p->lsp = lsp;
5879 atomic_inc(&lsp->ls_count);
5880 /* Ensure we don't close file until we're done freeing locks! */
5881 p->ctx = get_nfs_open_context(ctx);
5882 memcpy(&p->fl, fl, sizeof(p->fl));
5883 p->server = NFS_SERVER(inode);
5884 return p;
5885}
5886
Trond Myklebust06f814a2006-01-03 09:55:07 +01005887static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005888{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005889 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005890 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005891 nfs4_put_lock_state(calldata->lsp);
5892 put_nfs_open_context(calldata->ctx);
5893 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005894}
5895
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005896static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005897{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005898 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005899
Trond Myklebust14516c32010-07-31 14:29:06 -04005900 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5901 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005902 switch (task->tk_status) {
5903 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005904 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005905 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005906 if (nfs4_update_lock_stateid(calldata->lsp,
5907 &calldata->res.stateid))
5908 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005909 case -NFS4ERR_ADMIN_REVOKED:
5910 case -NFS4ERR_EXPIRED:
5911 nfs4_free_revoked_stateid(calldata->server,
5912 &calldata->arg.stateid,
5913 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005914 case -NFS4ERR_BAD_STATEID:
5915 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005917 if (!nfs4_stateid_match(&calldata->arg.stateid,
5918 &calldata->lsp->ls_stateid))
5919 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005920 break;
5921 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005922 if (nfs4_async_handle_error(task, calldata->server,
5923 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005924 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005925 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005926 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005927}
5928
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005929static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005930{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005931 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005933 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005934 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005935 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005936 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005937 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005938 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005940 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005941 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005942 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005943 &calldata->res.seq_res,
5944 task) != 0)
5945 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005946 return;
5947out_no_action:
5948 task->tk_action = NULL;
5949out_wait:
5950 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951}
5952
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005953static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005954 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005955 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005956 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005957};
5958
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005959static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5960 struct nfs_open_context *ctx,
5961 struct nfs4_lock_state *lsp,
5962 struct nfs_seqid *seqid)
5963{
5964 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005965 struct rpc_message msg = {
5966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5967 .rpc_cred = ctx->cred,
5968 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005969 struct rpc_task_setup task_setup_data = {
5970 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005971 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005972 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005973 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005974 .flags = RPC_TASK_ASYNC,
5975 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005976
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005977 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5978 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5979
Frank Filz137d6ac2007-07-09 15:32:29 -07005980 /* Ensure this is an unlock - when canceling a lock, the
5981 * canceled lock is passed in, and it won't be an unlock.
5982 */
5983 fl->fl_type = F_UNLCK;
5984
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005985 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5986 if (data == NULL) {
5987 nfs_free_seqid(seqid);
5988 return ERR_PTR(-ENOMEM);
5989 }
5990
Chuck Levera9c92d62013-08-09 12:48:18 -04005991 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005992 msg.rpc_argp = &data->arg;
5993 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005994 task_setup_data.callback_data = data;
5995 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005996}
5997
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5999{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006000 struct inode *inode = state->inode;
6001 struct nfs4_state_owner *sp = state->owner;
6002 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006003 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006004 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006005 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006006 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006007 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006008 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
Trond Myklebust9b073572006-06-29 16:38:34 -04006010 status = nfs4_set_lock_state(state, request);
6011 /* Unlock _before_ we do the RPC call */
6012 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006013 /* Exclude nfs_delegation_claim_locks() */
6014 mutex_lock(&sp->so_delegreturn_mutex);
6015 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006016 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006017 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006018 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006019 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006020 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006021 }
6022 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 if (status != 0)
6025 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006026 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006027 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006028 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6029 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006030 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6031 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006032 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006033 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006034 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006035 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006036 status = PTR_ERR(task);
6037 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006038 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006039 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006040 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006041out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006042 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006043 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006044 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045}
6046
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047struct nfs4_lockdata {
6048 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006049 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006050 struct nfs4_lock_state *lsp;
6051 struct nfs_open_context *ctx;
6052 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006053 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006054 int rpc_status;
6055 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006056 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006057};
6058
6059static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006060 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6061 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006062{
6063 struct nfs4_lockdata *p;
6064 struct inode *inode = lsp->ls_state->inode;
6065 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006066 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006067
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006068 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006069 if (p == NULL)
6070 return NULL;
6071
6072 p->arg.fh = NFS_FH(inode);
6073 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006074 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006075 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006076 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006077 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6078 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006079 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006080 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006081 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006082 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006083 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006084 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006086 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087 atomic_inc(&lsp->ls_count);
6088 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006089 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006090 memcpy(&p->fl, fl, sizeof(p->fl));
6091 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006092out_free_seqid:
6093 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094out_free:
6095 kfree(p);
6096 return NULL;
6097}
6098
6099static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6100{
6101 struct nfs4_lockdata *data = calldata;
6102 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103
Harvey Harrison3110ff82008-05-02 13:42:44 -07006104 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006105 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006106 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006107 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006108 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006109 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006110 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006111 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006112 nfs4_stateid_copy(&data->arg.open_stateid,
6113 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006114 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006115 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006116 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006117 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006118 nfs4_stateid_copy(&data->arg.lock_stateid,
6119 &data->lsp->ls_stateid);
6120 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006121 if (!nfs4_valid_open_stateid(state)) {
6122 data->rpc_status = -EBADF;
6123 task->tk_action = NULL;
6124 goto out_release_open_seqid;
6125 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006126 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006127 if (nfs4_setup_sequence(data->server,
6128 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006129 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006130 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006131 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006132out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006133 nfs_release_seqid(data->arg.open_seqid);
6134out_release_lock_seqid:
6135 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006136out_wait:
6137 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006138 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006139}
6140
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006141static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6142{
6143 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006144 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145
Harvey Harrison3110ff82008-05-02 13:42:44 -07006146 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147
Trond Myklebust14516c32010-07-31 14:29:06 -04006148 if (!nfs4_sequence_done(task, &data->res.seq_res))
6149 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006150
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006152 switch (task->tk_status) {
6153 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006154 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006155 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006156 if (data->arg.new_lock) {
6157 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006158 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006159 rpc_restart_call_prepare(task);
6160 break;
6161 }
6162 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006163 if (data->arg.new_lock_owner != 0) {
6164 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6165 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6166 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6167 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6168 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006169 break;
6170 case -NFS4ERR_BAD_STATEID:
6171 case -NFS4ERR_OLD_STATEID:
6172 case -NFS4ERR_STALE_STATEID:
6173 case -NFS4ERR_EXPIRED:
6174 if (data->arg.new_lock_owner != 0) {
6175 if (!nfs4_stateid_match(&data->arg.open_stateid,
6176 &lsp->ls_state->open_stateid))
6177 rpc_restart_call_prepare(task);
6178 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6179 &lsp->ls_stateid))
6180 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006182 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006183}
6184
6185static void nfs4_lock_release(void *calldata)
6186{
6187 struct nfs4_lockdata *data = calldata;
6188
Harvey Harrison3110ff82008-05-02 13:42:44 -07006189 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006190 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006191 if (data->cancelled != 0) {
6192 struct rpc_task *task;
6193 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6194 data->arg.lock_seqid);
6195 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006196 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006197 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006198 } else
6199 nfs_free_seqid(data->arg.lock_seqid);
6200 nfs4_put_lock_state(data->lsp);
6201 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006202 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006203 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006204 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205}
6206
6207static const struct rpc_call_ops nfs4_lock_ops = {
6208 .rpc_call_prepare = nfs4_lock_prepare,
6209 .rpc_call_done = nfs4_lock_done,
6210 .rpc_release = nfs4_lock_release,
6211};
6212
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006213static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6214{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006215 switch (error) {
6216 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006217 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006218 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006219 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006220 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006221 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006222 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006223 break;
6224 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006225 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006226 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006227 };
6228}
6229
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006230static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231{
6232 struct nfs4_lockdata *data;
6233 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006234 struct rpc_message msg = {
6235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6236 .rpc_cred = state->owner->so_cred,
6237 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006238 struct rpc_task_setup task_setup_data = {
6239 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006240 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006241 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006242 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006243 .flags = RPC_TASK_ASYNC,
6244 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006245 int ret;
6246
Harvey Harrison3110ff82008-05-02 13:42:44 -07006247 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006248 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006249 fl->fl_u.nfs4_fl.owner,
6250 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006251 if (data == NULL)
6252 return -ENOMEM;
6253 if (IS_SETLKW(cmd))
6254 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006255 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006256 msg.rpc_argp = &data->arg;
6257 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006258 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006259 if (recovery_type > NFS_LOCK_NEW) {
6260 if (recovery_type == NFS_LOCK_RECLAIM)
6261 data->arg.reclaim = NFS_LOCK_RECLAIM;
6262 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006263 } else
6264 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006265 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006266 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006267 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006268 ret = nfs4_wait_for_completion_rpc_task(task);
6269 if (ret == 0) {
6270 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006271 if (ret)
6272 nfs4_handle_setlk_error(data->server, data->lsp,
6273 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274 } else
6275 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006276 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006277 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006278 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
6282static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6283{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006284 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006285 struct nfs4_exception exception = {
6286 .inode = state->inode,
6287 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006288 int err;
6289
6290 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006291 /* Cache the lock if possible... */
6292 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6293 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006294 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006295 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006296 break;
6297 nfs4_handle_exception(server, err, &exception);
6298 } while (exception.retry);
6299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300}
6301
6302static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6303{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006304 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006305 struct nfs4_exception exception = {
6306 .inode = state->inode,
6307 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006308 int err;
6309
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006310 err = nfs4_set_lock_state(state, request);
6311 if (err != 0)
6312 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006313 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006314 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6315 return 0;
6316 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006317 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006318 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6319 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006320 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006321 switch (err) {
6322 default:
6323 goto out;
6324 case -NFS4ERR_GRACE:
6325 case -NFS4ERR_DELAY:
6326 nfs4_handle_exception(server, err, &exception);
6327 err = 0;
6328 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006329 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006330out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332}
6333
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006334#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006335static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6336{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006337 struct nfs4_lock_state *lsp;
6338 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006339
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006340 status = nfs4_set_lock_state(state, request);
6341 if (status != 0)
6342 return status;
6343 lsp = request->fl_u.nfs4_fl.owner;
6344 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6345 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6346 return 0;
6347 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006348 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006349}
6350#endif
6351
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6353{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006354 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006355 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006356 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006357 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Trond Myklebust01c3b862006-06-29 16:38:39 -04006359 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006360 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006361 if (status < 0)
6362 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006363 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006364 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006365 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006366 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006367 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006368 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006369 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006370 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006371 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006372 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006373 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006374 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006375 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006376 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006377out:
6378 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 return status;
6380}
6381
6382static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6383{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006384 struct nfs4_exception exception = {
6385 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006386 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006387 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 int err;
6389
6390 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006391 err = _nfs4_proc_setlk(state, cmd, request);
6392 if (err == -NFS4ERR_DENIED)
6393 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006395 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 } while (exception.retry);
6397 return err;
6398}
6399
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006400#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6401#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6402
6403static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006404nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6405 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006406{
6407 int status = -ERESTARTSYS;
6408 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6409
6410 while(!signalled()) {
6411 status = nfs4_proc_setlk(state, cmd, request);
6412 if ((status != -EAGAIN) || IS_SETLK(cmd))
6413 break;
6414 freezable_schedule_timeout_interruptible(timeout);
6415 timeout *= 2;
6416 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6417 status = -ERESTARTSYS;
6418 }
6419 return status;
6420}
6421
Jeff Laytona1d617d82016-09-17 18:17:39 -04006422#ifdef CONFIG_NFS_V4_1
6423struct nfs4_lock_waiter {
6424 struct task_struct *task;
6425 struct inode *inode;
6426 struct nfs_lowner *owner;
6427 bool notified;
6428};
6429
6430static int
6431nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6432{
6433 int ret;
6434 struct cb_notify_lock_args *cbnl = key;
6435 struct nfs4_lock_waiter *waiter = wait->private;
6436 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6437 *wowner = waiter->owner;
6438
6439 /* Only wake if the callback was for the same owner */
6440 if (lowner->clientid != wowner->clientid ||
6441 lowner->id != wowner->id ||
6442 lowner->s_dev != wowner->s_dev)
6443 return 0;
6444
6445 /* Make sure it's for the right inode */
6446 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6447 return 0;
6448
6449 waiter->notified = true;
6450
6451 /* override "private" so we can use default_wake_function */
6452 wait->private = waiter->task;
6453 ret = autoremove_wake_function(wait, mode, flags, key);
6454 wait->private = waiter;
6455 return ret;
6456}
6457
6458static int
6459nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6460{
6461 int status = -ERESTARTSYS;
6462 unsigned long flags;
6463 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6464 struct nfs_server *server = NFS_SERVER(state->inode);
6465 struct nfs_client *clp = server->nfs_client;
6466 wait_queue_head_t *q = &clp->cl_lock_waitq;
6467 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6468 .id = lsp->ls_seqid.owner_id,
6469 .s_dev = server->s_dev };
6470 struct nfs4_lock_waiter waiter = { .task = current,
6471 .inode = state->inode,
6472 .owner = &owner,
6473 .notified = false };
6474 wait_queue_t wait;
6475
6476 /* Don't bother with waitqueue if we don't expect a callback */
6477 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6478 return nfs4_retry_setlk_simple(state, cmd, request);
6479
6480 init_wait(&wait);
6481 wait.private = &waiter;
6482 wait.func = nfs4_wake_lock_waiter;
6483 add_wait_queue(q, &wait);
6484
6485 while(!signalled()) {
6486 status = nfs4_proc_setlk(state, cmd, request);
6487 if ((status != -EAGAIN) || IS_SETLK(cmd))
6488 break;
6489
6490 status = -ERESTARTSYS;
6491 spin_lock_irqsave(&q->lock, flags);
6492 if (waiter.notified) {
6493 spin_unlock_irqrestore(&q->lock, flags);
6494 continue;
6495 }
6496 set_current_state(TASK_INTERRUPTIBLE);
6497 spin_unlock_irqrestore(&q->lock, flags);
6498
6499 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6500 }
6501
6502 finish_wait(q, &wait);
6503 return status;
6504}
6505#else /* !CONFIG_NFS_V4_1 */
6506static inline int
6507nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6508{
6509 return nfs4_retry_setlk_simple(state, cmd, request);
6510}
6511#endif
6512
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513static int
6514nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6515{
6516 struct nfs_open_context *ctx;
6517 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 int status;
6519
6520 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006521 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 state = ctx->state;
6523
6524 if (request->fl_start < 0 || request->fl_end < 0)
6525 return -EINVAL;
6526
Trond Myklebustd9531262009-07-21 19:22:38 -04006527 if (IS_GETLK(cmd)) {
6528 if (state != NULL)
6529 return nfs4_proc_getlk(state, F_GETLK, request);
6530 return 0;
6531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532
6533 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6534 return -EINVAL;
6535
Trond Myklebustd9531262009-07-21 19:22:38 -04006536 if (request->fl_type == F_UNLCK) {
6537 if (state != NULL)
6538 return nfs4_proc_unlck(state, cmd, request);
6539 return 0;
6540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
Trond Myklebustd9531262009-07-21 19:22:38 -04006542 if (state == NULL)
6543 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006544
6545 if ((request->fl_flags & FL_POSIX) &&
6546 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6547 return -ENOLCK;
6548
Trond Myklebust55725512012-04-18 12:48:35 -04006549 /*
6550 * Don't rely on the VFS having checked the file open mode,
6551 * since it won't do this for flock() locks.
6552 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006553 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006554 case F_RDLCK:
6555 if (!(filp->f_mode & FMODE_READ))
6556 return -EBADF;
6557 break;
6558 case F_WRLCK:
6559 if (!(filp->f_mode & FMODE_WRITE))
6560 return -EBADF;
6561 }
6562
Jeff Layton1ea67db2016-09-17 18:17:37 -04006563 status = nfs4_set_lock_state(state, request);
6564 if (status != 0)
6565 return status;
6566
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006567 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568}
6569
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006570int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006571{
6572 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006573 int err;
6574
6575 err = nfs4_set_lock_state(state, fl);
6576 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006577 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006578 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006579 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006580}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006581
Trond Myklebustcf470c32012-03-07 13:49:12 -05006582struct nfs_release_lockowner_data {
6583 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006584 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006585 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006586 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006587 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006588};
6589
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006590static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6591{
6592 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006593 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006594 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6595 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006596 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006597 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006598}
6599
6600static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6601{
6602 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006603 struct nfs_server *server = data->server;
6604
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006605 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006606
6607 switch (task->tk_status) {
6608 case 0:
6609 renew_lease(server, data->timestamp);
6610 break;
6611 case -NFS4ERR_STALE_CLIENTID:
6612 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006613 nfs4_schedule_lease_recovery(server->nfs_client);
6614 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006615 case -NFS4ERR_LEASE_MOVED:
6616 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006617 if (nfs4_async_handle_error(task, server,
6618 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006619 rpc_restart_call_prepare(task);
6620 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006621}
6622
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006623static void nfs4_release_lockowner_release(void *calldata)
6624{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006625 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006626 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006627 kfree(calldata);
6628}
6629
Trond Myklebust17280172012-03-11 13:11:00 -04006630static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006631 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6632 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006633 .rpc_release = nfs4_release_lockowner_release,
6634};
6635
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006636static void
6637nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006638{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006639 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006640 struct rpc_message msg = {
6641 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6642 };
6643
6644 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006645 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006646
Trond Myklebustcf470c32012-03-07 13:49:12 -05006647 data = kmalloc(sizeof(*data), GFP_NOFS);
6648 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006649 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006650 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006651 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006652 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6653 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6654 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006655
Trond Myklebustcf470c32012-03-07 13:49:12 -05006656 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006657 msg.rpc_resp = &data->res;
6658 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006659 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006660}
6661
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006662#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6663
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006664static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006665 struct dentry *unused, struct inode *inode,
6666 const char *key, const void *buf,
6667 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006668{
Al Viro59301222016-05-27 10:19:30 -04006669 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006670}
6671
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006672static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006673 struct dentry *unused, struct inode *inode,
6674 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006675{
Al Virob2968212016-04-10 20:48:24 -04006676 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006677}
6678
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006679static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006680{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006681 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006682}
6683
David Quigleyc9bccef2013-05-22 12:50:45 -04006684#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006685
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006686static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006687 struct dentry *unused, struct inode *inode,
6688 const char *key, const void *buf,
6689 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006690{
6691 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006692 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006693
6694 return -EOPNOTSUPP;
6695}
6696
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006697static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006698 struct dentry *unused, struct inode *inode,
6699 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006700{
6701 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006702 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006703 return -EOPNOTSUPP;
6704}
6705
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006706static ssize_t
6707nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006708{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006709 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006710
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006711 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6712 len = security_inode_listsecurity(inode, list, list_len);
6713 if (list_len && len > list_len)
6714 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006715 }
6716 return len;
6717}
6718
6719static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6720 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006721 .get = nfs4_xattr_get_nfs4_label,
6722 .set = nfs4_xattr_set_nfs4_label,
6723};
David Quigleyc9bccef2013-05-22 12:50:45 -04006724
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006725#else
6726
6727static ssize_t
6728nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6729{
6730 return 0;
6731}
6732
6733#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006734
Andy Adamson533eb462011-06-13 18:25:56 -04006735/*
6736 * nfs_fhget will use either the mounted_on_fileid or the fileid
6737 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006738static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6739{
Andy Adamson533eb462011-06-13 18:25:56 -04006740 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6741 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6742 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006743 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006744 return;
6745
6746 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006747 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006748 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6749 fattr->nlink = 2;
6750}
6751
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006752static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6753 const struct qstr *name,
6754 struct nfs4_fs_locations *fs_locations,
6755 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006756{
6757 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006758 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006759 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006760 };
6761 struct nfs4_fs_locations_arg args = {
6762 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006763 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006764 .page = page,
6765 .bitmask = bitmask,
6766 };
Benny Halevy22958462009-04-01 09:22:02 -04006767 struct nfs4_fs_locations_res res = {
6768 .fs_locations = fs_locations,
6769 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006770 struct rpc_message msg = {
6771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6772 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006773 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006774 };
6775 int status;
6776
Harvey Harrison3110ff82008-05-02 13:42:44 -07006777 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006778
6779 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6780 * is not supported */
6781 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6782 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6783 else
6784 bitmask[0] |= FATTR4_WORD0_FILEID;
6785
Trond Myklebustc228fd32007-01-13 02:28:11 -05006786 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006787 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006788 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006789 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006790 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006791 return status;
6792}
6793
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006794int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6795 const struct qstr *name,
6796 struct nfs4_fs_locations *fs_locations,
6797 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006798{
6799 struct nfs4_exception exception = { };
6800 int err;
6801 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006802 err = _nfs4_proc_fs_locations(client, dir, name,
6803 fs_locations, page);
6804 trace_nfs4_get_fs_locations(dir, name, err);
6805 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006806 &exception);
6807 } while (exception.retry);
6808 return err;
6809}
6810
Chuck Leverb03d7352013-10-17 14:12:50 -04006811/*
6812 * This operation also signals the server that this client is
6813 * performing migration recovery. The server can stop returning
6814 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6815 * appended to this compound to identify the client ID which is
6816 * performing recovery.
6817 */
6818static int _nfs40_proc_get_locations(struct inode *inode,
6819 struct nfs4_fs_locations *locations,
6820 struct page *page, struct rpc_cred *cred)
6821{
6822 struct nfs_server *server = NFS_SERVER(inode);
6823 struct rpc_clnt *clnt = server->client;
6824 u32 bitmask[2] = {
6825 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6826 };
6827 struct nfs4_fs_locations_arg args = {
6828 .clientid = server->nfs_client->cl_clientid,
6829 .fh = NFS_FH(inode),
6830 .page = page,
6831 .bitmask = bitmask,
6832 .migration = 1, /* skip LOOKUP */
6833 .renew = 1, /* append RENEW */
6834 };
6835 struct nfs4_fs_locations_res res = {
6836 .fs_locations = locations,
6837 .migration = 1,
6838 .renew = 1,
6839 };
6840 struct rpc_message msg = {
6841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6842 .rpc_argp = &args,
6843 .rpc_resp = &res,
6844 .rpc_cred = cred,
6845 };
6846 unsigned long now = jiffies;
6847 int status;
6848
6849 nfs_fattr_init(&locations->fattr);
6850 locations->server = server;
6851 locations->nlocations = 0;
6852
6853 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6854 nfs4_set_sequence_privileged(&args.seq_args);
6855 status = nfs4_call_sync_sequence(clnt, server, &msg,
6856 &args.seq_args, &res.seq_res);
6857 if (status)
6858 return status;
6859
6860 renew_lease(server, now);
6861 return 0;
6862}
6863
6864#ifdef CONFIG_NFS_V4_1
6865
6866/*
6867 * This operation also signals the server that this client is
6868 * performing migration recovery. The server can stop asserting
6869 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6870 * performing this operation is identified in the SEQUENCE
6871 * operation in this compound.
6872 *
6873 * When the client supports GETATTR(fs_locations_info), it can
6874 * be plumbed in here.
6875 */
6876static int _nfs41_proc_get_locations(struct inode *inode,
6877 struct nfs4_fs_locations *locations,
6878 struct page *page, struct rpc_cred *cred)
6879{
6880 struct nfs_server *server = NFS_SERVER(inode);
6881 struct rpc_clnt *clnt = server->client;
6882 u32 bitmask[2] = {
6883 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6884 };
6885 struct nfs4_fs_locations_arg args = {
6886 .fh = NFS_FH(inode),
6887 .page = page,
6888 .bitmask = bitmask,
6889 .migration = 1, /* skip LOOKUP */
6890 };
6891 struct nfs4_fs_locations_res res = {
6892 .fs_locations = locations,
6893 .migration = 1,
6894 };
6895 struct rpc_message msg = {
6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6897 .rpc_argp = &args,
6898 .rpc_resp = &res,
6899 .rpc_cred = cred,
6900 };
6901 int status;
6902
6903 nfs_fattr_init(&locations->fattr);
6904 locations->server = server;
6905 locations->nlocations = 0;
6906
6907 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6908 nfs4_set_sequence_privileged(&args.seq_args);
6909 status = nfs4_call_sync_sequence(clnt, server, &msg,
6910 &args.seq_args, &res.seq_res);
6911 if (status == NFS4_OK &&
6912 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6913 status = -NFS4ERR_LEASE_MOVED;
6914 return status;
6915}
6916
6917#endif /* CONFIG_NFS_V4_1 */
6918
6919/**
6920 * nfs4_proc_get_locations - discover locations for a migrated FSID
6921 * @inode: inode on FSID that is migrating
6922 * @locations: result of query
6923 * @page: buffer
6924 * @cred: credential to use for this operation
6925 *
6926 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6927 * operation failed, or a negative errno if a local error occurred.
6928 *
6929 * On success, "locations" is filled in, but if the server has
6930 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6931 * asserted.
6932 *
6933 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6934 * from this client that require migration recovery.
6935 */
6936int nfs4_proc_get_locations(struct inode *inode,
6937 struct nfs4_fs_locations *locations,
6938 struct page *page, struct rpc_cred *cred)
6939{
6940 struct nfs_server *server = NFS_SERVER(inode);
6941 struct nfs_client *clp = server->nfs_client;
6942 const struct nfs4_mig_recovery_ops *ops =
6943 clp->cl_mvops->mig_recovery_ops;
6944 struct nfs4_exception exception = { };
6945 int status;
6946
6947 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6948 (unsigned long long)server->fsid.major,
6949 (unsigned long long)server->fsid.minor,
6950 clp->cl_hostname);
6951 nfs_display_fhandle(NFS_FH(inode), __func__);
6952
6953 do {
6954 status = ops->get_locations(inode, locations, page, cred);
6955 if (status != -NFS4ERR_DELAY)
6956 break;
6957 nfs4_handle_exception(server, status, &exception);
6958 } while (exception.retry);
6959 return status;
6960}
6961
Chuck Lever44c99932013-10-17 14:13:30 -04006962/*
6963 * This operation also signals the server that this client is
6964 * performing "lease moved" recovery. The server can stop
6965 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6966 * is appended to this compound to identify the client ID which is
6967 * performing recovery.
6968 */
6969static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6970{
6971 struct nfs_server *server = NFS_SERVER(inode);
6972 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6973 struct rpc_clnt *clnt = server->client;
6974 struct nfs4_fsid_present_arg args = {
6975 .fh = NFS_FH(inode),
6976 .clientid = clp->cl_clientid,
6977 .renew = 1, /* append RENEW */
6978 };
6979 struct nfs4_fsid_present_res res = {
6980 .renew = 1,
6981 };
6982 struct rpc_message msg = {
6983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6984 .rpc_argp = &args,
6985 .rpc_resp = &res,
6986 .rpc_cred = cred,
6987 };
6988 unsigned long now = jiffies;
6989 int status;
6990
6991 res.fh = nfs_alloc_fhandle();
6992 if (res.fh == NULL)
6993 return -ENOMEM;
6994
6995 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6996 nfs4_set_sequence_privileged(&args.seq_args);
6997 status = nfs4_call_sync_sequence(clnt, server, &msg,
6998 &args.seq_args, &res.seq_res);
6999 nfs_free_fhandle(res.fh);
7000 if (status)
7001 return status;
7002
7003 do_renew_lease(clp, now);
7004 return 0;
7005}
7006
7007#ifdef CONFIG_NFS_V4_1
7008
7009/*
7010 * This operation also signals the server that this client is
7011 * performing "lease moved" recovery. The server can stop asserting
7012 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7013 * this operation is identified in the SEQUENCE operation in this
7014 * compound.
7015 */
7016static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7017{
7018 struct nfs_server *server = NFS_SERVER(inode);
7019 struct rpc_clnt *clnt = server->client;
7020 struct nfs4_fsid_present_arg args = {
7021 .fh = NFS_FH(inode),
7022 };
7023 struct nfs4_fsid_present_res res = {
7024 };
7025 struct rpc_message msg = {
7026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7027 .rpc_argp = &args,
7028 .rpc_resp = &res,
7029 .rpc_cred = cred,
7030 };
7031 int status;
7032
7033 res.fh = nfs_alloc_fhandle();
7034 if (res.fh == NULL)
7035 return -ENOMEM;
7036
7037 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7038 nfs4_set_sequence_privileged(&args.seq_args);
7039 status = nfs4_call_sync_sequence(clnt, server, &msg,
7040 &args.seq_args, &res.seq_res);
7041 nfs_free_fhandle(res.fh);
7042 if (status == NFS4_OK &&
7043 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7044 status = -NFS4ERR_LEASE_MOVED;
7045 return status;
7046}
7047
7048#endif /* CONFIG_NFS_V4_1 */
7049
7050/**
7051 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7052 * @inode: inode on FSID to check
7053 * @cred: credential to use for this operation
7054 *
7055 * Server indicates whether the FSID is present, moved, or not
7056 * recognized. This operation is necessary to clear a LEASE_MOVED
7057 * condition for this client ID.
7058 *
7059 * Returns NFS4_OK if the FSID is present on this server,
7060 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7061 * NFS4ERR code if some error occurred on the server, or a
7062 * negative errno if a local failure occurred.
7063 */
7064int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7065{
7066 struct nfs_server *server = NFS_SERVER(inode);
7067 struct nfs_client *clp = server->nfs_client;
7068 const struct nfs4_mig_recovery_ops *ops =
7069 clp->cl_mvops->mig_recovery_ops;
7070 struct nfs4_exception exception = { };
7071 int status;
7072
7073 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7074 (unsigned long long)server->fsid.major,
7075 (unsigned long long)server->fsid.minor,
7076 clp->cl_hostname);
7077 nfs_display_fhandle(NFS_FH(inode), __func__);
7078
7079 do {
7080 status = ops->fsid_present(inode, cred);
7081 if (status != -NFS4ERR_DELAY)
7082 break;
7083 nfs4_handle_exception(server, status, &exception);
7084 } while (exception.retry);
7085 return status;
7086}
7087
Andy Adamson5ec16a82013-08-08 10:57:55 -04007088/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007089 * If 'use_integrity' is true and the state managment nfs_client
7090 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7091 * and the machine credential as per RFC3530bis and RFC5661 Security
7092 * Considerations sections. Otherwise, just use the user cred with the
7093 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007094 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007095static 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 +00007096{
7097 int status;
7098 struct nfs4_secinfo_arg args = {
7099 .dir_fh = NFS_FH(dir),
7100 .name = name,
7101 };
7102 struct nfs4_secinfo_res res = {
7103 .flavors = flavors,
7104 };
7105 struct rpc_message msg = {
7106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7107 .rpc_argp = &args,
7108 .rpc_resp = &res,
7109 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007110 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007111 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007112
7113 if (use_integrity) {
7114 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007115 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7116 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007117 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007118
7119 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007120
7121 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7122 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7123
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007124 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7125 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007126 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007127
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007128 if (cred)
7129 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007130
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007131 return status;
7132}
7133
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007134int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7135 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007136{
7137 struct nfs4_exception exception = { };
7138 int err;
7139 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007140 err = -NFS4ERR_WRONGSEC;
7141
7142 /* try to use integrity protection with machine cred */
7143 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7144 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7145
7146 /*
7147 * if unable to use integrity protection, or SECINFO with
7148 * integrity protection returns NFS4ERR_WRONGSEC (which is
7149 * disallowed by spec, but exists in deployed servers) use
7150 * the current filesystem's rpc_client and the user cred.
7151 */
7152 if (err == -NFS4ERR_WRONGSEC)
7153 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7154
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007155 trace_nfs4_secinfo(dir, name, err);
7156 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007157 &exception);
7158 } while (exception.retry);
7159 return err;
7160}
7161
Andy Adamson557134a2009-04-01 09:21:53 -04007162#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007163/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007164 * Check the exchange flags returned by the server for invalid flags, having
7165 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7166 * DS flags set.
7167 */
7168static int nfs4_check_cl_exchange_flags(u32 flags)
7169{
7170 if (flags & ~EXCHGID4_FLAG_MASK_R)
7171 goto out_inval;
7172 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7173 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7174 goto out_inval;
7175 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7176 goto out_inval;
7177 return NFS_OK;
7178out_inval:
7179 return -NFS4ERR_INVAL;
7180}
7181
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007182static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007183nfs41_same_server_scope(struct nfs41_server_scope *a,
7184 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007185{
7186 if (a->server_scope_sz == b->server_scope_sz &&
7187 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7188 return true;
7189
7190 return false;
7191}
7192
Andy Adamson02a95de2016-02-05 16:08:37 -05007193static void
7194nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7195{
7196}
7197
7198static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7199 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7200};
7201
Andy Adamson357f54d2010-12-14 10:11:57 -05007202/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007203 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007204 *
7205 * The 4.1 client currently uses the same TCP connection for the
7206 * fore and backchannel.
7207 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007208static
7209int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7210 struct rpc_xprt *xprt,
7211 struct nfs_client *clp,
7212 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007213{
7214 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007215 struct nfs41_bind_conn_to_session_args args = {
7216 .client = clp,
7217 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7218 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219 struct nfs41_bind_conn_to_session_res res;
7220 struct rpc_message msg = {
7221 .rpc_proc =
7222 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007223 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007224 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007225 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007226 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007227 struct rpc_task_setup task_setup_data = {
7228 .rpc_client = clnt,
7229 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007230 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007231 .rpc_message = &msg,
7232 .flags = RPC_TASK_TIMEOUT,
7233 };
7234 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007235
7236 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007237
Trond Myklebust71a097c2015-02-18 09:27:18 -08007238 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7239 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7240 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007241
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007242 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7243 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7244 args.dir = NFS4_CDFC4_FORE;
7245
7246 task = rpc_run_task(&task_setup_data);
7247 if (!IS_ERR(task)) {
7248 status = task->tk_status;
7249 rpc_put_task(task);
7250 } else
7251 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007252 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007253 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007254 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007255 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7256 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7257 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007258 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007260 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007261 dprintk("NFS: %s: Unexpected direction from server\n",
7262 __func__);
7263 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007264 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007265 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007266 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7268 __func__);
7269 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007270 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007271 }
7272 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273out:
7274 dprintk("<-- %s status= %d\n", __func__, status);
7275 return status;
7276}
7277
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007278struct rpc_bind_conn_calldata {
7279 struct nfs_client *clp;
7280 struct rpc_cred *cred;
7281};
7282
7283static int
7284nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7285 struct rpc_xprt *xprt,
7286 void *calldata)
7287{
7288 struct rpc_bind_conn_calldata *p = calldata;
7289
7290 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7291}
7292
7293int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7294{
7295 struct rpc_bind_conn_calldata data = {
7296 .clp = clp,
7297 .cred = cred,
7298 };
7299 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7300 nfs4_proc_bind_conn_to_session_callback, &data);
7301}
7302
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007304 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7305 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007306 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007307static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7308 .how = SP4_MACH_CRED,
7309 .enforce.u.words = {
7310 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7311 1 << (OP_EXCHANGE_ID - 32) |
7312 1 << (OP_CREATE_SESSION - 32) |
7313 1 << (OP_DESTROY_SESSION - 32) |
7314 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007315 },
7316 .allow.u.words = {
7317 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007318 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007319 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007320 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007321 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007322 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007323 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007324 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007325 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007326 1 << (OP_FREE_STATEID - 32) |
7327 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007328 }
7329};
7330
7331/*
7332 * Select the state protection mode for client `clp' given the server results
7333 * from exchange_id in `sp'.
7334 *
7335 * Returns 0 on success, negative errno otherwise.
7336 */
7337static int nfs4_sp4_select_mode(struct nfs_client *clp,
7338 struct nfs41_state_protection *sp)
7339{
7340 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7341 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7342 1 << (OP_EXCHANGE_ID - 32) |
7343 1 << (OP_CREATE_SESSION - 32) |
7344 1 << (OP_DESTROY_SESSION - 32) |
7345 1 << (OP_DESTROY_CLIENTID - 32)
7346 };
7347 unsigned int i;
7348
7349 if (sp->how == SP4_MACH_CRED) {
7350 /* Print state protect result */
7351 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7352 for (i = 0; i <= LAST_NFS4_OP; i++) {
7353 if (test_bit(i, sp->enforce.u.longs))
7354 dfprintk(MOUNT, " enforce op %d\n", i);
7355 if (test_bit(i, sp->allow.u.longs))
7356 dfprintk(MOUNT, " allow op %d\n", i);
7357 }
7358
7359 /* make sure nothing is on enforce list that isn't supported */
7360 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7361 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7362 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7363 return -EINVAL;
7364 }
7365 }
7366
7367 /*
7368 * Minimal mode - state operations are allowed to use machine
7369 * credential. Note this already happens by default, so the
7370 * client doesn't have to do anything more than the negotiation.
7371 *
7372 * NOTE: we don't care if EXCHANGE_ID is in the list -
7373 * we're already using the machine cred for exchange_id
7374 * and will never use a different cred.
7375 */
7376 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7377 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7378 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7379 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7380 dfprintk(MOUNT, "sp4_mach_cred:\n");
7381 dfprintk(MOUNT, " minimal mode enabled\n");
7382 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7383 } else {
7384 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7385 return -EINVAL;
7386 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007387
7388 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007389 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7390 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007391 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7392 dfprintk(MOUNT, " cleanup mode enabled\n");
7393 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7394 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007395
Andrew Elble99ade3c2015-12-02 09:39:51 -05007396 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7397 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7398 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7399 &clp->cl_sp4_flags);
7400 }
7401
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007402 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7403 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7404 dfprintk(MOUNT, " secinfo mode enabled\n");
7405 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7406 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007407
7408 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7409 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7410 dfprintk(MOUNT, " stateid mode enabled\n");
7411 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7412 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007413
7414 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7415 dfprintk(MOUNT, " write mode enabled\n");
7416 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7417 }
7418
7419 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7420 dfprintk(MOUNT, " commit mode enabled\n");
7421 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7422 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007423 }
7424
7425 return 0;
7426}
7427
Andy Adamson8d89bd72016-09-09 09:22:18 -04007428struct nfs41_exchange_id_data {
7429 struct nfs41_exchange_id_res res;
7430 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007431 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007432 int rpc_status;
7433};
7434
7435static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007436{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007437 struct nfs41_exchange_id_data *cdata =
7438 (struct nfs41_exchange_id_data *)data;
7439 struct nfs_client *clp = cdata->args.client;
7440 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007441
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007442 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007443
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007444 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007445 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007446
7447 if (cdata->xprt && status == 0) {
7448 status = nfs4_detect_session_trunking(clp, &cdata->res,
7449 cdata->xprt);
7450 goto out;
7451 }
7452
Andy Adamson8d89bd72016-09-09 09:22:18 -04007453 if (status == 0)
7454 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007455
Chuck Lever177313f2012-05-21 22:44:58 -04007456 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457 clp->cl_clientid = cdata->res.clientid;
7458 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007459 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007460 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007461 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007462 &clp->cl_session->session_state);
7463 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007464 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007465
Chuck Leveracdeb692012-05-21 22:46:16 -04007466 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 clp->cl_serverowner = cdata->res.server_owner;
7468 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007469
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007470 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007471 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007472 clp->cl_implid = cdata->res.impl_id;
7473 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007474
Chuck Lever177313f2012-05-21 22:44:58 -04007475 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007476 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007478 dprintk("%s: server_scope mismatch detected\n",
7479 __func__);
7480 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007481 kfree(clp->cl_serverscope);
7482 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007483 }
7484
Chuck Lever177313f2012-05-21 22:44:58 -04007485 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 clp->cl_serverscope = cdata->res.server_scope;
7487 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007488 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007489 /* Save the EXCHANGE_ID verifier session trunk tests */
7490 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7491 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007493out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007494 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007495 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007496}
7497
7498static void nfs4_exchange_id_release(void *data)
7499{
7500 struct nfs41_exchange_id_data *cdata =
7501 (struct nfs41_exchange_id_data *)data;
7502
7503 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007504 if (cdata->xprt) {
7505 xprt_put(cdata->xprt);
7506 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7507 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007508 kfree(cdata->res.impl_id);
7509 kfree(cdata->res.server_scope);
7510 kfree(cdata->res.server_owner);
7511 kfree(cdata);
7512}
7513
7514static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7515 .rpc_call_done = nfs4_exchange_id_done,
7516 .rpc_release = nfs4_exchange_id_release,
7517};
7518
Benny Halevy99fe60d2009-04-01 09:22:29 -04007519/*
7520 * _nfs4_proc_exchange_id()
7521 *
7522 * Wrapper for EXCHANGE_ID operation.
7523 */
7524static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007525 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007526{
7527 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007528 struct rpc_message msg = {
7529 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007530 .rpc_cred = cred,
7531 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 struct rpc_task_setup task_setup_data = {
7533 .rpc_client = clp->cl_rpcclient,
7534 .callback_ops = &nfs4_exchange_id_call_ops,
7535 .rpc_message = &msg,
7536 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7537 };
7538 struct nfs41_exchange_id_data *calldata;
7539 struct rpc_task *task;
7540 int status = -EIO;
7541
7542 if (!atomic_inc_not_zero(&clp->cl_count))
7543 goto out;
7544
7545 status = -ENOMEM;
7546 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7547 if (!calldata)
7548 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549
Andy Adamsonad0849a2016-09-09 09:22:28 -04007550 if (!xprt)
7551 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007552
7553 status = nfs4_init_uniform_client_string(clp);
7554 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007555 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007556
7557 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7558 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7559 clp->cl_owner_id);
7560
Andy Adamson8d89bd72016-09-09 09:22:18 -04007561 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7562 GFP_NOFS);
7563 status = -ENOMEM;
7564 if (unlikely(calldata->res.server_owner == NULL))
7565 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007566
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007568 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007571
Andy Adamson8d89bd72016-09-09 09:22:18 -04007572 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7573 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007574 goto out_server_scope;
7575
7576 switch (sp4_how) {
7577 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007578 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007579 break;
7580
7581 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007582 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007583 break;
7584
7585 default:
7586 /* unsupported! */
7587 WARN_ON_ONCE(1);
7588 status = -EINVAL;
7589 goto out_impl_id;
7590 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007591 if (xprt) {
7592 calldata->xprt = xprt;
7593 task_setup_data.rpc_xprt = xprt;
7594 task_setup_data.flags =
7595 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7596 calldata->args.verifier = &clp->cl_confirm;
7597 } else {
7598 calldata->args.verifier = &verifier;
7599 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007600 calldata->args.client = clp;
7601#ifdef CONFIG_NFS_V4_1_MIGRATION
7602 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7603 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7604 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7605#else
7606 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7607 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7608#endif
7609 msg.rpc_argp = &calldata->args;
7610 msg.rpc_resp = &calldata->res;
7611 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007612
Andy Adamson8d89bd72016-09-09 09:22:18 -04007613 task = rpc_run_task(&task_setup_data);
7614 if (IS_ERR(task)) {
7615 status = PTR_ERR(task);
7616 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007617 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007618
Andy Adamsonad0849a2016-09-09 09:22:28 -04007619 if (!xprt) {
7620 status = rpc_wait_for_completion_task(task);
7621 if (!status)
7622 status = calldata->rpc_status;
7623 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007624 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007625
Andy Adamson8d89bd72016-09-09 09:22:18 -04007626 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007627out:
Chuck Lever177313f2012-05-21 22:44:58 -04007628 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007629 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007630 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007631 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007632 clp->cl_implid->date.seconds,
7633 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007634 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007635 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007636
7637out_impl_id:
7638 kfree(calldata->res.impl_id);
7639out_server_scope:
7640 kfree(calldata->res.server_scope);
7641out_server_owner:
7642 kfree(calldata->res.server_owner);
7643out_calldata:
7644 kfree(calldata);
7645 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007646}
7647
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007648/*
7649 * nfs4_proc_exchange_id()
7650 *
7651 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7652 *
7653 * Since the clientid has expired, all compounds using sessions
7654 * associated with the stale clientid will be returning
7655 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7656 * be in some phase of session reset.
7657 *
7658 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7659 */
7660int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7661{
7662 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7663 int status;
7664
7665 /* try SP4_MACH_CRED if krb5i/p */
7666 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7667 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007668 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007669 if (!status)
7670 return 0;
7671 }
7672
7673 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007674 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007675}
7676
Andy Adamson04fa2c62016-09-09 09:22:29 -04007677/**
7678 * nfs4_test_session_trunk
7679 *
7680 * This is an add_xprt_test() test function called from
7681 * rpc_clnt_setup_test_and_add_xprt.
7682 *
7683 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7684 * and is dereferrenced in nfs4_exchange_id_release
7685 *
7686 * Upon success, add the new transport to the rpc_clnt
7687 *
7688 * @clnt: struct rpc_clnt to get new transport
7689 * @xprt: the rpc_xprt to test
7690 * @data: call data for _nfs4_proc_exchange_id.
7691 */
7692int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7693 void *data)
7694{
7695 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7696 u32 sp4_how;
7697
7698 dprintk("--> %s try %s\n", __func__,
7699 xprt->address_strings[RPC_DISPLAY_ADDR]);
7700
7701 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7702
7703 /* Test connection for session trunking. Async exchange_id call */
7704 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7705}
7706EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7707
Trond Myklebust66245532012-05-25 17:18:09 -04007708static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7709 struct rpc_cred *cred)
7710{
7711 struct rpc_message msg = {
7712 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7713 .rpc_argp = clp,
7714 .rpc_cred = cred,
7715 };
7716 int status;
7717
7718 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007719 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007720 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007721 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007722 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7723 return status;
7724}
7725
7726static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7727 struct rpc_cred *cred)
7728{
7729 unsigned int loop;
7730 int ret;
7731
7732 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7733 ret = _nfs4_proc_destroy_clientid(clp, cred);
7734 switch (ret) {
7735 case -NFS4ERR_DELAY:
7736 case -NFS4ERR_CLIENTID_BUSY:
7737 ssleep(1);
7738 break;
7739 default:
7740 return ret;
7741 }
7742 }
7743 return 0;
7744}
7745
7746int nfs4_destroy_clientid(struct nfs_client *clp)
7747{
7748 struct rpc_cred *cred;
7749 int ret = 0;
7750
7751 if (clp->cl_mvops->minor_version < 1)
7752 goto out;
7753 if (clp->cl_exchange_flags == 0)
7754 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007755 if (clp->cl_preserve_clid)
7756 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007757 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007758 ret = nfs4_proc_destroy_clientid(clp, cred);
7759 if (cred)
7760 put_rpccred(cred);
7761 switch (ret) {
7762 case 0:
7763 case -NFS4ERR_STALE_CLIENTID:
7764 clp->cl_exchange_flags = 0;
7765 }
7766out:
7767 return ret;
7768}
7769
Andy Adamson2050f0c2009-04-01 09:22:30 -04007770struct nfs4_get_lease_time_data {
7771 struct nfs4_get_lease_time_args *args;
7772 struct nfs4_get_lease_time_res *res;
7773 struct nfs_client *clp;
7774};
7775
7776static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7777 void *calldata)
7778{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007779 struct nfs4_get_lease_time_data *data =
7780 (struct nfs4_get_lease_time_data *)calldata;
7781
7782 dprintk("--> %s\n", __func__);
7783 /* just setup sequence, do not trigger session recovery
7784 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007785 nfs41_setup_sequence(data->clp->cl_session,
7786 &data->args->la_seq_args,
7787 &data->res->lr_seq_res,
7788 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007789 dprintk("<-- %s\n", __func__);
7790}
7791
7792/*
7793 * Called from nfs4_state_manager thread for session setup, so don't recover
7794 * from sequence operation or clientid errors.
7795 */
7796static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7797{
7798 struct nfs4_get_lease_time_data *data =
7799 (struct nfs4_get_lease_time_data *)calldata;
7800
7801 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007802 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7803 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007804 switch (task->tk_status) {
7805 case -NFS4ERR_DELAY:
7806 case -NFS4ERR_GRACE:
7807 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7808 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7809 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007810 /* fall through */
7811 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007812 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007813 return;
7814 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007815 dprintk("<-- %s\n", __func__);
7816}
7817
Trond Myklebust17280172012-03-11 13:11:00 -04007818static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007819 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7820 .rpc_call_done = nfs4_get_lease_time_done,
7821};
7822
7823int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7824{
7825 struct rpc_task *task;
7826 struct nfs4_get_lease_time_args args;
7827 struct nfs4_get_lease_time_res res = {
7828 .lr_fsinfo = fsinfo,
7829 };
7830 struct nfs4_get_lease_time_data data = {
7831 .args = &args,
7832 .res = &res,
7833 .clp = clp,
7834 };
7835 struct rpc_message msg = {
7836 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7837 .rpc_argp = &args,
7838 .rpc_resp = &res,
7839 };
7840 struct rpc_task_setup task_setup = {
7841 .rpc_client = clp->cl_rpcclient,
7842 .rpc_message = &msg,
7843 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007844 .callback_data = &data,
7845 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007846 };
7847 int status;
7848
Chuck Levera9c92d62013-08-09 12:48:18 -04007849 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007850 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007851 dprintk("--> %s\n", __func__);
7852 task = rpc_run_task(&task_setup);
7853
7854 if (IS_ERR(task))
7855 status = PTR_ERR(task);
7856 else {
7857 status = task->tk_status;
7858 rpc_put_task(task);
7859 }
7860 dprintk("<-- %s return %d\n", __func__, status);
7861
7862 return status;
7863}
7864
Andy Adamsonfc931582009-04-01 09:22:31 -04007865/*
7866 * Initialize the values to be used by the client in CREATE_SESSION
7867 * If nfs4_init_session set the fore channel request and response sizes,
7868 * use them.
7869 *
7870 * Set the back channel max_resp_sz_cached to zero to force the client to
7871 * always set csa_cachethis to FALSE because the current implementation
7872 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7873 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007874static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7875 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007876{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007877 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007878 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007879
Andy Adamson18aad3d2013-06-26 12:21:49 -04007880 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7881 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7882
Andy Adamsonfc931582009-04-01 09:22:31 -04007883 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007884 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7885 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007886 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007887 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007888
7889 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007890 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007891 __func__,
7892 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007893 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007894
7895 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007896 args->bc_attrs.max_rqst_sz = max_bc_payload;
7897 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007898 args->bc_attrs.max_resp_sz_cached = 0;
7899 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007900 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007901
7902 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7903 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7904 __func__,
7905 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7906 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7907 args->bc_attrs.max_reqs);
7908}
7909
Trond Myklebust79969dd2015-02-18 11:30:18 -08007910static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7911 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007912{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007913 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007914 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007915
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007916 if (rcvd->max_resp_sz > sent->max_resp_sz)
7917 return -EINVAL;
7918 /*
7919 * Our requested max_ops is the minimum we need; we're not
7920 * prepared to break up compounds into smaller pieces than that.
7921 * So, no point even trying to continue if the server won't
7922 * cooperate:
7923 */
7924 if (rcvd->max_ops < sent->max_ops)
7925 return -EINVAL;
7926 if (rcvd->max_reqs == 0)
7927 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007928 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7929 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007930 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007931}
7932
Trond Myklebust79969dd2015-02-18 11:30:18 -08007933static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7934 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007935{
7936 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007937 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007938
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007939 if (!(res->flags & SESSION4_BACK_CHAN))
7940 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007941 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7942 return -EINVAL;
7943 if (rcvd->max_resp_sz < sent->max_resp_sz)
7944 return -EINVAL;
7945 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7946 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007947 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007948 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007949 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007950 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007951out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007952 return 0;
7953}
Andy Adamson8d353012009-04-01 09:22:32 -04007954
Andy Adamson8d353012009-04-01 09:22:32 -04007955static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007956 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007957{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007958 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007959
Trond Myklebust79969dd2015-02-18 11:30:18 -08007960 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007961 if (ret)
7962 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007963 return nfs4_verify_back_channel_attrs(args, res);
7964}
7965
7966static void nfs4_update_session(struct nfs4_session *session,
7967 struct nfs41_create_session_res *res)
7968{
7969 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007970 /* Mark client id and session as being confirmed */
7971 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7972 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007973 session->flags = res->flags;
7974 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007975 if (res->flags & SESSION4_BACK_CHAN)
7976 memcpy(&session->bc_attrs, &res->bc_attrs,
7977 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007978}
7979
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007980static int _nfs4_proc_create_session(struct nfs_client *clp,
7981 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007982{
7983 struct nfs4_session *session = clp->cl_session;
7984 struct nfs41_create_session_args args = {
7985 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007986 .clientid = clp->cl_clientid,
7987 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007988 .cb_program = NFS4_CALLBACK,
7989 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007990 struct nfs41_create_session_res res;
7991
Andy Adamsonfc931582009-04-01 09:22:31 -04007992 struct rpc_message msg = {
7993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7994 .rpc_argp = &args,
7995 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007996 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007997 };
7998 int status;
7999
Chuck Lever6b26cc82016-05-02 14:40:40 -04008000 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008001 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008002
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008003 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008004 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008005
Trond Myklebustb519d402016-09-11 14:50:01 -04008006 switch (status) {
8007 case -NFS4ERR_STALE_CLIENTID:
8008 case -NFS4ERR_DELAY:
8009 case -ETIMEDOUT:
8010 case -EACCES:
8011 case -EAGAIN:
8012 goto out;
8013 };
8014
8015 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008016 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008017 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008018 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008019 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008020 if (status)
8021 goto out;
8022 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008023 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008024out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008025 return status;
8026}
8027
8028/*
8029 * Issues a CREATE_SESSION operation to the server.
8030 * It is the responsibility of the caller to verify the session is
8031 * expired before calling this routine.
8032 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008033int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008034{
8035 int status;
8036 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 struct nfs4_session *session = clp->cl_session;
8038
8039 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8040
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008041 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008042 if (status)
8043 goto out;
8044
Andy Adamsonaacd5532011-11-09 13:58:21 -05008045 /* Init or reset the session slot tables */
8046 status = nfs4_setup_session_slot_tables(session);
8047 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008048 if (status)
8049 goto out;
8050
8051 ptr = (unsigned *)&session->sess_id.data[0];
8052 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8053 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008054out:
8055 dprintk("<-- %s\n", __func__);
8056 return status;
8057}
8058
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008059/*
8060 * Issue the over-the-wire RPC DESTROY_SESSION.
8061 * The caller must serialize access to this routine.
8062 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008063int nfs4_proc_destroy_session(struct nfs4_session *session,
8064 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008065{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008066 struct rpc_message msg = {
8067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8068 .rpc_argp = session,
8069 .rpc_cred = cred,
8070 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008071 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008072
8073 dprintk("--> nfs4_proc_destroy_session\n");
8074
8075 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008076 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8077 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008078
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008079 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008080 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008081
8082 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008083 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008084 "Session has been destroyed regardless...\n", status);
8085
8086 dprintk("<-- nfs4_proc_destroy_session\n");
8087 return status;
8088}
8089
Trond Myklebust7b38c362012-05-23 13:23:31 -04008090/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008091 * Renew the cl_session lease.
8092 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008093struct nfs4_sequence_data {
8094 struct nfs_client *clp;
8095 struct nfs4_sequence_args args;
8096 struct nfs4_sequence_res res;
8097};
8098
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008099static void nfs41_sequence_release(void *data)
8100{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008101 struct nfs4_sequence_data *calldata = data;
8102 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008103
Alexandros Batsakis71358402010-02-05 03:45:05 -08008104 if (atomic_read(&clp->cl_count) > 1)
8105 nfs4_schedule_state_renewal(clp);
8106 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008107 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008108}
8109
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008110static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8111{
8112 switch(task->tk_status) {
8113 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008114 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8115 return -EAGAIN;
8116 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008117 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008118 }
8119 return 0;
8120}
8121
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008122static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008123{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008124 struct nfs4_sequence_data *calldata = data;
8125 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008126
Trond Myklebust14516c32010-07-31 14:29:06 -04008127 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8128 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008129
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008130 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008131 if (task->tk_status < 0) {
8132 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008133 if (atomic_read(&clp->cl_count) == 1)
8134 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008136 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8137 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008138 return;
8139 }
8140 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008142out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008143 dprintk("<-- %s\n", __func__);
8144}
8145
8146static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8147{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008148 struct nfs4_sequence_data *calldata = data;
8149 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008150 struct nfs4_sequence_args *args;
8151 struct nfs4_sequence_res *res;
8152
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008153 args = task->tk_msg.rpc_argp;
8154 res = task->tk_msg.rpc_resp;
8155
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008156 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008157}
8158
8159static const struct rpc_call_ops nfs41_sequence_ops = {
8160 .rpc_call_done = nfs41_sequence_call_done,
8161 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008162 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008163};
8164
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008165static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8166 struct rpc_cred *cred,
8167 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008168{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008169 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008170 struct rpc_message msg = {
8171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8172 .rpc_cred = cred,
8173 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008174 struct rpc_task_setup task_setup_data = {
8175 .rpc_client = clp->cl_rpcclient,
8176 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008177 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008178 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008179 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180
Alexandros Batsakis71358402010-02-05 03:45:05 -08008181 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008182 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008183 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008184 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008185 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008186 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008188 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008189 if (is_privileged)
8190 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008191 msg.rpc_argp = &calldata->args;
8192 msg.rpc_resp = &calldata->res;
8193 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008194 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008195
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008196 return rpc_run_task(&task_setup_data);
8197}
8198
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008199static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008200{
8201 struct rpc_task *task;
8202 int ret = 0;
8203
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008204 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008205 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008206 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008207 if (IS_ERR(task))
8208 ret = PTR_ERR(task);
8209 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008210 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008211 dprintk("<-- %s status=%d\n", __func__, ret);
8212 return ret;
8213}
8214
8215static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8216{
8217 struct rpc_task *task;
8218 int ret;
8219
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008220 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008221 if (IS_ERR(task)) {
8222 ret = PTR_ERR(task);
8223 goto out;
8224 }
8225 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008226 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008227 ret = task->tk_status;
8228 rpc_put_task(task);
8229out:
8230 dprintk("<-- %s status=%d\n", __func__, ret);
8231 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008232}
8233
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008234struct nfs4_reclaim_complete_data {
8235 struct nfs_client *clp;
8236 struct nfs41_reclaim_complete_args arg;
8237 struct nfs41_reclaim_complete_res res;
8238};
8239
8240static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8241{
8242 struct nfs4_reclaim_complete_data *calldata = data;
8243
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008244 nfs41_setup_sequence(calldata->clp->cl_session,
8245 &calldata->arg.seq_args,
8246 &calldata->res.seq_res,
8247 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248}
8249
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008250static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8251{
8252 switch(task->tk_status) {
8253 case 0:
8254 case -NFS4ERR_COMPLETE_ALREADY:
8255 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8256 break;
8257 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008258 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008259 /* fall through */
8260 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008261 return -EAGAIN;
8262 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008263 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008264 }
8265 return 0;
8266}
8267
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008268static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8269{
8270 struct nfs4_reclaim_complete_data *calldata = data;
8271 struct nfs_client *clp = calldata->clp;
8272 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8273
8274 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008275 if (!nfs41_sequence_done(task, res))
8276 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008277
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008278 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008279 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8280 rpc_restart_call_prepare(task);
8281 return;
8282 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008283 dprintk("<-- %s\n", __func__);
8284}
8285
8286static void nfs4_free_reclaim_complete_data(void *data)
8287{
8288 struct nfs4_reclaim_complete_data *calldata = data;
8289
8290 kfree(calldata);
8291}
8292
8293static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8294 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8295 .rpc_call_done = nfs4_reclaim_complete_done,
8296 .rpc_release = nfs4_free_reclaim_complete_data,
8297};
8298
8299/*
8300 * Issue a global reclaim complete.
8301 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008302static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8303 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008304{
8305 struct nfs4_reclaim_complete_data *calldata;
8306 struct rpc_task *task;
8307 struct rpc_message msg = {
8308 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008309 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310 };
8311 struct rpc_task_setup task_setup_data = {
8312 .rpc_client = clp->cl_rpcclient,
8313 .rpc_message = &msg,
8314 .callback_ops = &nfs4_reclaim_complete_call_ops,
8315 .flags = RPC_TASK_ASYNC,
8316 };
8317 int status = -ENOMEM;
8318
8319 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008320 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008321 if (calldata == NULL)
8322 goto out;
8323 calldata->clp = clp;
8324 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008325
Chuck Levera9c92d62013-08-09 12:48:18 -04008326 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008327 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008328 msg.rpc_argp = &calldata->arg;
8329 msg.rpc_resp = &calldata->res;
8330 task_setup_data.callback_data = calldata;
8331 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008332 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008333 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008334 goto out;
8335 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008336 status = nfs4_wait_for_completion_rpc_task(task);
8337 if (status == 0)
8338 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008339 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008340 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008341out:
8342 dprintk("<-- %s status=%d\n", __func__, status);
8343 return status;
8344}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008345
8346static void
8347nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8348{
8349 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008350 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008351 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008352
8353 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008354 nfs41_setup_sequence(session, &lgp->args.seq_args,
8355 &lgp->res.seq_res, task);
8356 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008357}
8358
8359static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8360{
8361 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008362
8363 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008364 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008365 dprintk("<-- %s\n", __func__);
8366}
8367
8368static int
8369nfs4_layoutget_handle_exception(struct rpc_task *task,
8370 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8371{
Trond Myklebustee314c22012-10-01 17:25:48 -07008372 struct inode *inode = lgp->args.inode;
8373 struct nfs_server *server = NFS_SERVER(inode);
8374 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008375 int nfs4err = task->tk_status;
8376 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008377 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008378
Boaz Harroshed7e5422014-01-22 20:34:54 +02008379 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008380
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008381 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008382 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008383 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008384
8385 /*
8386 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8387 * on the file. set tk_status to -ENODATA to tell upper layer to
8388 * retry go inband.
8389 */
8390 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008391 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008392 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008393 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008394 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8395 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8396 */
8397 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008398 status = -EOVERFLOW;
8399 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008400 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008401 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008402 * (or clients) writing to the same RAID stripe except when
8403 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008404 *
8405 * Treat it like we would RECALLCONFLICT -- we retry for a little
8406 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008407 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008408 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008409 if (lgp->args.minlength == 0) {
8410 status = -EOVERFLOW;
8411 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008412 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008413 status = -EBUSY;
8414 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008415 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008416 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008417 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008418 case -NFS4ERR_DELEG_REVOKED:
8419 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008420 case -NFS4ERR_EXPIRED:
8421 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008422 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008423 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008424 lo = NFS_I(inode)->layout;
8425 /* If the open stateid was bad, then recover it. */
8426 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8427 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008428 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008429 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008430 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008431 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008432 break;
8433 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008434
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008435 /*
8436 * Mark the bad layout state as invalid, then retry
8437 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008438 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008439 spin_unlock(&inode->i_lock);
8440 pnfs_free_lseg_list(&head);
8441 status = -EAGAIN;
8442 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008443 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008444
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008445 err = nfs4_handle_exception(server, nfs4err, exception);
8446 if (!status) {
8447 if (exception->retry)
8448 status = -EAGAIN;
8449 else
8450 status = err;
8451 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008452out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008453 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008454 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008455}
8456
Idan Kedar85541162012-08-02 11:47:10 +03008457static size_t max_response_pages(struct nfs_server *server)
8458{
8459 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8460 return nfs_page_array_len(0, max_resp_sz);
8461}
8462
8463static void nfs4_free_pages(struct page **pages, size_t size)
8464{
8465 int i;
8466
8467 if (!pages)
8468 return;
8469
8470 for (i = 0; i < size; i++) {
8471 if (!pages[i])
8472 break;
8473 __free_page(pages[i]);
8474 }
8475 kfree(pages);
8476}
8477
8478static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8479{
8480 struct page **pages;
8481 int i;
8482
8483 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8484 if (!pages) {
8485 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8486 return NULL;
8487 }
8488
8489 for (i = 0; i < size; i++) {
8490 pages[i] = alloc_page(gfp_flags);
8491 if (!pages[i]) {
8492 dprintk("%s: failed to allocate page\n", __func__);
8493 nfs4_free_pages(pages, size);
8494 return NULL;
8495 }
8496 }
8497
8498 return pages;
8499}
8500
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008501static void nfs4_layoutget_release(void *calldata)
8502{
8503 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008504 struct inode *inode = lgp->args.inode;
8505 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008506 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507
8508 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008509 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008510 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008511 put_nfs_open_context(lgp->args.ctx);
8512 kfree(calldata);
8513 dprintk("<-- %s\n", __func__);
8514}
8515
8516static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8517 .rpc_call_prepare = nfs4_layoutget_prepare,
8518 .rpc_call_done = nfs4_layoutget_done,
8519 .rpc_release = nfs4_layoutget_release,
8520};
8521
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008522struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008523nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008524{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008525 struct inode *inode = lgp->args.inode;
8526 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008527 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008528 struct rpc_task *task;
8529 struct rpc_message msg = {
8530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8531 .rpc_argp = &lgp->args,
8532 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008533 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008534 };
8535 struct rpc_task_setup task_setup_data = {
8536 .rpc_client = server->client,
8537 .rpc_message = &msg,
8538 .callback_ops = &nfs4_layoutget_call_ops,
8539 .callback_data = lgp,
8540 .flags = RPC_TASK_ASYNC,
8541 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008542 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008543 struct nfs4_exception exception = {
8544 .inode = inode,
8545 .timeout = *timeout,
8546 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008547 int status = 0;
8548
8549 dprintk("--> %s\n", __func__);
8550
Peng Tao4bd5a9802014-11-17 11:05:17 +08008551 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8552 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8553
Idan Kedar85541162012-08-02 11:47:10 +03008554 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8555 if (!lgp->args.layout.pages) {
8556 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008557 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008558 }
8559 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8560
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008561 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008562 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008563 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008564
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008565 task = rpc_run_task(&task_setup_data);
8566 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008567 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008568 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008569 if (status == 0) {
8570 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8571 *timeout = exception.timeout;
8572 }
8573
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008574 trace_nfs4_layoutget(lgp->args.ctx,
8575 &lgp->args.range,
8576 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008577 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008578 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008579
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008580 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8581 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008582 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008583 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008584 rpc_put_task(task);
8585 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008586 if (status)
8587 return ERR_PTR(status);
8588 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008589}
8590
Benny Halevycbe82602011-05-22 19:52:37 +03008591static void
8592nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8593{
8594 struct nfs4_layoutreturn *lrp = calldata;
8595
8596 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008597 nfs41_setup_sequence(lrp->clp->cl_session,
8598 &lrp->args.seq_args,
8599 &lrp->res.seq_res,
8600 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008601}
8602
8603static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8604{
8605 struct nfs4_layoutreturn *lrp = calldata;
8606 struct nfs_server *server;
8607
8608 dprintk("--> %s\n", __func__);
8609
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008610 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008611 return;
8612
8613 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008614 switch (task->tk_status) {
8615 default:
8616 task->tk_status = 0;
8617 case 0:
8618 break;
8619 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008620 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008621 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008622 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008623 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008624 return;
8625 }
Benny Halevycbe82602011-05-22 19:52:37 +03008626 dprintk("<-- %s\n", __func__);
8627}
8628
8629static void nfs4_layoutreturn_release(void *calldata)
8630{
8631 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008632 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008633
8634 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008635 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008636 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008637 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008638 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008639 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008640 kfree(calldata);
8641 dprintk("<-- %s\n", __func__);
8642}
8643
8644static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8645 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8646 .rpc_call_done = nfs4_layoutreturn_done,
8647 .rpc_release = nfs4_layoutreturn_release,
8648};
8649
Peng Tao6c166052014-11-17 09:30:40 +08008650int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008651{
8652 struct rpc_task *task;
8653 struct rpc_message msg = {
8654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8655 .rpc_argp = &lrp->args,
8656 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008657 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008658 };
8659 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008660 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008661 .rpc_message = &msg,
8662 .callback_ops = &nfs4_layoutreturn_call_ops,
8663 .callback_data = lrp,
8664 };
Peng Tao6c166052014-11-17 09:30:40 +08008665 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008666
Andrew Elble99ade3c2015-12-02 09:39:51 -05008667 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8668 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8669 &task_setup_data.rpc_client, &msg);
8670
Benny Halevycbe82602011-05-22 19:52:37 +03008671 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008672 if (!sync) {
8673 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8674 if (!lrp->inode) {
8675 nfs4_layoutreturn_release(lrp);
8676 return -EAGAIN;
8677 }
8678 task_setup_data.flags |= RPC_TASK_ASYNC;
8679 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008680 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008681 task = rpc_run_task(&task_setup_data);
8682 if (IS_ERR(task))
8683 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008684 if (sync)
8685 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008686 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008687 dprintk("<-- %s status=%d\n", __func__, status);
8688 rpc_put_task(task);
8689 return status;
8690}
8691
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008692static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008693_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8694 struct pnfs_device *pdev,
8695 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008696{
8697 struct nfs4_getdeviceinfo_args args = {
8698 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008699 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8700 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008701 };
8702 struct nfs4_getdeviceinfo_res res = {
8703 .pdev = pdev,
8704 };
8705 struct rpc_message msg = {
8706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8707 .rpc_argp = &args,
8708 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008709 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008710 };
8711 int status;
8712
8713 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008714 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008715 if (res.notification & ~args.notify_types)
8716 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008717 if (res.notification != args.notify_types)
8718 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008719
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008720 dprintk("<-- %s status=%d\n", __func__, status);
8721
8722 return status;
8723}
8724
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008725int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8726 struct pnfs_device *pdev,
8727 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008728{
8729 struct nfs4_exception exception = { };
8730 int err;
8731
8732 do {
8733 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008734 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008735 &exception);
8736 } while (exception.retry);
8737 return err;
8738}
8739EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8740
Andy Adamson863a3c62011-03-23 13:27:54 +00008741static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8742{
8743 struct nfs4_layoutcommit_data *data = calldata;
8744 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008745 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008746
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008747 nfs41_setup_sequence(session,
8748 &data->args.seq_args,
8749 &data->res.seq_res,
8750 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008751}
8752
8753static void
8754nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8755{
8756 struct nfs4_layoutcommit_data *data = calldata;
8757 struct nfs_server *server = NFS_SERVER(data->args.inode);
8758
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008759 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008760 return;
8761
8762 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008763 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8764 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8765 case -NFS4ERR_BADLAYOUT: /* no layout */
8766 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008767 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008768 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008769 break;
8770 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008771 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008772 rpc_restart_call_prepare(task);
8773 return;
8774 }
8775 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008776}
8777
8778static void nfs4_layoutcommit_release(void *calldata)
8779{
8780 struct nfs4_layoutcommit_data *data = calldata;
8781
Andy Adamsondb29c082011-07-30 20:52:38 -04008782 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008783 nfs_post_op_update_inode_force_wcc(data->args.inode,
8784 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008785 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008786 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008787 kfree(data);
8788}
8789
8790static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8791 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8792 .rpc_call_done = nfs4_layoutcommit_done,
8793 .rpc_release = nfs4_layoutcommit_release,
8794};
8795
8796int
Andy Adamsonef311532011-03-12 02:58:10 -05008797nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008798{
8799 struct rpc_message msg = {
8800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8801 .rpc_argp = &data->args,
8802 .rpc_resp = &data->res,
8803 .rpc_cred = data->cred,
8804 };
8805 struct rpc_task_setup task_setup_data = {
8806 .task = &data->task,
8807 .rpc_client = NFS_CLIENT(data->args.inode),
8808 .rpc_message = &msg,
8809 .callback_ops = &nfs4_layoutcommit_ops,
8810 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008811 };
8812 struct rpc_task *task;
8813 int status = 0;
8814
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008815 dprintk("NFS: initiating layoutcommit call. sync %d "
8816 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008817 data->args.lastbytewritten,
8818 data->args.inode->i_ino);
8819
Trond Myklebust472e2592015-02-05 16:50:30 -05008820 if (!sync) {
8821 data->inode = nfs_igrab_and_active(data->args.inode);
8822 if (data->inode == NULL) {
8823 nfs4_layoutcommit_release(data);
8824 return -EAGAIN;
8825 }
8826 task_setup_data.flags = RPC_TASK_ASYNC;
8827 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008828 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008829 task = rpc_run_task(&task_setup_data);
8830 if (IS_ERR(task))
8831 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008832 if (sync)
8833 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008834 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008835 dprintk("%s: status %d\n", __func__, status);
8836 rpc_put_task(task);
8837 return status;
8838}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008839
Andy Adamson97431202013-08-08 10:57:56 -04008840/**
8841 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8842 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8843 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008844static int
8845_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008846 struct nfs_fsinfo *info,
8847 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008848{
8849 struct nfs41_secinfo_no_name_args args = {
8850 .style = SECINFO_STYLE_CURRENT_FH,
8851 };
8852 struct nfs4_secinfo_res res = {
8853 .flavors = flavors,
8854 };
8855 struct rpc_message msg = {
8856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8857 .rpc_argp = &args,
8858 .rpc_resp = &res,
8859 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008860 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008861 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008862 int status;
8863
8864 if (use_integrity) {
8865 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008866 cred = nfs4_get_clid_cred(server->nfs_client);
8867 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008868 }
8869
8870 dprintk("--> %s\n", __func__);
8871 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8872 &res.seq_res, 0);
8873 dprintk("<-- %s status=%d\n", __func__, status);
8874
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008875 if (cred)
8876 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008877
8878 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879}
8880
8881static int
8882nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8883 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8884{
8885 struct nfs4_exception exception = { };
8886 int err;
8887 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008888 /* first try using integrity protection */
8889 err = -NFS4ERR_WRONGSEC;
8890
8891 /* try to use integrity protection with machine cred */
8892 if (_nfs4_is_integrity_protected(server->nfs_client))
8893 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8894 flavors, true);
8895
8896 /*
8897 * if unable to use integrity protection, or SECINFO with
8898 * integrity protection returns NFS4ERR_WRONGSEC (which is
8899 * disallowed by spec, but exists in deployed servers) use
8900 * the current filesystem's rpc_client and the user cred.
8901 */
8902 if (err == -NFS4ERR_WRONGSEC)
8903 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8904 flavors, false);
8905
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008906 switch (err) {
8907 case 0:
8908 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008909 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008910 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008911 default:
8912 err = nfs4_handle_exception(server, err, &exception);
8913 }
8914 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008915out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008916 return err;
8917}
8918
8919static int
8920nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8921 struct nfs_fsinfo *info)
8922{
8923 int err;
8924 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008925 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008926 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008927 struct nfs4_secinfo4 *secinfo;
8928 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008929
8930 page = alloc_page(GFP_KERNEL);
8931 if (!page) {
8932 err = -ENOMEM;
8933 goto out;
8934 }
8935
8936 flavors = page_address(page);
8937 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8938
8939 /*
8940 * Fall back on "guess and check" method if
8941 * the server doesn't support SECINFO_NO_NAME
8942 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008943 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008944 err = nfs4_find_root_sec(server, fhandle, info);
8945 goto out_freepage;
8946 }
8947 if (err)
8948 goto out_freepage;
8949
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008950 for (i = 0; i < flavors->num_flavors; i++) {
8951 secinfo = &flavors->flavors[i];
8952
8953 switch (secinfo->flavor) {
8954 case RPC_AUTH_NULL:
8955 case RPC_AUTH_UNIX:
8956 case RPC_AUTH_GSS:
8957 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8958 &secinfo->flavor_info);
8959 break;
8960 default:
8961 flavor = RPC_AUTH_MAXFLAVOR;
8962 break;
8963 }
8964
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008965 if (!nfs_auth_info_match(&server->auth_info, flavor))
8966 flavor = RPC_AUTH_MAXFLAVOR;
8967
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008968 if (flavor != RPC_AUTH_MAXFLAVOR) {
8969 err = nfs4_lookup_root_sec(server, fhandle,
8970 info, flavor);
8971 if (!err)
8972 break;
8973 }
8974 }
8975
8976 if (flavor == RPC_AUTH_MAXFLAVOR)
8977 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008978
8979out_freepage:
8980 put_page(page);
8981 if (err == -EACCES)
8982 return -EPERM;
8983out:
8984 return err;
8985}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008986
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008987static int _nfs41_test_stateid(struct nfs_server *server,
8988 nfs4_stateid *stateid,
8989 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008990{
8991 int status;
8992 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008993 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008994 };
8995 struct nfs41_test_stateid_res res;
8996 struct rpc_message msg = {
8997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8998 .rpc_argp = &args,
8999 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009000 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009001 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009002 struct rpc_clnt *rpc_client = server->client;
9003
9004 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9005 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009006
Chuck Lever38527b12012-07-11 16:30:23 -04009007 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009008 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009009 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009010 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009011 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009012 if (status != NFS_OK) {
9013 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009014 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009015 }
9016 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009017 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009018}
9019
Trond Myklebust43912bb2016-09-22 13:38:56 -04009020static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9021 int err, struct nfs4_exception *exception)
9022{
9023 exception->retry = 0;
9024 switch(err) {
9025 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009026 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009027 nfs4_handle_exception(server, err, exception);
9028 break;
9029 case -NFS4ERR_BADSESSION:
9030 case -NFS4ERR_BADSLOT:
9031 case -NFS4ERR_BAD_HIGH_SLOT:
9032 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9033 case -NFS4ERR_DEADSESSION:
9034 nfs4_do_handle_exception(server, err, exception);
9035 }
9036}
9037
Chuck Lever38527b12012-07-11 16:30:23 -04009038/**
9039 * nfs41_test_stateid - perform a TEST_STATEID operation
9040 *
9041 * @server: server / transport on which to perform the operation
9042 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009043 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009044 *
9045 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9046 * Otherwise a negative NFS4ERR value is returned if the operation
9047 * failed or the state ID is not currently valid.
9048 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009049static int nfs41_test_stateid(struct nfs_server *server,
9050 nfs4_stateid *stateid,
9051 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009052{
9053 struct nfs4_exception exception = { };
9054 int err;
9055 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009056 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009057 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009058 } while (exception.retry);
9059 return err;
9060}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009061
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009062struct nfs_free_stateid_data {
9063 struct nfs_server *server;
9064 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009065 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009066};
9067
9068static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9069{
9070 struct nfs_free_stateid_data *data = calldata;
9071 nfs41_setup_sequence(nfs4_get_session(data->server),
9072 &data->args.seq_args,
9073 &data->res.seq_res,
9074 task);
9075}
9076
9077static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9078{
9079 struct nfs_free_stateid_data *data = calldata;
9080
9081 nfs41_sequence_done(task, &data->res.seq_res);
9082
9083 switch (task->tk_status) {
9084 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009085 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009086 rpc_restart_call_prepare(task);
9087 }
9088}
9089
9090static void nfs41_free_stateid_release(void *calldata)
9091{
9092 kfree(calldata);
9093}
9094
Trond Myklebust17f26b12013-08-21 15:48:42 -04009095static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009096 .rpc_call_prepare = nfs41_free_stateid_prepare,
9097 .rpc_call_done = nfs41_free_stateid_done,
9098 .rpc_release = nfs41_free_stateid_release,
9099};
9100
9101static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009102 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009103 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009104 bool privileged)
9105{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009106 struct rpc_message msg = {
9107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009108 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009109 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009110 struct rpc_task_setup task_setup = {
9111 .rpc_client = server->client,
9112 .rpc_message = &msg,
9113 .callback_ops = &nfs41_free_stateid_ops,
9114 .flags = RPC_TASK_ASYNC,
9115 };
9116 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009117
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009118 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9119 &task_setup.rpc_client, &msg);
9120
Chuck Lever38527b12012-07-11 16:30:23 -04009121 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009122 data = kmalloc(sizeof(*data), GFP_NOFS);
9123 if (!data)
9124 return ERR_PTR(-ENOMEM);
9125 data->server = server;
9126 nfs4_stateid_copy(&data->args.stateid, stateid);
9127
9128 task_setup.callback_data = data;
9129
9130 msg.rpc_argp = &data->args;
9131 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009132 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009133 if (privileged)
9134 nfs4_set_sequence_privileged(&data->args.seq_args);
9135
9136 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009137}
9138
Chuck Lever38527b12012-07-11 16:30:23 -04009139/**
9140 * nfs41_free_stateid - perform a FREE_STATEID operation
9141 *
9142 * @server: server / transport on which to perform the operation
9143 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009144 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009145 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009146 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009147 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009148 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009149static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009150 const nfs4_stateid *stateid,
9151 struct rpc_cred *cred,
9152 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009153{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009154 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009155
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009156 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009157 if (IS_ERR(task))
9158 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009159 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009160 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009162
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009163static void
9164nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009165{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009166 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009167
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009168 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009169 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009170}
9171
Trond Myklebust36281ca2012-03-04 18:13:56 -05009172static bool nfs41_match_stateid(const nfs4_stateid *s1,
9173 const nfs4_stateid *s2)
9174{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009175 if (s1->type != s2->type)
9176 return false;
9177
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009178 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009179 return false;
9180
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009181 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009182 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009183 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009184 return true;
9185
9186 return false;
9187}
9188
Andy Adamson557134a2009-04-01 09:21:53 -04009189#endif /* CONFIG_NFS_V4_1 */
9190
Trond Myklebust36281ca2012-03-04 18:13:56 -05009191static bool nfs4_match_stateid(const nfs4_stateid *s1,
9192 const nfs4_stateid *s2)
9193{
Trond Myklebustf597c532012-03-04 18:13:56 -05009194 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009195}
9196
9197
Trond Myklebust17280172012-03-11 13:11:00 -04009198static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009199 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009200 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009201 .recover_open = nfs4_open_reclaim,
9202 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009203 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009204 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009205};
9206
Andy Adamson591d71c2009-04-01 09:22:47 -04009207#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009208static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009209 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9210 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9211 .recover_open = nfs4_open_reclaim,
9212 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009213 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009214 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009215 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009216};
9217#endif /* CONFIG_NFS_V4_1 */
9218
Trond Myklebust17280172012-03-11 13:11:00 -04009219static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009220 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009221 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009222 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009223 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009224 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009225};
9226
Andy Adamson591d71c2009-04-01 09:22:47 -04009227#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009228static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009229 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9230 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009231 .recover_open = nfs41_open_expired,
9232 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009233 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009234};
9235#endif /* CONFIG_NFS_V4_1 */
9236
Trond Myklebust17280172012-03-11 13:11:00 -04009237static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009238 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009239 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009240 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009241};
9242
9243#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009244static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009245 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009246 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009247 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009248};
9249#endif
9250
Chuck Leverec011fe2013-10-17 14:12:39 -04009251static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009252 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009253 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009254};
9255
9256#if defined(CONFIG_NFS_V4_1)
9257static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009258 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009259 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009260};
9261#endif /* CONFIG_NFS_V4_1 */
9262
Trond Myklebust97dc1352010-06-16 09:52:26 -04009263static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9264 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009265 .init_caps = NFS_CAP_READDIRPLUS
9266 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009267 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009268 .init_client = nfs40_init_client,
9269 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009270 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009271 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009272 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009273 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009274 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009275 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009276 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9277 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9278 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009279 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009280};
9281
9282#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009283static struct nfs_seqid *
9284nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9285{
9286 return NULL;
9287}
9288
Trond Myklebust97dc1352010-06-16 09:52:26 -04009289static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9290 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009291 .init_caps = NFS_CAP_READDIRPLUS
9292 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009293 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009294 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009295 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009296 .init_client = nfs41_init_client,
9297 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009298 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009299 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009300 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009301 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009302 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009303 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009304 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009305 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9306 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9307 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009308 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009309};
9310#endif
9311
Steve Dickson42c2c422013-05-22 12:50:38 -04009312#if defined(CONFIG_NFS_V4_2)
9313static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9314 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009315 .init_caps = NFS_CAP_READDIRPLUS
9316 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009317 | NFS_CAP_POSIX_LOCK
9318 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009319 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009320 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009321 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009322 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009323 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009324 | NFS_CAP_LAYOUTSTATS
9325 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009326 .init_client = nfs41_init_client,
9327 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009328 .match_stateid = nfs41_match_stateid,
9329 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009330 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009331 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009332 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009333 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009334 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009335 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9336 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9337 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009338 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009339};
9340#endif
9341
Trond Myklebust97dc1352010-06-16 09:52:26 -04009342const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9343 [0] = &nfs_v4_0_minor_ops,
9344#if defined(CONFIG_NFS_V4_1)
9345 [1] = &nfs_v4_1_minor_ops,
9346#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009347#if defined(CONFIG_NFS_V4_2)
9348 [2] = &nfs_v4_2_minor_ops,
9349#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009350};
9351
Trond Myklebust13997822016-07-24 17:10:52 -04009352static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009353{
9354 ssize_t error, error2;
9355
9356 error = generic_listxattr(dentry, list, size);
9357 if (error < 0)
9358 return error;
9359 if (list) {
9360 list += error;
9361 size -= error;
9362 }
9363
9364 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9365 if (error2 < 0)
9366 return error2;
9367 return error + error2;
9368}
9369
Trond Myklebust17f26b12013-08-21 15:48:42 -04009370static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009371 .create = nfs_create,
9372 .lookup = nfs_lookup,
9373 .atomic_open = nfs_atomic_open,
9374 .link = nfs_link,
9375 .unlink = nfs_unlink,
9376 .symlink = nfs_symlink,
9377 .mkdir = nfs_mkdir,
9378 .rmdir = nfs_rmdir,
9379 .mknod = nfs_mknod,
9380 .rename = nfs_rename,
9381 .permission = nfs_permission,
9382 .getattr = nfs_getattr,
9383 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009384 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009385};
9386
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009387static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009388 .permission = nfs_permission,
9389 .getattr = nfs_getattr,
9390 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009391 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009392};
9393
David Howells509de812006-08-22 20:06:11 -04009394const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009395 .version = 4, /* protocol version */
9396 .dentry_ops = &nfs4_dentry_operations,
9397 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009398 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009399 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009400 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009401 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009402 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 .getattr = nfs4_proc_getattr,
9404 .setattr = nfs4_proc_setattr,
9405 .lookup = nfs4_proc_lookup,
9406 .access = nfs4_proc_access,
9407 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408 .create = nfs4_proc_create,
9409 .remove = nfs4_proc_remove,
9410 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009411 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009412 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009413 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009414 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009415 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 .link = nfs4_proc_link,
9417 .symlink = nfs4_proc_symlink,
9418 .mkdir = nfs4_proc_mkdir,
9419 .rmdir = nfs4_proc_remove,
9420 .readdir = nfs4_proc_readdir,
9421 .mknod = nfs4_proc_mknod,
9422 .statfs = nfs4_proc_statfs,
9423 .fsinfo = nfs4_proc_fsinfo,
9424 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009425 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009427 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009429 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009431 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009433 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009434 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009436 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009437 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009438 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009439 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009440 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009441 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009442 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009443 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009444 .create_server = nfs4_create_server,
9445 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446};
9447
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009448static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009449 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009450 .list = nfs4_xattr_list_nfs4_acl,
9451 .get = nfs4_xattr_get_nfs4_acl,
9452 .set = nfs4_xattr_set_nfs4_acl,
9453};
9454
9455const struct xattr_handler *nfs4_xattr_handlers[] = {
9456 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009457#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9458 &nfs4_xattr_nfs4_label_handler,
9459#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009460 NULL
9461};
9462
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463/*
9464 * Local variables:
9465 * c-basic-offset: 8
9466 * End:
9467 */