blob: d33242c8d95d58a5366a4a57283005702852c29b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400258 FATTR4_WORD0_TYPE
259 | FATTR4_WORD0_CHANGE
260 | FATTR4_WORD0_SIZE
261 | FATTR4_WORD0_FSID
262 | FATTR4_WORD0_FILEID
263 | FATTR4_WORD0_FS_LOCATIONS,
264 FATTR4_WORD1_MODE
265 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Al Viro0dbb4c62006-10-19 23:28:49 -0700279 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000282 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
284 return;
285 }
286
287 readdir->cookie = 0;
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
289 if (cookie == 2)
290 return;
291
292 /*
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
297 * instead of 1 or 2.
298 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800299 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (cookie == 0) {
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000311 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 *p++ = xdr_one; /* next */
315 *p++ = xdr_zero; /* cookie, first word */
316 *p++ = xdr_two; /* cookie, second word */
317 *p++ = xdr_two; /* entry len */
318 memcpy(p, "..\0\0", 4); /* entry */
319 p++;
320 *p++ = xdr_one; /* bitmap length */
321 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
322 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000323 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 readdir->pgbase = (char *)p - (char *)start;
326 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800327 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Trond Myklebust26d36302016-09-22 13:39:05 -0400330static void nfs4_test_and_free_stateid(struct nfs_server *server,
331 nfs4_stateid *stateid,
332 struct rpc_cred *cred)
333{
334 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
335
336 ops->test_and_free_expired(server, stateid, cred);
337}
338
339static void __nfs4_free_revoked_stateid(struct nfs_server *server,
340 nfs4_stateid *stateid,
341 struct rpc_cred *cred)
342{
343 stateid->type = NFS4_REVOKED_STATEID_TYPE;
344 nfs4_test_and_free_stateid(server, stateid, cred);
345}
346
347static void nfs4_free_revoked_stateid(struct nfs_server *server,
348 const nfs4_stateid *stateid,
349 struct rpc_cred *cred)
350{
351 nfs4_stateid tmp;
352
353 nfs4_stateid_copy(&tmp, stateid);
354 __nfs4_free_revoked_stateid(server, &tmp, cred);
355}
356
NeilBrown8478eaa2014-09-18 16:09:27 +1000357static long nfs4_update_delay(long *timeout)
358{
359 long ret;
360 if (!timeout)
361 return NFS4_POLL_RETRY_MAX;
362 if (*timeout <= 0)
363 *timeout = NFS4_POLL_RETRY_MIN;
364 if (*timeout > NFS4_POLL_RETRY_MAX)
365 *timeout = NFS4_POLL_RETRY_MAX;
366 ret = *timeout;
367 *timeout <<= 1;
368 return ret;
369}
370
Trond Myklebust65de8722008-12-23 15:21:44 -0500371static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
372{
373 int res = 0;
374
375 might_sleep();
376
NeilBrown8478eaa2014-09-18 16:09:27 +1000377 freezable_schedule_timeout_killable_unsafe(
378 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 if (fatal_signal_pending(current))
380 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381 return res;
382}
383
384/* This is the error handling routine for processes that are allowed
385 * to sleep.
386 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400387static int nfs4_do_handle_exception(struct nfs_server *server,
388 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500389{
390 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500391 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400392 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500393 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500394 int ret = errorcode;
395
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400396 exception->delay = 0;
397 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400399
400 if (stateid == NULL && state != NULL)
401 stateid = &state->stateid;
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403 switch(errorcode) {
404 case 0:
405 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_DELEG_REVOKED:
407 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400408 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400410 if (inode != NULL && stateid != NULL) {
411 nfs_inode_find_state_and_recover(inode,
412 stateid);
413 goto wait_on_recovery;
414 }
415 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400416 if (inode) {
417 int err;
418
419 err = nfs_async_inode_return_delegation(inode,
420 stateid);
421 if (err == 0)
422 goto wait_on_recovery;
423 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
424 exception->retry = 1;
425 break;
426 }
427 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500428 if (state == NULL)
429 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400430 ret = nfs4_schedule_stateid_recovery(server, state);
431 if (ret < 0)
432 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500433 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500435 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436 nfs4_schedule_lease_recovery(clp);
437 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400438 case -NFS4ERR_MOVED:
439 ret = nfs4_schedule_migration_recovery(server);
440 if (ret < 0)
441 break;
442 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400443 case -NFS4ERR_LEASE_MOVED:
444 nfs4_schedule_lease_moved_recovery(clp);
445 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500446#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400447 case -NFS4ERR_BADSESSION:
448 case -NFS4ERR_BADSLOT:
449 case -NFS4ERR_BAD_HIGH_SLOT:
450 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
451 case -NFS4ERR_DEADSESSION:
452 case -NFS4ERR_SEQ_FALSE_RETRY:
453 case -NFS4ERR_SEQ_MISORDERED:
454 dprintk("%s ERROR: %d Reset session\n", __func__,
455 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400456 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400457 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500458#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500459 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500460 if (exception->timeout > HZ) {
461 /* We have retried a decent amount, time to
462 * fail
463 */
464 ret = -EBUSY;
465 break;
466 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400468 nfs_inc_server_stats(server, NFSIOS_DELAY);
469 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400470 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400471 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400472 exception->delay = 1;
473 return 0;
474
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400475 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500476 case -NFS4ERR_OLD_STATEID:
477 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800478 break;
479 case -NFS4ERR_BADOWNER:
480 /* The following works around a Linux server bug! */
481 case -NFS4ERR_BADNAME:
482 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
483 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
484 exception->retry = 1;
485 printk(KERN_WARNING "NFS: v4 server %s "
486 "does not accept raw "
487 "uid/gids. "
488 "Reenabling the idmapper.\n",
489 server->nfs_client->cl_hostname);
490 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500491 }
492 /* We failed to handle the error */
493 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500494wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400495 exception->recovering = 1;
496 return 0;
497}
498
499/* This is the error handling routine for processes that are allowed
500 * to sleep.
501 */
502int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
503{
504 struct nfs_client *clp = server->nfs_client;
505 int ret;
506
507 ret = nfs4_do_handle_exception(server, errorcode, exception);
508 if (exception->delay) {
509 ret = nfs4_delay(server->client, &exception->timeout);
510 goto out_retry;
511 }
512 if (exception->recovering) {
513 ret = nfs4_wait_clnt_recover(clp);
514 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
515 return -EIO;
516 goto out_retry;
517 }
518 return ret;
519out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500520 if (ret == 0)
521 exception->retry = 1;
522 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500523}
524
Trond Myklebust037fc982015-09-20 15:51:00 -0400525static int
526nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
527 int errorcode, struct nfs4_exception *exception)
528{
529 struct nfs_client *clp = server->nfs_client;
530 int ret;
531
532 ret = nfs4_do_handle_exception(server, errorcode, exception);
533 if (exception->delay) {
534 rpc_delay(task, nfs4_update_delay(&exception->timeout));
535 goto out_retry;
536 }
537 if (exception->recovering) {
538 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
539 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
540 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
541 goto out_retry;
542 }
543 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
544 ret = -EIO;
545 return ret;
546out_retry:
547 if (ret == 0)
548 exception->retry = 1;
549 return ret;
550}
551
552static int
553nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
554 struct nfs4_state *state, long *timeout)
555{
556 struct nfs4_exception exception = {
557 .state = state,
558 };
559
560 if (task->tk_status >= 0)
561 return 0;
562 if (timeout)
563 exception.timeout = *timeout;
564 task->tk_status = nfs4_async_handle_exception(task, server,
565 task->tk_status,
566 &exception);
567 if (exception.delay && timeout)
568 *timeout = exception.timeout;
569 if (exception.retry)
570 return -EAGAIN;
571 return 0;
572}
573
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400574/*
575 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
576 * or 'false' otherwise.
577 */
578static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
579{
580 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
581
582 if (flavor == RPC_AUTH_GSS_KRB5I ||
583 flavor == RPC_AUTH_GSS_KRB5P)
584 return true;
585
586 return false;
587}
Trond Myklebust65de8722008-12-23 15:21:44 -0500588
Trond Myklebust452e9352010-07-31 14:29:06 -0400589static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 spin_lock(&clp->cl_lock);
592 if (time_before(clp->cl_last_renewal,timestamp))
593 clp->cl_last_renewal = timestamp;
594 spin_unlock(&clp->cl_lock);
595}
596
Trond Myklebust452e9352010-07-31 14:29:06 -0400597static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
598{
Trond Myklebustbe824162015-07-05 14:50:46 -0400599 struct nfs_client *clp = server->nfs_client;
600
601 if (!nfs4_has_session(clp))
602 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400603}
604
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400605struct nfs4_call_sync_data {
606 const struct nfs_server *seq_server;
607 struct nfs4_sequence_args *seq_args;
608 struct nfs4_sequence_res *seq_res;
609};
610
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800611void nfs4_init_sequence(struct nfs4_sequence_args *args,
612 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400613{
614 args->sa_slot = NULL;
615 args->sa_cache_this = cache_reply;
616 args->sa_privileged = 0;
617
618 res->sr_slot = NULL;
619}
620
621static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
622{
623 args->sa_privileged = 1;
624}
625
Peng Taocb04ad22014-06-11 05:24:15 +0800626int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
627 struct nfs4_sequence_args *args,
628 struct nfs4_sequence_res *res,
629 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400630{
Chuck Lever3bd23842013-08-09 12:49:19 -0400631 struct nfs4_slot *slot;
632
633 /* slot already allocated? */
634 if (res->sr_slot != NULL)
635 goto out_start;
636
637 spin_lock(&tbl->slot_tbl_lock);
638 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
639 goto out_sleep;
640
641 slot = nfs4_alloc_slot(tbl);
642 if (IS_ERR(slot)) {
643 if (slot == ERR_PTR(-ENOMEM))
644 task->tk_timeout = HZ >> 2;
645 goto out_sleep;
646 }
647 spin_unlock(&tbl->slot_tbl_lock);
648
Trond Myklebust0a014a42016-09-22 13:38:51 -0400649 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400650 args->sa_slot = slot;
651 res->sr_slot = slot;
652
653out_start:
654 rpc_call_start(task);
655 return 0;
656
657out_sleep:
658 if (args->sa_privileged)
659 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
660 NULL, RPC_PRIORITY_PRIVILEGED);
661 else
662 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
663 spin_unlock(&tbl->slot_tbl_lock);
664 return -EAGAIN;
665}
Peng Taocb04ad22014-06-11 05:24:15 +0800666EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400667
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400668static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400669{
670 struct nfs4_slot *slot = res->sr_slot;
671 struct nfs4_slot_table *tbl;
672
Chuck Lever3bd23842013-08-09 12:49:19 -0400673 tbl = slot->table;
674 spin_lock(&tbl->slot_tbl_lock);
675 if (!nfs41_wake_and_assign_slot(tbl, slot))
676 nfs4_free_slot(tbl, slot);
677 spin_unlock(&tbl->slot_tbl_lock);
678
679 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400680}
681
682static int nfs40_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
684{
685 if (res->sr_slot != NULL)
686 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400687 return 1;
688}
689
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400690#if defined(CONFIG_NFS_V4_1)
691
Trond Myklebustd185a332010-06-16 09:52:25 -0400692static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400693{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500694 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400695 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500697 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400698
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500700 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500701
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400702 /* Bump the slot sequence number */
703 if (slot->seq_done)
704 slot->seq_nr++;
705 slot->seq_done = 0;
706
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500707 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500708 /* Be nice to the server: try to ensure that the last transmitted
709 * value for highest_user_slotid <= target_highest_slotid
710 */
711 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
712 send_new_highest_used_slotid = true;
713
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500714 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500715 send_new_highest_used_slotid = false;
716 goto out_unlock;
717 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500719
720 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
721 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500722out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500723 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400724 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500725 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400726 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400727 if (waitqueue_active(&tbl->slot_waitq))
728 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400729}
730
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400731static int nfs41_sequence_process(struct rpc_task *task,
732 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400733{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500735 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400736 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500737 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500738 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400739
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500740 if (slot == NULL)
741 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400742 /* don't increment the sequence number if the task wasn't sent */
743 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400744 goto out;
745
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500746 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500747
Trond Myklebustac20d162012-12-15 15:36:07 -0500748 if (slot->interrupted) {
749 slot->interrupted = 0;
750 interrupted = true;
751 }
752
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500754 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400755 switch (res->sr_status) {
756 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400757 /* If previous op on slot was interrupted and we reused
758 * the seq# and got a reply from the cache, then retry
759 */
760 if (task->tk_status == -EREMOTEIO && interrupted) {
761 ++slot->seq_nr;
762 goto retry_nowait;
763 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400764 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400765 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500766 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500768 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400769 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
770 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500771 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500773 case 1:
774 /*
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
777 * operation..
778 * Mark the slot as having hosted an interrupted RPC call.
779 */
780 slot->interrupted = 1;
781 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 case -NFS4ERR_DELAY:
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
785 * of RFC5661.
786 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400788 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500789 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500790 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400791 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500792 case -NFS4ERR_BADSLOT:
793 /*
794 * The slot id we used was probably retired. Try again
795 * using a different slot id.
796 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500797 goto retry_nowait;
798 case -NFS4ERR_SEQ_MISORDERED:
799 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500800 * Was the last operation on this sequence interrupted?
801 * If so, retry after bumping the sequence number.
802 */
803 if (interrupted) {
804 ++slot->seq_nr;
805 goto retry_nowait;
806 }
807 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500808 * Could this slot have been previously retired?
809 * If so, then the server may be expecting seq_nr = 1!
810 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500811 if (slot->seq_nr != 1) {
812 slot->seq_nr = 1;
813 goto retry_nowait;
814 }
815 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500816 case -NFS4ERR_SEQ_FALSE_RETRY:
817 ++slot->seq_nr;
818 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500819 case -NFS4ERR_DEADSESSION:
820 case -NFS4ERR_BADSESSION:
821 nfs4_schedule_session_recovery(session, res->sr_status);
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168a2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168a2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001106 struct nfs4_string owner_name;
1107 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001108 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001110 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001112 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 struct nfs4_state_owner *owner;
1114 struct nfs4_state *state;
1115 struct iattr attrs;
1116 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001117 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001118 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001119 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001120 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 int cancelled;
1122};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001123
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001124static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1125 int err, struct nfs4_exception *exception)
1126{
1127 if (err != -EINVAL)
1128 return false;
1129 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1130 return false;
1131 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1132 exception->retry = 1;
1133 return true;
1134}
1135
Trond Myklebust6ae37332015-01-30 14:21:14 -05001136static u32
1137nfs4_map_atomic_open_share(struct nfs_server *server,
1138 fmode_t fmode, int openflags)
1139{
1140 u32 res = 0;
1141
1142 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1143 case FMODE_READ:
1144 res = NFS4_SHARE_ACCESS_READ;
1145 break;
1146 case FMODE_WRITE:
1147 res = NFS4_SHARE_ACCESS_WRITE;
1148 break;
1149 case FMODE_READ|FMODE_WRITE:
1150 res = NFS4_SHARE_ACCESS_BOTH;
1151 }
1152 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1153 goto out;
1154 /* Want no delegation if we're using O_DIRECT */
1155 if (openflags & O_DIRECT)
1156 res |= NFS4_SHARE_WANT_NO_DELEG;
1157out:
1158 return res;
1159}
1160
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001161static enum open_claim_type4
1162nfs4_map_atomic_open_claim(struct nfs_server *server,
1163 enum open_claim_type4 claim)
1164{
1165 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1166 return claim;
1167 switch (claim) {
1168 default:
1169 return claim;
1170 case NFS4_OPEN_CLAIM_FH:
1171 return NFS4_OPEN_CLAIM_NULL;
1172 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1173 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1174 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1175 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1176 }
1177}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180{
1181 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001182 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183 p->o_res.seqid = p->o_arg.seqid;
1184 p->c_res.seqid = p->c_arg.seqid;
1185 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001186 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001187 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001188 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189}
1190
Al Viro82a2c1b2011-06-22 18:30:55 -04001191static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001192 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001194 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001195 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001196 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197{
Al Viro82a2c1b2011-06-22 18:30:55 -04001198 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001199 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001200 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001201 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 struct nfs4_opendata *p;
1203
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001204 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 if (p == NULL)
1206 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001207
1208 p->f_label = nfs4_label_alloc(server, gfp_mask);
1209 if (IS_ERR(p->f_label))
1210 goto err_free_p;
1211
Kinglong Meea49c2692015-07-27 15:31:38 +08001212 p->a_label = nfs4_label_alloc(server, gfp_mask);
1213 if (IS_ERR(p->a_label))
1214 goto err_free_f;
1215
Trond Myklebust63f5f792015-01-23 19:19:25 -05001216 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1217 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001218 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001219 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 nfs_sb_active(dentry->d_sb);
1221 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->dir = parent;
1223 p->owner = sp;
1224 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001225 p->o_arg.open_flags = flags;
1226 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001227 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001228 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001229 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1230 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001231 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1232 * will return permission denied for all bits until close */
1233 if (!(flags & O_EXCL)) {
1234 /* ask server to check for all possible rights as results
1235 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001236 switch (p->o_arg.claim) {
1237 default:
1238 break;
1239 case NFS4_OPEN_CLAIM_NULL:
1240 case NFS4_OPEN_CLAIM_FH:
1241 p->o_arg.access = NFS4_ACCESS_READ |
1242 NFS4_ACCESS_MODIFY |
1243 NFS4_ACCESS_EXTEND |
1244 NFS4_ACCESS_EXECUTE;
1245 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001246 }
David Howells7539bba2006-08-22 20:06:09 -04001247 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001248 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1249 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001250 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001252 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001253 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001254 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001255 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001256 case NFS4_OPEN_CLAIM_NULL:
1257 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1258 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1259 p->o_arg.fh = NFS_FH(dir);
1260 break;
1261 case NFS4_OPEN_CLAIM_PREVIOUS:
1262 case NFS4_OPEN_CLAIM_FH:
1263 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1264 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001265 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001266 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001267 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001268 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001269
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 p->o_arg.u.attrs = &p->attrs;
1271 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001272
1273 verf[0] = jiffies;
1274 verf[1] = current->pid;
1275 memcpy(p->o_arg.u.verifier.data, verf,
1276 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278 p->c_arg.fh = &p->o_res.fh;
1279 p->c_arg.stateid = &p->o_res.stateid;
1280 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001281 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001282 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001284
1285err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001286 nfs4_label_free(p->a_label);
1287err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001288 nfs4_label_free(p->f_label);
1289err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290 kfree(p);
1291err:
1292 dput(parent);
1293 return NULL;
1294}
1295
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001296static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001297{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 struct nfs4_opendata *p = container_of(kref,
1299 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001300 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001301
1302 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001303 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001304 if (p->state != NULL)
1305 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001306 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001307
Kinglong Meea49c2692015-07-27 15:31:38 +08001308 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001309 nfs4_label_free(p->f_label);
1310
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001312 dput(p->dentry);
1313 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001314 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001315 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001316 kfree(p);
1317}
1318
1319static void nfs4_opendata_put(struct nfs4_opendata *p)
1320{
1321 if (p != NULL)
1322 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323}
1324
Trond Myklebust06f814a2006-01-03 09:55:07 +01001325static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1326{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001327 int ret;
1328
Trond Myklebust06f814a2006-01-03 09:55:07 +01001329 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001330 return ret;
1331}
1332
Trond Myklebust24311f82015-09-20 10:50:17 -04001333static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1334 fmode_t fmode)
1335{
1336 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1337 case FMODE_READ|FMODE_WRITE:
1338 return state->n_rdwr != 0;
1339 case FMODE_WRITE:
1340 return state->n_wronly != 0;
1341 case FMODE_READ:
1342 return state->n_rdonly != 0;
1343 }
1344 WARN_ON_ONCE(1);
1345 return false;
1346}
1347
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001349{
1350 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351
Trond Myklebust536e43d2012-01-17 22:04:26 -05001352 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 goto out;
1354 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001356 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1357 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001358 break;
1359 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001360 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1361 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 break;
1363 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001364 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1365 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001368 return ret;
1369}
1370
Trond Myklebust2a606182015-08-19 22:30:00 -05001371static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1372 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001374 if (delegation == NULL)
1375 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001376 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001377 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001378 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1379 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001380 switch (claim) {
1381 case NFS4_OPEN_CLAIM_NULL:
1382 case NFS4_OPEN_CLAIM_FH:
1383 break;
1384 case NFS4_OPEN_CLAIM_PREVIOUS:
1385 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1386 break;
1387 default:
1388 return 0;
1389 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001390 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 return 1;
1392}
1393
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001394static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001395{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001396 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001397 case FMODE_WRITE:
1398 state->n_wronly++;
1399 break;
1400 case FMODE_READ:
1401 state->n_rdonly++;
1402 break;
1403 case FMODE_READ|FMODE_WRITE:
1404 state->n_rdwr++;
1405 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001406 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001407}
1408
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001409#ifdef CONFIG_NFS_V4_1
1410static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1411{
1412 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1413 return true;
1414 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1415 return true;
1416 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1417 return true;
1418 return false;
1419}
1420#endif /* CONFIG_NFS_V4_1 */
1421
Trond Myklebust4f14c192014-02-12 19:15:06 -05001422static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001423{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001424 struct nfs_client *clp = state->owner->so_server->nfs_client;
1425 bool need_recover = false;
1426
1427 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1428 need_recover = true;
1429 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1430 need_recover = true;
1431 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1432 need_recover = true;
1433 if (need_recover)
1434 nfs4_state_mark_reclaim_nograce(clp, state);
1435}
1436
Trond Myklebuste999e802014-02-10 18:20:47 -05001437static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001438 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001439{
1440 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1441 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001442 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001443 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001444 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001445 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001446 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001447 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1448 return true;
1449 return false;
1450}
1451
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1453{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001454 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1455 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001456 if (state->n_wronly)
1457 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1458 if (state->n_rdonly)
1459 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1460 if (state->n_rdwr)
1461 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001462 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463}
1464
Trond Myklebust226056c2014-02-11 10:41:07 -05001465static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1466 nfs4_stateid *stateid, fmode_t fmode)
1467{
1468 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1469 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1470 case FMODE_WRITE:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 break;
1473 case FMODE_READ:
1474 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1475 break;
1476 case 0:
1477 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1478 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1479 clear_bit(NFS_OPEN_STATE, &state->flags);
1480 }
1481 if (stateid == NULL)
1482 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001483 /* Handle OPEN+OPEN_DOWNGRADE races */
1484 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1485 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001486 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001487 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001488 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001489 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001490 nfs4_stateid_copy(&state->stateid, stateid);
1491 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001492}
1493
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001494static void nfs_clear_open_stateid(struct nfs4_state *state,
1495 nfs4_stateid *arg_stateid,
1496 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001497{
1498 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001499 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1500 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1501 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001502 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001503 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1504 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001505}
1506
Trond Myklebust1393d962016-09-22 13:39:13 -04001507static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1508 const nfs4_stateid *stateid, fmode_t fmode,
1509 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001510{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001511 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001512 case FMODE_READ:
1513 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1514 break;
1515 case FMODE_WRITE:
1516 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 break;
1518 case FMODE_READ|FMODE_WRITE:
1519 set_bit(NFS_O_RDWR_STATE, &state->flags);
1520 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001521 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001522 return;
1523 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1524 nfs4_stateid_copy(&state->stateid, stateid);
1525 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001526}
1527
Trond Myklebust1393d962016-09-22 13:39:13 -04001528static void __update_open_stateid(struct nfs4_state *state,
1529 const nfs4_stateid *open_stateid,
1530 const nfs4_stateid *deleg_stateid,
1531 fmode_t fmode,
1532 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001534 /*
1535 * Protect the call to nfs4_state_set_mode_locked and
1536 * serialise the stateid update
1537 */
Andrew Elble361cad32015-12-02 09:20:57 -05001538 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001539 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001540 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001541 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001542 set_bit(NFS_DELEGATED_STATE, &state->flags);
1543 }
1544 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001545 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001546 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001547 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001548 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Trond Myklebust1393d962016-09-22 13:39:13 -04001551static int update_open_stateid(struct nfs4_state *state,
1552 const nfs4_stateid *open_stateid,
1553 const nfs4_stateid *delegation,
1554 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001555{
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 struct nfs_server *server = NFS_SERVER(state->inode);
1557 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001558 struct nfs_inode *nfsi = NFS_I(state->inode);
1559 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001560 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001561 int ret = 0;
1562
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001563 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001564
1565 rcu_read_lock();
1566 deleg_cur = rcu_dereference(nfsi->delegation);
1567 if (deleg_cur == NULL)
1568 goto no_delegation;
1569
1570 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001571 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001572 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001573 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001574 goto no_delegation_unlock;
1575
1576 if (delegation == NULL)
1577 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001578 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001579 goto no_delegation_unlock;
1580
Trond Myklebustb7391f42008-12-23 15:21:52 -05001581 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001582 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1583 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001584 ret = 1;
1585no_delegation_unlock:
1586 spin_unlock(&deleg_cur->lock);
1587no_delegation:
1588 rcu_read_unlock();
1589
1590 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001591 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001592 ret = 1;
1593 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001594 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001595 nfs4_schedule_state_manager(clp);
1596 if (freeme.type != 0)
1597 nfs4_test_and_free_stateid(server, &freeme,
1598 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001599
1600 return ret;
1601}
1602
Trond Myklebust39071e62015-01-24 15:07:56 -05001603static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1604 const nfs4_stateid *stateid)
1605{
1606 struct nfs4_state *state = lsp->ls_state;
1607 bool ret = false;
1608
1609 spin_lock(&state->state_lock);
1610 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1611 goto out_noupdate;
1612 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1613 goto out_noupdate;
1614 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1615 ret = true;
1616out_noupdate:
1617 spin_unlock(&state->state_lock);
1618 return ret;
1619}
Trond Myklebust34310432008-12-23 15:21:38 -05001620
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001621static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001622{
1623 struct nfs_delegation *delegation;
1624
1625 rcu_read_lock();
1626 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001627 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001628 rcu_read_unlock();
1629 return;
1630 }
1631 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001632 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001633}
1634
Trond Myklebust6ee41262007-07-08 14:11:36 -04001635static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636{
1637 struct nfs4_state *state = opendata->state;
1638 struct nfs_inode *nfsi = NFS_I(state->inode);
1639 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001640 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001641 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001642 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001643 nfs4_stateid stateid;
1644 int ret = -EAGAIN;
1645
Trond Myklebustaac00a82007-07-05 19:02:21 -04001646 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001647 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001648 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001649 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001650 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001651 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001652 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001653 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001654 rcu_read_lock();
1655 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001656 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001657 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001658 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001659 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001660 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001661 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001663 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001664 if (!opendata->is_recover) {
1665 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1666 if (ret != 0)
1667 goto out;
1668 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001670
1671 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001672 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001673 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675out:
1676 return ERR_PTR(ret);
1677out_return_state:
1678 atomic_inc(&state->count);
1679 return state;
1680}
1681
Andy Adamsone23008e2012-10-02 21:07:32 -04001682static void
1683nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1684{
1685 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1686 struct nfs_delegation *delegation;
1687 int delegation_flags = 0;
1688
1689 rcu_read_lock();
1690 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1691 if (delegation)
1692 delegation_flags = delegation->flags;
1693 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001694 switch (data->o_arg.claim) {
1695 default:
1696 break;
1697 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1698 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001699 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1700 "returning a delegation for "
1701 "OPEN(CLAIM_DELEGATE_CUR)\n",
1702 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001703 return;
1704 }
1705 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001706 nfs_inode_set_delegation(state->inode,
1707 data->owner->so_cred,
1708 &data->o_res);
1709 else
1710 nfs_inode_reclaim_delegation(state->inode,
1711 data->owner->so_cred,
1712 &data->o_res);
1713}
1714
1715/*
1716 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1717 * and update the nfs4_state.
1718 */
1719static struct nfs4_state *
1720_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1721{
1722 struct inode *inode = data->state->inode;
1723 struct nfs4_state *state = data->state;
1724 int ret;
1725
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001726 if (!data->rpc_done) {
1727 if (data->rpc_status) {
1728 ret = data->rpc_status;
1729 goto err;
1730 }
1731 /* cached opens have already been processed */
1732 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001733 }
1734
Andy Adamsone23008e2012-10-02 21:07:32 -04001735 ret = nfs_refresh_inode(inode, &data->f_attr);
1736 if (ret)
1737 goto err;
1738
1739 if (data->o_res.delegation_type != 0)
1740 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001741update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001742 update_open_stateid(state, &data->o_res.stateid, NULL,
1743 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001744 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001745
1746 return state;
1747err:
1748 return ERR_PTR(ret);
1749
1750}
1751
1752static struct nfs4_state *
1753_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001754{
1755 struct inode *inode;
1756 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001757 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001758
Trond Myklebustaac00a82007-07-05 19:02:21 -04001759 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001760 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001761 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762 goto out;
1763 }
1764
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001765 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001767 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001768 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001769 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001770 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001771 goto err;
1772 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001773 state = nfs4_get_open_state(inode, data->owner);
1774 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001775 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001776 if (data->o_res.delegation_type != 0)
1777 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001778 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001779 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001780 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001781out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001782 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001784err_put_inode:
1785 iput(inode);
1786err:
1787 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001788}
1789
Andy Adamsone23008e2012-10-02 21:07:32 -04001790static struct nfs4_state *
1791nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1792{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001793 struct nfs4_state *ret;
1794
Andy Adamsone23008e2012-10-02 21:07:32 -04001795 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001796 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1797 else
1798 ret = _nfs4_opendata_to_nfs4_state(data);
1799 nfs4_sequence_free_slot(&data->o_res.seq_res);
1800 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001801}
1802
Trond Myklebust864472e2006-01-03 09:55:15 +01001803static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1804{
1805 struct nfs_inode *nfsi = NFS_I(state->inode);
1806 struct nfs_open_context *ctx;
1807
1808 spin_lock(&state->inode->i_lock);
1809 list_for_each_entry(ctx, &nfsi->open_files, list) {
1810 if (ctx->state != state)
1811 continue;
1812 get_nfs_open_context(ctx);
1813 spin_unlock(&state->inode->i_lock);
1814 return ctx;
1815 }
1816 spin_unlock(&state->inode->i_lock);
1817 return ERR_PTR(-ENOENT);
1818}
1819
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001820static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1821 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001822{
1823 struct nfs4_opendata *opendata;
1824
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001825 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001826 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001827 if (opendata == NULL)
1828 return ERR_PTR(-ENOMEM);
1829 opendata->state = state;
1830 atomic_inc(&state->count);
1831 return opendata;
1832}
1833
Trond Myklebust24311f82015-09-20 10:50:17 -04001834static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1835 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001836{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001837 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001838 int ret;
1839
Trond Myklebust24311f82015-09-20 10:50:17 -04001840 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001841 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001842 opendata->o_arg.open_flags = 0;
1843 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001844 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1845 NFS_SB(opendata->dentry->d_sb),
1846 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001847 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1848 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1849 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001850 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001851 if (ret != 0)
1852 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001853 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001854 if (IS_ERR(newstate))
1855 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001856 if (newstate != opendata->state)
1857 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001858 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001859 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001860}
1861
1862static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1863{
Trond Myklebust864472e2006-01-03 09:55:15 +01001864 int ret;
1865
Trond Myklebust4f14c192014-02-12 19:15:06 -05001866 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1867 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1868 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1869 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001870 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001871 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001872 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001873 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001874 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1875 if (ret != 0)
1876 return ret;
1877 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1878 if (ret != 0)
1879 return ret;
1880 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1881 if (ret != 0)
1882 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001883 /*
1884 * We may have performed cached opens for all three recoveries.
1885 * Check if we need to update the current stateid.
1886 */
1887 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001888 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001889 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001890 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001891 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001892 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001893 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001894 return 0;
1895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897/*
1898 * OPEN_RECLAIM:
1899 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001901static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001903 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001904 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001905 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int status;
1907
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001908 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1909 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001910 if (IS_ERR(opendata))
1911 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001912 rcu_read_lock();
1913 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001914 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001915 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001916 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001917 opendata->o_arg.u.delegation_type = delegation_type;
1918 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001919 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 return status;
1921}
1922
Trond Myklebust539cd032007-06-05 11:46:42 -04001923static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct nfs_server *server = NFS_SERVER(state->inode);
1926 struct nfs4_exception exception = { };
1927 int err;
1928 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001929 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001930 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001931 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1932 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001933 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001934 break;
1935 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 } while (exception.retry);
1937 return err;
1938}
1939
Trond Myklebust864472e2006-01-03 09:55:15 +01001940static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1941{
1942 struct nfs_open_context *ctx;
1943 int ret;
1944
1945 ctx = nfs4_state_find_open_context(state);
1946 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001947 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001948 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001949 put_nfs_open_context(ctx);
1950 return ret;
1951}
1952
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001953static 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 -07001954{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001955 switch (err) {
1956 default:
1957 printk(KERN_ERR "NFS: %s: unhandled error "
1958 "%d.\n", __func__, err);
1959 case 0:
1960 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001961 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001962 case -ESTALE:
1963 break;
1964 case -NFS4ERR_BADSESSION:
1965 case -NFS4ERR_BADSLOT:
1966 case -NFS4ERR_BAD_HIGH_SLOT:
1967 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1968 case -NFS4ERR_DEADSESSION:
1969 set_bit(NFS_DELEGATED_STATE, &state->flags);
1970 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1971 return -EAGAIN;
1972 case -NFS4ERR_STALE_CLIENTID:
1973 case -NFS4ERR_STALE_STATEID:
1974 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001975 /* Don't recall a delegation if it was lost */
1976 nfs4_schedule_lease_recovery(server->nfs_client);
1977 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001978 case -NFS4ERR_MOVED:
1979 nfs4_schedule_migration_recovery(server);
1980 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001981 case -NFS4ERR_LEASE_MOVED:
1982 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1983 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001984 case -NFS4ERR_DELEG_REVOKED:
1985 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001986 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001987 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001988 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001989 nfs_inode_find_state_and_recover(state->inode,
1990 stateid);
1991 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001992 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001993 case -NFS4ERR_DELAY:
1994 case -NFS4ERR_GRACE:
1995 set_bit(NFS_DELEGATED_STATE, &state->flags);
1996 ssleep(1);
1997 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001998 case -ENOMEM:
1999 case -NFS4ERR_DENIED:
2000 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2001 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return err;
2004}
2005
Trond Myklebust24311f82015-09-20 10:50:17 -04002006int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2007 struct nfs4_state *state, const nfs4_stateid *stateid,
2008 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002009{
2010 struct nfs_server *server = NFS_SERVER(state->inode);
2011 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002012 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002013
2014 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2015 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2016 if (IS_ERR(opendata))
2017 return PTR_ERR(opendata);
2018 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002019 write_seqlock(&state->seqlock);
2020 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2021 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002022 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2023 switch (type & (FMODE_READ|FMODE_WRITE)) {
2024 case FMODE_READ|FMODE_WRITE:
2025 case FMODE_WRITE:
2026 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2027 if (err)
2028 break;
2029 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2030 if (err)
2031 break;
2032 case FMODE_READ:
2033 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2034 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002035 nfs4_opendata_put(opendata);
2036 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2037}
2038
Chuck Leverbe05c862013-08-09 12:49:47 -04002039static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2040{
2041 struct nfs4_opendata *data = calldata;
2042
Peng Taocb04ad22014-06-11 05:24:15 +08002043 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2044 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002045}
2046
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002047static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2048{
2049 struct nfs4_opendata *data = calldata;
2050
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002051 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002052
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002053 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002054 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002055 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002056 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002057 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002058 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002059 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002060}
2061
2062static void nfs4_open_confirm_release(void *calldata)
2063{
2064 struct nfs4_opendata *data = calldata;
2065 struct nfs4_state *state = NULL;
2066
2067 /* If this request hasn't been cancelled, do nothing */
2068 if (data->cancelled == 0)
2069 goto out_free;
2070 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002071 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002072 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002073 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002074 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002075 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002076out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002077 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002078}
2079
2080static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002081 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002082 .rpc_call_done = nfs4_open_confirm_done,
2083 .rpc_release = nfs4_open_confirm_release,
2084};
2085
2086/*
2087 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2088 */
2089static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2090{
David Howells2b0143b2015-03-17 22:25:59 +00002091 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002092 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002093 struct rpc_message msg = {
2094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2095 .rpc_argp = &data->c_arg,
2096 .rpc_resp = &data->c_res,
2097 .rpc_cred = data->owner->so_cred,
2098 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002099 struct rpc_task_setup task_setup_data = {
2100 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002101 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002102 .callback_ops = &nfs4_open_confirm_ops,
2103 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002104 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002105 .flags = RPC_TASK_ASYNC,
2106 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 int status;
2108
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002109 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002110 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002111 data->rpc_done = 0;
2112 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002113 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002114 if (data->is_recover)
2115 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002116 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002117 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002118 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002119 status = nfs4_wait_for_completion_rpc_task(task);
2120 if (status != 0) {
2121 data->cancelled = 1;
2122 smp_wmb();
2123 } else
2124 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002125 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 return status;
2127}
2128
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002129static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002131 struct nfs4_opendata *data = calldata;
2132 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002133 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002134 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002135
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002136 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002137 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002138 /*
2139 * Check if we still need to send an OPEN call, or if we can use
2140 * a delegation instead.
2141 */
2142 if (data->state != NULL) {
2143 struct nfs_delegation *delegation;
2144
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002145 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002146 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002147 rcu_read_lock();
2148 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002149 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002150 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002151 rcu_read_unlock();
2152 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002153 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002154 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002155 switch (claim) {
2156 default:
2157 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002158 case NFS4_OPEN_CLAIM_PREVIOUS:
2159 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2160 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002161 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002162 case NFS4_OPEN_CLAIM_FH:
2163 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002164 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2165 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002166 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002167 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002168 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002169 &data->o_res.seq_res,
2170 task) != 0)
2171 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002172
2173 /* Set the create mode (note dependency on the session type) */
2174 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2175 if (data->o_arg.open_flags & O_EXCL) {
2176 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2177 if (nfs4_has_persistent_session(clp))
2178 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2179 else if (clp->cl_mvops->minor_version > 0)
2180 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2181 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002182 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002183unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002184 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002185 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002186out_no_action:
2187 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002188out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002189 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002190}
2191
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002192static void nfs4_open_done(struct rpc_task *task, void *calldata)
2193{
2194 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002196 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002197
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002198 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002199 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002200
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002201 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002202 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2203 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002204 case S_IFREG:
2205 break;
2206 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002208 break;
2209 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002211 break;
2212 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002213 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002214 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002215 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002216 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002217 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2218 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002219 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002220 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002221}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002222
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002223static void nfs4_open_release(void *calldata)
2224{
2225 struct nfs4_opendata *data = calldata;
2226 struct nfs4_state *state = NULL;
2227
2228 /* If this request hasn't been cancelled, do nothing */
2229 if (data->cancelled == 0)
2230 goto out_free;
2231 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002232 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002233 goto out_free;
2234 /* In case we need an open_confirm, no cleanup! */
2235 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2236 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002237 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002238 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002239 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002240out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002241 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002242}
2243
2244static const struct rpc_call_ops nfs4_open_ops = {
2245 .rpc_call_prepare = nfs4_open_prepare,
2246 .rpc_call_done = nfs4_open_done,
2247 .rpc_release = nfs4_open_release,
2248};
2249
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002250static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002251{
David Howells2b0143b2015-03-17 22:25:59 +00002252 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002253 struct nfs_server *server = NFS_SERVER(dir);
2254 struct nfs_openargs *o_arg = &data->o_arg;
2255 struct nfs_openres *o_res = &data->o_res;
2256 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002257 struct rpc_message msg = {
2258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2259 .rpc_argp = o_arg,
2260 .rpc_resp = o_res,
2261 .rpc_cred = data->owner->so_cred,
2262 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002263 struct rpc_task_setup task_setup_data = {
2264 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002265 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002266 .callback_ops = &nfs4_open_ops,
2267 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002268 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002269 .flags = RPC_TASK_ASYNC,
2270 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002271 int status;
2272
Chuck Levera9c92d62013-08-09 12:48:18 -04002273 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002274 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002275 data->rpc_done = 0;
2276 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002277 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002278 data->is_recover = 0;
2279 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002280 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002281 data->is_recover = 1;
2282 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002283 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002284 if (IS_ERR(task))
2285 return PTR_ERR(task);
2286 status = nfs4_wait_for_completion_rpc_task(task);
2287 if (status != 0) {
2288 data->cancelled = 1;
2289 smp_wmb();
2290 } else
2291 status = data->rpc_status;
2292 rpc_put_task(task);
2293
2294 return status;
2295}
2296
2297static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2298{
David Howells2b0143b2015-03-17 22:25:59 +00002299 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002300 struct nfs_openres *o_res = &data->o_res;
2301 int status;
2302
2303 status = nfs4_run_open_task(data, 1);
2304 if (status != 0 || !data->rpc_done)
2305 return status;
2306
Trond Myklebust6926afd2012-01-07 13:22:46 -05002307 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2308
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002309 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2310 status = _nfs4_proc_open_confirm(data);
2311 if (status != 0)
2312 return status;
2313 }
2314
2315 return status;
2316}
2317
Trond Myklebustf3792d62014-07-10 08:54:32 -04002318/*
2319 * Additional permission checks in order to distinguish between an
2320 * open for read, and an open for execute. This works around the
2321 * fact that NFSv4 OPEN treats read and execute permissions as being
2322 * the same.
2323 * Note that in the non-execute case, we want to turn off permission
2324 * checking if we just created a new file (POSIX open() semantics).
2325 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002326static int nfs4_opendata_access(struct rpc_cred *cred,
2327 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002328 struct nfs4_state *state, fmode_t fmode,
2329 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002330{
2331 struct nfs_access_entry cache;
2332 u32 mask;
2333
2334 /* access call failed or for some reason the server doesn't
2335 * support any access modes -- defer access call until later */
2336 if (opendata->o_res.access_supported == 0)
2337 return 0;
2338
2339 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002340 /*
2341 * Use openflags to check for exec, because fmode won't
2342 * always have FMODE_EXEC set when file open for exec.
2343 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002344 if (openflags & __FMODE_EXEC) {
2345 /* ONLY check for exec rights */
2346 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002347 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002348 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002349
2350 cache.cred = cred;
2351 cache.jiffies = jiffies;
2352 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2353 nfs_access_add_cache(state->inode, &cache);
2354
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002355 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002356 return 0;
2357
2358 /* even though OPEN succeeded, access is denied. Close the file */
2359 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002360 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002361}
2362
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002363/*
2364 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2365 */
2366static int _nfs4_proc_open(struct nfs4_opendata *data)
2367{
David Howells2b0143b2015-03-17 22:25:59 +00002368 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002369 struct nfs_server *server = NFS_SERVER(dir);
2370 struct nfs_openargs *o_arg = &data->o_arg;
2371 struct nfs_openres *o_res = &data->o_res;
2372 int status;
2373
2374 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002375 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002376 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002377 if (status != 0) {
2378 if (status == -NFS4ERR_BADNAME &&
2379 !(o_arg->open_flags & O_CREAT))
2380 return -ENOENT;
2381 return status;
2382 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002383
Trond Myklebust6926afd2012-01-07 13:22:46 -05002384 nfs_fattr_map_and_free_names(server, &data->f_attr);
2385
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002386 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002387 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002388 if (o_arg->open_flags & O_EXCL)
2389 data->file_created = 1;
2390 else if (o_res->cinfo.before != o_res->cinfo.after)
2391 data->file_created = 1;
2392 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002393 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2394 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002396 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002398 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
2400 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002401 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002402 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Andy Adamsond83217c2011-03-01 01:34:17 +00002405static int nfs4_recover_expired_lease(struct nfs_server *server)
2406{
2407 return nfs4_client_recover_expired_lease(server->nfs_client);
2408}
2409
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410/*
2411 * OPEN_EXPIRED:
2412 * reclaim state on the server after a network partition.
2413 * Assumes caller holds the appropriate lock
2414 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002415static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002417 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002418 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002420 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002421 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002422 if (IS_ERR(opendata))
2423 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002424 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002425 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002426 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002427 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002431static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002432{
Trond Myklebust539cd032007-06-05 11:46:42 -04002433 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002434 struct nfs4_exception exception = { };
2435 int err;
2436
2437 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002438 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002439 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002440 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2441 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002442 switch (err) {
2443 default:
2444 goto out;
2445 case -NFS4ERR_GRACE:
2446 case -NFS4ERR_DELAY:
2447 nfs4_handle_exception(server, err, &exception);
2448 err = 0;
2449 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002450 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002451out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002452 return err;
2453}
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2456{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002458 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Trond Myklebust864472e2006-01-03 09:55:15 +01002460 ctx = nfs4_state_find_open_context(state);
2461 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002462 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002463 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002464 put_nfs_open_context(ctx);
2465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Trond Myklebust41020b62016-09-22 13:38:58 -04002468static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2469 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002470{
Trond Myklebust41020b62016-09-22 13:38:58 -04002471 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002472 write_seqlock(&state->seqlock);
2473 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2474 write_sequnlock(&state->seqlock);
2475 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2476}
2477
2478static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2479{
2480 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002481 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002482}
2483
2484static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2485{
2486 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2487 nfs40_clear_delegation_stateid(state);
2488 return nfs4_open_expired(sp, state);
2489}
2490
Trond Myklebust45870d62016-09-22 13:38:59 -04002491static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2492 nfs4_stateid *stateid,
2493 struct rpc_cred *cred)
2494{
2495 return -NFS4ERR_BAD_STATEID;
2496}
2497
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002498#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002499static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2500 nfs4_stateid *stateid,
2501 struct rpc_cred *cred)
2502{
2503 int status;
2504
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002505 switch (stateid->type) {
2506 default:
2507 break;
2508 case NFS4_INVALID_STATEID_TYPE:
2509 case NFS4_SPECIAL_STATEID_TYPE:
2510 return -NFS4ERR_BAD_STATEID;
2511 case NFS4_REVOKED_STATEID_TYPE:
2512 goto out_free;
2513 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002514
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002516 switch (status) {
2517 case -NFS4ERR_EXPIRED:
2518 case -NFS4ERR_ADMIN_REVOKED:
2519 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002520 break;
2521 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002522 return status;
2523 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002524out_free:
2525 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002526 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002527 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002528}
2529
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002530static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002531{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002532 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002533 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002534 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002535 struct rpc_cred *cred;
2536 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002537
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002538 /* Get the delegation credential for use by test/free_stateid */
2539 rcu_read_lock();
2540 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002541 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002542 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002543 return;
2544 }
2545
2546 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002547 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2548 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002549 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002550 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002551 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002552
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002553 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2554 rcu_read_unlock();
2555 return;
2556 }
2557
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002558 cred = get_rpccred(delegation->cred);
2559 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002560 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002561 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002562 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002563 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002564
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002565 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002566}
2567
2568/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002569 * nfs41_check_expired_locks - possibly free a lock stateid
2570 *
2571 * @state: NFSv4 state for an inode
2572 *
2573 * Returns NFS_OK if recovery for this stateid is now finished.
2574 * Otherwise a negative NFS4ERR value is returned.
2575 */
2576static int nfs41_check_expired_locks(struct nfs4_state *state)
2577{
2578 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002579 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002580 struct nfs_server *server = NFS_SERVER(state->inode);
2581
2582 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2583 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002584
2585 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002586 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2587 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2588 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2589
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002590 atomic_inc(&lsp->ls_count);
2591 spin_unlock(&state->state_lock);
2592
2593 nfs4_put_lock_state(prev);
2594 prev = lsp;
2595
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002596 status = nfs41_test_and_free_expired_stateid(server,
2597 &lsp->ls_stateid,
2598 cred);
2599 trace_nfs4_test_lock_stateid(state, lsp, status);
2600 if (status == -NFS4ERR_EXPIRED ||
2601 status == -NFS4ERR_BAD_STATEID) {
2602 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002603 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002604 if (!recover_lost_locks)
2605 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2606 } else if (status != NFS_OK) {
2607 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002608 nfs4_put_lock_state(prev);
2609 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002610 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002611 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002612 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002613 }
2614 spin_unlock(&state->state_lock);
2615 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002616out:
2617 return ret;
2618}
2619
2620/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002621 * nfs41_check_open_stateid - possibly free an open stateid
2622 *
2623 * @state: NFSv4 state for an inode
2624 *
2625 * Returns NFS_OK if recovery for this stateid is now finished.
2626 * Otherwise a negative NFS4ERR value is returned.
2627 */
2628static int nfs41_check_open_stateid(struct nfs4_state *state)
2629{
2630 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002631 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002632 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002633 int status;
2634
Trond Myklebustb134fc42016-09-22 13:39:16 -04002635 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002636 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2637 if (nfs4_have_delegation(state->inode, state->state))
2638 return NFS_OK;
2639 return -NFS4ERR_OPENMODE;
2640 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002641 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002642 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002643 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002644 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002645 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002646 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2647 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2648 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002649 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002650 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002651 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002652 if (status != NFS_OK)
2653 return status;
2654 if (nfs_open_stateid_recover_openmode(state))
2655 return -NFS4ERR_OPENMODE;
2656 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002657}
2658
2659static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2660{
Chuck Levereb64cf92012-07-11 16:30:05 -04002661 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002662
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002663 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002664 status = nfs41_check_expired_locks(state);
2665 if (status != NFS_OK)
2666 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002667 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002668 if (status != NFS_OK)
2669 status = nfs4_open_expired(sp, state);
2670 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002671}
2672#endif
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002675 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2676 * fields corresponding to attributes that were used to store the verifier.
2677 * Make sure we clobber those fields in the later setattr call
2678 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002679static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2680 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002681{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002682 const u32 *attrset = opendata->o_res.attrset;
2683
2684 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002685 !(sattr->ia_valid & ATTR_ATIME_SET))
2686 sattr->ia_valid |= ATTR_ATIME;
2687
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002688 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002689 !(sattr->ia_valid & ATTR_MTIME_SET))
2690 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002691
2692 /* Except MODE, it seems harmless of setting twice. */
2693 if ((attrset[1] & FATTR4_WORD1_MODE))
2694 sattr->ia_valid &= ~ATTR_MODE;
2695
2696 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2697 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002698}
2699
Trond Myklebustc21443c2013-02-07 14:26:21 -05002700static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2701 fmode_t fmode,
2702 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002703 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002704{
2705 struct nfs4_state_owner *sp = opendata->owner;
2706 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002707 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002708 struct nfs4_state *state;
2709 unsigned int seq;
2710 int ret;
2711
2712 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2713
2714 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002715 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002716 goto out;
2717
2718 state = nfs4_opendata_to_nfs4_state(opendata);
2719 ret = PTR_ERR(state);
2720 if (IS_ERR(state))
2721 goto out;
2722 if (server->caps & NFS_CAP_POSIX_LOCK)
2723 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002724 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2725 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002726
Trond Myklebust275bb302013-05-29 13:11:28 -04002727 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002728 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002729 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002731 alias = d_exact_alias(dentry, state->inode);
2732 if (!alias)
2733 alias = d_splice_alias(igrab(state->inode), dentry);
2734 /* d_splice_alias() can't fail here - it's a non-directory */
2735 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002736 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002737 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002738 }
2739 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002740 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002741 }
2742
Trond Myklebustc21443c2013-02-07 14:26:21 -05002743 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2744 if (ret != 0)
2745 goto out;
2746
Trond Myklebust3efb9722013-05-29 13:17:04 -04002747 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002748 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002749 nfs_inode_attach_open_context(ctx);
2750 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2751 nfs4_schedule_stateid_recovery(server, state);
2752 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002753out:
2754 return ret;
2755}
2756
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002757/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002758 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
Andy Adamson82be4172012-05-23 05:02:35 -04002760static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002761 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002762 int flags,
2763 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002764 struct nfs4_label *label,
2765 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 struct nfs4_state_owner *sp;
2768 struct nfs4_state *state = NULL;
2769 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002770 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002771 struct dentry *dentry = ctx->dentry;
2772 struct rpc_cred *cred = ctx->cred;
2773 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2774 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002775 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002776 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002777 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002781 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2782 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2784 goto out_err;
2785 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002786 status = nfs4_recover_expired_lease(server);
2787 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002788 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002789 if (d_really_is_positive(dentry))
2790 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002791 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002792 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002793 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002794 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002795 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002796 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002797 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
David Quigley14c43f72013-05-22 12:50:43 -04002799 if (label) {
2800 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2801 if (IS_ERR(olabel)) {
2802 status = PTR_ERR(olabel);
2803 goto err_opendata_put;
2804 }
2805 }
2806
Trond Myklebuste911b812014-03-26 13:24:37 -07002807 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2808 if (!opendata->f_attr.mdsthreshold) {
2809 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2810 if (!opendata->f_attr.mdsthreshold)
2811 goto err_free_label;
2812 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002813 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002814 }
David Howells2b0143b2015-03-17 22:25:59 +00002815 if (d_really_is_positive(dentry))
2816 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002817
Trond Myklebust3efb9722013-05-29 13:17:04 -04002818 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002819 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002820 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002821 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002822
NeilBrownefcbc042015-07-30 13:00:56 +10002823 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002824 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002825 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002826 /*
2827 * send create attributes which was not set by open
2828 * with an extra setattr.
2829 */
2830 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2831 nfs_fattr_init(opendata->o_res.f_attr);
2832 status = nfs4_do_setattr(state->inode, cred,
2833 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002834 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002835 if (status == 0) {
2836 nfs_setattr_update_inode(state->inode, sattr,
2837 opendata->o_res.f_attr);
2838 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2839 }
David Quigley1775fd32013-05-22 12:50:42 -04002840 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002841 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002842 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002843 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002844
Trond Myklebuste911b812014-03-26 13:24:37 -07002845 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002846 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002847 opendata->f_attr.mdsthreshold = NULL;
2848 }
Andy Adamson82be4172012-05-23 05:02:35 -04002849
David Quigley14c43f72013-05-22 12:50:43 -04002850 nfs4_label_free(olabel);
2851
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002852 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002855err_free_label:
2856 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002857err_opendata_put:
2858 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002859err_put_state_owner:
2860 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 return status;
2863}
2864
2865
Andy Adamson82be4172012-05-23 05:02:35 -04002866static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002867 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002868 int flags,
2869 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002870 struct nfs4_label *label,
2871 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002873 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 struct nfs4_exception exception = { };
2875 struct nfs4_state *res;
2876 int status;
2877
2878 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002879 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002880 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002881 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (status == 0)
2883 break;
2884 /* NOTE: BAD_SEQID means the server and client disagree about the
2885 * book-keeping w.r.t. state-changing operations
2886 * (OPEN/CLOSE/LOCK/LOCKU...)
2887 * It is actually a sign of a bug on the client or on the server.
2888 *
2889 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002890 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 * have unhashed the old state_owner for us, and that we can
2892 * therefore safely retry using a new one. We should still warn
2893 * the user though...
2894 */
2895 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002896 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002897 " returned a bad sequence-id error!\n",
2898 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 exception.retry = 1;
2900 continue;
2901 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002902 /*
2903 * BAD_STATEID on OPEN means that the server cancelled our
2904 * state before it received the OPEN_CONFIRM.
2905 * Recover by retrying the request as per the discussion
2906 * on Page 181 of RFC3530.
2907 */
2908 if (status == -NFS4ERR_BAD_STATEID) {
2909 exception.retry = 1;
2910 continue;
2911 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002912 if (status == -EAGAIN) {
2913 /* We must have found a delegation */
2914 exception.retry = 1;
2915 continue;
2916 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002917 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2918 continue;
2919 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 status, &exception));
2921 } while (exception.retry);
2922 return res;
2923}
2924
Trond Myklebust8487c472016-06-26 08:44:35 -04002925static int _nfs4_do_setattr(struct inode *inode,
2926 struct nfs_setattrargs *arg,
2927 struct nfs_setattrres *res,
2928 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002929 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002930{
2931 struct nfs_server *server = NFS_SERVER(inode);
2932 struct rpc_message msg = {
2933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2934 .rpc_argp = arg,
2935 .rpc_resp = res,
2936 .rpc_cred = cred,
2937 };
2938 struct rpc_cred *delegation_cred = NULL;
2939 unsigned long timestamp = jiffies;
2940 fmode_t fmode;
2941 bool truncate;
2942 int status;
2943
2944 nfs_fattr_init(res->fattr);
2945
2946 /* Servers should only apply open mode checks for file size changes */
2947 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2948 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2949
2950 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2951 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002952 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002953 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002954 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002955 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002956 l_ctx = nfs_get_lock_context(ctx);
2957 if (IS_ERR(l_ctx))
2958 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002959 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2960 &arg->stateid, &delegation_cred);
2961 nfs_put_lock_context(l_ctx);
2962 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002963 return -EBADF;
2964 } else
2965 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2966 if (delegation_cred)
2967 msg.rpc_cred = delegation_cred;
2968
2969 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2970
2971 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002972 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002973 renew_lease(server, timestamp);
2974 trace_nfs4_setattr(inode, &arg->stateid, status);
2975 return status;
2976}
2977
2978static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2979 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002980 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002981 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002983 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002984 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002986 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .iap = sattr,
2988 .server = server,
2989 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002990 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 };
2992 struct nfs_setattrres res = {
2993 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002994 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 .server = server,
2996 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002997 struct nfs4_exception exception = {
2998 .state = state,
2999 .inode = inode,
3000 .stateid = &arg.stateid,
3001 };
3002 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
David Quigleyaa9c2662013-05-22 12:50:44 -04003004 arg.bitmask = nfs4_bitmask(server, ilabel);
3005 if (ilabel)
3006 arg.bitmask = nfs4_bitmask(server, olabel);
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003009 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003010 switch (err) {
3011 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003012 if (!(sattr->ia_valid & ATTR_SIZE)) {
3013 pr_warn_once("NFSv4: server %s is incorrectly "
3014 "applying open mode checks to "
3015 "a SETATTR that is not "
3016 "changing file size.\n",
3017 server->nfs_client->cl_hostname);
3018 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003019 if (state && !(state->state & FMODE_WRITE)) {
3020 err = -EBADF;
3021 if (sattr->ia_valid & ATTR_OPEN)
3022 err = -EACCES;
3023 goto out;
3024 }
3025 }
3026 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003028out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return err;
3030}
3031
Peng Tao500d7012015-09-22 11:35:22 +08003032static bool
3033nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3034{
3035 if (inode == NULL || !nfs_have_layout(inode))
3036 return false;
3037
3038 return pnfs_wait_on_layoutreturn(inode, task);
3039}
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041struct nfs4_closedata {
3042 struct inode *inode;
3043 struct nfs4_state *state;
3044 struct nfs_closeargs arg;
3045 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003046 struct {
3047 struct nfs4_layoutreturn_args arg;
3048 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003049 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003050 u32 roc_barrier;
3051 bool roc;
3052 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003053 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003054 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055};
3056
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003057static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003058{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003059 struct nfs4_closedata *calldata = data;
3060 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003061 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003062
Trond Myklebustcf805162016-11-15 14:56:07 -05003063 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003064 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3065 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003066 nfs4_put_open_state(calldata->state);
3067 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003068 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003069 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003070 kfree(calldata);
3071}
3072
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003073static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003075 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003078 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Chuck Levera3ca5652012-03-01 17:00:40 -05003080 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003081 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3082 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003083 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003084
3085 /* Handle Layoutreturn errors */
3086 if (calldata->arg.lr_args && task->tk_status != 0) {
3087 switch (calldata->res.lr_ret) {
3088 default:
3089 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3090 break;
3091 case 0:
3092 calldata->arg.lr_args = NULL;
3093 calldata->res.lr_res = NULL;
3094 break;
3095 case -NFS4ERR_ADMIN_REVOKED:
3096 case -NFS4ERR_DELEG_REVOKED:
3097 case -NFS4ERR_EXPIRED:
3098 case -NFS4ERR_BAD_STATEID:
3099 case -NFS4ERR_OLD_STATEID:
3100 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3101 case -NFS4ERR_WRONG_CRED:
3102 calldata->arg.lr_args = NULL;
3103 calldata->res.lr_res = NULL;
3104 calldata->res.lr_ret = 0;
3105 rpc_restart_call_prepare(task);
3106 return;
3107 }
3108 }
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 /* hmm. we are done with the inode, and in the process of freeing
3111 * the state_owner. we keep this around to process errors
3112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 switch (task->tk_status) {
3114 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003115 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003116 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003117 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003118 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003120 case -NFS4ERR_EXPIRED:
3121 nfs4_free_revoked_stateid(server,
3122 &calldata->arg.stateid,
3123 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003124 case -NFS4ERR_OLD_STATEID:
3125 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003126 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003127 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003128 rpc_restart_call_prepare(task);
3129 goto out_release;
3130 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003131 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003134 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003135 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003136 goto out_release;
3137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003139 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3140 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003141out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003142 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003143 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003144 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003147static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003149 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003150 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003151 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003152 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003153 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003154
Chuck Levera3ca5652012-03-01 17:00:40 -05003155 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003157 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003158
Trond Myklebust88069f72009-12-08 08:33:16 -05003159 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003160 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003161 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3162 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3163 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003164 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003165 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003166 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003167 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003168 if (state->n_rdonly == 0)
3169 call_close |= is_rdonly;
3170 else if (is_rdonly)
3171 calldata->arg.fmode |= FMODE_READ;
3172 if (state->n_wronly == 0)
3173 call_close |= is_wronly;
3174 else if (is_wronly)
3175 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003176 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3177 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003178 } else if (is_rdwr)
3179 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3180
Trond Myklebust5cc78612016-11-14 11:19:56 -05003181 if (!nfs4_valid_open_stateid(state) ||
3182 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003183 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003184 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003185
3186 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003187 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003188 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003189 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003190
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003191 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003192 nfs_release_seqid(calldata->arg.seqid);
3193 goto out_wait;
3194 }
3195
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003196 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05003197 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003198
3199 /* Close-to-open cache consistency revalidation */
3200 if (!nfs4_have_delegation(inode, FMODE_READ))
3201 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3202 else
3203 calldata->arg.bitmask = NULL;
3204 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003205
Trond Myklebust6ae37332015-01-30 14:21:14 -05003206 calldata->arg.share_access =
3207 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3208 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003209
Trond Myklebust516a6af2005-10-27 22:12:41 -04003210 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003211 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003212 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003213 &calldata->arg.seq_args,
3214 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003215 task) != 0)
3216 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003217 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003218 return;
3219out_no_action:
3220 task->tk_action = NULL;
3221out_wait:
3222 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223}
3224
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003225static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003226 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003227 .rpc_call_done = nfs4_close_done,
3228 .rpc_release = nfs4_free_closedata,
3229};
3230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231/*
3232 * It is possible for data to be read/written from a mem-mapped file
3233 * after the sys_close call (which hits the vfs layer as a flush).
3234 * This means that we can't safely call nfsv4 close on a file until
3235 * the inode is cleared. This in turn means that we are not good
3236 * NFSv4 citizens - we do not indicate to the server to update the file's
3237 * share state even when we are done with one of the three share
3238 * stateid's in the inode.
3239 *
3240 * NOTE: Caller must be holding the sp->so_owner semaphore!
3241 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003242int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003244 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003245 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003247 struct nfs4_state_owner *sp = state->owner;
3248 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003249 struct rpc_message msg = {
3250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3251 .rpc_cred = state->owner->so_cred,
3252 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003253 struct rpc_task_setup task_setup_data = {
3254 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003255 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003256 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003257 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003258 .flags = RPC_TASK_ASYNC,
3259 };
Trond Myklebust95121352005-10-18 14:20:12 -07003260 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003262 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3263 &task_setup_data.rpc_client, &msg);
3264
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003265 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003267 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003268 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003269 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003271 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003273 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3274 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003275 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003276 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003277 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003278 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003279 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003280 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003281 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003282 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003283 calldata->lr.roc = pnfs_roc(state->inode,
3284 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3285 if (calldata->lr.roc) {
3286 calldata->arg.lr_args = &calldata->lr.arg;
3287 calldata->res.lr_res = &calldata->lr.res;
3288 }
Al Viro643168c2011-06-22 18:20:23 -04003289 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003290
Trond Myklebust1174dd12010-12-21 10:52:24 -05003291 msg.rpc_argp = &calldata->arg;
3292 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003293 task_setup_data.callback_data = calldata;
3294 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003295 if (IS_ERR(task))
3296 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003297 status = 0;
3298 if (wait)
3299 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003300 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003301 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003302out_free_calldata:
3303 kfree(calldata);
3304out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003305 nfs4_put_open_state(state);
3306 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003307 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308}
3309
Trond Myklebust2b484292010-09-17 10:56:51 -04003310static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003311nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3312 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003315 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3316
3317 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Trond Myklebust565277f2007-10-15 18:17:53 -04003319 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003320 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003321
3322 nfs4_label_release_security(label);
3323
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003324 if (IS_ERR(state))
3325 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003326 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
Trond Myklebust1185a552009-12-03 15:54:02 -05003329static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003330{
3331 if (ctx->state == NULL)
3332 return;
3333 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003334 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003335 else
Al Viro643168c2011-06-22 18:20:23 -04003336 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003337}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
Trond Myklebustb944dba2013-11-04 15:20:20 -05003339#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3340#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003341#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003342
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3344{
Kinglong Mee8c612822015-08-26 21:12:58 +08003345 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003346 struct nfs4_server_caps_arg args = {
3347 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003348 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 struct nfs4_server_caps_res res = {};
3351 struct rpc_message msg = {
3352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003353 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 .rpc_resp = &res,
3355 };
3356 int status;
3357
Kinglong Mee8c612822015-08-26 21:12:58 +08003358 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3359 FATTR4_WORD0_FH_EXPIRE_TYPE |
3360 FATTR4_WORD0_LINK_SUPPORT |
3361 FATTR4_WORD0_SYMLINK_SUPPORT |
3362 FATTR4_WORD0_ACLSUPPORT;
3363 if (minorversion)
3364 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3365
Bryan Schumaker7c513052011-03-24 17:12:24 +00003366 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003368 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003369 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003370 case 0:
3371 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3372 res.attr_bitmask[2] = 0;
3373 break;
3374 case 1:
3375 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3376 break;
3377 case 2:
3378 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003381 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3382 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3383 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3384 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003385 NFS_CAP_CTIME|NFS_CAP_MTIME|
3386 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003387 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3388 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 server->caps |= NFS_CAP_ACLS;
3390 if (res.has_links != 0)
3391 server->caps |= NFS_CAP_HARDLINKS;
3392 if (res.has_symlinks != 0)
3393 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003394 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3395 server->caps |= NFS_CAP_FILEID;
3396 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3397 server->caps |= NFS_CAP_MODE;
3398 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3399 server->caps |= NFS_CAP_NLINK;
3400 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3401 server->caps |= NFS_CAP_OWNER;
3402 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3403 server->caps |= NFS_CAP_OWNER_GROUP;
3404 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3405 server->caps |= NFS_CAP_ATIME;
3406 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3407 server->caps |= NFS_CAP_CTIME;
3408 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3409 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003410#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3411 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3412 server->caps |= NFS_CAP_SECURITY_LABEL;
3413#endif
3414 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3415 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003416 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003417
Trond Myklebusta65318b2009-03-11 14:10:28 -04003418 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3419 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3420 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003421 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003422 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3423 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003425 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 return status;
3429}
3430
Trond Myklebust55a97592006-06-09 09:34:19 -04003431int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
3433 struct nfs4_exception exception = { };
3434 int err;
3435 do {
3436 err = nfs4_handle_exception(server,
3437 _nfs4_server_capabilities(server, fhandle),
3438 &exception);
3439 } while (exception.retry);
3440 return err;
3441}
3442
3443static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3444 struct nfs_fsinfo *info)
3445{
David Quigleyaa9c2662013-05-22 12:50:44 -04003446 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003448 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 };
3450 struct nfs4_lookup_res res = {
3451 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003452 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 .fh = fhandle,
3454 };
3455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3457 .rpc_argp = &args,
3458 .rpc_resp = &res,
3459 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003460
David Quigleyaa9c2662013-05-22 12:50:44 -04003461 bitmask[0] = nfs4_fattr_bitmap[0];
3462 bitmask[1] = nfs4_fattr_bitmap[1];
3463 /*
3464 * Process the label in the upcoming getfattr
3465 */
3466 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3467
Trond Myklebust0e574af2005-10-27 22:12:38 -04003468 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003469 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
3472static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3473 struct nfs_fsinfo *info)
3474{
3475 struct nfs4_exception exception = { };
3476 int err;
3477 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003478 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003479 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003480 switch (err) {
3481 case 0:
3482 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003483 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003484 default:
3485 err = nfs4_handle_exception(server, err, &exception);
3486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return err;
3490}
3491
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003492static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3493 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3494{
Trond Myklebustc2190662013-08-26 19:23:04 -04003495 struct rpc_auth_create_args auth_args = {
3496 .pseudoflavor = flavor,
3497 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003498 struct rpc_auth *auth;
3499 int ret;
3500
Trond Myklebustc2190662013-08-26 19:23:04 -04003501 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003502 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003503 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003504 goto out;
3505 }
3506 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003507out:
3508 return ret;
3509}
3510
Chuck Lever9a744ba2013-03-16 15:56:02 -04003511/*
3512 * Retry pseudoroot lookup with various security flavors. We do this when:
3513 *
3514 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3515 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3516 *
3517 * Returns zero on success, or a negative NFS4ERR value, or a
3518 * negative errno value.
3519 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003520static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003521 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003523 /* Per 3530bis 15.33.5 */
3524 static const rpc_authflavor_t flav_array[] = {
3525 RPC_AUTH_GSS_KRB5P,
3526 RPC_AUTH_GSS_KRB5I,
3527 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003528 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003529 RPC_AUTH_NULL,
3530 };
3531 int status = -EPERM;
3532 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003534 if (server->auth_info.flavor_len > 0) {
3535 /* try each flavor specified by user */
3536 for (i = 0; i < server->auth_info.flavor_len; i++) {
3537 status = nfs4_lookup_root_sec(server, fhandle, info,
3538 server->auth_info.flavors[i]);
3539 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3540 continue;
3541 break;
3542 }
3543 } else {
3544 /* no flavors specified by user, try default list */
3545 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3546 status = nfs4_lookup_root_sec(server, fhandle, info,
3547 flav_array[i]);
3548 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3549 continue;
3550 break;
3551 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003552 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003553
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003554 /*
3555 * -EACCESS could mean that the user doesn't have correct permissions
3556 * to access the mount. It could also mean that we tried to mount
3557 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3558 * existing mount programs don't handle -EACCES very well so it should
3559 * be mapped to -EPERM instead.
3560 */
3561 if (status == -EACCES)
3562 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003563 return status;
3564}
3565
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003566/**
3567 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3568 * @server: initialized nfs_server handle
3569 * @fhandle: we fill in the pseudo-fs root file handle
3570 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003571 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003572 *
3573 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003574 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003575int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003576 struct nfs_fsinfo *info,
3577 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003578{
Andre Przywarac7757072015-04-23 17:17:40 +01003579 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003580
Andre Przywarac7757072015-04-23 17:17:40 +01003581 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003582 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003583
3584 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003585 status = server->nfs_client->cl_mvops->find_root_sec(server,
3586 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (status == 0)
3589 status = nfs4_server_capabilities(server, fhandle);
3590 if (status == 0)
3591 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003592
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003593 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594}
3595
Bryan Schumakerbae36242012-05-10 15:07:31 -04003596static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3597 struct nfs_fsinfo *info)
3598{
3599 int error;
3600 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003601 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003602
3603 error = nfs4_server_capabilities(server, mntfh);
3604 if (error < 0) {
3605 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3606 return error;
3607 }
3608
David Quigley14c43f72013-05-22 12:50:43 -04003609 label = nfs4_label_alloc(server, GFP_KERNEL);
3610 if (IS_ERR(label))
3611 return PTR_ERR(label);
3612
David Quigley1775fd32013-05-22 12:50:42 -04003613 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003614 if (error < 0) {
3615 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003616 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003617 }
3618
3619 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3620 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3621 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3622
David Quigley14c43f72013-05-22 12:50:43 -04003623err_free_label:
3624 nfs4_label_free(label);
3625
Bryan Schumakerbae36242012-05-10 15:07:31 -04003626 return error;
3627}
3628
Manoj Naik6b97fd32006-06-09 09:34:29 -04003629/*
3630 * Get locations and (maybe) other attributes of a referral.
3631 * Note that we'll actually follow the referral later when
3632 * we detect fsid mismatch in inode revalidation
3633 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003634static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3635 const struct qstr *name, struct nfs_fattr *fattr,
3636 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003637{
3638 int status = -ENOMEM;
3639 struct page *page = NULL;
3640 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003641
3642 page = alloc_page(GFP_KERNEL);
3643 if (page == NULL)
3644 goto out;
3645 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3646 if (locations == NULL)
3647 goto out;
3648
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003649 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003650 if (status != 0)
3651 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003652
3653 /*
3654 * If the fsid didn't change, this is a migration event, not a
3655 * referral. Cause us to drop into the exception handler, which
3656 * will kick off migration recovery.
3657 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003658 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003659 dprintk("%s: server did not return a different fsid for"
3660 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003661 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003662 goto out;
3663 }
Andy Adamson533eb462011-06-13 18:25:56 -04003664 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3665 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003666
Andy Adamson533eb462011-06-13 18:25:56 -04003667 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003668 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003669 memset(fhandle, 0, sizeof(struct nfs_fh));
3670out:
3671 if (page)
3672 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003673 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003674 return status;
3675}
3676
David Quigley1775fd32013-05-22 12:50:42 -04003677static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3678 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 struct nfs4_getattr_arg args = {
3681 .fh = fhandle,
3682 .bitmask = server->attr_bitmask,
3683 };
3684 struct nfs4_getattr_res res = {
3685 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003686 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 .server = server,
3688 };
3689 struct rpc_message msg = {
3690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3691 .rpc_argp = &args,
3692 .rpc_resp = &res,
3693 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003694
3695 args.bitmask = nfs4_bitmask(server, label);
3696
Trond Myklebust0e574af2005-10-27 22:12:38 -04003697 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003698 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699}
3700
David Quigley1775fd32013-05-22 12:50:42 -04003701static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3702 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
3704 struct nfs4_exception exception = { };
3705 int err;
3706 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003707 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3708 trace_nfs4_getattr(server, fhandle, fattr, err);
3709 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 &exception);
3711 } while (exception.retry);
3712 return err;
3713}
3714
3715/*
3716 * The file is not closed if it is opened due to the a request to change
3717 * the size of the file. The open call will not be needed once the
3718 * VFS layer lookup-intents are implemented.
3719 *
3720 * Close is called when the inode is destroyed.
3721 * If we haven't opened the file for O_WRONLY, we
3722 * need to in the size_change case to obtain a stateid.
3723 *
3724 * Got race?
3725 * Because OPEN is always done by name in nfsv4, it is
3726 * possible that we opened a different file by the same
3727 * name. We can recognize this race condition, but we
3728 * can't do anything about it besides returning an error.
3729 *
3730 * This will be fixed with VFS changes (lookup-intent).
3731 */
3732static int
3733nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3734 struct iattr *sattr)
3735{
David Howells2b0143b2015-03-17 22:25:59 +00003736 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003737 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003738 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003739 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 int status;
3741
Peng Tao88ac8152014-09-12 11:04:10 +08003742 if (pnfs_ld_layoutret_on_setattr(inode) &&
3743 sattr->ia_valid & ATTR_SIZE &&
3744 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003745 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003746
Trond Myklebust0e574af2005-10-27 22:12:38 -04003747 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
Andy Adamson26699402012-05-30 16:12:24 -04003749 /* Deal with open(O_TRUNC) */
3750 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003751 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003752
3753 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003754 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003755 return 0;
3756
Trond Myklebustd5308382005-11-04 15:33:38 -05003757 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003758 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003759
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003760 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003761 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003762 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003763 }
3764
David Quigley14c43f72013-05-22 12:50:43 -04003765 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3766 if (IS_ERR(label))
3767 return PTR_ERR(label);
3768
NeilBrown29b59f92016-10-13 15:26:47 +11003769 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003770 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003771 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003772 nfs_setsecurity(inode, fattr, label);
3773 }
David Quigley14c43f72013-05-22 12:50:43 -04003774 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return status;
3776}
3777
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003778static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3779 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003780 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003781{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003782 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003783 int status;
3784 struct nfs4_lookup_arg args = {
3785 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003786 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003787 .name = name,
3788 };
3789 struct nfs4_lookup_res res = {
3790 .server = server,
3791 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003792 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003793 .fh = fhandle,
3794 };
3795 struct rpc_message msg = {
3796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3797 .rpc_argp = &args,
3798 .rpc_resp = &res,
3799 };
3800
David Quigleyaa9c2662013-05-22 12:50:44 -04003801 args.bitmask = nfs4_bitmask(server, label);
3802
David Howells2b3de442006-08-22 20:06:09 -04003803 nfs_fattr_init(fattr);
3804
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003806 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 dprintk("NFS reply lookup: %d\n", status);
3808 return status;
3809}
3810
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003811static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003812{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003813 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003814 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003815 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3816 fattr->nlink = 2;
3817}
3818
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003819static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003820 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003821 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003822{
3823 struct nfs4_exception exception = { };
3824 struct rpc_clnt *client = *clnt;
3825 int err;
3826 do {
David Quigley1775fd32013-05-22 12:50:42 -04003827 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003828 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003829 switch (err) {
3830 case -NFS4ERR_BADNAME:
3831 err = -ENOENT;
3832 goto out;
3833 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003834 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003835 if (err == -NFS4ERR_MOVED)
3836 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003837 goto out;
3838 case -NFS4ERR_WRONGSEC:
3839 err = -EPERM;
3840 if (client != *clnt)
3841 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003842 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003843 if (IS_ERR(client))
3844 return PTR_ERR(client);
3845
3846 exception.retry = 1;
3847 break;
3848 default:
3849 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3850 }
3851 } while (exception.retry);
3852
3853out:
3854 if (err == 0)
3855 *clnt = client;
3856 else if (client != *clnt)
3857 rpc_shutdown_client(client);
3858
3859 return err;
3860}
3861
Al Virobeffb8f2016-07-20 16:34:42 -04003862static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003863 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3864 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003866 int status;
3867 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003868
David Quigley1775fd32013-05-22 12:50:42 -04003869 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003870 if (client != NFS_CLIENT(dir)) {
3871 rpc_shutdown_client(client);
3872 nfs_fixup_secinfo_attributes(fattr);
3873 }
3874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875}
3876
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003877struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003878nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003879 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3880{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003881 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003882 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003883
David Quigley1775fd32013-05-22 12:50:42 -04003884 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003885 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003886 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003887 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003888}
3889
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3891{
Trond Myklebust76b32992007-08-10 17:45:11 -04003892 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct nfs4_accessargs args = {
3894 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003895 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003897 struct nfs4_accessres res = {
3898 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003899 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 struct rpc_message msg = {
3901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3902 .rpc_argp = &args,
3903 .rpc_resp = &res,
3904 .rpc_cred = entry->cred,
3905 };
3906 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003907 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
3909 /*
3910 * Determine which access bits we want to ask for...
3911 */
3912 if (mode & MAY_READ)
3913 args.access |= NFS4_ACCESS_READ;
3914 if (S_ISDIR(inode->i_mode)) {
3915 if (mode & MAY_WRITE)
3916 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3917 if (mode & MAY_EXEC)
3918 args.access |= NFS4_ACCESS_LOOKUP;
3919 } else {
3920 if (mode & MAY_WRITE)
3921 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3922 if (mode & MAY_EXEC)
3923 args.access |= NFS4_ACCESS_EXECUTE;
3924 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003925
3926 res.fattr = nfs_alloc_fattr();
3927 if (res.fattr == NULL)
3928 return -ENOMEM;
3929
Bryan Schumaker7c513052011-03-24 17:12:24 +00003930 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003932 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003933 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003935 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return status;
3937}
3938
3939static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3940{
3941 struct nfs4_exception exception = { };
3942 int err;
3943 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003944 err = _nfs4_proc_access(inode, entry);
3945 trace_nfs4_access(inode, err);
3946 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 &exception);
3948 } while (exception.retry);
3949 return err;
3950}
3951
3952/*
3953 * TODO: For the time being, we don't try to get any attributes
3954 * along with any of the zero-copy operations READ, READDIR,
3955 * READLINK, WRITE.
3956 *
3957 * In the case of the first three, we want to put the GETATTR
3958 * after the read-type operation -- this is because it is hard
3959 * to predict the length of a GETATTR response in v4, and thus
3960 * align the READ data correctly. This means that the GETATTR
3961 * may end up partially falling into the page cache, and we should
3962 * shift it into the 'tail' of the xdr_buf before processing.
3963 * To do this efficiently, we need to know the total length
3964 * of data received, which doesn't seem to be available outside
3965 * of the RPC layer.
3966 *
3967 * In the case of WRITE, we also want to put the GETATTR after
3968 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003969 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 *
3971 * Both of these changes to the XDR layer would in fact be quite
3972 * minor, but I decided to leave them for a subsequent patch.
3973 */
3974static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3975 unsigned int pgbase, unsigned int pglen)
3976{
3977 struct nfs4_readlink args = {
3978 .fh = NFS_FH(inode),
3979 .pgbase = pgbase,
3980 .pglen = pglen,
3981 .pages = &page,
3982 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003983 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 struct rpc_message msg = {
3985 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3986 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003987 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 };
3989
Bryan Schumaker7c513052011-03-24 17:12:24 +00003990 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 -07003991}
3992
3993static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3994 unsigned int pgbase, unsigned int pglen)
3995{
3996 struct nfs4_exception exception = { };
3997 int err;
3998 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003999 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4000 trace_nfs4_readlink(inode, err);
4001 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 &exception);
4003 } while (exception.retry);
4004 return err;
4005}
4006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004008 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010static int
4011nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004012 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004014 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004015 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004016 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 int status = 0;
4019
NeilBrown532d4de2016-10-13 15:26:47 +11004020 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004021 if (IS_ERR(ctx))
4022 return PTR_ERR(ctx);
4023
David Quigleyaa9c2662013-05-22 12:50:44 -04004024 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4025
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004026 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4027 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004028 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (IS_ERR(state)) {
4030 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004031 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004034 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004035 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return status;
4037}
4038
Al Virobeffb8f2016-07-20 16:34:42 -04004039static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Trond Myklebust16e42952005-10-27 22:12:44 -04004041 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004042 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004044 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004046 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004047 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4051 .rpc_argp = &args,
4052 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004054 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004055
Bryan Schumaker7c513052011-03-24 17:12:24 +00004056 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004057 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004058 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return status;
4060}
4061
Al Virobeffb8f2016-07-20 16:34:42 -04004062static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
4064 struct nfs4_exception exception = { };
4065 int err;
4066 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004067 err = _nfs4_proc_remove(dir, name);
4068 trace_nfs4_remove(dir, name, err);
4069 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 &exception);
4071 } while (exception.retry);
4072 return err;
4073}
4074
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004075static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004077 struct nfs_server *server = NFS_SERVER(dir);
4078 struct nfs_removeargs *args = msg->rpc_argp;
4079 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004081 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004083 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004084
4085 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004088static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4089{
Al Viro884be172016-04-28 23:56:31 -04004090 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004091 &data->args.seq_args,
4092 &data->res.seq_res,
4093 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
4095
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004096static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004098 struct nfs_unlinkdata *data = task->tk_calldata;
4099 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004100
Trond Myklebust14516c32010-07-31 14:29:06 -04004101 if (!nfs4_sequence_done(task, &res->seq_res))
4102 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004103 if (nfs4_async_handle_error(task, res->server, NULL,
4104 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004105 return 0;
4106 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004107 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Jeff Laytond3d41522010-09-17 17:31:57 -04004110static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4111{
4112 struct nfs_server *server = NFS_SERVER(dir);
4113 struct nfs_renameargs *arg = msg->rpc_argp;
4114 struct nfs_renameres *res = msg->rpc_resp;
4115
4116 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004117 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004118 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004119}
4120
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004121static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4122{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004123 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4124 &data->args.seq_args,
4125 &data->res.seq_res,
4126 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004127}
4128
4129static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4130 struct inode *new_dir)
4131{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004132 struct nfs_renamedata *data = task->tk_calldata;
4133 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004134
4135 if (!nfs4_sequence_done(task, &res->seq_res))
4136 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004137 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004138 return 0;
4139
4140 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004141 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004142 return 1;
4143}
4144
Al Virobeffb8f2016-07-20 16:34:42 -04004145static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004147 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 struct nfs4_link_arg arg = {
4149 .fh = NFS_FH(inode),
4150 .dir_fh = NFS_FH(dir),
4151 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004152 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004154 struct nfs4_link_res res = {
4155 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004156 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004157 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct rpc_message msg = {
4159 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4160 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004161 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004163 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Trond Myklebust136f2622010-04-16 16:22:49 -04004165 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004166 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004167 goto out;
4168
David Quigley14c43f72013-05-22 12:50:43 -04004169 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4170 if (IS_ERR(res.label)) {
4171 status = PTR_ERR(res.label);
4172 goto out;
4173 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004174 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004175
Bryan Schumaker7c513052011-03-24 17:12:24 +00004176 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004177 if (!status) {
4178 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004179 status = nfs_post_op_update_inode(inode, res.fattr);
4180 if (!status)
4181 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004182 }
David Quigley14c43f72013-05-22 12:50:43 -04004183
4184
4185 nfs4_label_free(res.label);
4186
Trond Myklebust136f2622010-04-16 16:22:49 -04004187out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004188 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return status;
4190}
4191
Al Virobeffb8f2016-07-20 16:34:42 -04004192static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
4194 struct nfs4_exception exception = { };
4195 int err;
4196 do {
4197 err = nfs4_handle_exception(NFS_SERVER(inode),
4198 _nfs4_proc_link(inode, dir, name),
4199 &exception);
4200 } while (exception.retry);
4201 return err;
4202}
4203
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004204struct nfs4_createdata {
4205 struct rpc_message msg;
4206 struct nfs4_create_arg arg;
4207 struct nfs4_create_res res;
4208 struct nfs_fh fh;
4209 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004210 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004211};
4212
4213static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004214 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004215{
4216 struct nfs4_createdata *data;
4217
4218 data = kzalloc(sizeof(*data), GFP_KERNEL);
4219 if (data != NULL) {
4220 struct nfs_server *server = NFS_SERVER(dir);
4221
David Quigley14c43f72013-05-22 12:50:43 -04004222 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4223 if (IS_ERR(data->label))
4224 goto out_free;
4225
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004226 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4227 data->msg.rpc_argp = &data->arg;
4228 data->msg.rpc_resp = &data->res;
4229 data->arg.dir_fh = NFS_FH(dir);
4230 data->arg.server = server;
4231 data->arg.name = name;
4232 data->arg.attrs = sattr;
4233 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004234 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004235 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236 data->res.server = server;
4237 data->res.fh = &data->fh;
4238 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004239 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004240 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004241 }
4242 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004243out_free:
4244 kfree(data);
4245 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004246}
4247
4248static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4249{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004250 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004251 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 if (status == 0) {
4253 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004254 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004255 }
4256 return status;
4257}
4258
4259static void nfs4_free_createdata(struct nfs4_createdata *data)
4260{
David Quigley14c43f72013-05-22 12:50:43 -04004261 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004262 kfree(data);
4263}
4264
Chuck Lever4f390c12006-08-22 20:06:22 -04004265static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004266 struct page *page, unsigned int len, struct iattr *sattr,
4267 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004269 struct nfs4_createdata *data;
4270 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Chuck Lever94a6d752006-08-22 20:06:23 -04004272 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004273 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004274
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 status = -ENOMEM;
4276 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4277 if (data == NULL)
4278 goto out;
4279
4280 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4281 data->arg.u.symlink.pages = &page;
4282 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004283 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004285 status = nfs4_do_create(dir, dentry, data);
4286
4287 nfs4_free_createdata(data);
4288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 return status;
4290}
4291
Chuck Lever4f390c12006-08-22 20:06:22 -04004292static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004293 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294{
4295 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004296 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004298
4299 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004302 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4303 trace_nfs4_symlink(dir, &dentry->d_name, err);
4304 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 &exception);
4306 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004307
4308 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return err;
4310}
4311
4312static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004313 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004315 struct nfs4_createdata *data;
4316 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004318 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4319 if (data == NULL)
4320 goto out;
4321
David Quigley1775fd32013-05-22 12:50:42 -04004322 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004323 status = nfs4_do_create(dir, dentry, data);
4324
4325 nfs4_free_createdata(data);
4326out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return status;
4328}
4329
4330static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4331 struct iattr *sattr)
4332{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004333 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004335 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004337
David Quigleyaa9c2662013-05-22 12:50:44 -04004338 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4339
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004340 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4341 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004343 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4344 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4345 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 &exception);
4347 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004348 nfs4_label_release_security(label);
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 return err;
4351}
4352
4353static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004354 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
David Howells2b0143b2015-03-17 22:25:59 +00004356 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct nfs4_readdir_arg args = {
4358 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004359 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 .pgbase = 0,
4361 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004362 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004363 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 };
4365 struct nfs4_readdir_res res;
4366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4368 .rpc_argp = &args,
4369 .rpc_resp = &res,
4370 .rpc_cred = cred,
4371 };
4372 int status;
4373
Al Viro6de14722013-09-16 10:53:17 -04004374 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4375 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004376 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004377 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004379 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 -05004380 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004381 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004382 status += args.pgbase;
4383 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004384
4385 nfs_invalidate_atime(dir);
4386
Harvey Harrison3110ff82008-05-02 13:42:44 -07004387 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 return status;
4389}
4390
4391static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004392 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393{
4394 struct nfs4_exception exception = { };
4395 int err;
4396 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004397 err = _nfs4_proc_readdir(dentry, cred, cookie,
4398 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004399 trace_nfs4_readdir(d_inode(dentry), err);
4400 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 &exception);
4402 } while (exception.retry);
4403 return err;
4404}
4405
4406static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004407 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004409 struct nfs4_createdata *data;
4410 int mode = sattr->ia_mode;
4411 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004413 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4414 if (data == NULL)
4415 goto out;
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004418 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420 data->arg.ftype = NF4BLK;
4421 data->arg.u.device.specdata1 = MAJOR(rdev);
4422 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
4424 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004425 data->arg.ftype = NF4CHR;
4426 data->arg.u.device.specdata1 = MAJOR(rdev);
4427 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004428 } else if (!S_ISSOCK(mode)) {
4429 status = -EINVAL;
4430 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
David Quigley1775fd32013-05-22 12:50:42 -04004432
David Quigleyaa9c2662013-05-22 12:50:44 -04004433 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004434 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004435out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004436 nfs4_free_createdata(data);
4437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return status;
4439}
4440
4441static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4442 struct iattr *sattr, dev_t rdev)
4443{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004444 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004446 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004448
David Quigleyaa9c2662013-05-22 12:50:44 -04004449 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4450
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004451 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4452 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004454 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4455 trace_nfs4_mknod(dir, &dentry->d_name, err);
4456 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 &exception);
4458 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004459
4460 nfs4_label_release_security(label);
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 return err;
4463}
4464
4465static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4466 struct nfs_fsstat *fsstat)
4467{
4468 struct nfs4_statfs_arg args = {
4469 .fh = fhandle,
4470 .bitmask = server->attr_bitmask,
4471 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004472 struct nfs4_statfs_res res = {
4473 .fsstat = fsstat,
4474 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4477 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004478 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 };
4480
Trond Myklebust0e574af2005-10-27 22:12:38 -04004481 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004482 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483}
4484
4485static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4486{
4487 struct nfs4_exception exception = { };
4488 int err;
4489 do {
4490 err = nfs4_handle_exception(server,
4491 _nfs4_proc_statfs(server, fhandle, fsstat),
4492 &exception);
4493 } while (exception.retry);
4494 return err;
4495}
4496
4497static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4498 struct nfs_fsinfo *fsinfo)
4499{
4500 struct nfs4_fsinfo_arg args = {
4501 .fh = fhandle,
4502 .bitmask = server->attr_bitmask,
4503 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004504 struct nfs4_fsinfo_res res = {
4505 .fsinfo = fsinfo,
4506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4509 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004510 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 };
4512
Bryan Schumaker7c513052011-03-24 17:12:24 +00004513 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514}
4515
4516static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4517{
4518 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004519 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 int err;
4521
4522 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004523 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004524 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004525 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004526 nfs4_set_lease_period(server->nfs_client,
4527 fsinfo->lease_time * HZ,
4528 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004529 break;
4530 }
4531 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 } while (exception.retry);
4533 return err;
4534}
4535
4536static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4537{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004538 int error;
4539
Trond Myklebust0e574af2005-10-27 22:12:38 -04004540 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004541 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004542 if (error == 0) {
4543 /* block layout checks this! */
4544 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004545 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004546 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004547
4548 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
4551static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4552 struct nfs_pathconf *pathconf)
4553{
4554 struct nfs4_pathconf_arg args = {
4555 .fh = fhandle,
4556 .bitmask = server->attr_bitmask,
4557 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004558 struct nfs4_pathconf_res res = {
4559 .pathconf = pathconf,
4560 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 struct rpc_message msg = {
4562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4563 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004564 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 };
4566
4567 /* None of the pathconf attributes are mandatory to implement */
4568 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4569 memset(pathconf, 0, sizeof(*pathconf));
4570 return 0;
4571 }
4572
Trond Myklebust0e574af2005-10-27 22:12:38 -04004573 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004574 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
4577static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4578 struct nfs_pathconf *pathconf)
4579{
4580 struct nfs4_exception exception = { };
4581 int err;
4582
4583 do {
4584 err = nfs4_handle_exception(server,
4585 _nfs4_proc_pathconf(server, fhandle, pathconf),
4586 &exception);
4587 } while (exception.retry);
4588 return err;
4589}
4590
Trond Myklebust5521abf2013-03-16 20:54:34 -04004591int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004592 const struct nfs_open_context *ctx,
4593 const struct nfs_lock_context *l_ctx,
4594 fmode_t fmode)
4595{
NeilBrown17393472016-10-13 15:26:47 +11004596 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004597}
4598EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4599
Trond Myklebust5521abf2013-03-16 20:54:34 -04004600static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4601 const struct nfs_open_context *ctx,
4602 const struct nfs_lock_context *l_ctx,
4603 fmode_t fmode)
4604{
4605 nfs4_stateid current_stateid;
4606
Trond Myklebuste1253be2014-03-05 08:44:23 -05004607 /* If the current stateid represents a lost lock, then exit */
4608 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4609 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004610 return nfs4_stateid_match(stateid, &current_stateid);
4611}
4612
4613static bool nfs4_error_stateid_expired(int err)
4614{
4615 switch (err) {
4616 case -NFS4ERR_DELEG_REVOKED:
4617 case -NFS4ERR_ADMIN_REVOKED:
4618 case -NFS4ERR_BAD_STATEID:
4619 case -NFS4ERR_STALE_STATEID:
4620 case -NFS4ERR_OLD_STATEID:
4621 case -NFS4ERR_OPENMODE:
4622 case -NFS4ERR_EXPIRED:
4623 return true;
4624 }
4625 return false;
4626}
4627
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004630 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004632 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004633 if (task->tk_status < 0) {
4634 struct nfs4_exception exception = {
4635 .inode = hdr->inode,
4636 .state = hdr->args.context->state,
4637 .stateid = &hdr->args.stateid,
4638 };
4639 task->tk_status = nfs4_async_handle_exception(task,
4640 server, task->tk_status, &exception);
4641 if (exception.retry) {
4642 rpc_restart_call_prepare(task);
4643 return -EAGAIN;
4644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004648 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004649 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650}
4651
Trond Myklebust5521abf2013-03-16 20:54:34 -04004652static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004653 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004654{
4655
4656 if (!nfs4_error_stateid_expired(task->tk_status) ||
4657 nfs4_stateid_is_current(&args->stateid,
4658 args->context,
4659 args->lock_context,
4660 FMODE_READ))
4661 return false;
4662 rpc_restart_call_prepare(task);
4663 return true;
4664}
4665
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004667{
4668
4669 dprintk("--> %s\n", __func__);
4670
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004671 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004672 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004674 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004675 if (task->tk_status > 0)
4676 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004677 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4678 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004679}
4680
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004681static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4682 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004685 if (!hdr->pgio_done_cb)
4686 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004687 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004688 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689}
4690
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4692 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004693{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4695 &hdr->args.seq_args,
4696 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004697 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004698 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004699 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4700 hdr->args.lock_context,
4701 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004702 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004704 return -EIO;
4705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706}
4707
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708static int nfs4_write_done_cb(struct rpc_task *task,
4709 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004711 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004712
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004713 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004714 if (task->tk_status < 0) {
4715 struct nfs4_exception exception = {
4716 .inode = hdr->inode,
4717 .state = hdr->args.context->state,
4718 .stateid = &hdr->args.stateid,
4719 };
4720 task->tk_status = nfs4_async_handle_exception(task,
4721 NFS_SERVER(inode), task->tk_status,
4722 &exception);
4723 if (exception.retry) {
4724 rpc_restart_call_prepare(task);
4725 return -EAGAIN;
4726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004728 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004729 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004730 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004731 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004732 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733}
4734
Trond Myklebust5521abf2013-03-16 20:54:34 -04004735static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004736 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004737{
4738
4739 if (!nfs4_error_stateid_expired(task->tk_status) ||
4740 nfs4_stateid_is_current(&args->stateid,
4741 args->context,
4742 args->lock_context,
4743 FMODE_WRITE))
4744 return false;
4745 rpc_restart_call_prepare(task);
4746 return true;
4747}
4748
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004749static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004750{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004751 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004752 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004753 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004754 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4756 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004757}
4758
Trond Myklebust5a37f852012-04-28 14:55:16 -04004759static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004760bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004761{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004762 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004763 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004764 return false;
4765 /* Otherwise, request attributes if and only if we don't hold
4766 * a delegation
4767 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004768 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004769}
Fred Isamana69aef12011-03-03 15:13:47 +00004770
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004771static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4772 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004774 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004775
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4777 hdr->args.bitmask = NULL;
4778 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004779 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004781
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004782 if (!hdr->pgio_done_cb)
4783 hdr->pgio_done_cb = nfs4_write_done_cb;
4784 hdr->res.server = server;
4785 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004787 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004788 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Fred Isaman0b7c0152012-04-20 14:47:39 -04004791static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4792{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004793 nfs4_setup_sequence(NFS_SERVER(data->inode),
4794 &data->args.seq_args,
4795 &data->res.seq_res,
4796 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797}
4798
Fred Isaman0b7c0152012-04-20 14:47:39 -04004799static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004802
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004803 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004804 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4805 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004806 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004807 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Fred Isaman0b7c0152012-04-20 14:47:39 -04004812static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004813{
4814 if (!nfs4_sequence_done(task, &data->res.seq_res))
4815 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004816 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004817}
4818
Fred Isaman0b7c0152012-04-20 14:47:39 -04004819static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004821 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004822
Fred Isaman0b7c0152012-04-20 14:47:39 -04004823 if (data->commit_done_cb == NULL)
4824 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004825 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004826 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004827 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828}
4829
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004830struct nfs4_renewdata {
4831 struct nfs_client *client;
4832 unsigned long timestamp;
4833};
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835/*
4836 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4837 * standalone procedure for queueing an asynchronous RENEW.
4838 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004839static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004840{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004841 struct nfs4_renewdata *data = calldata;
4842 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004843
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004844 if (atomic_read(&clp->cl_count) > 1)
4845 nfs4_schedule_state_renewal(clp);
4846 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004847 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004848}
4849
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004850static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852 struct nfs4_renewdata *data = calldata;
4853 struct nfs_client *clp = data->client;
4854 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004856 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004857 switch (task->tk_status) {
4858 case 0:
4859 break;
4860 case -NFS4ERR_LEASE_MOVED:
4861 nfs4_schedule_lease_moved_recovery(clp);
4862 break;
4863 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004864 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004865 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4866 return;
4867 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004868 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004869 return;
4870 }
4871 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004873 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874}
4875
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004876static const struct rpc_call_ops nfs4_renew_ops = {
4877 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004878 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004879};
4880
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004881static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882{
4883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4885 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004886 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004888 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004890 if (renew_flags == 0)
4891 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004892 if (!atomic_inc_not_zero(&clp->cl_count))
4893 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004894 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004895 if (data == NULL)
4896 return -ENOMEM;
4897 data->client = clp;
4898 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004899 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004900 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901}
4902
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004903static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904{
4905 struct rpc_message msg = {
4906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4907 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004908 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 };
4910 unsigned long now = jiffies;
4911 int status;
4912
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004913 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 if (status < 0)
4915 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004916 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 return 0;
4918}
4919
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004920static inline int nfs4_server_supports_acls(struct nfs_server *server)
4921{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004922 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004923}
4924
Trond Myklebust21f498c2012-08-24 10:59:25 -04004925/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4926 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004927 * the stack.
4928 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004929#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004930
Neil Hormane9e3d722011-03-04 19:26:03 -05004931static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004932 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004933{
4934 struct page *newpage, **spages;
4935 int rc = 0;
4936 size_t len;
4937 spages = pages;
4938
4939 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004940 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004941 newpage = alloc_page(GFP_KERNEL);
4942
4943 if (newpage == NULL)
4944 goto unwind;
4945 memcpy(page_address(newpage), buf, len);
4946 buf += len;
4947 buflen -= len;
4948 *pages++ = newpage;
4949 rc++;
4950 } while (buflen != 0);
4951
4952 return rc;
4953
4954unwind:
4955 for(; rc > 0; rc--)
4956 __free_page(spages[rc-1]);
4957 return -ENOMEM;
4958}
4959
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004960struct nfs4_cached_acl {
4961 int cached;
4962 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004963 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004964};
4965
4966static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004967{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004968 struct nfs_inode *nfsi = NFS_I(inode);
4969
4970 spin_lock(&inode->i_lock);
4971 kfree(nfsi->nfs4_acl);
4972 nfsi->nfs4_acl = acl;
4973 spin_unlock(&inode->i_lock);
4974}
4975
4976static void nfs4_zap_acl_attr(struct inode *inode)
4977{
4978 nfs4_set_cached_acl(inode, NULL);
4979}
4980
4981static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4982{
4983 struct nfs_inode *nfsi = NFS_I(inode);
4984 struct nfs4_cached_acl *acl;
4985 int ret = -ENOENT;
4986
4987 spin_lock(&inode->i_lock);
4988 acl = nfsi->nfs4_acl;
4989 if (acl == NULL)
4990 goto out;
4991 if (buf == NULL) /* user is just asking for length */
4992 goto out_len;
4993 if (acl->cached == 0)
4994 goto out;
4995 ret = -ERANGE; /* see getxattr(2) man page */
4996 if (acl->len > buflen)
4997 goto out;
4998 memcpy(buf, acl->data, acl->len);
4999out_len:
5000 ret = acl->len;
5001out:
5002 spin_unlock(&inode->i_lock);
5003 return ret;
5004}
5005
Sachin Prabhu5794d212012-04-17 14:36:40 +01005006static 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 +00005007{
5008 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005009 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005010
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005011 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005012 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005013 if (acl == NULL)
5014 goto out;
5015 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005016 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005017 } else {
5018 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5019 if (acl == NULL)
5020 goto out;
5021 acl->cached = 0;
5022 }
5023 acl->len = acl_len;
5024out:
5025 nfs4_set_cached_acl(inode, acl);
5026}
5027
Andy Adamsonbf118a32011-12-07 11:55:27 -05005028/*
5029 * The getxattr API returns the required buffer length when called with a
5030 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5031 * the required buf. On a NULL buf, we send a page of data to the server
5032 * guessing that the ACL request can be serviced by a page. If so, we cache
5033 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5034 * the cache. If not so, we throw away the page, and cache the required
5035 * length. The next getxattr call will then produce another round trip to
5036 * the server, this time with the input buf of the required size.
5037 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005038static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005039{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005040 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005041 struct nfs_getaclargs args = {
5042 .fh = NFS_FH(inode),
5043 .acl_pages = pages,
5044 .acl_len = buflen,
5045 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005046 struct nfs_getaclres res = {
5047 .acl_len = buflen,
5048 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005049 struct rpc_message msg = {
5050 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5051 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005052 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005053 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005054 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5055 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005056
Andy Adamsonbf118a32011-12-07 11:55:27 -05005057 /* As long as we're doing a round trip to the server anyway,
5058 * let's be prepared for a page of acl data. */
5059 if (npages == 0)
5060 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005061 if (npages > ARRAY_SIZE(pages))
5062 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005063
Andy Adamsonbf118a32011-12-07 11:55:27 -05005064 for (i = 0; i < npages; i++) {
5065 pages[i] = alloc_page(GFP_KERNEL);
5066 if (!pages[i])
5067 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005068 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005069
5070 /* for decoding across pages */
5071 res.acl_scratch = alloc_page(GFP_KERNEL);
5072 if (!res.acl_scratch)
5073 goto out_free;
5074
Andy Adamsonbf118a32011-12-07 11:55:27 -05005075 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005076
Peng Taode040be2012-01-10 22:42:47 +08005077 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005078 __func__, buf, buflen, npages, args.acl_len);
5079 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5080 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 if (ret)
5082 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005083
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005084 /* Handle the case where the passed-in buffer is too short */
5085 if (res.acl_flags & NFS4_ACL_TRUNC) {
5086 /* Did the user only issue a request for the acl length? */
5087 if (buf == NULL)
5088 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005089 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005090 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005091 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005092 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005093 if (buf) {
5094 if (res.acl_len > buflen) {
5095 ret = -ERANGE;
5096 goto out_free;
5097 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005098 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005099 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005100out_ok:
5101 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005103 for (i = 0; i < npages; i++)
5104 if (pages[i])
5105 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005106 if (res.acl_scratch)
5107 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108 return ret;
5109}
5110
Trond Myklebust16b42892006-08-24 12:27:15 -04005111static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5112{
5113 struct nfs4_exception exception = { };
5114 ssize_t ret;
5115 do {
5116 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005117 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005118 if (ret >= 0)
5119 break;
5120 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5121 } while (exception.retry);
5122 return ret;
5123}
5124
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005125static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5126{
5127 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005128 int ret;
5129
5130 if (!nfs4_server_supports_acls(server))
5131 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005132 ret = nfs_revalidate_inode(server, inode);
5133 if (ret < 0)
5134 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005135 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5136 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005137 ret = nfs4_read_cached_acl(inode, buf, buflen);
5138 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005139 /* -ENOENT is returned if there is no ACL or if there is an ACL
5140 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005141 return ret;
5142 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005143}
5144
Trond Myklebust16b42892006-08-24 12:27:15 -04005145static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005146{
5147 struct nfs_server *server = NFS_SERVER(inode);
5148 struct page *pages[NFS4ACL_MAXPAGES];
5149 struct nfs_setaclargs arg = {
5150 .fh = NFS_FH(inode),
5151 .acl_pages = pages,
5152 .acl_len = buflen,
5153 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005154 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005155 struct rpc_message msg = {
5156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5157 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005158 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005159 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005160 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005161 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005162
5163 if (!nfs4_server_supports_acls(server))
5164 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005165 if (npages > ARRAY_SIZE(pages))
5166 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005167 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005168 if (i < 0)
5169 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005170 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005171 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005172
5173 /*
5174 * Free each page after tx, so the only ref left is
5175 * held by the network stack
5176 */
5177 for (; i > 0; i--)
5178 put_page(pages[i-1]);
5179
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005180 /*
5181 * Acl update can result in inode attribute update.
5182 * so mark the attribute cache invalid.
5183 */
5184 spin_lock(&inode->i_lock);
5185 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5186 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005187 nfs_access_zap_cache(inode);
5188 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005189 return ret;
5190}
5191
Trond Myklebust16b42892006-08-24 12:27:15 -04005192static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5193{
5194 struct nfs4_exception exception = { };
5195 int err;
5196 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005197 err = __nfs4_proc_set_acl(inode, buf, buflen);
5198 trace_nfs4_set_acl(inode, err);
5199 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005200 &exception);
5201 } while (exception.retry);
5202 return err;
5203}
5204
David Quigleyaa9c2662013-05-22 12:50:44 -04005205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5206static int _nfs4_get_security_label(struct inode *inode, void *buf,
5207 size_t buflen)
5208{
5209 struct nfs_server *server = NFS_SERVER(inode);
5210 struct nfs_fattr fattr;
5211 struct nfs4_label label = {0, 0, buflen, buf};
5212
5213 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005214 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005215 .fh = NFS_FH(inode),
5216 .bitmask = bitmask,
5217 };
5218 struct nfs4_getattr_res res = {
5219 .fattr = &fattr,
5220 .label = &label,
5221 .server = server,
5222 };
5223 struct rpc_message msg = {
5224 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005225 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005226 .rpc_resp = &res,
5227 };
5228 int ret;
5229
5230 nfs_fattr_init(&fattr);
5231
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005232 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005233 if (ret)
5234 return ret;
5235 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5236 return -ENOENT;
5237 if (buflen < label.len)
5238 return -ERANGE;
5239 return 0;
5240}
5241
5242static int nfs4_get_security_label(struct inode *inode, void *buf,
5243 size_t buflen)
5244{
5245 struct nfs4_exception exception = { };
5246 int err;
5247
5248 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5249 return -EOPNOTSUPP;
5250
5251 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005252 err = _nfs4_get_security_label(inode, buf, buflen);
5253 trace_nfs4_get_security_label(inode, err);
5254 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005255 &exception);
5256 } while (exception.retry);
5257 return err;
5258}
5259
5260static int _nfs4_do_set_security_label(struct inode *inode,
5261 struct nfs4_label *ilabel,
5262 struct nfs_fattr *fattr,
5263 struct nfs4_label *olabel)
5264{
5265
5266 struct iattr sattr = {0};
5267 struct nfs_server *server = NFS_SERVER(inode);
5268 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005269 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005270 .fh = NFS_FH(inode),
5271 .iap = &sattr,
5272 .server = server,
5273 .bitmask = bitmask,
5274 .label = ilabel,
5275 };
5276 struct nfs_setattrres res = {
5277 .fattr = fattr,
5278 .label = olabel,
5279 .server = server,
5280 };
5281 struct rpc_message msg = {
5282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005283 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005284 .rpc_resp = &res,
5285 };
5286 int status;
5287
Jeff Layton12207f62013-11-01 10:49:32 -04005288 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005289
Jeff Layton12207f62013-11-01 10:49:32 -04005290 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005291 if (status)
5292 dprintk("%s failed: %d\n", __func__, status);
5293
5294 return status;
5295}
5296
5297static int nfs4_do_set_security_label(struct inode *inode,
5298 struct nfs4_label *ilabel,
5299 struct nfs_fattr *fattr,
5300 struct nfs4_label *olabel)
5301{
5302 struct nfs4_exception exception = { };
5303 int err;
5304
5305 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005306 err = _nfs4_do_set_security_label(inode, ilabel,
5307 fattr, olabel);
5308 trace_nfs4_set_security_label(inode, err);
5309 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005310 &exception);
5311 } while (exception.retry);
5312 return err;
5313}
5314
5315static int
Al Viro59301222016-05-27 10:19:30 -04005316nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005317{
5318 struct nfs4_label ilabel, *olabel = NULL;
5319 struct nfs_fattr fattr;
5320 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005321 int status;
5322
5323 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5324 return -EOPNOTSUPP;
5325
5326 nfs_fattr_init(&fattr);
5327
5328 ilabel.pi = 0;
5329 ilabel.lfs = 0;
5330 ilabel.label = (char *)buf;
5331 ilabel.len = buflen;
5332
5333 cred = rpc_lookup_cred();
5334 if (IS_ERR(cred))
5335 return PTR_ERR(cred);
5336
5337 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5338 if (IS_ERR(olabel)) {
5339 status = -PTR_ERR(olabel);
5340 goto out;
5341 }
5342
5343 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5344 if (status == 0)
5345 nfs_setsecurity(inode, &fattr, olabel);
5346
5347 nfs4_label_free(olabel);
5348out:
5349 put_rpccred(cred);
5350 return status;
5351}
5352#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5353
5354
Chuck Leverf0920752012-05-21 22:45:41 -04005355static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5356 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005357{
5358 __be32 verf[2];
5359
Chuck Lever2c820d92012-05-21 22:45:33 -04005360 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5361 /* An impossible timestamp guarantees this value
5362 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005363 verf[0] = cpu_to_be32(U32_MAX);
5364 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005365 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005366 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005367 u64 ns = ktime_to_ns(nn->boot_time);
5368
5369 verf[0] = cpu_to_be32(ns >> 32);
5370 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005371 }
Chuck Levercd937102012-03-02 17:14:31 -05005372 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5373}
5374
Jeff Laytona3192682015-06-09 19:43:59 -04005375static int
5376nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005377{
Jeff Laytona3192682015-06-09 19:43:59 -04005378 size_t len;
5379 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005380
Trond Myklebustceb3a162015-01-03 15:16:04 -05005381 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005382 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005383
Jeff Laytona3192682015-06-09 19:43:59 -04005384 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005385 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005386 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5387 1 +
5388 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5389 1;
5390 rcu_read_unlock();
5391
5392 if (len > NFS4_OPAQUE_LIMIT + 1)
5393 return -EINVAL;
5394
5395 /*
5396 * Since this string is allocated at mount time, and held until the
5397 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5398 * about a memory-reclaim deadlock.
5399 */
5400 str = kmalloc(len, GFP_KERNEL);
5401 if (!str)
5402 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005403
Chuck Levere984a552012-09-14 17:24:21 -04005404 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005405 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005406 clp->cl_ipaddr,
5407 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005409 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005410
Jeff Laytona3192682015-06-09 19:43:59 -04005411 clp->cl_owner_id = str;
5412 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005413}
5414
Jeff Layton873e3852015-06-09 19:44:00 -04005415static int
5416nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005417{
Jeff Layton873e3852015-06-09 19:44:00 -04005418 size_t len;
5419 char *str;
5420
5421 len = 10 + 10 + 1 + 10 + 1 +
5422 strlen(nfs4_client_id_uniquifier) + 1 +
5423 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5424
5425 if (len > NFS4_OPAQUE_LIMIT + 1)
5426 return -EINVAL;
5427
5428 /*
5429 * Since this string is allocated at mount time, and held until the
5430 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5431 * about a memory-reclaim deadlock.
5432 */
5433 str = kmalloc(len, GFP_KERNEL);
5434 if (!str)
5435 return -ENOMEM;
5436
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005437 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005438 clp->rpc_ops->version, clp->cl_minorversion,
5439 nfs4_client_id_uniquifier,
5440 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005441 clp->cl_owner_id = str;
5442 return 0;
5443}
5444
5445static int
5446nfs4_init_uniform_client_string(struct nfs_client *clp)
5447{
Jeff Layton873e3852015-06-09 19:44:00 -04005448 size_t len;
5449 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005450
5451 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005452 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005453
5454 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005455 return nfs4_init_uniquifier_client_string(clp);
5456
5457 len = 10 + 10 + 1 + 10 + 1 +
5458 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5459
5460 if (len > NFS4_OPAQUE_LIMIT + 1)
5461 return -EINVAL;
5462
5463 /*
5464 * Since this string is allocated at mount time, and held until the
5465 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5466 * about a memory-reclaim deadlock.
5467 */
5468 str = kmalloc(len, GFP_KERNEL);
5469 if (!str)
5470 return -ENOMEM;
5471
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005472 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005473 clp->rpc_ops->version, clp->cl_minorversion,
5474 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005475 clp->cl_owner_id = str;
5476 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005477}
5478
Chuck Lever706cb8d2014-03-12 12:51:47 -04005479/*
5480 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5481 * services. Advertise one based on the address family of the
5482 * clientaddr.
5483 */
5484static unsigned int
5485nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5486{
5487 if (strchr(clp->cl_ipaddr, ':') != NULL)
5488 return scnprintf(buf, len, "tcp6");
5489 else
5490 return scnprintf(buf, len, "tcp");
5491}
5492
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005493static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5494{
5495 struct nfs4_setclientid *sc = calldata;
5496
5497 if (task->tk_status == 0)
5498 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5499}
5500
5501static const struct rpc_call_ops nfs4_setclientid_ops = {
5502 .rpc_call_done = nfs4_setclientid_done,
5503};
5504
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005505/**
5506 * nfs4_proc_setclientid - Negotiate client ID
5507 * @clp: state data structure
5508 * @program: RPC program for NFSv4 callback service
5509 * @port: IP port number for NFS4 callback service
5510 * @cred: RPC credential to use for this call
5511 * @res: where to place the result
5512 *
5513 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5514 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005515int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5516 unsigned short port, struct rpc_cred *cred,
5517 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518{
5519 nfs4_verifier sc_verifier;
5520 struct nfs4_setclientid setclientid = {
5521 .sc_verifier = &sc_verifier,
5522 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005523 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 };
5525 struct rpc_message msg = {
5526 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5527 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005528 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005529 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005531 struct rpc_task *task;
5532 struct rpc_task_setup task_setup_data = {
5533 .rpc_client = clp->cl_rpcclient,
5534 .rpc_message = &msg,
5535 .callback_ops = &nfs4_setclientid_ops,
5536 .callback_data = &setclientid,
5537 .flags = RPC_TASK_TIMEOUT,
5538 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005539 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Chuck Leverde734832012-07-11 16:30:50 -04005541 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005542 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005543
5544 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5545 status = nfs4_init_uniform_client_string(clp);
5546 else
Jeff Laytona3192682015-06-09 19:43:59 -04005547 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005548
5549 if (status)
5550 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005551
Chuck Leverde734832012-07-11 16:30:50 -04005552 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005553 setclientid.sc_netid_len =
5554 nfs4_init_callback_netid(clp,
5555 setclientid.sc_netid,
5556 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005557 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005558 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 clp->cl_ipaddr, port >> 8, port & 255);
5560
Jeff Layton3a6bb732015-06-09 19:43:57 -04005561 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005562 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005563 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005564 task = rpc_run_task(&task_setup_data);
5565 if (IS_ERR(task)) {
5566 status = PTR_ERR(task);
5567 goto out;
5568 }
5569 status = task->tk_status;
5570 if (setclientid.sc_cred) {
5571 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5572 put_rpccred(setclientid.sc_cred);
5573 }
5574 rpc_put_task(task);
5575out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005576 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005577 dprintk("NFS reply setclientid: %d\n", status);
5578 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579}
5580
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005581/**
5582 * nfs4_proc_setclientid_confirm - Confirm client ID
5583 * @clp: state data structure
5584 * @res: result of a previous SETCLIENTID
5585 * @cred: RPC credential to use for this call
5586 *
5587 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5588 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005589int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005590 struct nfs4_setclientid_res *arg,
5591 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005595 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005596 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 int status;
5599
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005600 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5601 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5602 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005603 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005604 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005605 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 return status;
5607}
5608
Trond Myklebustfe650402006-01-03 09:55:18 +01005609struct nfs4_delegreturndata {
5610 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005611 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005612 struct nfs_fh fh;
5613 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005614 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005615 struct {
5616 struct nfs4_layoutreturn_args arg;
5617 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005618 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005619 u32 roc_barrier;
5620 bool roc;
5621 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005622 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005623 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005624 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005625};
5626
Trond Myklebustfe650402006-01-03 09:55:18 +01005627static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5628{
5629 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005630
Trond Myklebust14516c32010-07-31 14:29:06 -04005631 if (!nfs4_sequence_done(task, &data->res.seq_res))
5632 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005633
Trond Myklebustca8acf82013-08-13 10:36:56 -04005634 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005635
5636 /* Handle Layoutreturn errors */
5637 if (data->args.lr_args && task->tk_status != 0) {
5638 switch(data->res.lr_ret) {
5639 default:
5640 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5641 break;
5642 case 0:
5643 data->args.lr_args = NULL;
5644 data->res.lr_res = NULL;
5645 break;
5646 case -NFS4ERR_ADMIN_REVOKED:
5647 case -NFS4ERR_DELEG_REVOKED:
5648 case -NFS4ERR_EXPIRED:
5649 case -NFS4ERR_BAD_STATEID:
5650 case -NFS4ERR_OLD_STATEID:
5651 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5652 case -NFS4ERR_WRONG_CRED:
5653 data->args.lr_args = NULL;
5654 data->res.lr_res = NULL;
5655 data->res.lr_ret = 0;
5656 rpc_restart_call_prepare(task);
5657 return;
5658 }
5659 }
5660
Ricardo Labiaga79708862009-12-07 09:23:21 -05005661 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005662 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005663 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005664 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005665 case -NFS4ERR_ADMIN_REVOKED:
5666 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005667 case -NFS4ERR_EXPIRED:
5668 nfs4_free_revoked_stateid(data->res.server,
5669 data->args.stateid,
5670 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005671 case -NFS4ERR_BAD_STATEID:
5672 case -NFS4ERR_OLD_STATEID:
5673 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005674 task->tk_status = 0;
5675 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005676 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005677 if (nfs4_async_handle_error(task, data->res.server,
5678 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005679 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005680 return;
5681 }
5682 }
5683 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005684}
5685
5686static void nfs4_delegreturn_release(void *calldata)
5687{
Peng Tao039b7562014-07-03 13:05:02 +08005688 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005689 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005690
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005691 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005692 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005693 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5694 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005695 nfs_iput_and_deactive(inode);
5696 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005697 kfree(calldata);
5698}
5699
Andy Adamson938e1012009-04-01 09:22:28 -04005700static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5701{
5702 struct nfs4_delegreturndata *d_data;
5703
5704 d_data = (struct nfs4_delegreturndata *)data;
5705
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005706 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005707 return;
5708
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005709 nfs4_setup_sequence(d_data->res.server,
5710 &d_data->args.seq_args,
5711 &d_data->res.seq_res,
5712 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005713}
Andy Adamson938e1012009-04-01 09:22:28 -04005714
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005715static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005716 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005717 .rpc_call_done = nfs4_delegreturn_done,
5718 .rpc_release = nfs4_delegreturn_release,
5719};
5720
Trond Myklebuste6f81072008-01-24 18:14:34 -05005721static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005722{
5723 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005724 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005725 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005726 struct rpc_message msg = {
5727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5728 .rpc_cred = cred,
5729 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005730 struct rpc_task_setup task_setup_data = {
5731 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005732 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005733 .callback_ops = &nfs4_delegreturn_ops,
5734 .flags = RPC_TASK_ASYNC,
5735 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005736 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005737
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005738 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005739 if (data == NULL)
5740 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005741 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005742
5743 nfs4_state_protect(server->nfs_client,
5744 NFS_SP4_MACH_CRED_CLEANUP,
5745 &task_setup_data.rpc_client, &msg);
5746
Trond Myklebustfe650402006-01-03 09:55:18 +01005747 data->args.fhandle = &data->fh;
5748 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005749 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005750 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005751 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005752 data->res.fattr = &data->fattr;
5753 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005754 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005755 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005756 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005757 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005758 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005759 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005760 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005761 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005762 if (data->lr.roc) {
5763 data->args.lr_args = &data->lr.arg;
5764 data->res.lr_res = &data->lr.res;
5765 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005766 } else if (data->lr.roc) {
5767 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5768 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005769 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005770
Trond Myklebustc970aa82007-07-14 15:39:59 -04005771 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005772 msg.rpc_argp = &data->args;
5773 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005774 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005775 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005776 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005777 if (!issync)
5778 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005779 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005780 if (status != 0)
5781 goto out;
5782 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005783 if (status == 0)
5784 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5785 else
5786 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005787out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005788 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
Trond Myklebuste6f81072008-01-24 18:14:34 -05005792int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
5794 struct nfs_server *server = NFS_SERVER(inode);
5795 struct nfs4_exception exception = { };
5796 int err;
5797 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005798 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005799 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 switch (err) {
5801 case -NFS4ERR_STALE_STATEID:
5802 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 case 0:
5804 return 0;
5805 }
5806 err = nfs4_handle_exception(server, err, &exception);
5807 } while (exception.retry);
5808 return err;
5809}
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5812{
5813 struct inode *inode = state->inode;
5814 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005815 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005816 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005818 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005820 struct nfs_lockt_res res = {
5821 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 };
5823 struct rpc_message msg = {
5824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5825 .rpc_argp = &arg,
5826 .rpc_resp = &res,
5827 .rpc_cred = state->owner->so_cred,
5828 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 struct nfs4_lock_state *lsp;
5830 int status;
5831
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005832 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005833 status = nfs4_set_lock_state(state, request);
5834 if (status != 0)
5835 goto out;
5836 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005837 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005838 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005839 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005840 switch (status) {
5841 case 0:
5842 request->fl_type = F_UNLCK;
5843 break;
5844 case -NFS4ERR_DENIED:
5845 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005847 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005848 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005849out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 return status;
5851}
5852
5853static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5854{
5855 struct nfs4_exception exception = { };
5856 int err;
5857
5858 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005859 err = _nfs4_proc_getlk(state, cmd, request);
5860 trace_nfs4_get_lock(request, state, cmd, err);
5861 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 &exception);
5863 } while (exception.retry);
5864 return err;
5865}
5866
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005868 struct nfs_locku_args arg;
5869 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005870 struct nfs4_lock_state *lsp;
5871 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005872 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005873 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005874 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005875};
5876
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005877static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5878 struct nfs_open_context *ctx,
5879 struct nfs4_lock_state *lsp,
5880 struct nfs_seqid *seqid)
5881{
5882 struct nfs4_unlockdata *p;
5883 struct inode *inode = lsp->ls_state->inode;
5884
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005885 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005886 if (p == NULL)
5887 return NULL;
5888 p->arg.fh = NFS_FH(inode);
5889 p->arg.fl = &p->fl;
5890 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005891 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005892 p->lsp = lsp;
5893 atomic_inc(&lsp->ls_count);
5894 /* Ensure we don't close file until we're done freeing locks! */
5895 p->ctx = get_nfs_open_context(ctx);
5896 memcpy(&p->fl, fl, sizeof(p->fl));
5897 p->server = NFS_SERVER(inode);
5898 return p;
5899}
5900
Trond Myklebust06f814a2006-01-03 09:55:07 +01005901static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005902{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005903 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005904 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005905 nfs4_put_lock_state(calldata->lsp);
5906 put_nfs_open_context(calldata->ctx);
5907 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005908}
5909
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005910static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005911{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005912 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005913
Trond Myklebust14516c32010-07-31 14:29:06 -04005914 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5915 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005916 switch (task->tk_status) {
5917 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005918 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005919 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005920 if (nfs4_update_lock_stateid(calldata->lsp,
5921 &calldata->res.stateid))
5922 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005923 case -NFS4ERR_ADMIN_REVOKED:
5924 case -NFS4ERR_EXPIRED:
5925 nfs4_free_revoked_stateid(calldata->server,
5926 &calldata->arg.stateid,
5927 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005928 case -NFS4ERR_BAD_STATEID:
5929 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005930 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005931 if (!nfs4_stateid_match(&calldata->arg.stateid,
5932 &calldata->lsp->ls_stateid))
5933 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005934 break;
5935 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005936 if (nfs4_async_handle_error(task, calldata->server,
5937 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005938 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005939 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005940 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005941}
5942
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005943static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005944{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005945 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005947 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005948 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005949 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005950 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005951 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005952 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005953 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005954 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005955 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005956 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005957 &calldata->res.seq_res,
5958 task) != 0)
5959 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005960 return;
5961out_no_action:
5962 task->tk_action = NULL;
5963out_wait:
5964 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
5966
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005967static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005968 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005969 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005970 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005971};
5972
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005973static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5974 struct nfs_open_context *ctx,
5975 struct nfs4_lock_state *lsp,
5976 struct nfs_seqid *seqid)
5977{
5978 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005979 struct rpc_message msg = {
5980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5981 .rpc_cred = ctx->cred,
5982 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005983 struct rpc_task_setup task_setup_data = {
5984 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005985 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005986 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005987 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005988 .flags = RPC_TASK_ASYNC,
5989 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005990
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005991 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5992 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5993
Frank Filz137d6ac2007-07-09 15:32:29 -07005994 /* Ensure this is an unlock - when canceling a lock, the
5995 * canceled lock is passed in, and it won't be an unlock.
5996 */
5997 fl->fl_type = F_UNLCK;
5998
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005999 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6000 if (data == NULL) {
6001 nfs_free_seqid(seqid);
6002 return ERR_PTR(-ENOMEM);
6003 }
6004
Chuck Levera9c92d62013-08-09 12:48:18 -04006005 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006006 msg.rpc_argp = &data->arg;
6007 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006008 task_setup_data.callback_data = data;
6009 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010}
6011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6013{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006014 struct inode *inode = state->inode;
6015 struct nfs4_state_owner *sp = state->owner;
6016 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006017 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006018 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006019 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006020 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006021 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006022 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
Trond Myklebust9b073572006-06-29 16:38:34 -04006024 status = nfs4_set_lock_state(state, request);
6025 /* Unlock _before_ we do the RPC call */
6026 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006027 /* Exclude nfs_delegation_claim_locks() */
6028 mutex_lock(&sp->so_delegreturn_mutex);
6029 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006030 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006031 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006032 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006033 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006034 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006035 }
6036 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006037 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006038 if (status != 0)
6039 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006040 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006041 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006042 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6043 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006044 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6045 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006046 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006047 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006048 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006049 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006050 status = PTR_ERR(task);
6051 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006052 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006054 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006055out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006056 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006057 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006058 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059}
6060
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006061struct nfs4_lockdata {
6062 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006063 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006064 struct nfs4_lock_state *lsp;
6065 struct nfs_open_context *ctx;
6066 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006067 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006068 int rpc_status;
6069 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006070 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071};
6072
6073static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006074 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6075 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006076{
6077 struct nfs4_lockdata *p;
6078 struct inode *inode = lsp->ls_state->inode;
6079 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006080 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006082 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006083 if (p == NULL)
6084 return NULL;
6085
6086 p->arg.fh = NFS_FH(inode);
6087 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006088 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006089 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006090 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006091 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6092 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006093 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006094 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006095 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006096 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006097 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006098 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006100 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101 atomic_inc(&lsp->ls_count);
6102 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006103 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006104 memcpy(&p->fl, fl, sizeof(p->fl));
6105 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006106out_free_seqid:
6107 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006108out_free:
6109 kfree(p);
6110 return NULL;
6111}
6112
6113static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6114{
6115 struct nfs4_lockdata *data = calldata;
6116 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
Harvey Harrison3110ff82008-05-02 13:42:44 -07006118 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006119 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006120 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006121 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006122 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006123 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006124 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006125 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006126 nfs4_stateid_copy(&data->arg.open_stateid,
6127 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006128 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006129 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006130 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006131 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006132 nfs4_stateid_copy(&data->arg.lock_stateid,
6133 &data->lsp->ls_stateid);
6134 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006135 if (!nfs4_valid_open_stateid(state)) {
6136 data->rpc_status = -EBADF;
6137 task->tk_action = NULL;
6138 goto out_release_open_seqid;
6139 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006140 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04006141 if (nfs4_setup_sequence(data->server,
6142 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006143 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006144 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006145 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006146out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006147 nfs_release_seqid(data->arg.open_seqid);
6148out_release_lock_seqid:
6149 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006150out_wait:
6151 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006152 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006153}
6154
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006155static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6156{
6157 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006158 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006159
Harvey Harrison3110ff82008-05-02 13:42:44 -07006160 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006161
Trond Myklebust14516c32010-07-31 14:29:06 -04006162 if (!nfs4_sequence_done(task, &data->res.seq_res))
6163 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006164
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006165 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006166 switch (task->tk_status) {
6167 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006168 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006169 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006170 if (data->arg.new_lock) {
6171 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006172 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006173 rpc_restart_call_prepare(task);
6174 break;
6175 }
6176 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006177 if (data->arg.new_lock_owner != 0) {
6178 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6179 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6180 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6181 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6182 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006183 break;
6184 case -NFS4ERR_BAD_STATEID:
6185 case -NFS4ERR_OLD_STATEID:
6186 case -NFS4ERR_STALE_STATEID:
6187 case -NFS4ERR_EXPIRED:
6188 if (data->arg.new_lock_owner != 0) {
6189 if (!nfs4_stateid_match(&data->arg.open_stateid,
6190 &lsp->ls_state->open_stateid))
6191 rpc_restart_call_prepare(task);
6192 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6193 &lsp->ls_stateid))
6194 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006195 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006196 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006197}
6198
6199static void nfs4_lock_release(void *calldata)
6200{
6201 struct nfs4_lockdata *data = calldata;
6202
Harvey Harrison3110ff82008-05-02 13:42:44 -07006203 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006204 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006205 if (data->cancelled != 0) {
6206 struct rpc_task *task;
6207 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6208 data->arg.lock_seqid);
6209 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006210 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006211 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006212 } else
6213 nfs_free_seqid(data->arg.lock_seqid);
6214 nfs4_put_lock_state(data->lsp);
6215 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006216 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006217 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006218 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006219}
6220
6221static const struct rpc_call_ops nfs4_lock_ops = {
6222 .rpc_call_prepare = nfs4_lock_prepare,
6223 .rpc_call_done = nfs4_lock_done,
6224 .rpc_release = nfs4_lock_release,
6225};
6226
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006227static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6228{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006229 switch (error) {
6230 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006231 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006232 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006233 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006234 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006235 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006236 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006237 break;
6238 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006239 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006240 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006241 };
6242}
6243
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006244static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006245{
6246 struct nfs4_lockdata *data;
6247 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006248 struct rpc_message msg = {
6249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6250 .rpc_cred = state->owner->so_cred,
6251 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006252 struct rpc_task_setup task_setup_data = {
6253 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006254 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006255 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006256 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006257 .flags = RPC_TASK_ASYNC,
6258 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006259 int ret;
6260
Harvey Harrison3110ff82008-05-02 13:42:44 -07006261 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006262 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006263 fl->fl_u.nfs4_fl.owner,
6264 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006265 if (data == NULL)
6266 return -ENOMEM;
6267 if (IS_SETLKW(cmd))
6268 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006269 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006270 msg.rpc_argp = &data->arg;
6271 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006272 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006273 if (recovery_type > NFS_LOCK_NEW) {
6274 if (recovery_type == NFS_LOCK_RECLAIM)
6275 data->arg.reclaim = NFS_LOCK_RECLAIM;
6276 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006277 } else
6278 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006279 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006280 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006281 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 ret = nfs4_wait_for_completion_rpc_task(task);
6283 if (ret == 0) {
6284 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006285 if (ret)
6286 nfs4_handle_setlk_error(data->server, data->lsp,
6287 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006288 } else
6289 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006290 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006291 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006292 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294}
6295
6296static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6297{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006298 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006299 struct nfs4_exception exception = {
6300 .inode = state->inode,
6301 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006302 int err;
6303
6304 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006305 /* Cache the lock if possible... */
6306 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6307 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006308 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006309 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006310 break;
6311 nfs4_handle_exception(server, err, &exception);
6312 } while (exception.retry);
6313 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314}
6315
6316static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6317{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006318 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006319 struct nfs4_exception exception = {
6320 .inode = state->inode,
6321 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006322 int err;
6323
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006324 err = nfs4_set_lock_state(state, request);
6325 if (err != 0)
6326 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006327 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006328 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6329 return 0;
6330 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006331 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006332 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6333 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006334 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006335 switch (err) {
6336 default:
6337 goto out;
6338 case -NFS4ERR_GRACE:
6339 case -NFS4ERR_DELAY:
6340 nfs4_handle_exception(server, err, &exception);
6341 err = 0;
6342 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006343 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006344out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006345 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346}
6347
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006348#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006349static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6350{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006351 struct nfs4_lock_state *lsp;
6352 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006353
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006354 status = nfs4_set_lock_state(state, request);
6355 if (status != 0)
6356 return status;
6357 lsp = request->fl_u.nfs4_fl.owner;
6358 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6359 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6360 return 0;
6361 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006362 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006363}
6364#endif
6365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6367{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006368 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006369 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006370 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006371 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
Trond Myklebust01c3b862006-06-29 16:38:39 -04006373 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006374 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006375 if (status < 0)
6376 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006377 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006378 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006379 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006380 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006381 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006382 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006383 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006384 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006385 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006386 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006387 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006388 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006389 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006390 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006391out:
6392 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 return status;
6394}
6395
6396static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6397{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006398 struct nfs4_exception exception = {
6399 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006400 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006401 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 int err;
6403
6404 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006405 err = _nfs4_proc_setlk(state, cmd, request);
6406 if (err == -NFS4ERR_DENIED)
6407 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006409 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 } while (exception.retry);
6411 return err;
6412}
6413
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006414#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6415#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6416
6417static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006418nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6419 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006420{
6421 int status = -ERESTARTSYS;
6422 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6423
6424 while(!signalled()) {
6425 status = nfs4_proc_setlk(state, cmd, request);
6426 if ((status != -EAGAIN) || IS_SETLK(cmd))
6427 break;
6428 freezable_schedule_timeout_interruptible(timeout);
6429 timeout *= 2;
6430 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6431 status = -ERESTARTSYS;
6432 }
6433 return status;
6434}
6435
Jeff Laytona1d617d82016-09-17 18:17:39 -04006436#ifdef CONFIG_NFS_V4_1
6437struct nfs4_lock_waiter {
6438 struct task_struct *task;
6439 struct inode *inode;
6440 struct nfs_lowner *owner;
6441 bool notified;
6442};
6443
6444static int
6445nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6446{
6447 int ret;
6448 struct cb_notify_lock_args *cbnl = key;
6449 struct nfs4_lock_waiter *waiter = wait->private;
6450 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6451 *wowner = waiter->owner;
6452
6453 /* Only wake if the callback was for the same owner */
6454 if (lowner->clientid != wowner->clientid ||
6455 lowner->id != wowner->id ||
6456 lowner->s_dev != wowner->s_dev)
6457 return 0;
6458
6459 /* Make sure it's for the right inode */
6460 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6461 return 0;
6462
6463 waiter->notified = true;
6464
6465 /* override "private" so we can use default_wake_function */
6466 wait->private = waiter->task;
6467 ret = autoremove_wake_function(wait, mode, flags, key);
6468 wait->private = waiter;
6469 return ret;
6470}
6471
6472static int
6473nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6474{
6475 int status = -ERESTARTSYS;
6476 unsigned long flags;
6477 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6478 struct nfs_server *server = NFS_SERVER(state->inode);
6479 struct nfs_client *clp = server->nfs_client;
6480 wait_queue_head_t *q = &clp->cl_lock_waitq;
6481 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6482 .id = lsp->ls_seqid.owner_id,
6483 .s_dev = server->s_dev };
6484 struct nfs4_lock_waiter waiter = { .task = current,
6485 .inode = state->inode,
6486 .owner = &owner,
6487 .notified = false };
6488 wait_queue_t wait;
6489
6490 /* Don't bother with waitqueue if we don't expect a callback */
6491 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6492 return nfs4_retry_setlk_simple(state, cmd, request);
6493
6494 init_wait(&wait);
6495 wait.private = &waiter;
6496 wait.func = nfs4_wake_lock_waiter;
6497 add_wait_queue(q, &wait);
6498
6499 while(!signalled()) {
6500 status = nfs4_proc_setlk(state, cmd, request);
6501 if ((status != -EAGAIN) || IS_SETLK(cmd))
6502 break;
6503
6504 status = -ERESTARTSYS;
6505 spin_lock_irqsave(&q->lock, flags);
6506 if (waiter.notified) {
6507 spin_unlock_irqrestore(&q->lock, flags);
6508 continue;
6509 }
6510 set_current_state(TASK_INTERRUPTIBLE);
6511 spin_unlock_irqrestore(&q->lock, flags);
6512
6513 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6514 }
6515
6516 finish_wait(q, &wait);
6517 return status;
6518}
6519#else /* !CONFIG_NFS_V4_1 */
6520static inline int
6521nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6522{
6523 return nfs4_retry_setlk_simple(state, cmd, request);
6524}
6525#endif
6526
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527static int
6528nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6529{
6530 struct nfs_open_context *ctx;
6531 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 int status;
6533
6534 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006535 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 state = ctx->state;
6537
6538 if (request->fl_start < 0 || request->fl_end < 0)
6539 return -EINVAL;
6540
Trond Myklebustd9531262009-07-21 19:22:38 -04006541 if (IS_GETLK(cmd)) {
6542 if (state != NULL)
6543 return nfs4_proc_getlk(state, F_GETLK, request);
6544 return 0;
6545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
6547 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6548 return -EINVAL;
6549
Trond Myklebustd9531262009-07-21 19:22:38 -04006550 if (request->fl_type == F_UNLCK) {
6551 if (state != NULL)
6552 return nfs4_proc_unlck(state, cmd, request);
6553 return 0;
6554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555
Trond Myklebustd9531262009-07-21 19:22:38 -04006556 if (state == NULL)
6557 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006558
6559 if ((request->fl_flags & FL_POSIX) &&
6560 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6561 return -ENOLCK;
6562
Trond Myklebust55725512012-04-18 12:48:35 -04006563 /*
6564 * Don't rely on the VFS having checked the file open mode,
6565 * since it won't do this for flock() locks.
6566 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006567 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006568 case F_RDLCK:
6569 if (!(filp->f_mode & FMODE_READ))
6570 return -EBADF;
6571 break;
6572 case F_WRLCK:
6573 if (!(filp->f_mode & FMODE_WRITE))
6574 return -EBADF;
6575 }
6576
Jeff Layton1ea67db2016-09-17 18:17:37 -04006577 status = nfs4_set_lock_state(state, request);
6578 if (status != 0)
6579 return status;
6580
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006581 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582}
6583
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006584int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006585{
6586 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006587 int err;
6588
6589 err = nfs4_set_lock_state(state, fl);
6590 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006591 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006592 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006593 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006594}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006595
Trond Myklebustcf470c32012-03-07 13:49:12 -05006596struct nfs_release_lockowner_data {
6597 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006598 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006599 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006600 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006601 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006602};
6603
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006604static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6605{
6606 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006607 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006608 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6609 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006610 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006611 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006612}
6613
6614static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6615{
6616 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006617 struct nfs_server *server = data->server;
6618
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006619 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006620
6621 switch (task->tk_status) {
6622 case 0:
6623 renew_lease(server, data->timestamp);
6624 break;
6625 case -NFS4ERR_STALE_CLIENTID:
6626 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006627 nfs4_schedule_lease_recovery(server->nfs_client);
6628 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006629 case -NFS4ERR_LEASE_MOVED:
6630 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006631 if (nfs4_async_handle_error(task, server,
6632 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006633 rpc_restart_call_prepare(task);
6634 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006635}
6636
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006637static void nfs4_release_lockowner_release(void *calldata)
6638{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006639 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006640 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006641 kfree(calldata);
6642}
6643
Trond Myklebust17280172012-03-11 13:11:00 -04006644static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006645 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6646 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006647 .rpc_release = nfs4_release_lockowner_release,
6648};
6649
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006650static void
6651nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006652{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006653 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006654 struct rpc_message msg = {
6655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6656 };
6657
6658 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006659 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006660
Trond Myklebustcf470c32012-03-07 13:49:12 -05006661 data = kmalloc(sizeof(*data), GFP_NOFS);
6662 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006663 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006664 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006665 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006666 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6667 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6668 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006669
Trond Myklebustcf470c32012-03-07 13:49:12 -05006670 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006671 msg.rpc_resp = &data->res;
6672 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006673 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006674}
6675
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006676#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6677
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006678static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006679 struct dentry *unused, struct inode *inode,
6680 const char *key, const void *buf,
6681 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006682{
Al Viro59301222016-05-27 10:19:30 -04006683 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006684}
6685
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006686static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006687 struct dentry *unused, struct inode *inode,
6688 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006689{
Al Virob2968212016-04-10 20:48:24 -04006690 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006691}
6692
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006693static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006694{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006695 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006696}
6697
David Quigleyc9bccef2013-05-22 12:50:45 -04006698#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006699
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006700static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006701 struct dentry *unused, struct inode *inode,
6702 const char *key, const void *buf,
6703 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006704{
6705 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006706 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006707
6708 return -EOPNOTSUPP;
6709}
6710
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006711static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006712 struct dentry *unused, struct inode *inode,
6713 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006714{
6715 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006716 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006717 return -EOPNOTSUPP;
6718}
6719
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006720static ssize_t
6721nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006722{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006723 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006724
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006725 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6726 len = security_inode_listsecurity(inode, list, list_len);
6727 if (list_len && len > list_len)
6728 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006729 }
6730 return len;
6731}
6732
6733static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6734 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006735 .get = nfs4_xattr_get_nfs4_label,
6736 .set = nfs4_xattr_set_nfs4_label,
6737};
David Quigleyc9bccef2013-05-22 12:50:45 -04006738
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006739#else
6740
6741static ssize_t
6742nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6743{
6744 return 0;
6745}
6746
6747#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006748
Andy Adamson533eb462011-06-13 18:25:56 -04006749/*
6750 * nfs_fhget will use either the mounted_on_fileid or the fileid
6751 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006752static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6753{
Andy Adamson533eb462011-06-13 18:25:56 -04006754 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6755 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6756 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006757 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006758 return;
6759
6760 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006761 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006762 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6763 fattr->nlink = 2;
6764}
6765
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006766static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6767 const struct qstr *name,
6768 struct nfs4_fs_locations *fs_locations,
6769 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006770{
6771 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006772 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006773 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006774 };
6775 struct nfs4_fs_locations_arg args = {
6776 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006777 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006778 .page = page,
6779 .bitmask = bitmask,
6780 };
Benny Halevy22958462009-04-01 09:22:02 -04006781 struct nfs4_fs_locations_res res = {
6782 .fs_locations = fs_locations,
6783 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006784 struct rpc_message msg = {
6785 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6786 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006787 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006788 };
6789 int status;
6790
Harvey Harrison3110ff82008-05-02 13:42:44 -07006791 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006792
6793 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6794 * is not supported */
6795 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6796 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6797 else
6798 bitmask[0] |= FATTR4_WORD0_FILEID;
6799
Trond Myklebustc228fd32007-01-13 02:28:11 -05006800 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006801 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006802 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006803 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006804 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006805 return status;
6806}
6807
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006808int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6809 const struct qstr *name,
6810 struct nfs4_fs_locations *fs_locations,
6811 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006812{
6813 struct nfs4_exception exception = { };
6814 int err;
6815 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006816 err = _nfs4_proc_fs_locations(client, dir, name,
6817 fs_locations, page);
6818 trace_nfs4_get_fs_locations(dir, name, err);
6819 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006820 &exception);
6821 } while (exception.retry);
6822 return err;
6823}
6824
Chuck Leverb03d7352013-10-17 14:12:50 -04006825/*
6826 * This operation also signals the server that this client is
6827 * performing migration recovery. The server can stop returning
6828 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6829 * appended to this compound to identify the client ID which is
6830 * performing recovery.
6831 */
6832static int _nfs40_proc_get_locations(struct inode *inode,
6833 struct nfs4_fs_locations *locations,
6834 struct page *page, struct rpc_cred *cred)
6835{
6836 struct nfs_server *server = NFS_SERVER(inode);
6837 struct rpc_clnt *clnt = server->client;
6838 u32 bitmask[2] = {
6839 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6840 };
6841 struct nfs4_fs_locations_arg args = {
6842 .clientid = server->nfs_client->cl_clientid,
6843 .fh = NFS_FH(inode),
6844 .page = page,
6845 .bitmask = bitmask,
6846 .migration = 1, /* skip LOOKUP */
6847 .renew = 1, /* append RENEW */
6848 };
6849 struct nfs4_fs_locations_res res = {
6850 .fs_locations = locations,
6851 .migration = 1,
6852 .renew = 1,
6853 };
6854 struct rpc_message msg = {
6855 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6856 .rpc_argp = &args,
6857 .rpc_resp = &res,
6858 .rpc_cred = cred,
6859 };
6860 unsigned long now = jiffies;
6861 int status;
6862
6863 nfs_fattr_init(&locations->fattr);
6864 locations->server = server;
6865 locations->nlocations = 0;
6866
6867 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6868 nfs4_set_sequence_privileged(&args.seq_args);
6869 status = nfs4_call_sync_sequence(clnt, server, &msg,
6870 &args.seq_args, &res.seq_res);
6871 if (status)
6872 return status;
6873
6874 renew_lease(server, now);
6875 return 0;
6876}
6877
6878#ifdef CONFIG_NFS_V4_1
6879
6880/*
6881 * This operation also signals the server that this client is
6882 * performing migration recovery. The server can stop asserting
6883 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6884 * performing this operation is identified in the SEQUENCE
6885 * operation in this compound.
6886 *
6887 * When the client supports GETATTR(fs_locations_info), it can
6888 * be plumbed in here.
6889 */
6890static int _nfs41_proc_get_locations(struct inode *inode,
6891 struct nfs4_fs_locations *locations,
6892 struct page *page, struct rpc_cred *cred)
6893{
6894 struct nfs_server *server = NFS_SERVER(inode);
6895 struct rpc_clnt *clnt = server->client;
6896 u32 bitmask[2] = {
6897 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6898 };
6899 struct nfs4_fs_locations_arg args = {
6900 .fh = NFS_FH(inode),
6901 .page = page,
6902 .bitmask = bitmask,
6903 .migration = 1, /* skip LOOKUP */
6904 };
6905 struct nfs4_fs_locations_res res = {
6906 .fs_locations = locations,
6907 .migration = 1,
6908 };
6909 struct rpc_message msg = {
6910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6911 .rpc_argp = &args,
6912 .rpc_resp = &res,
6913 .rpc_cred = cred,
6914 };
6915 int status;
6916
6917 nfs_fattr_init(&locations->fattr);
6918 locations->server = server;
6919 locations->nlocations = 0;
6920
6921 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6922 nfs4_set_sequence_privileged(&args.seq_args);
6923 status = nfs4_call_sync_sequence(clnt, server, &msg,
6924 &args.seq_args, &res.seq_res);
6925 if (status == NFS4_OK &&
6926 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6927 status = -NFS4ERR_LEASE_MOVED;
6928 return status;
6929}
6930
6931#endif /* CONFIG_NFS_V4_1 */
6932
6933/**
6934 * nfs4_proc_get_locations - discover locations for a migrated FSID
6935 * @inode: inode on FSID that is migrating
6936 * @locations: result of query
6937 * @page: buffer
6938 * @cred: credential to use for this operation
6939 *
6940 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6941 * operation failed, or a negative errno if a local error occurred.
6942 *
6943 * On success, "locations" is filled in, but if the server has
6944 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6945 * asserted.
6946 *
6947 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6948 * from this client that require migration recovery.
6949 */
6950int nfs4_proc_get_locations(struct inode *inode,
6951 struct nfs4_fs_locations *locations,
6952 struct page *page, struct rpc_cred *cred)
6953{
6954 struct nfs_server *server = NFS_SERVER(inode);
6955 struct nfs_client *clp = server->nfs_client;
6956 const struct nfs4_mig_recovery_ops *ops =
6957 clp->cl_mvops->mig_recovery_ops;
6958 struct nfs4_exception exception = { };
6959 int status;
6960
6961 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6962 (unsigned long long)server->fsid.major,
6963 (unsigned long long)server->fsid.minor,
6964 clp->cl_hostname);
6965 nfs_display_fhandle(NFS_FH(inode), __func__);
6966
6967 do {
6968 status = ops->get_locations(inode, locations, page, cred);
6969 if (status != -NFS4ERR_DELAY)
6970 break;
6971 nfs4_handle_exception(server, status, &exception);
6972 } while (exception.retry);
6973 return status;
6974}
6975
Chuck Lever44c99932013-10-17 14:13:30 -04006976/*
6977 * This operation also signals the server that this client is
6978 * performing "lease moved" recovery. The server can stop
6979 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6980 * is appended to this compound to identify the client ID which is
6981 * performing recovery.
6982 */
6983static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6984{
6985 struct nfs_server *server = NFS_SERVER(inode);
6986 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6987 struct rpc_clnt *clnt = server->client;
6988 struct nfs4_fsid_present_arg args = {
6989 .fh = NFS_FH(inode),
6990 .clientid = clp->cl_clientid,
6991 .renew = 1, /* append RENEW */
6992 };
6993 struct nfs4_fsid_present_res res = {
6994 .renew = 1,
6995 };
6996 struct rpc_message msg = {
6997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6998 .rpc_argp = &args,
6999 .rpc_resp = &res,
7000 .rpc_cred = cred,
7001 };
7002 unsigned long now = jiffies;
7003 int status;
7004
7005 res.fh = nfs_alloc_fhandle();
7006 if (res.fh == NULL)
7007 return -ENOMEM;
7008
7009 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7010 nfs4_set_sequence_privileged(&args.seq_args);
7011 status = nfs4_call_sync_sequence(clnt, server, &msg,
7012 &args.seq_args, &res.seq_res);
7013 nfs_free_fhandle(res.fh);
7014 if (status)
7015 return status;
7016
7017 do_renew_lease(clp, now);
7018 return 0;
7019}
7020
7021#ifdef CONFIG_NFS_V4_1
7022
7023/*
7024 * This operation also signals the server that this client is
7025 * performing "lease moved" recovery. The server can stop asserting
7026 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7027 * this operation is identified in the SEQUENCE operation in this
7028 * compound.
7029 */
7030static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7031{
7032 struct nfs_server *server = NFS_SERVER(inode);
7033 struct rpc_clnt *clnt = server->client;
7034 struct nfs4_fsid_present_arg args = {
7035 .fh = NFS_FH(inode),
7036 };
7037 struct nfs4_fsid_present_res res = {
7038 };
7039 struct rpc_message msg = {
7040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7041 .rpc_argp = &args,
7042 .rpc_resp = &res,
7043 .rpc_cred = cred,
7044 };
7045 int status;
7046
7047 res.fh = nfs_alloc_fhandle();
7048 if (res.fh == NULL)
7049 return -ENOMEM;
7050
7051 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7052 nfs4_set_sequence_privileged(&args.seq_args);
7053 status = nfs4_call_sync_sequence(clnt, server, &msg,
7054 &args.seq_args, &res.seq_res);
7055 nfs_free_fhandle(res.fh);
7056 if (status == NFS4_OK &&
7057 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7058 status = -NFS4ERR_LEASE_MOVED;
7059 return status;
7060}
7061
7062#endif /* CONFIG_NFS_V4_1 */
7063
7064/**
7065 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7066 * @inode: inode on FSID to check
7067 * @cred: credential to use for this operation
7068 *
7069 * Server indicates whether the FSID is present, moved, or not
7070 * recognized. This operation is necessary to clear a LEASE_MOVED
7071 * condition for this client ID.
7072 *
7073 * Returns NFS4_OK if the FSID is present on this server,
7074 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7075 * NFS4ERR code if some error occurred on the server, or a
7076 * negative errno if a local failure occurred.
7077 */
7078int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7079{
7080 struct nfs_server *server = NFS_SERVER(inode);
7081 struct nfs_client *clp = server->nfs_client;
7082 const struct nfs4_mig_recovery_ops *ops =
7083 clp->cl_mvops->mig_recovery_ops;
7084 struct nfs4_exception exception = { };
7085 int status;
7086
7087 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7088 (unsigned long long)server->fsid.major,
7089 (unsigned long long)server->fsid.minor,
7090 clp->cl_hostname);
7091 nfs_display_fhandle(NFS_FH(inode), __func__);
7092
7093 do {
7094 status = ops->fsid_present(inode, cred);
7095 if (status != -NFS4ERR_DELAY)
7096 break;
7097 nfs4_handle_exception(server, status, &exception);
7098 } while (exception.retry);
7099 return status;
7100}
7101
Andy Adamson5ec16a82013-08-08 10:57:55 -04007102/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007103 * If 'use_integrity' is true and the state managment nfs_client
7104 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7105 * and the machine credential as per RFC3530bis and RFC5661 Security
7106 * Considerations sections. Otherwise, just use the user cred with the
7107 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007108 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007109static 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 +00007110{
7111 int status;
7112 struct nfs4_secinfo_arg args = {
7113 .dir_fh = NFS_FH(dir),
7114 .name = name,
7115 };
7116 struct nfs4_secinfo_res res = {
7117 .flavors = flavors,
7118 };
7119 struct rpc_message msg = {
7120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7121 .rpc_argp = &args,
7122 .rpc_resp = &res,
7123 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007124 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007125 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007126
7127 if (use_integrity) {
7128 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007129 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7130 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007131 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007132
7133 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007134
7135 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7136 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7137
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007138 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7139 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007140 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007141
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007142 if (cred)
7143 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007144
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007145 return status;
7146}
7147
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007148int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7149 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007150{
7151 struct nfs4_exception exception = { };
7152 int err;
7153 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007154 err = -NFS4ERR_WRONGSEC;
7155
7156 /* try to use integrity protection with machine cred */
7157 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7158 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7159
7160 /*
7161 * if unable to use integrity protection, or SECINFO with
7162 * integrity protection returns NFS4ERR_WRONGSEC (which is
7163 * disallowed by spec, but exists in deployed servers) use
7164 * the current filesystem's rpc_client and the user cred.
7165 */
7166 if (err == -NFS4ERR_WRONGSEC)
7167 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7168
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007169 trace_nfs4_secinfo(dir, name, err);
7170 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007171 &exception);
7172 } while (exception.retry);
7173 return err;
7174}
7175
Andy Adamson557134a2009-04-01 09:21:53 -04007176#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007177/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007178 * Check the exchange flags returned by the server for invalid flags, having
7179 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7180 * DS flags set.
7181 */
7182static int nfs4_check_cl_exchange_flags(u32 flags)
7183{
7184 if (flags & ~EXCHGID4_FLAG_MASK_R)
7185 goto out_inval;
7186 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7187 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7188 goto out_inval;
7189 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7190 goto out_inval;
7191 return NFS_OK;
7192out_inval:
7193 return -NFS4ERR_INVAL;
7194}
7195
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007196static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007197nfs41_same_server_scope(struct nfs41_server_scope *a,
7198 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007199{
7200 if (a->server_scope_sz == b->server_scope_sz &&
7201 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7202 return true;
7203
7204 return false;
7205}
7206
Andy Adamson02a95de2016-02-05 16:08:37 -05007207static void
7208nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7209{
7210}
7211
7212static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7213 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7214};
7215
Andy Adamson357f54d2010-12-14 10:11:57 -05007216/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007217 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007218 *
7219 * The 4.1 client currently uses the same TCP connection for the
7220 * fore and backchannel.
7221 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007222static
7223int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7224 struct rpc_xprt *xprt,
7225 struct nfs_client *clp,
7226 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007227{
7228 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 struct nfs41_bind_conn_to_session_args args = {
7230 .client = clp,
7231 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7232 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007233 struct nfs41_bind_conn_to_session_res res;
7234 struct rpc_message msg = {
7235 .rpc_proc =
7236 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007237 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007238 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007239 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007240 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007241 struct rpc_task_setup task_setup_data = {
7242 .rpc_client = clnt,
7243 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007244 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007245 .rpc_message = &msg,
7246 .flags = RPC_TASK_TIMEOUT,
7247 };
7248 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007249
7250 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007251
Trond Myklebust71a097c2015-02-18 09:27:18 -08007252 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7253 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7254 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007255
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007256 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7257 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7258 args.dir = NFS4_CDFC4_FORE;
7259
7260 task = rpc_run_task(&task_setup_data);
7261 if (!IS_ERR(task)) {
7262 status = task->tk_status;
7263 rpc_put_task(task);
7264 } else
7265 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007266 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007268 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007269 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7270 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7271 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007272 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007274 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007275 dprintk("NFS: %s: Unexpected direction from server\n",
7276 __func__);
7277 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007278 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007279 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007280 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007281 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7282 __func__);
7283 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007284 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007285 }
7286 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007287out:
7288 dprintk("<-- %s status= %d\n", __func__, status);
7289 return status;
7290}
7291
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007292struct rpc_bind_conn_calldata {
7293 struct nfs_client *clp;
7294 struct rpc_cred *cred;
7295};
7296
7297static int
7298nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7299 struct rpc_xprt *xprt,
7300 void *calldata)
7301{
7302 struct rpc_bind_conn_calldata *p = calldata;
7303
7304 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7305}
7306
7307int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7308{
7309 struct rpc_bind_conn_calldata data = {
7310 .clp = clp,
7311 .cred = cred,
7312 };
7313 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7314 nfs4_proc_bind_conn_to_session_callback, &data);
7315}
7316
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007317/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007318 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7319 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007320 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007321static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7322 .how = SP4_MACH_CRED,
7323 .enforce.u.words = {
7324 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7325 1 << (OP_EXCHANGE_ID - 32) |
7326 1 << (OP_CREATE_SESSION - 32) |
7327 1 << (OP_DESTROY_SESSION - 32) |
7328 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007329 },
7330 .allow.u.words = {
7331 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007332 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007333 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007334 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007335 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007336 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007337 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007338 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007339 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007340 1 << (OP_FREE_STATEID - 32) |
7341 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007342 }
7343};
7344
7345/*
7346 * Select the state protection mode for client `clp' given the server results
7347 * from exchange_id in `sp'.
7348 *
7349 * Returns 0 on success, negative errno otherwise.
7350 */
7351static int nfs4_sp4_select_mode(struct nfs_client *clp,
7352 struct nfs41_state_protection *sp)
7353{
7354 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7355 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7356 1 << (OP_EXCHANGE_ID - 32) |
7357 1 << (OP_CREATE_SESSION - 32) |
7358 1 << (OP_DESTROY_SESSION - 32) |
7359 1 << (OP_DESTROY_CLIENTID - 32)
7360 };
7361 unsigned int i;
7362
7363 if (sp->how == SP4_MACH_CRED) {
7364 /* Print state protect result */
7365 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7366 for (i = 0; i <= LAST_NFS4_OP; i++) {
7367 if (test_bit(i, sp->enforce.u.longs))
7368 dfprintk(MOUNT, " enforce op %d\n", i);
7369 if (test_bit(i, sp->allow.u.longs))
7370 dfprintk(MOUNT, " allow op %d\n", i);
7371 }
7372
7373 /* make sure nothing is on enforce list that isn't supported */
7374 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7375 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7376 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7377 return -EINVAL;
7378 }
7379 }
7380
7381 /*
7382 * Minimal mode - state operations are allowed to use machine
7383 * credential. Note this already happens by default, so the
7384 * client doesn't have to do anything more than the negotiation.
7385 *
7386 * NOTE: we don't care if EXCHANGE_ID is in the list -
7387 * we're already using the machine cred for exchange_id
7388 * and will never use a different cred.
7389 */
7390 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7391 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7392 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7393 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7394 dfprintk(MOUNT, "sp4_mach_cred:\n");
7395 dfprintk(MOUNT, " minimal mode enabled\n");
7396 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7397 } else {
7398 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7399 return -EINVAL;
7400 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007401
7402 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007403 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7404 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007405 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7406 dfprintk(MOUNT, " cleanup mode enabled\n");
7407 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7408 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007409
Andrew Elble99ade3c2015-12-02 09:39:51 -05007410 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7411 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7412 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7413 &clp->cl_sp4_flags);
7414 }
7415
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007416 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7417 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7418 dfprintk(MOUNT, " secinfo mode enabled\n");
7419 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7420 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007421
7422 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7423 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7424 dfprintk(MOUNT, " stateid mode enabled\n");
7425 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7426 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007427
7428 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7429 dfprintk(MOUNT, " write mode enabled\n");
7430 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7431 }
7432
7433 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7434 dfprintk(MOUNT, " commit mode enabled\n");
7435 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7436 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007437 }
7438
7439 return 0;
7440}
7441
Andy Adamson8d89bd72016-09-09 09:22:18 -04007442struct nfs41_exchange_id_data {
7443 struct nfs41_exchange_id_res res;
7444 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007445 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007446 int rpc_status;
7447};
7448
7449static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007450{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451 struct nfs41_exchange_id_data *cdata =
7452 (struct nfs41_exchange_id_data *)data;
7453 struct nfs_client *clp = cdata->args.client;
7454 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007455
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007456 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007457
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007458 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007459 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007460
7461 if (cdata->xprt && status == 0) {
7462 status = nfs4_detect_session_trunking(clp, &cdata->res,
7463 cdata->xprt);
7464 goto out;
7465 }
7466
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 if (status == 0)
7468 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007469
Chuck Lever177313f2012-05-21 22:44:58 -04007470 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 clp->cl_clientid = cdata->res.clientid;
7472 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007473 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007474 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007475 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007476 &clp->cl_session->session_state);
7477 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007478 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007479
Chuck Leveracdeb692012-05-21 22:46:16 -04007480 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007481 clp->cl_serverowner = cdata->res.server_owner;
7482 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007483
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007484 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007485 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007486 clp->cl_implid = cdata->res.impl_id;
7487 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007488
Chuck Lever177313f2012-05-21 22:44:58 -04007489 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007490 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007491 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007492 dprintk("%s: server_scope mismatch detected\n",
7493 __func__);
7494 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007495 kfree(clp->cl_serverscope);
7496 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007497 }
7498
Chuck Lever177313f2012-05-21 22:44:58 -04007499 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 clp->cl_serverscope = cdata->res.server_scope;
7501 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007502 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007503 /* Save the EXCHANGE_ID verifier session trunk tests */
7504 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7505 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007506 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007507out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007508 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007509 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510}
7511
7512static void nfs4_exchange_id_release(void *data)
7513{
7514 struct nfs41_exchange_id_data *cdata =
7515 (struct nfs41_exchange_id_data *)data;
7516
7517 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007518 if (cdata->xprt) {
7519 xprt_put(cdata->xprt);
7520 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7521 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007522 kfree(cdata->res.impl_id);
7523 kfree(cdata->res.server_scope);
7524 kfree(cdata->res.server_owner);
7525 kfree(cdata);
7526}
7527
7528static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7529 .rpc_call_done = nfs4_exchange_id_done,
7530 .rpc_release = nfs4_exchange_id_release,
7531};
7532
Benny Halevy99fe60d2009-04-01 09:22:29 -04007533/*
7534 * _nfs4_proc_exchange_id()
7535 *
7536 * Wrapper for EXCHANGE_ID operation.
7537 */
7538static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007539 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007540{
7541 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007542 struct rpc_message msg = {
7543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007544 .rpc_cred = cred,
7545 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007546 struct rpc_task_setup task_setup_data = {
7547 .rpc_client = clp->cl_rpcclient,
7548 .callback_ops = &nfs4_exchange_id_call_ops,
7549 .rpc_message = &msg,
7550 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7551 };
7552 struct nfs41_exchange_id_data *calldata;
7553 struct rpc_task *task;
7554 int status = -EIO;
7555
7556 if (!atomic_inc_not_zero(&clp->cl_count))
7557 goto out;
7558
7559 status = -ENOMEM;
7560 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7561 if (!calldata)
7562 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007563
Andy Adamsonad0849a2016-09-09 09:22:28 -04007564 if (!xprt)
7565 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007566
7567 status = nfs4_init_uniform_client_string(clp);
7568 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007569 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007570
7571 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7572 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7573 clp->cl_owner_id);
7574
Andy Adamson8d89bd72016-09-09 09:22:18 -04007575 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7576 GFP_NOFS);
7577 status = -ENOMEM;
7578 if (unlikely(calldata->res.server_owner == NULL))
7579 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007580
Andy Adamson8d89bd72016-09-09 09:22:18 -04007581 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007582 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007583 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007584 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007585
Andy Adamson8d89bd72016-09-09 09:22:18 -04007586 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7587 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007588 goto out_server_scope;
7589
7590 switch (sp4_how) {
7591 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007592 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007593 break;
7594
7595 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007596 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007597 break;
7598
7599 default:
7600 /* unsupported! */
7601 WARN_ON_ONCE(1);
7602 status = -EINVAL;
7603 goto out_impl_id;
7604 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007605 if (xprt) {
7606 calldata->xprt = xprt;
7607 task_setup_data.rpc_xprt = xprt;
7608 task_setup_data.flags =
7609 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7610 calldata->args.verifier = &clp->cl_confirm;
7611 } else {
7612 calldata->args.verifier = &verifier;
7613 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007614 calldata->args.client = clp;
7615#ifdef CONFIG_NFS_V4_1_MIGRATION
7616 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7617 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7618 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7619#else
7620 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7621 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7622#endif
7623 msg.rpc_argp = &calldata->args;
7624 msg.rpc_resp = &calldata->res;
7625 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007626
Andy Adamson8d89bd72016-09-09 09:22:18 -04007627 task = rpc_run_task(&task_setup_data);
7628 if (IS_ERR(task)) {
7629 status = PTR_ERR(task);
7630 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007631 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007632
Andy Adamsonad0849a2016-09-09 09:22:28 -04007633 if (!xprt) {
7634 status = rpc_wait_for_completion_task(task);
7635 if (!status)
7636 status = calldata->rpc_status;
7637 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007638 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007639
Andy Adamson8d89bd72016-09-09 09:22:18 -04007640 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007641out:
Chuck Lever177313f2012-05-21 22:44:58 -04007642 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007643 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007644 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007645 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007646 clp->cl_implid->date.seconds,
7647 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007648 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007649 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007650
7651out_impl_id:
7652 kfree(calldata->res.impl_id);
7653out_server_scope:
7654 kfree(calldata->res.server_scope);
7655out_server_owner:
7656 kfree(calldata->res.server_owner);
7657out_calldata:
7658 kfree(calldata);
7659 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007660}
7661
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007662/*
7663 * nfs4_proc_exchange_id()
7664 *
7665 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7666 *
7667 * Since the clientid has expired, all compounds using sessions
7668 * associated with the stale clientid will be returning
7669 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7670 * be in some phase of session reset.
7671 *
7672 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7673 */
7674int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7675{
7676 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7677 int status;
7678
7679 /* try SP4_MACH_CRED if krb5i/p */
7680 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7681 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007682 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007683 if (!status)
7684 return 0;
7685 }
7686
7687 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007688 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007689}
7690
Andy Adamson04fa2c62016-09-09 09:22:29 -04007691/**
7692 * nfs4_test_session_trunk
7693 *
7694 * This is an add_xprt_test() test function called from
7695 * rpc_clnt_setup_test_and_add_xprt.
7696 *
7697 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7698 * and is dereferrenced in nfs4_exchange_id_release
7699 *
7700 * Upon success, add the new transport to the rpc_clnt
7701 *
7702 * @clnt: struct rpc_clnt to get new transport
7703 * @xprt: the rpc_xprt to test
7704 * @data: call data for _nfs4_proc_exchange_id.
7705 */
7706int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7707 void *data)
7708{
7709 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7710 u32 sp4_how;
7711
7712 dprintk("--> %s try %s\n", __func__,
7713 xprt->address_strings[RPC_DISPLAY_ADDR]);
7714
7715 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7716
7717 /* Test connection for session trunking. Async exchange_id call */
7718 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7719}
7720EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7721
Trond Myklebust66245532012-05-25 17:18:09 -04007722static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7723 struct rpc_cred *cred)
7724{
7725 struct rpc_message msg = {
7726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7727 .rpc_argp = clp,
7728 .rpc_cred = cred,
7729 };
7730 int status;
7731
7732 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007733 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007734 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007735 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007736 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7737 return status;
7738}
7739
7740static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7741 struct rpc_cred *cred)
7742{
7743 unsigned int loop;
7744 int ret;
7745
7746 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7747 ret = _nfs4_proc_destroy_clientid(clp, cred);
7748 switch (ret) {
7749 case -NFS4ERR_DELAY:
7750 case -NFS4ERR_CLIENTID_BUSY:
7751 ssleep(1);
7752 break;
7753 default:
7754 return ret;
7755 }
7756 }
7757 return 0;
7758}
7759
7760int nfs4_destroy_clientid(struct nfs_client *clp)
7761{
7762 struct rpc_cred *cred;
7763 int ret = 0;
7764
7765 if (clp->cl_mvops->minor_version < 1)
7766 goto out;
7767 if (clp->cl_exchange_flags == 0)
7768 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007769 if (clp->cl_preserve_clid)
7770 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007771 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007772 ret = nfs4_proc_destroy_clientid(clp, cred);
7773 if (cred)
7774 put_rpccred(cred);
7775 switch (ret) {
7776 case 0:
7777 case -NFS4ERR_STALE_CLIENTID:
7778 clp->cl_exchange_flags = 0;
7779 }
7780out:
7781 return ret;
7782}
7783
Andy Adamson2050f0c2009-04-01 09:22:30 -04007784struct nfs4_get_lease_time_data {
7785 struct nfs4_get_lease_time_args *args;
7786 struct nfs4_get_lease_time_res *res;
7787 struct nfs_client *clp;
7788};
7789
7790static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7791 void *calldata)
7792{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007793 struct nfs4_get_lease_time_data *data =
7794 (struct nfs4_get_lease_time_data *)calldata;
7795
7796 dprintk("--> %s\n", __func__);
7797 /* just setup sequence, do not trigger session recovery
7798 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007799 nfs41_setup_sequence(data->clp->cl_session,
7800 &data->args->la_seq_args,
7801 &data->res->lr_seq_res,
7802 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007803 dprintk("<-- %s\n", __func__);
7804}
7805
7806/*
7807 * Called from nfs4_state_manager thread for session setup, so don't recover
7808 * from sequence operation or clientid errors.
7809 */
7810static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7811{
7812 struct nfs4_get_lease_time_data *data =
7813 (struct nfs4_get_lease_time_data *)calldata;
7814
7815 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007816 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7817 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007818 switch (task->tk_status) {
7819 case -NFS4ERR_DELAY:
7820 case -NFS4ERR_GRACE:
7821 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7822 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7823 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007824 /* fall through */
7825 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007826 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007827 return;
7828 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007829 dprintk("<-- %s\n", __func__);
7830}
7831
Trond Myklebust17280172012-03-11 13:11:00 -04007832static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007833 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7834 .rpc_call_done = nfs4_get_lease_time_done,
7835};
7836
7837int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7838{
7839 struct rpc_task *task;
7840 struct nfs4_get_lease_time_args args;
7841 struct nfs4_get_lease_time_res res = {
7842 .lr_fsinfo = fsinfo,
7843 };
7844 struct nfs4_get_lease_time_data data = {
7845 .args = &args,
7846 .res = &res,
7847 .clp = clp,
7848 };
7849 struct rpc_message msg = {
7850 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7851 .rpc_argp = &args,
7852 .rpc_resp = &res,
7853 };
7854 struct rpc_task_setup task_setup = {
7855 .rpc_client = clp->cl_rpcclient,
7856 .rpc_message = &msg,
7857 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007858 .callback_data = &data,
7859 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007860 };
7861 int status;
7862
Chuck Levera9c92d62013-08-09 12:48:18 -04007863 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007864 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007865 dprintk("--> %s\n", __func__);
7866 task = rpc_run_task(&task_setup);
7867
7868 if (IS_ERR(task))
7869 status = PTR_ERR(task);
7870 else {
7871 status = task->tk_status;
7872 rpc_put_task(task);
7873 }
7874 dprintk("<-- %s return %d\n", __func__, status);
7875
7876 return status;
7877}
7878
Andy Adamsonfc931582009-04-01 09:22:31 -04007879/*
7880 * Initialize the values to be used by the client in CREATE_SESSION
7881 * If nfs4_init_session set the fore channel request and response sizes,
7882 * use them.
7883 *
7884 * Set the back channel max_resp_sz_cached to zero to force the client to
7885 * always set csa_cachethis to FALSE because the current implementation
7886 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7887 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007888static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7889 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007890{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007891 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007892 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007893
Andy Adamson18aad3d2013-06-26 12:21:49 -04007894 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7895 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7896
Andy Adamsonfc931582009-04-01 09:22:31 -04007897 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007898 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7899 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007900 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007901 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007902
7903 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007904 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007905 __func__,
7906 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007907 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007908
7909 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007910 args->bc_attrs.max_rqst_sz = max_bc_payload;
7911 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007912 args->bc_attrs.max_resp_sz_cached = 0;
7913 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007914 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007915
7916 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7917 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7918 __func__,
7919 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7920 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7921 args->bc_attrs.max_reqs);
7922}
7923
Trond Myklebust79969dd2015-02-18 11:30:18 -08007924static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7925 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007926{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007927 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007928 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007929
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007930 if (rcvd->max_resp_sz > sent->max_resp_sz)
7931 return -EINVAL;
7932 /*
7933 * Our requested max_ops is the minimum we need; we're not
7934 * prepared to break up compounds into smaller pieces than that.
7935 * So, no point even trying to continue if the server won't
7936 * cooperate:
7937 */
7938 if (rcvd->max_ops < sent->max_ops)
7939 return -EINVAL;
7940 if (rcvd->max_reqs == 0)
7941 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007942 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7943 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007944 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007945}
7946
Trond Myklebust79969dd2015-02-18 11:30:18 -08007947static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7948 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007949{
7950 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007951 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007952
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007953 if (!(res->flags & SESSION4_BACK_CHAN))
7954 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007955 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7956 return -EINVAL;
7957 if (rcvd->max_resp_sz < sent->max_resp_sz)
7958 return -EINVAL;
7959 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7960 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007961 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007962 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007963 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007964 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007965out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007966 return 0;
7967}
Andy Adamson8d353012009-04-01 09:22:32 -04007968
Andy Adamson8d353012009-04-01 09:22:32 -04007969static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007970 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007971{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007972 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007973
Trond Myklebust79969dd2015-02-18 11:30:18 -08007974 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007975 if (ret)
7976 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007977 return nfs4_verify_back_channel_attrs(args, res);
7978}
7979
7980static void nfs4_update_session(struct nfs4_session *session,
7981 struct nfs41_create_session_res *res)
7982{
7983 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007984 /* Mark client id and session as being confirmed */
7985 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7986 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007987 session->flags = res->flags;
7988 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007989 if (res->flags & SESSION4_BACK_CHAN)
7990 memcpy(&session->bc_attrs, &res->bc_attrs,
7991 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007992}
7993
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007994static int _nfs4_proc_create_session(struct nfs_client *clp,
7995 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007996{
7997 struct nfs4_session *session = clp->cl_session;
7998 struct nfs41_create_session_args args = {
7999 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008000 .clientid = clp->cl_clientid,
8001 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008002 .cb_program = NFS4_CALLBACK,
8003 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008004 struct nfs41_create_session_res res;
8005
Andy Adamsonfc931582009-04-01 09:22:31 -04008006 struct rpc_message msg = {
8007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8008 .rpc_argp = &args,
8009 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008010 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008011 };
8012 int status;
8013
Chuck Lever6b26cc82016-05-02 14:40:40 -04008014 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008015 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008016
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008017 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008018 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008019
Trond Myklebustb519d402016-09-11 14:50:01 -04008020 switch (status) {
8021 case -NFS4ERR_STALE_CLIENTID:
8022 case -NFS4ERR_DELAY:
8023 case -ETIMEDOUT:
8024 case -EACCES:
8025 case -EAGAIN:
8026 goto out;
8027 };
8028
8029 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008030 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008031 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008032 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008033 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008034 if (status)
8035 goto out;
8036 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008037 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008038out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008039 return status;
8040}
8041
8042/*
8043 * Issues a CREATE_SESSION operation to the server.
8044 * It is the responsibility of the caller to verify the session is
8045 * expired before calling this routine.
8046 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008047int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008048{
8049 int status;
8050 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008051 struct nfs4_session *session = clp->cl_session;
8052
8053 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8054
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008055 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008056 if (status)
8057 goto out;
8058
Andy Adamsonaacd5532011-11-09 13:58:21 -05008059 /* Init or reset the session slot tables */
8060 status = nfs4_setup_session_slot_tables(session);
8061 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008062 if (status)
8063 goto out;
8064
8065 ptr = (unsigned *)&session->sess_id.data[0];
8066 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8067 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008068out:
8069 dprintk("<-- %s\n", __func__);
8070 return status;
8071}
8072
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008073/*
8074 * Issue the over-the-wire RPC DESTROY_SESSION.
8075 * The caller must serialize access to this routine.
8076 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008077int nfs4_proc_destroy_session(struct nfs4_session *session,
8078 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008079{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008080 struct rpc_message msg = {
8081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8082 .rpc_argp = session,
8083 .rpc_cred = cred,
8084 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008085 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008086
8087 dprintk("--> nfs4_proc_destroy_session\n");
8088
8089 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008090 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8091 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008092
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008093 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008094 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008095
8096 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008097 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008098 "Session has been destroyed regardless...\n", status);
8099
8100 dprintk("<-- nfs4_proc_destroy_session\n");
8101 return status;
8102}
8103
Trond Myklebust7b38c362012-05-23 13:23:31 -04008104/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008105 * Renew the cl_session lease.
8106 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008107struct nfs4_sequence_data {
8108 struct nfs_client *clp;
8109 struct nfs4_sequence_args args;
8110 struct nfs4_sequence_res res;
8111};
8112
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008113static void nfs41_sequence_release(void *data)
8114{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008115 struct nfs4_sequence_data *calldata = data;
8116 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008117
Alexandros Batsakis71358402010-02-05 03:45:05 -08008118 if (atomic_read(&clp->cl_count) > 1)
8119 nfs4_schedule_state_renewal(clp);
8120 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008121 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008122}
8123
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008124static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8125{
8126 switch(task->tk_status) {
8127 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008128 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8129 return -EAGAIN;
8130 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008131 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008132 }
8133 return 0;
8134}
8135
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008136static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008137{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008138 struct nfs4_sequence_data *calldata = data;
8139 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008140
Trond Myklebust14516c32010-07-31 14:29:06 -04008141 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8142 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008143
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008144 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008145 if (task->tk_status < 0) {
8146 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008147 if (atomic_read(&clp->cl_count) == 1)
8148 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008149
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008150 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8151 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008152 return;
8153 }
8154 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008155 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008156out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008157 dprintk("<-- %s\n", __func__);
8158}
8159
8160static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8161{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008162 struct nfs4_sequence_data *calldata = data;
8163 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008164 struct nfs4_sequence_args *args;
8165 struct nfs4_sequence_res *res;
8166
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008167 args = task->tk_msg.rpc_argp;
8168 res = task->tk_msg.rpc_resp;
8169
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008170 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008171}
8172
8173static const struct rpc_call_ops nfs41_sequence_ops = {
8174 .rpc_call_done = nfs41_sequence_call_done,
8175 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008176 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008177};
8178
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008179static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8180 struct rpc_cred *cred,
8181 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008182{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008183 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008184 struct rpc_message msg = {
8185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8186 .rpc_cred = cred,
8187 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008188 struct rpc_task_setup task_setup_data = {
8189 .rpc_client = clp->cl_rpcclient,
8190 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008191 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008192 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008193 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008194
Alexandros Batsakis71358402010-02-05 03:45:05 -08008195 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008196 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008197 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008198 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008199 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008200 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008201 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008202 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008203 if (is_privileged)
8204 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008205 msg.rpc_argp = &calldata->args;
8206 msg.rpc_resp = &calldata->res;
8207 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008208 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008209
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008210 return rpc_run_task(&task_setup_data);
8211}
8212
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008213static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008214{
8215 struct rpc_task *task;
8216 int ret = 0;
8217
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008218 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008219 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008220 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008221 if (IS_ERR(task))
8222 ret = PTR_ERR(task);
8223 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008224 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008225 dprintk("<-- %s status=%d\n", __func__, ret);
8226 return ret;
8227}
8228
8229static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8230{
8231 struct rpc_task *task;
8232 int ret;
8233
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008234 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008235 if (IS_ERR(task)) {
8236 ret = PTR_ERR(task);
8237 goto out;
8238 }
8239 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008240 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008241 ret = task->tk_status;
8242 rpc_put_task(task);
8243out:
8244 dprintk("<-- %s status=%d\n", __func__, ret);
8245 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008246}
8247
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008248struct nfs4_reclaim_complete_data {
8249 struct nfs_client *clp;
8250 struct nfs41_reclaim_complete_args arg;
8251 struct nfs41_reclaim_complete_res res;
8252};
8253
8254static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8255{
8256 struct nfs4_reclaim_complete_data *calldata = data;
8257
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008258 nfs41_setup_sequence(calldata->clp->cl_session,
8259 &calldata->arg.seq_args,
8260 &calldata->res.seq_res,
8261 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008262}
8263
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008264static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8265{
8266 switch(task->tk_status) {
8267 case 0:
8268 case -NFS4ERR_COMPLETE_ALREADY:
8269 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8270 break;
8271 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008272 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008273 /* fall through */
8274 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008275 return -EAGAIN;
8276 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008277 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008278 }
8279 return 0;
8280}
8281
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008282static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8283{
8284 struct nfs4_reclaim_complete_data *calldata = data;
8285 struct nfs_client *clp = calldata->clp;
8286 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8287
8288 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008289 if (!nfs41_sequence_done(task, res))
8290 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008291
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008292 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008293 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8294 rpc_restart_call_prepare(task);
8295 return;
8296 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008297 dprintk("<-- %s\n", __func__);
8298}
8299
8300static void nfs4_free_reclaim_complete_data(void *data)
8301{
8302 struct nfs4_reclaim_complete_data *calldata = data;
8303
8304 kfree(calldata);
8305}
8306
8307static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8308 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8309 .rpc_call_done = nfs4_reclaim_complete_done,
8310 .rpc_release = nfs4_free_reclaim_complete_data,
8311};
8312
8313/*
8314 * Issue a global reclaim complete.
8315 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008316static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8317 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008318{
8319 struct nfs4_reclaim_complete_data *calldata;
8320 struct rpc_task *task;
8321 struct rpc_message msg = {
8322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008323 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008324 };
8325 struct rpc_task_setup task_setup_data = {
8326 .rpc_client = clp->cl_rpcclient,
8327 .rpc_message = &msg,
8328 .callback_ops = &nfs4_reclaim_complete_call_ops,
8329 .flags = RPC_TASK_ASYNC,
8330 };
8331 int status = -ENOMEM;
8332
8333 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008334 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008335 if (calldata == NULL)
8336 goto out;
8337 calldata->clp = clp;
8338 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008339
Chuck Levera9c92d62013-08-09 12:48:18 -04008340 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008341 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008342 msg.rpc_argp = &calldata->arg;
8343 msg.rpc_resp = &calldata->res;
8344 task_setup_data.callback_data = calldata;
8345 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008346 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008347 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008348 goto out;
8349 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008350 status = nfs4_wait_for_completion_rpc_task(task);
8351 if (status == 0)
8352 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008353 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008354 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008355out:
8356 dprintk("<-- %s status=%d\n", __func__, status);
8357 return status;
8358}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008359
8360static void
8361nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8362{
8363 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008364 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008365 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008366
8367 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008368 nfs41_setup_sequence(session, &lgp->args.seq_args,
8369 &lgp->res.seq_res, task);
8370 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008371}
8372
8373static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8374{
8375 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008376
8377 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008378 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008379 dprintk("<-- %s\n", __func__);
8380}
8381
8382static int
8383nfs4_layoutget_handle_exception(struct rpc_task *task,
8384 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8385{
Trond Myklebustee314c22012-10-01 17:25:48 -07008386 struct inode *inode = lgp->args.inode;
8387 struct nfs_server *server = NFS_SERVER(inode);
8388 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008389 int nfs4err = task->tk_status;
8390 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008391 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008392
Boaz Harroshed7e5422014-01-22 20:34:54 +02008393 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008394
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008395 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008396 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008397 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008398
8399 /*
8400 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8401 * on the file. set tk_status to -ENODATA to tell upper layer to
8402 * retry go inband.
8403 */
8404 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008405 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008406 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008407 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008408 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8409 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8410 */
8411 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008412 status = -EOVERFLOW;
8413 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008414 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008415 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008416 * (or clients) writing to the same RAID stripe except when
8417 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008418 *
8419 * Treat it like we would RECALLCONFLICT -- we retry for a little
8420 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008421 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008422 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008423 if (lgp->args.minlength == 0) {
8424 status = -EOVERFLOW;
8425 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008426 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008427 status = -EBUSY;
8428 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008429 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008430 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008431 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008432 case -NFS4ERR_DELEG_REVOKED:
8433 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008434 case -NFS4ERR_EXPIRED:
8435 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008436 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008437 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008438 lo = NFS_I(inode)->layout;
8439 /* If the open stateid was bad, then recover it. */
8440 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8441 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008442 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008443 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008444 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008445 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008446 break;
8447 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008448
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008449 /*
8450 * Mark the bad layout state as invalid, then retry
8451 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008452 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008453 spin_unlock(&inode->i_lock);
8454 pnfs_free_lseg_list(&head);
8455 status = -EAGAIN;
8456 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008457 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008458
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008459 err = nfs4_handle_exception(server, nfs4err, exception);
8460 if (!status) {
8461 if (exception->retry)
8462 status = -EAGAIN;
8463 else
8464 status = err;
8465 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008466out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008467 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008468 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008469}
8470
Idan Kedar85541162012-08-02 11:47:10 +03008471static size_t max_response_pages(struct nfs_server *server)
8472{
8473 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8474 return nfs_page_array_len(0, max_resp_sz);
8475}
8476
8477static void nfs4_free_pages(struct page **pages, size_t size)
8478{
8479 int i;
8480
8481 if (!pages)
8482 return;
8483
8484 for (i = 0; i < size; i++) {
8485 if (!pages[i])
8486 break;
8487 __free_page(pages[i]);
8488 }
8489 kfree(pages);
8490}
8491
8492static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8493{
8494 struct page **pages;
8495 int i;
8496
8497 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8498 if (!pages) {
8499 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8500 return NULL;
8501 }
8502
8503 for (i = 0; i < size; i++) {
8504 pages[i] = alloc_page(gfp_flags);
8505 if (!pages[i]) {
8506 dprintk("%s: failed to allocate page\n", __func__);
8507 nfs4_free_pages(pages, size);
8508 return NULL;
8509 }
8510 }
8511
8512 return pages;
8513}
8514
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008515static void nfs4_layoutget_release(void *calldata)
8516{
8517 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008518 struct inode *inode = lgp->args.inode;
8519 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008520 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008521
8522 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008523 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008524 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008525 put_nfs_open_context(lgp->args.ctx);
8526 kfree(calldata);
8527 dprintk("<-- %s\n", __func__);
8528}
8529
8530static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8531 .rpc_call_prepare = nfs4_layoutget_prepare,
8532 .rpc_call_done = nfs4_layoutget_done,
8533 .rpc_release = nfs4_layoutget_release,
8534};
8535
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008536struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008537nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008538{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008539 struct inode *inode = lgp->args.inode;
8540 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008541 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008542 struct rpc_task *task;
8543 struct rpc_message msg = {
8544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8545 .rpc_argp = &lgp->args,
8546 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008547 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008548 };
8549 struct rpc_task_setup task_setup_data = {
8550 .rpc_client = server->client,
8551 .rpc_message = &msg,
8552 .callback_ops = &nfs4_layoutget_call_ops,
8553 .callback_data = lgp,
8554 .flags = RPC_TASK_ASYNC,
8555 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008556 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008557 struct nfs4_exception exception = {
8558 .inode = inode,
8559 .timeout = *timeout,
8560 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008561 int status = 0;
8562
8563 dprintk("--> %s\n", __func__);
8564
Peng Tao4bd5a9802014-11-17 11:05:17 +08008565 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8566 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8567
Idan Kedar85541162012-08-02 11:47:10 +03008568 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8569 if (!lgp->args.layout.pages) {
8570 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008571 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008572 }
8573 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8574
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008575 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008576 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008577 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008578
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008579 task = rpc_run_task(&task_setup_data);
8580 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008581 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008582 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008583 if (status == 0) {
8584 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8585 *timeout = exception.timeout;
8586 }
8587
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008588 trace_nfs4_layoutget(lgp->args.ctx,
8589 &lgp->args.range,
8590 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008591 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008592 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008593
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008594 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8595 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008596 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008597 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598 rpc_put_task(task);
8599 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008600 if (status)
8601 return ERR_PTR(status);
8602 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008603}
8604
Benny Halevycbe82602011-05-22 19:52:37 +03008605static void
8606nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8607{
8608 struct nfs4_layoutreturn *lrp = calldata;
8609
8610 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008611 nfs41_setup_sequence(lrp->clp->cl_session,
8612 &lrp->args.seq_args,
8613 &lrp->res.seq_res,
8614 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008615}
8616
8617static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8618{
8619 struct nfs4_layoutreturn *lrp = calldata;
8620 struct nfs_server *server;
8621
8622 dprintk("--> %s\n", __func__);
8623
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008624 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008625 return;
8626
8627 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008628 switch (task->tk_status) {
8629 default:
8630 task->tk_status = 0;
8631 case 0:
8632 break;
8633 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008634 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008635 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008636 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008637 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008638 return;
8639 }
Benny Halevycbe82602011-05-22 19:52:37 +03008640 dprintk("<-- %s\n", __func__);
8641}
8642
8643static void nfs4_layoutreturn_release(void *calldata)
8644{
8645 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008646 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008647
8648 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008649 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008650 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008651 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008652 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8653 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008654 pnfs_put_layout_hdr(lrp->args.layout);
8655 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008656 kfree(calldata);
8657 dprintk("<-- %s\n", __func__);
8658}
8659
8660static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8661 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8662 .rpc_call_done = nfs4_layoutreturn_done,
8663 .rpc_release = nfs4_layoutreturn_release,
8664};
8665
Peng Tao6c166052014-11-17 09:30:40 +08008666int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008667{
8668 struct rpc_task *task;
8669 struct rpc_message msg = {
8670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8671 .rpc_argp = &lrp->args,
8672 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008673 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008674 };
8675 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008676 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008677 .rpc_message = &msg,
8678 .callback_ops = &nfs4_layoutreturn_call_ops,
8679 .callback_data = lrp,
8680 };
Peng Tao6c166052014-11-17 09:30:40 +08008681 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008682
Andrew Elble99ade3c2015-12-02 09:39:51 -05008683 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8684 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8685 &task_setup_data.rpc_client, &msg);
8686
Benny Halevycbe82602011-05-22 19:52:37 +03008687 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008688 if (!sync) {
8689 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8690 if (!lrp->inode) {
8691 nfs4_layoutreturn_release(lrp);
8692 return -EAGAIN;
8693 }
8694 task_setup_data.flags |= RPC_TASK_ASYNC;
8695 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008696 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008697 task = rpc_run_task(&task_setup_data);
8698 if (IS_ERR(task))
8699 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008700 if (sync)
8701 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008702 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008703 dprintk("<-- %s status=%d\n", __func__, status);
8704 rpc_put_task(task);
8705 return status;
8706}
8707
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008708static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008709_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8710 struct pnfs_device *pdev,
8711 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008712{
8713 struct nfs4_getdeviceinfo_args args = {
8714 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008715 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8716 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008717 };
8718 struct nfs4_getdeviceinfo_res res = {
8719 .pdev = pdev,
8720 };
8721 struct rpc_message msg = {
8722 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8723 .rpc_argp = &args,
8724 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008725 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008726 };
8727 int status;
8728
8729 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008730 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008731 if (res.notification & ~args.notify_types)
8732 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008733 if (res.notification != args.notify_types)
8734 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008735
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008736 dprintk("<-- %s status=%d\n", __func__, status);
8737
8738 return status;
8739}
8740
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008741int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8742 struct pnfs_device *pdev,
8743 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744{
8745 struct nfs4_exception exception = { };
8746 int err;
8747
8748 do {
8749 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008750 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008751 &exception);
8752 } while (exception.retry);
8753 return err;
8754}
8755EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8756
Andy Adamson863a3c62011-03-23 13:27:54 +00008757static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8758{
8759 struct nfs4_layoutcommit_data *data = calldata;
8760 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008761 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008762
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008763 nfs41_setup_sequence(session,
8764 &data->args.seq_args,
8765 &data->res.seq_res,
8766 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008767}
8768
8769static void
8770nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8771{
8772 struct nfs4_layoutcommit_data *data = calldata;
8773 struct nfs_server *server = NFS_SERVER(data->args.inode);
8774
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008775 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008776 return;
8777
8778 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008779 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8780 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8781 case -NFS4ERR_BADLAYOUT: /* no layout */
8782 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008783 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008784 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008785 break;
8786 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008787 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008788 rpc_restart_call_prepare(task);
8789 return;
8790 }
8791 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008792}
8793
8794static void nfs4_layoutcommit_release(void *calldata)
8795{
8796 struct nfs4_layoutcommit_data *data = calldata;
8797
Andy Adamsondb29c082011-07-30 20:52:38 -04008798 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008799 nfs_post_op_update_inode_force_wcc(data->args.inode,
8800 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008801 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008802 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008803 kfree(data);
8804}
8805
8806static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8807 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8808 .rpc_call_done = nfs4_layoutcommit_done,
8809 .rpc_release = nfs4_layoutcommit_release,
8810};
8811
8812int
Andy Adamsonef311532011-03-12 02:58:10 -05008813nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008814{
8815 struct rpc_message msg = {
8816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8817 .rpc_argp = &data->args,
8818 .rpc_resp = &data->res,
8819 .rpc_cred = data->cred,
8820 };
8821 struct rpc_task_setup task_setup_data = {
8822 .task = &data->task,
8823 .rpc_client = NFS_CLIENT(data->args.inode),
8824 .rpc_message = &msg,
8825 .callback_ops = &nfs4_layoutcommit_ops,
8826 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008827 };
8828 struct rpc_task *task;
8829 int status = 0;
8830
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008831 dprintk("NFS: initiating layoutcommit call. sync %d "
8832 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008833 data->args.lastbytewritten,
8834 data->args.inode->i_ino);
8835
Trond Myklebust472e2592015-02-05 16:50:30 -05008836 if (!sync) {
8837 data->inode = nfs_igrab_and_active(data->args.inode);
8838 if (data->inode == NULL) {
8839 nfs4_layoutcommit_release(data);
8840 return -EAGAIN;
8841 }
8842 task_setup_data.flags = RPC_TASK_ASYNC;
8843 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008844 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008845 task = rpc_run_task(&task_setup_data);
8846 if (IS_ERR(task))
8847 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008848 if (sync)
8849 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008850 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008851 dprintk("%s: status %d\n", __func__, status);
8852 rpc_put_task(task);
8853 return status;
8854}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008855
Andy Adamson97431202013-08-08 10:57:56 -04008856/**
8857 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8858 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8859 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008860static int
8861_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008862 struct nfs_fsinfo *info,
8863 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008864{
8865 struct nfs41_secinfo_no_name_args args = {
8866 .style = SECINFO_STYLE_CURRENT_FH,
8867 };
8868 struct nfs4_secinfo_res res = {
8869 .flavors = flavors,
8870 };
8871 struct rpc_message msg = {
8872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8873 .rpc_argp = &args,
8874 .rpc_resp = &res,
8875 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008876 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008877 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008878 int status;
8879
8880 if (use_integrity) {
8881 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008882 cred = nfs4_get_clid_cred(server->nfs_client);
8883 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008884 }
8885
8886 dprintk("--> %s\n", __func__);
8887 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8888 &res.seq_res, 0);
8889 dprintk("<-- %s status=%d\n", __func__, status);
8890
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008891 if (cred)
8892 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008893
8894 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008895}
8896
8897static int
8898nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8899 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8900{
8901 struct nfs4_exception exception = { };
8902 int err;
8903 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008904 /* first try using integrity protection */
8905 err = -NFS4ERR_WRONGSEC;
8906
8907 /* try to use integrity protection with machine cred */
8908 if (_nfs4_is_integrity_protected(server->nfs_client))
8909 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8910 flavors, true);
8911
8912 /*
8913 * if unable to use integrity protection, or SECINFO with
8914 * integrity protection returns NFS4ERR_WRONGSEC (which is
8915 * disallowed by spec, but exists in deployed servers) use
8916 * the current filesystem's rpc_client and the user cred.
8917 */
8918 if (err == -NFS4ERR_WRONGSEC)
8919 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8920 flavors, false);
8921
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008922 switch (err) {
8923 case 0:
8924 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008925 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008926 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008927 default:
8928 err = nfs4_handle_exception(server, err, &exception);
8929 }
8930 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008931out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008932 return err;
8933}
8934
8935static int
8936nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8937 struct nfs_fsinfo *info)
8938{
8939 int err;
8940 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008941 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008942 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008943 struct nfs4_secinfo4 *secinfo;
8944 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008945
8946 page = alloc_page(GFP_KERNEL);
8947 if (!page) {
8948 err = -ENOMEM;
8949 goto out;
8950 }
8951
8952 flavors = page_address(page);
8953 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8954
8955 /*
8956 * Fall back on "guess and check" method if
8957 * the server doesn't support SECINFO_NO_NAME
8958 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008959 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008960 err = nfs4_find_root_sec(server, fhandle, info);
8961 goto out_freepage;
8962 }
8963 if (err)
8964 goto out_freepage;
8965
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008966 for (i = 0; i < flavors->num_flavors; i++) {
8967 secinfo = &flavors->flavors[i];
8968
8969 switch (secinfo->flavor) {
8970 case RPC_AUTH_NULL:
8971 case RPC_AUTH_UNIX:
8972 case RPC_AUTH_GSS:
8973 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8974 &secinfo->flavor_info);
8975 break;
8976 default:
8977 flavor = RPC_AUTH_MAXFLAVOR;
8978 break;
8979 }
8980
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008981 if (!nfs_auth_info_match(&server->auth_info, flavor))
8982 flavor = RPC_AUTH_MAXFLAVOR;
8983
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008984 if (flavor != RPC_AUTH_MAXFLAVOR) {
8985 err = nfs4_lookup_root_sec(server, fhandle,
8986 info, flavor);
8987 if (!err)
8988 break;
8989 }
8990 }
8991
8992 if (flavor == RPC_AUTH_MAXFLAVOR)
8993 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008994
8995out_freepage:
8996 put_page(page);
8997 if (err == -EACCES)
8998 return -EPERM;
8999out:
9000 return err;
9001}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009002
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009003static int _nfs41_test_stateid(struct nfs_server *server,
9004 nfs4_stateid *stateid,
9005 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009006{
9007 int status;
9008 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009009 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009010 };
9011 struct nfs41_test_stateid_res res;
9012 struct rpc_message msg = {
9013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9014 .rpc_argp = &args,
9015 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009016 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009017 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009018 struct rpc_clnt *rpc_client = server->client;
9019
9020 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9021 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009022
Chuck Lever38527b12012-07-11 16:30:23 -04009023 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009024 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009025 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009026 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009027 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009028 if (status != NFS_OK) {
9029 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009030 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009031 }
9032 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009033 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009034}
9035
Trond Myklebust43912bb2016-09-22 13:38:56 -04009036static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9037 int err, struct nfs4_exception *exception)
9038{
9039 exception->retry = 0;
9040 switch(err) {
9041 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009042 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009043 nfs4_handle_exception(server, err, exception);
9044 break;
9045 case -NFS4ERR_BADSESSION:
9046 case -NFS4ERR_BADSLOT:
9047 case -NFS4ERR_BAD_HIGH_SLOT:
9048 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9049 case -NFS4ERR_DEADSESSION:
9050 nfs4_do_handle_exception(server, err, exception);
9051 }
9052}
9053
Chuck Lever38527b12012-07-11 16:30:23 -04009054/**
9055 * nfs41_test_stateid - perform a TEST_STATEID operation
9056 *
9057 * @server: server / transport on which to perform the operation
9058 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009059 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009060 *
9061 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9062 * Otherwise a negative NFS4ERR value is returned if the operation
9063 * failed or the state ID is not currently valid.
9064 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009065static int nfs41_test_stateid(struct nfs_server *server,
9066 nfs4_stateid *stateid,
9067 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009068{
9069 struct nfs4_exception exception = { };
9070 int err;
9071 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009072 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009073 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009074 } while (exception.retry);
9075 return err;
9076}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009077
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009078struct nfs_free_stateid_data {
9079 struct nfs_server *server;
9080 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009081 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009082};
9083
9084static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9085{
9086 struct nfs_free_stateid_data *data = calldata;
9087 nfs41_setup_sequence(nfs4_get_session(data->server),
9088 &data->args.seq_args,
9089 &data->res.seq_res,
9090 task);
9091}
9092
9093static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9094{
9095 struct nfs_free_stateid_data *data = calldata;
9096
9097 nfs41_sequence_done(task, &data->res.seq_res);
9098
9099 switch (task->tk_status) {
9100 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009101 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009102 rpc_restart_call_prepare(task);
9103 }
9104}
9105
9106static void nfs41_free_stateid_release(void *calldata)
9107{
9108 kfree(calldata);
9109}
9110
Trond Myklebust17f26b12013-08-21 15:48:42 -04009111static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009112 .rpc_call_prepare = nfs41_free_stateid_prepare,
9113 .rpc_call_done = nfs41_free_stateid_done,
9114 .rpc_release = nfs41_free_stateid_release,
9115};
9116
9117static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009118 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009119 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009120 bool privileged)
9121{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009122 struct rpc_message msg = {
9123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009124 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009125 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009126 struct rpc_task_setup task_setup = {
9127 .rpc_client = server->client,
9128 .rpc_message = &msg,
9129 .callback_ops = &nfs41_free_stateid_ops,
9130 .flags = RPC_TASK_ASYNC,
9131 };
9132 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009133
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009134 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9135 &task_setup.rpc_client, &msg);
9136
Chuck Lever38527b12012-07-11 16:30:23 -04009137 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009138 data = kmalloc(sizeof(*data), GFP_NOFS);
9139 if (!data)
9140 return ERR_PTR(-ENOMEM);
9141 data->server = server;
9142 nfs4_stateid_copy(&data->args.stateid, stateid);
9143
9144 task_setup.callback_data = data;
9145
9146 msg.rpc_argp = &data->args;
9147 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009148 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009149 if (privileged)
9150 nfs4_set_sequence_privileged(&data->args.seq_args);
9151
9152 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009153}
9154
Chuck Lever38527b12012-07-11 16:30:23 -04009155/**
9156 * nfs41_free_stateid - perform a FREE_STATEID operation
9157 *
9158 * @server: server / transport on which to perform the operation
9159 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009160 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009161 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009162 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009163 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009164 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009165static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009166 const nfs4_stateid *stateid,
9167 struct rpc_cred *cred,
9168 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009169{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009170 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009171
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009172 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009173 if (IS_ERR(task))
9174 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009175 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009176 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009177}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009178
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009179static void
9180nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009181{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009182 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009183
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009184 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009185 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009186}
9187
Trond Myklebust36281ca2012-03-04 18:13:56 -05009188static bool nfs41_match_stateid(const nfs4_stateid *s1,
9189 const nfs4_stateid *s2)
9190{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009191 if (s1->type != s2->type)
9192 return false;
9193
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009194 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009195 return false;
9196
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009197 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009198 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009199 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009200 return true;
9201
9202 return false;
9203}
9204
Andy Adamson557134a2009-04-01 09:21:53 -04009205#endif /* CONFIG_NFS_V4_1 */
9206
Trond Myklebust36281ca2012-03-04 18:13:56 -05009207static bool nfs4_match_stateid(const nfs4_stateid *s1,
9208 const nfs4_stateid *s2)
9209{
Trond Myklebustf597c532012-03-04 18:13:56 -05009210 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009211}
9212
9213
Trond Myklebust17280172012-03-11 13:11:00 -04009214static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009215 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009216 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009217 .recover_open = nfs4_open_reclaim,
9218 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009219 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009220 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009221};
9222
Andy Adamson591d71c2009-04-01 09:22:47 -04009223#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009224static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009225 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9226 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9227 .recover_open = nfs4_open_reclaim,
9228 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009229 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009230 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009231 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009232};
9233#endif /* CONFIG_NFS_V4_1 */
9234
Trond Myklebust17280172012-03-11 13:11:00 -04009235static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009236 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009237 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009238 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009239 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009240 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009241};
9242
Andy Adamson591d71c2009-04-01 09:22:47 -04009243#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009244static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009245 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9246 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009247 .recover_open = nfs41_open_expired,
9248 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009249 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009250};
9251#endif /* CONFIG_NFS_V4_1 */
9252
Trond Myklebust17280172012-03-11 13:11:00 -04009253static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009254 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009255 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009256 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009257};
9258
9259#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009260static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009261 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009262 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009263 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009264};
9265#endif
9266
Chuck Leverec011fe2013-10-17 14:12:39 -04009267static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009268 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009269 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009270};
9271
9272#if defined(CONFIG_NFS_V4_1)
9273static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009274 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009275 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009276};
9277#endif /* CONFIG_NFS_V4_1 */
9278
Trond Myklebust97dc1352010-06-16 09:52:26 -04009279static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9280 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009281 .init_caps = NFS_CAP_READDIRPLUS
9282 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009283 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009284 .init_client = nfs40_init_client,
9285 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009286 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009287 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009288 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009289 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009290 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009291 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009292 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9293 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9294 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009295 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009296};
9297
9298#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009299static struct nfs_seqid *
9300nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9301{
9302 return NULL;
9303}
9304
Trond Myklebust97dc1352010-06-16 09:52:26 -04009305static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9306 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009307 .init_caps = NFS_CAP_READDIRPLUS
9308 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009309 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009310 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009311 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009312 .init_client = nfs41_init_client,
9313 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009314 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009315 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009316 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009317 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009318 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009319 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009320 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009321 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9322 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9323 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009324 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009325};
9326#endif
9327
Steve Dickson42c2c422013-05-22 12:50:38 -04009328#if defined(CONFIG_NFS_V4_2)
9329static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9330 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009331 .init_caps = NFS_CAP_READDIRPLUS
9332 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009333 | NFS_CAP_POSIX_LOCK
9334 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009335 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009336 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009337 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009338 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009339 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009340 | NFS_CAP_LAYOUTSTATS
9341 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009342 .init_client = nfs41_init_client,
9343 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009344 .match_stateid = nfs41_match_stateid,
9345 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009346 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009347 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009348 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009349 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009350 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009351 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9352 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9353 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009354 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009355};
9356#endif
9357
Trond Myklebust97dc1352010-06-16 09:52:26 -04009358const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9359 [0] = &nfs_v4_0_minor_ops,
9360#if defined(CONFIG_NFS_V4_1)
9361 [1] = &nfs_v4_1_minor_ops,
9362#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009363#if defined(CONFIG_NFS_V4_2)
9364 [2] = &nfs_v4_2_minor_ops,
9365#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009366};
9367
Trond Myklebust13997822016-07-24 17:10:52 -04009368static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009369{
9370 ssize_t error, error2;
9371
9372 error = generic_listxattr(dentry, list, size);
9373 if (error < 0)
9374 return error;
9375 if (list) {
9376 list += error;
9377 size -= error;
9378 }
9379
9380 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9381 if (error2 < 0)
9382 return error2;
9383 return error + error2;
9384}
9385
Trond Myklebust17f26b12013-08-21 15:48:42 -04009386static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009387 .create = nfs_create,
9388 .lookup = nfs_lookup,
9389 .atomic_open = nfs_atomic_open,
9390 .link = nfs_link,
9391 .unlink = nfs_unlink,
9392 .symlink = nfs_symlink,
9393 .mkdir = nfs_mkdir,
9394 .rmdir = nfs_rmdir,
9395 .mknod = nfs_mknod,
9396 .rename = nfs_rename,
9397 .permission = nfs_permission,
9398 .getattr = nfs_getattr,
9399 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009400 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009401};
9402
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009403static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009404 .permission = nfs_permission,
9405 .getattr = nfs_getattr,
9406 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009407 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009408};
9409
David Howells509de812006-08-22 20:06:11 -04009410const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 .version = 4, /* protocol version */
9412 .dentry_ops = &nfs4_dentry_operations,
9413 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009414 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009415 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009417 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009418 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009419 .getattr = nfs4_proc_getattr,
9420 .setattr = nfs4_proc_setattr,
9421 .lookup = nfs4_proc_lookup,
9422 .access = nfs4_proc_access,
9423 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .create = nfs4_proc_create,
9425 .remove = nfs4_proc_remove,
9426 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009427 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009429 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009430 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009431 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009432 .link = nfs4_proc_link,
9433 .symlink = nfs4_proc_symlink,
9434 .mkdir = nfs4_proc_mkdir,
9435 .rmdir = nfs4_proc_remove,
9436 .readdir = nfs4_proc_readdir,
9437 .mknod = nfs4_proc_mknod,
9438 .statfs = nfs4_proc_statfs,
9439 .fsinfo = nfs4_proc_fsinfo,
9440 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009441 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009442 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009443 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009444 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009445 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009447 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009449 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009450 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009451 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009452 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009453 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009454 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009455 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009456 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009457 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009458 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009459 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009460 .create_server = nfs4_create_server,
9461 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009462};
9463
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009464static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009465 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009466 .list = nfs4_xattr_list_nfs4_acl,
9467 .get = nfs4_xattr_get_nfs4_acl,
9468 .set = nfs4_xattr_set_nfs4_acl,
9469};
9470
9471const struct xattr_handler *nfs4_xattr_handlers[] = {
9472 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009473#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9474 &nfs4_xattr_nfs4_label_handler,
9475#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009476 NULL
9477};
9478
Linus Torvalds1da177e2005-04-16 15:20:36 -07009479/*
9480 * Local variables:
9481 * c-basic-offset: 8
9482 * End:
9483 */