blob: c5a5086696555c8039bb3953d8e50078e345c369 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400102static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
103 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400104#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400105
106#ifdef CONFIG_NFS_V4_SECURITY_LABEL
107static inline struct nfs4_label *
108nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
109 struct iattr *sattr, struct nfs4_label *label)
110{
111 int err;
112
113 if (label == NULL)
114 return NULL;
115
116 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
117 return NULL;
118
David Quigleyaa9c2662013-05-22 12:50:44 -0400119 err = security_dentry_init_security(dentry, sattr->ia_mode,
120 &dentry->d_name, (void **)&label->label, &label->len);
121 if (err == 0)
122 return label;
123
124 return NULL;
125}
126static inline void
127nfs4_label_release_security(struct nfs4_label *label)
128{
129 if (label)
130 security_release_secctx(label->label, label->len);
131}
132static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
133{
134 if (label)
135 return server->attr_bitmask;
136
137 return server->attr_bitmask_nl;
138}
139#else
140static inline struct nfs4_label *
141nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
142 struct iattr *sattr, struct nfs4_label *l)
143{ return NULL; }
144static inline void
145nfs4_label_release_security(struct nfs4_label *label)
146{ return; }
147static inline u32 *
148nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
149{ return server->attr_bitmask; }
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500153static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Trond Myklebust52567b02009-10-23 14:46:42 -0400155 if (err >= -1000)
156 return err;
157 switch (err) {
158 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500159 case -NFS4ERR_LAYOUTTRYLATER:
160 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400161 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000162 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400163 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000164 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800165 case -NFS4ERR_BADOWNER:
166 case -NFS4ERR_BADNAME:
167 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400168 case -NFS4ERR_SHARE_DENIED:
169 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400170 case -NFS4ERR_MINOR_VERS_MISMATCH:
171 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400172 case -NFS4ERR_FILE_OPEN:
173 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400174 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700176 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/*
183 * This is our standard bitmap for GETATTR requests.
184 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400185const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 FATTR4_WORD0_TYPE
187 | FATTR4_WORD0_CHANGE
188 | FATTR4_WORD0_SIZE
189 | FATTR4_WORD0_FSID
190 | FATTR4_WORD0_FILEID,
191 FATTR4_WORD1_MODE
192 | FATTR4_WORD1_NUMLINKS
193 | FATTR4_WORD1_OWNER
194 | FATTR4_WORD1_OWNER_GROUP
195 | FATTR4_WORD1_RAWDEV
196 | FATTR4_WORD1_SPACE_USED
197 | FATTR4_WORD1_TIME_ACCESS
198 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400199 | FATTR4_WORD1_TIME_MODIFY
200 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400201#ifdef CONFIG_NFS_V4_SECURITY_LABEL
202 FATTR4_WORD2_SECURITY_LABEL
203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Trond Myklebust1549210f2012-06-05 09:16:47 -0400206static const u32 nfs4_pnfs_open_bitmap[3] = {
207 FATTR4_WORD0_TYPE
208 | FATTR4_WORD0_CHANGE
209 | FATTR4_WORD0_SIZE
210 | FATTR4_WORD0_FSID
211 | FATTR4_WORD0_FILEID,
212 FATTR4_WORD1_MODE
213 | FATTR4_WORD1_NUMLINKS
214 | FATTR4_WORD1_OWNER
215 | FATTR4_WORD1_OWNER_GROUP
216 | FATTR4_WORD1_RAWDEV
217 | FATTR4_WORD1_SPACE_USED
218 | FATTR4_WORD1_TIME_ACCESS
219 | FATTR4_WORD1_TIME_METADATA
220 | FATTR4_WORD1_TIME_MODIFY,
221 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500222#ifdef CONFIG_NFS_V4_SECURITY_LABEL
223 | FATTR4_WORD2_SECURITY_LABEL
224#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400225};
226
Andy Adamsone23008e2012-10-02 21:07:32 -0400227static const u32 nfs4_open_noattr_bitmap[3] = {
228 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400229 | FATTR4_WORD0_FILEID,
230};
231
David Quigleya09df2c2013-05-22 12:50:41 -0400232const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 FATTR4_WORD0_FILES_AVAIL
234 | FATTR4_WORD0_FILES_FREE
235 | FATTR4_WORD0_FILES_TOTAL,
236 FATTR4_WORD1_SPACE_AVAIL
237 | FATTR4_WORD1_SPACE_FREE
238 | FATTR4_WORD1_SPACE_TOTAL
239};
240
David Quigleya09df2c2013-05-22 12:50:41 -0400241const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 FATTR4_WORD0_MAXLINK
243 | FATTR4_WORD0_MAXNAME,
244 0
245};
246
Fred Isamandae100c2011-07-30 20:52:37 -0400247const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 | FATTR4_WORD0_MAXREAD
249 | FATTR4_WORD0_MAXWRITE
250 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700251 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400252 | FATTR4_WORD1_FS_LAYOUT_TYPES,
253 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800254 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255};
256
David Quigleya09df2c2013-05-22 12:50:41 -0400257const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400258 FATTR4_WORD0_TYPE
259 | FATTR4_WORD0_CHANGE
260 | FATTR4_WORD0_SIZE
261 | FATTR4_WORD0_FSID
262 | FATTR4_WORD0_FILEID
263 | FATTR4_WORD0_FS_LOCATIONS,
264 FATTR4_WORD1_MODE
265 | FATTR4_WORD1_NUMLINKS
266 | FATTR4_WORD1_OWNER
267 | FATTR4_WORD1_OWNER_GROUP
268 | FATTR4_WORD1_RAWDEV
269 | FATTR4_WORD1_SPACE_USED
270 | FATTR4_WORD1_TIME_ACCESS
271 | FATTR4_WORD1_TIME_METADATA
272 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400273 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400274};
275
Al Virobc4785c2006-10-19 23:28:51 -0700276static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct nfs4_readdir_arg *readdir)
278{
Al Viro0dbb4c62006-10-19 23:28:49 -0700279 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000282 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
284 return;
285 }
286
287 readdir->cookie = 0;
288 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
289 if (cookie == 2)
290 return;
291
292 /*
293 * NFSv4 servers do not return entries for '.' and '..'
294 * Therefore, we fake these entries here. We let '.'
295 * have cookie 0 and '..' have cookie 1. Note that
296 * when talking to the server, we always send cookie 0
297 * instead of 1 or 2.
298 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800299 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 if (cookie == 0) {
302 *p++ = xdr_one; /* next */
303 *p++ = xdr_zero; /* cookie, first word */
304 *p++ = xdr_one; /* cookie, second word */
305 *p++ = xdr_one; /* entry len */
306 memcpy(p, ".\0\0\0", 4); /* entry */
307 p++;
308 *p++ = xdr_one; /* bitmap length */
309 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
310 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000311 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 *p++ = xdr_one; /* next */
315 *p++ = xdr_zero; /* cookie, first word */
316 *p++ = xdr_two; /* cookie, second word */
317 *p++ = xdr_two; /* entry len */
318 memcpy(p, "..\0\0", 4); /* entry */
319 p++;
320 *p++ = xdr_one; /* bitmap length */
321 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
322 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000323 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 readdir->pgbase = (char *)p - (char *)start;
326 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800327 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Trond Myklebust26d36302016-09-22 13:39:05 -0400330static void nfs4_test_and_free_stateid(struct nfs_server *server,
331 nfs4_stateid *stateid,
332 struct rpc_cred *cred)
333{
334 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
335
336 ops->test_and_free_expired(server, stateid, cred);
337}
338
339static void __nfs4_free_revoked_stateid(struct nfs_server *server,
340 nfs4_stateid *stateid,
341 struct rpc_cred *cred)
342{
343 stateid->type = NFS4_REVOKED_STATEID_TYPE;
344 nfs4_test_and_free_stateid(server, stateid, cred);
345}
346
347static void nfs4_free_revoked_stateid(struct nfs_server *server,
348 const nfs4_stateid *stateid,
349 struct rpc_cred *cred)
350{
351 nfs4_stateid tmp;
352
353 nfs4_stateid_copy(&tmp, stateid);
354 __nfs4_free_revoked_stateid(server, &tmp, cred);
355}
356
NeilBrown8478eaa2014-09-18 16:09:27 +1000357static long nfs4_update_delay(long *timeout)
358{
359 long ret;
360 if (!timeout)
361 return NFS4_POLL_RETRY_MAX;
362 if (*timeout <= 0)
363 *timeout = NFS4_POLL_RETRY_MIN;
364 if (*timeout > NFS4_POLL_RETRY_MAX)
365 *timeout = NFS4_POLL_RETRY_MAX;
366 ret = *timeout;
367 *timeout <<= 1;
368 return ret;
369}
370
Trond Myklebust65de8722008-12-23 15:21:44 -0500371static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
372{
373 int res = 0;
374
375 might_sleep();
376
NeilBrown8478eaa2014-09-18 16:09:27 +1000377 freezable_schedule_timeout_killable_unsafe(
378 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500379 if (fatal_signal_pending(current))
380 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500381 return res;
382}
383
384/* This is the error handling routine for processes that are allowed
385 * to sleep.
386 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400387static int nfs4_do_handle_exception(struct nfs_server *server,
388 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500389{
390 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500391 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400392 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500393 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500394 int ret = errorcode;
395
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400396 exception->delay = 0;
397 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400399
400 if (stateid == NULL && state != NULL)
401 stateid = &state->stateid;
402
Trond Myklebust65de8722008-12-23 15:21:44 -0500403 switch(errorcode) {
404 case 0:
405 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400406 case -NFS4ERR_DELEG_REVOKED:
407 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400408 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400409 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400410 if (inode != NULL && stateid != NULL) {
411 nfs_inode_find_state_and_recover(inode,
412 stateid);
413 goto wait_on_recovery;
414 }
415 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400416 if (inode) {
417 int err;
418
419 err = nfs_async_inode_return_delegation(inode,
420 stateid);
421 if (err == 0)
422 goto wait_on_recovery;
423 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
424 exception->retry = 1;
425 break;
426 }
427 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500428 if (state == NULL)
429 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400430 ret = nfs4_schedule_stateid_recovery(server, state);
431 if (ret < 0)
432 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500433 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500435 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500436 nfs4_schedule_lease_recovery(clp);
437 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400438 case -NFS4ERR_MOVED:
439 ret = nfs4_schedule_migration_recovery(server);
440 if (ret < 0)
441 break;
442 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400443 case -NFS4ERR_LEASE_MOVED:
444 nfs4_schedule_lease_moved_recovery(clp);
445 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500446#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400447 case -NFS4ERR_BADSESSION:
448 case -NFS4ERR_BADSLOT:
449 case -NFS4ERR_BAD_HIGH_SLOT:
450 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
451 case -NFS4ERR_DEADSESSION:
452 case -NFS4ERR_SEQ_FALSE_RETRY:
453 case -NFS4ERR_SEQ_MISORDERED:
454 dprintk("%s ERROR: %d Reset session\n", __func__,
455 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400456 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400457 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500458#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500459 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500460 if (exception->timeout > HZ) {
461 /* We have retried a decent amount, time to
462 * fail
463 */
464 ret = -EBUSY;
465 break;
466 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400468 nfs_inc_server_stats(server, NFSIOS_DELAY);
469 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400470 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400471 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400472 exception->delay = 1;
473 return 0;
474
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400475 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500476 case -NFS4ERR_OLD_STATEID:
477 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800478 break;
479 case -NFS4ERR_BADOWNER:
480 /* The following works around a Linux server bug! */
481 case -NFS4ERR_BADNAME:
482 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
483 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
484 exception->retry = 1;
485 printk(KERN_WARNING "NFS: v4 server %s "
486 "does not accept raw "
487 "uid/gids. "
488 "Reenabling the idmapper.\n",
489 server->nfs_client->cl_hostname);
490 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500491 }
492 /* We failed to handle the error */
493 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500494wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400495 exception->recovering = 1;
496 return 0;
497}
498
499/* This is the error handling routine for processes that are allowed
500 * to sleep.
501 */
502int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
503{
504 struct nfs_client *clp = server->nfs_client;
505 int ret;
506
507 ret = nfs4_do_handle_exception(server, errorcode, exception);
508 if (exception->delay) {
509 ret = nfs4_delay(server->client, &exception->timeout);
510 goto out_retry;
511 }
512 if (exception->recovering) {
513 ret = nfs4_wait_clnt_recover(clp);
514 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
515 return -EIO;
516 goto out_retry;
517 }
518 return ret;
519out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500520 if (ret == 0)
521 exception->retry = 1;
522 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500523}
524
Trond Myklebust037fc982015-09-20 15:51:00 -0400525static int
526nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
527 int errorcode, struct nfs4_exception *exception)
528{
529 struct nfs_client *clp = server->nfs_client;
530 int ret;
531
532 ret = nfs4_do_handle_exception(server, errorcode, exception);
533 if (exception->delay) {
534 rpc_delay(task, nfs4_update_delay(&exception->timeout));
535 goto out_retry;
536 }
537 if (exception->recovering) {
538 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
539 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
540 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
541 goto out_retry;
542 }
543 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
544 ret = -EIO;
545 return ret;
546out_retry:
547 if (ret == 0)
548 exception->retry = 1;
549 return ret;
550}
551
552static int
553nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
554 struct nfs4_state *state, long *timeout)
555{
556 struct nfs4_exception exception = {
557 .state = state,
558 };
559
560 if (task->tk_status >= 0)
561 return 0;
562 if (timeout)
563 exception.timeout = *timeout;
564 task->tk_status = nfs4_async_handle_exception(task, server,
565 task->tk_status,
566 &exception);
567 if (exception.delay && timeout)
568 *timeout = exception.timeout;
569 if (exception.retry)
570 return -EAGAIN;
571 return 0;
572}
573
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400574/*
575 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
576 * or 'false' otherwise.
577 */
578static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
579{
580 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
581
582 if (flavor == RPC_AUTH_GSS_KRB5I ||
583 flavor == RPC_AUTH_GSS_KRB5P)
584 return true;
585
586 return false;
587}
Trond Myklebust65de8722008-12-23 15:21:44 -0500588
Trond Myklebust452e9352010-07-31 14:29:06 -0400589static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 spin_lock(&clp->cl_lock);
592 if (time_before(clp->cl_last_renewal,timestamp))
593 clp->cl_last_renewal = timestamp;
594 spin_unlock(&clp->cl_lock);
595}
596
Trond Myklebust452e9352010-07-31 14:29:06 -0400597static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
598{
Trond Myklebustbe824162015-07-05 14:50:46 -0400599 struct nfs_client *clp = server->nfs_client;
600
601 if (!nfs4_has_session(clp))
602 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400603}
604
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400605struct nfs4_call_sync_data {
606 const struct nfs_server *seq_server;
607 struct nfs4_sequence_args *seq_args;
608 struct nfs4_sequence_res *seq_res;
609};
610
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800611void nfs4_init_sequence(struct nfs4_sequence_args *args,
612 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400613{
614 args->sa_slot = NULL;
615 args->sa_cache_this = cache_reply;
616 args->sa_privileged = 0;
617
618 res->sr_slot = NULL;
619}
620
621static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
622{
623 args->sa_privileged = 1;
624}
625
Peng Taocb04ad22014-06-11 05:24:15 +0800626int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
627 struct nfs4_sequence_args *args,
628 struct nfs4_sequence_res *res,
629 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400630{
Chuck Lever3bd23842013-08-09 12:49:19 -0400631 struct nfs4_slot *slot;
632
633 /* slot already allocated? */
634 if (res->sr_slot != NULL)
635 goto out_start;
636
637 spin_lock(&tbl->slot_tbl_lock);
638 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
639 goto out_sleep;
640
641 slot = nfs4_alloc_slot(tbl);
642 if (IS_ERR(slot)) {
643 if (slot == ERR_PTR(-ENOMEM))
644 task->tk_timeout = HZ >> 2;
645 goto out_sleep;
646 }
647 spin_unlock(&tbl->slot_tbl_lock);
648
Trond Myklebust0a014a42016-09-22 13:38:51 -0400649 slot->privileged = args->sa_privileged ? 1 : 0;
Chuck Lever3bd23842013-08-09 12:49:19 -0400650 args->sa_slot = slot;
651 res->sr_slot = slot;
652
653out_start:
654 rpc_call_start(task);
655 return 0;
656
657out_sleep:
658 if (args->sa_privileged)
659 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
660 NULL, RPC_PRIORITY_PRIVILEGED);
661 else
662 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
663 spin_unlock(&tbl->slot_tbl_lock);
664 return -EAGAIN;
665}
Peng Taocb04ad22014-06-11 05:24:15 +0800666EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400667
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400668static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400669{
670 struct nfs4_slot *slot = res->sr_slot;
671 struct nfs4_slot_table *tbl;
672
Chuck Lever3bd23842013-08-09 12:49:19 -0400673 tbl = slot->table;
674 spin_lock(&tbl->slot_tbl_lock);
675 if (!nfs41_wake_and_assign_slot(tbl, slot))
676 nfs4_free_slot(tbl, slot);
677 spin_unlock(&tbl->slot_tbl_lock);
678
679 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400680}
681
682static int nfs40_sequence_done(struct rpc_task *task,
683 struct nfs4_sequence_res *res)
684{
685 if (res->sr_slot != NULL)
686 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400687 return 1;
688}
689
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400690#if defined(CONFIG_NFS_V4_1)
691
Trond Myklebustd185a332010-06-16 09:52:25 -0400692static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400693{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500694 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400695 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500697 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400698
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500700 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500701
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400702 /* Bump the slot sequence number */
703 if (slot->seq_done)
704 slot->seq_nr++;
705 slot->seq_done = 0;
706
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500707 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500708 /* Be nice to the server: try to ensure that the last transmitted
709 * value for highest_user_slotid <= target_highest_slotid
710 */
711 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
712 send_new_highest_used_slotid = true;
713
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500714 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500715 send_new_highest_used_slotid = false;
716 goto out_unlock;
717 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500718 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500719
720 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
721 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500722out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500723 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400724 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500725 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400726 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400727 if (waitqueue_active(&tbl->slot_waitq))
728 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400729}
730
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400731static int nfs41_sequence_process(struct rpc_task *task,
732 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400733{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500735 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400736 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500737 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500738 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400739
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500740 if (slot == NULL)
741 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400742 /* don't increment the sequence number if the task wasn't sent */
743 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400744 goto out;
745
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500746 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500747
Trond Myklebustac20d162012-12-15 15:36:07 -0500748 if (slot->interrupted) {
749 slot->interrupted = 0;
750 interrupted = true;
751 }
752
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400753 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500754 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400755 switch (res->sr_status) {
756 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400757 /* If previous op on slot was interrupted and we reused
758 * the seq# and got a reply from the cache, then retry
759 */
760 if (task->tk_status == -EREMOTEIO && interrupted) {
761 ++slot->seq_nr;
762 goto retry_nowait;
763 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400764 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400765 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500766 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500767 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500768 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400769 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
770 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500771 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500773 case 1:
774 /*
775 * sr_status remains 1 if an RPC level error occurred.
776 * The server may or may not have processed the sequence
777 * operation..
778 * Mark the slot as having hosted an interrupted RPC call.
779 */
780 slot->interrupted = 1;
781 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400782 case -NFS4ERR_DELAY:
783 /* The server detected a resend of the RPC call and
784 * returned NFS4ERR_DELAY as per Section 2.10.6.2
785 * of RFC5661.
786 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500787 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400788 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500789 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500790 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400791 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500792 case -NFS4ERR_BADSLOT:
793 /*
794 * The slot id we used was probably retired. Try again
795 * using a different slot id.
796 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500797 goto retry_nowait;
798 case -NFS4ERR_SEQ_MISORDERED:
799 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500800 * Was the last operation on this sequence interrupted?
801 * If so, retry after bumping the sequence number.
802 */
803 if (interrupted) {
804 ++slot->seq_nr;
805 goto retry_nowait;
806 }
807 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500808 * Could this slot have been previously retired?
809 * If so, then the server may be expecting seq_nr = 1!
810 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500811 if (slot->seq_nr != 1) {
812 slot->seq_nr = 1;
813 goto retry_nowait;
814 }
815 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500816 case -NFS4ERR_SEQ_FALSE_RETRY:
817 ++slot->seq_nr;
818 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500819 case -NFS4ERR_DEADSESSION:
820 case -NFS4ERR_BADSESSION:
821 nfs4_schedule_session_recovery(session, res->sr_status);
822 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 default:
824 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400825 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400826 }
827out:
828 /* The session may be reset by one of the error handlers. */
829 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500830out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500831 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500832retry_nowait:
833 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400834 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500835 task->tk_status = 0;
836 ret = 0;
837 }
838 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400839out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400840 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 goto out;
842 rpc_delay(task, NFS4_POLL_RETRY_MAX);
843 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845
846int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
847{
848 if (!nfs41_sequence_process(task, res))
849 return 0;
850 if (res->sr_slot != NULL)
851 nfs41_sequence_free_slot(res);
852 return 1;
853
854}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500855EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400856
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400857static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
858{
859 if (res->sr_slot == NULL)
860 return 1;
861 if (res->sr_slot->table->session != NULL)
862 return nfs41_sequence_process(task, res);
863 return nfs40_sequence_done(task, res);
864}
865
866static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
867{
868 if (res->sr_slot != NULL) {
869 if (res->sr_slot->table->session != NULL)
870 nfs41_sequence_free_slot(res);
871 else
872 nfs40_sequence_free_slot(res);
873 }
874}
875
Peng Tao2c4b1312014-06-11 05:24:16 +0800876int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400877{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500878 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400879 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400880 if (!res->sr_slot->table->session)
881 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400882 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400883}
Peng Tao2c4b1312014-06-11 05:24:16 +0800884EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400885
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000886int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887 struct nfs4_sequence_args *args,
888 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400889 struct rpc_task *task)
890{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400891 struct nfs4_slot *slot;
892 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400893
894 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400896 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400897 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 tbl = &session->fc_slot_table;
900
Trond Myklebust69d206b2012-11-22 13:21:02 -0500901 task->tk_timeout = 0;
902
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400903 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400904 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400905 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500906 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500907 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400908 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400909 }
910
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500911 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500912 if (IS_ERR(slot)) {
913 /* If out of memory, try again in 1/4 second */
914 if (slot == ERR_PTR(-ENOMEM))
915 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400916 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400917 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400918 }
919 spin_unlock(&tbl->slot_tbl_lock);
920
Trond Myklebust0a014a42016-09-22 13:38:51 -0400921 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500922 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400923
Chuck Levere8d92382013-08-09 12:47:51 -0400924 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500925 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400926
Benny Halevydfb4f3092010-09-24 09:17:01 -0400927 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500928 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400929 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400930 /*
931 * sr_status is only set in decode_sequence, and so will remain
932 * set to 1 if an rpc level failure occurs.
933 */
934 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400935 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400936out_success:
937 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400939out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400940 /* Privileged tasks are queued with top priority */
941 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400942 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
943 NULL, RPC_PRIORITY_PRIVILEGED);
944 else
945 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400946 spin_unlock(&tbl->slot_tbl_lock);
947 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400948}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000949EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400950
Chuck Lever5a580e02013-08-09 12:48:09 -0400951static int nfs4_setup_sequence(const struct nfs_server *server,
952 struct nfs4_sequence_args *args,
953 struct nfs4_sequence_res *res,
954 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400956 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957 int ret = 0;
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800960 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
961 args, res, task);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400962
Chuck Levere8d92382013-08-09 12:47:51 -0400963 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400964 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400965 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400966
Trond Myklebust9d12b212012-01-17 22:04:25 -0500967 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400968
Andy Adamsonce5039c2009-04-01 09:22:13 -0400969 dprintk("<-- %s status=%d\n", __func__, ret);
970 return ret;
971}
972
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
974{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400975 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400976 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400977
Trond Myklebust035168ab2010-06-16 09:52:26 -0400978 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
979
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400980 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400981}
982
Andy Adamson69ab40c2009-04-01 09:22:19 -0400983static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
984{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400985 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400986
Trond Myklebust14516c32010-07-31 14:29:06 -0400987 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400988}
989
Trond Myklebust17280172012-03-11 13:11:00 -0400990static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400991 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400992 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400993};
994
Chuck Lever3bd23842013-08-09 12:49:19 -0400995#else /* !CONFIG_NFS_V4_1 */
996
Chuck Lever5a580e02013-08-09 12:48:09 -0400997static int nfs4_setup_sequence(const struct nfs_server *server,
998 struct nfs4_sequence_args *args,
999 struct nfs4_sequence_res *res,
1000 struct rpc_task *task)
1001{
Peng Taocb04ad22014-06-11 05:24:15 +08001002 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
1003 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -04001004}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001005
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001006static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
1007{
1008 return nfs40_sequence_done(task, res);
1009}
1010
1011static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
1012{
1013 if (res->sr_slot != NULL)
1014 nfs40_sequence_free_slot(res);
1015}
1016
Peng Tao2c4b1312014-06-11 05:24:16 +08001017int nfs4_sequence_done(struct rpc_task *task,
1018 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001019{
Chuck Lever3bd23842013-08-09 12:49:19 -04001020 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001021}
Peng Tao2c4b1312014-06-11 05:24:16 +08001022EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001023
1024#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001025
Chuck Lever9915ea72013-08-09 12:48:27 -04001026static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1027{
1028 struct nfs4_call_sync_data *data = calldata;
1029 nfs4_setup_sequence(data->seq_server,
1030 data->seq_args, data->seq_res, task);
1031}
1032
1033static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1034{
1035 struct nfs4_call_sync_data *data = calldata;
1036 nfs4_sequence_done(task, data->seq_res);
1037}
1038
1039static const struct rpc_call_ops nfs40_call_sync_ops = {
1040 .rpc_call_prepare = nfs40_call_sync_prepare,
1041 .rpc_call_done = nfs40_call_sync_done,
1042};
1043
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001045 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001047 struct nfs4_sequence_args *args,
1048 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001049{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001050 int ret;
1051 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001052 struct nfs_client *clp = server->nfs_client;
1053 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001054 .seq_server = server,
1055 .seq_args = args,
1056 .seq_res = res,
1057 };
1058 struct rpc_task_setup task_setup = {
1059 .rpc_client = clnt,
1060 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001061 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 .callback_data = &data
1063 };
1064
1065 task = rpc_run_task(&task_setup);
1066 if (IS_ERR(task))
1067 ret = PTR_ERR(task);
1068 else {
1069 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001070 rpc_put_task(task);
1071 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001072 return ret;
1073}
1074
Bryan Schumaker7c513052011-03-24 17:12:24 +00001075int nfs4_call_sync(struct rpc_clnt *clnt,
1076 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001077 struct rpc_message *msg,
1078 struct nfs4_sequence_args *args,
1079 struct nfs4_sequence_res *res,
1080 int cache_reply)
1081{
Chuck Levera9c92d62013-08-09 12:48:18 -04001082 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001083 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
1087{
1088 struct nfs_inode *nfsi = NFS_I(dir);
1089
1090 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001091 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001092 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001094 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -05001095 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001096 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 spin_unlock(&dir->i_lock);
1098}
1099
1100struct nfs4_opendata {
1101 struct kref kref;
1102 struct nfs_openargs o_arg;
1103 struct nfs_openres o_res;
1104 struct nfs_open_confirmargs c_arg;
1105 struct nfs_open_confirmres c_res;
Trond 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);
Trond Myklebust536585c2016-11-10 15:40:34 -05001227 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001228 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1229 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001230 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1231 * will return permission denied for all bits until close */
1232 if (!(flags & O_EXCL)) {
1233 /* ask server to check for all possible rights as results
1234 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001235 switch (p->o_arg.claim) {
1236 default:
1237 break;
1238 case NFS4_OPEN_CLAIM_NULL:
1239 case NFS4_OPEN_CLAIM_FH:
1240 p->o_arg.access = NFS4_ACCESS_READ |
1241 NFS4_ACCESS_MODIFY |
1242 NFS4_ACCESS_EXTEND |
1243 NFS4_ACCESS_EXECUTE;
1244 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001245 }
David Howells7539bba2006-08-22 20:06:09 -04001246 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001247 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1248 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001249 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001251 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001252 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001253 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001254 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 case NFS4_OPEN_CLAIM_NULL:
1256 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1257 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1258 p->o_arg.fh = NFS_FH(dir);
1259 break;
1260 case NFS4_OPEN_CLAIM_PREVIOUS:
1261 case NFS4_OPEN_CLAIM_FH:
1262 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1263 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001264 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001265 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001266 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001267 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001268
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001269 p->o_arg.u.attrs = &p->attrs;
1270 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001271
1272 verf[0] = jiffies;
1273 verf[1] = current->pid;
1274 memcpy(p->o_arg.u.verifier.data, verf,
1275 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277 p->c_arg.fh = &p->o_res.fh;
1278 p->c_arg.stateid = &p->o_res.stateid;
1279 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001280 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001282 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001283
1284err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001285 nfs4_label_free(p->a_label);
1286err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001287 nfs4_label_free(p->f_label);
1288err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001289 kfree(p);
1290err:
1291 dput(parent);
1292 return NULL;
1293}
1294
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001296{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001297 struct nfs4_opendata *p = container_of(kref,
1298 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001299 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300
1301 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001302 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303 if (p->state != NULL)
1304 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001306
Kinglong Meea49c2692015-07-27 15:31:38 +08001307 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001308 nfs4_label_free(p->f_label);
1309
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001310 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 dput(p->dentry);
1312 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001313 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001314 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001315 kfree(p);
1316}
1317
1318static void nfs4_opendata_put(struct nfs4_opendata *p)
1319{
1320 if (p != NULL)
1321 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001322}
1323
Trond Myklebust06f814a2006-01-03 09:55:07 +01001324static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1325{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001326 int ret;
1327
Trond Myklebust06f814a2006-01-03 09:55:07 +01001328 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001329 return ret;
1330}
1331
Trond Myklebust24311f82015-09-20 10:50:17 -04001332static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1333 fmode_t fmode)
1334{
1335 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1336 case FMODE_READ|FMODE_WRITE:
1337 return state->n_rdwr != 0;
1338 case FMODE_WRITE:
1339 return state->n_wronly != 0;
1340 case FMODE_READ:
1341 return state->n_rdonly != 0;
1342 }
1343 WARN_ON_ONCE(1);
1344 return false;
1345}
1346
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001347static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001348{
1349 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350
Trond Myklebust536e43d2012-01-17 22:04:26 -05001351 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352 goto out;
1353 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001354 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001355 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1356 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 break;
1358 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001359 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1360 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 break;
1362 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001363 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1364 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 return ret;
1368}
1369
Trond Myklebust2a606182015-08-19 22:30:00 -05001370static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1371 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001373 if (delegation == NULL)
1374 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001375 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001377 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1378 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001379 switch (claim) {
1380 case NFS4_OPEN_CLAIM_NULL:
1381 case NFS4_OPEN_CLAIM_FH:
1382 break;
1383 case NFS4_OPEN_CLAIM_PREVIOUS:
1384 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1385 break;
1386 default:
1387 return 0;
1388 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001389 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390 return 1;
1391}
1392
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001393static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001394{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001396 case FMODE_WRITE:
1397 state->n_wronly++;
1398 break;
1399 case FMODE_READ:
1400 state->n_rdonly++;
1401 break;
1402 case FMODE_READ|FMODE_WRITE:
1403 state->n_rdwr++;
1404 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001405 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001406}
1407
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001408#ifdef CONFIG_NFS_V4_1
1409static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1410{
1411 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1412 return true;
1413 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1414 return true;
1415 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1416 return true;
1417 return false;
1418}
1419#endif /* CONFIG_NFS_V4_1 */
1420
Trond Myklebust4f14c192014-02-12 19:15:06 -05001421static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001422{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001423 struct nfs_client *clp = state->owner->so_server->nfs_client;
1424 bool need_recover = false;
1425
1426 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1427 need_recover = true;
1428 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1429 need_recover = true;
1430 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1431 need_recover = true;
1432 if (need_recover)
1433 nfs4_state_mark_reclaim_nograce(clp, state);
1434}
1435
Trond Myklebuste999e802014-02-10 18:20:47 -05001436static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001437 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001438{
1439 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1440 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001441 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001442 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001443 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001444 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001445 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001446 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1447 return true;
1448 return false;
1449}
1450
Trond Myklebustf95549c2015-01-23 18:06:09 -05001451static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1452{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001453 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1454 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455 if (state->n_wronly)
1456 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1457 if (state->n_rdonly)
1458 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1459 if (state->n_rdwr)
1460 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001461 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001462}
1463
Trond Myklebust226056c2014-02-11 10:41:07 -05001464static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1465 nfs4_stateid *stateid, fmode_t fmode)
1466{
1467 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1468 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1469 case FMODE_WRITE:
1470 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1471 break;
1472 case FMODE_READ:
1473 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1474 break;
1475 case 0:
1476 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1477 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1478 clear_bit(NFS_OPEN_STATE, &state->flags);
1479 }
1480 if (stateid == NULL)
1481 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001482 /* Handle OPEN+OPEN_DOWNGRADE races */
1483 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1484 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001485 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001486 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001487 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001488 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001489 nfs4_stateid_copy(&state->stateid, stateid);
1490 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001491}
1492
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001493static void nfs_clear_open_stateid(struct nfs4_state *state,
1494 nfs4_stateid *arg_stateid,
1495 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001496{
1497 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001498 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1499 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1500 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001501 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001502 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1503 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001504}
1505
Trond Myklebust1393d962016-09-22 13:39:13 -04001506static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1507 const nfs4_stateid *stateid, fmode_t fmode,
1508 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001509{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001510 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001511 case FMODE_READ:
1512 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1513 break;
1514 case FMODE_WRITE:
1515 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1516 break;
1517 case FMODE_READ|FMODE_WRITE:
1518 set_bit(NFS_O_RDWR_STATE, &state->flags);
1519 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001520 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001521 return;
1522 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1523 nfs4_stateid_copy(&state->stateid, stateid);
1524 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001525}
1526
Trond Myklebust1393d962016-09-22 13:39:13 -04001527static void __update_open_stateid(struct nfs4_state *state,
1528 const nfs4_stateid *open_stateid,
1529 const nfs4_stateid *deleg_stateid,
1530 fmode_t fmode,
1531 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001533 /*
1534 * Protect the call to nfs4_state_set_mode_locked and
1535 * serialise the stateid update
1536 */
Andrew Elble361cad32015-12-02 09:20:57 -05001537 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001538 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001539 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001540 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001541 set_bit(NFS_DELEGATED_STATE, &state->flags);
1542 }
1543 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001544 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001545 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001546 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001547 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Trond Myklebust1393d962016-09-22 13:39:13 -04001550static int update_open_stateid(struct nfs4_state *state,
1551 const nfs4_stateid *open_stateid,
1552 const nfs4_stateid *delegation,
1553 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001554{
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 struct nfs_server *server = NFS_SERVER(state->inode);
1556 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001557 struct nfs_inode *nfsi = NFS_I(state->inode);
1558 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001559 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001560 int ret = 0;
1561
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001562 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001563
1564 rcu_read_lock();
1565 deleg_cur = rcu_dereference(nfsi->delegation);
1566 if (deleg_cur == NULL)
1567 goto no_delegation;
1568
1569 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001570 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001571 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001572 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001573 goto no_delegation_unlock;
1574
1575 if (delegation == NULL)
1576 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001577 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001578 goto no_delegation_unlock;
1579
Trond Myklebustb7391f42008-12-23 15:21:52 -05001580 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001581 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1582 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001583 ret = 1;
1584no_delegation_unlock:
1585 spin_unlock(&deleg_cur->lock);
1586no_delegation:
1587 rcu_read_unlock();
1588
1589 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001590 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001591 ret = 1;
1592 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001593 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001594 nfs4_schedule_state_manager(clp);
1595 if (freeme.type != 0)
1596 nfs4_test_and_free_stateid(server, &freeme,
1597 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001598
1599 return ret;
1600}
1601
Trond Myklebust39071e62015-01-24 15:07:56 -05001602static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1603 const nfs4_stateid *stateid)
1604{
1605 struct nfs4_state *state = lsp->ls_state;
1606 bool ret = false;
1607
1608 spin_lock(&state->state_lock);
1609 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1610 goto out_noupdate;
1611 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1612 goto out_noupdate;
1613 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1614 ret = true;
1615out_noupdate:
1616 spin_unlock(&state->state_lock);
1617 return ret;
1618}
Trond Myklebust34310432008-12-23 15:21:38 -05001619
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001620static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001621{
1622 struct nfs_delegation *delegation;
1623
1624 rcu_read_lock();
1625 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001626 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001627 rcu_read_unlock();
1628 return;
1629 }
1630 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001631 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001632}
1633
Trond Myklebust6ee41262007-07-08 14:11:36 -04001634static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635{
1636 struct nfs4_state *state = opendata->state;
1637 struct nfs_inode *nfsi = NFS_I(state->inode);
1638 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001639 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001640 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001641 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001642 nfs4_stateid stateid;
1643 int ret = -EAGAIN;
1644
Trond Myklebustaac00a82007-07-05 19:02:21 -04001645 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001646 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001648 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001649 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001650 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001651 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001652 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001653 rcu_read_lock();
1654 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001655 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001656 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001657 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001658 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001660 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001661 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001662 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001663 if (!opendata->is_recover) {
1664 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1665 if (ret != 0)
1666 goto out;
1667 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001669
1670 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001671 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001672 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674out:
1675 return ERR_PTR(ret);
1676out_return_state:
1677 atomic_inc(&state->count);
1678 return state;
1679}
1680
Andy Adamsone23008e2012-10-02 21:07:32 -04001681static void
1682nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1683{
1684 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1685 struct nfs_delegation *delegation;
1686 int delegation_flags = 0;
1687
1688 rcu_read_lock();
1689 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1690 if (delegation)
1691 delegation_flags = delegation->flags;
1692 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001693 switch (data->o_arg.claim) {
1694 default:
1695 break;
1696 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1697 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001698 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1699 "returning a delegation for "
1700 "OPEN(CLAIM_DELEGATE_CUR)\n",
1701 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001702 return;
1703 }
1704 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001705 nfs_inode_set_delegation(state->inode,
1706 data->owner->so_cred,
1707 &data->o_res);
1708 else
1709 nfs_inode_reclaim_delegation(state->inode,
1710 data->owner->so_cred,
1711 &data->o_res);
1712}
1713
1714/*
1715 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1716 * and update the nfs4_state.
1717 */
1718static struct nfs4_state *
1719_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1720{
1721 struct inode *inode = data->state->inode;
1722 struct nfs4_state *state = data->state;
1723 int ret;
1724
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001725 if (!data->rpc_done) {
1726 if (data->rpc_status) {
1727 ret = data->rpc_status;
1728 goto err;
1729 }
1730 /* cached opens have already been processed */
1731 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001732 }
1733
Andy Adamsone23008e2012-10-02 21:07:32 -04001734 ret = nfs_refresh_inode(inode, &data->f_attr);
1735 if (ret)
1736 goto err;
1737
1738 if (data->o_res.delegation_type != 0)
1739 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001740update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001741 update_open_stateid(state, &data->o_res.stateid, NULL,
1742 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001743 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001744
1745 return state;
1746err:
1747 return ERR_PTR(ret);
1748
1749}
1750
1751static struct nfs4_state *
1752_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753{
1754 struct inode *inode;
1755 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757
Trond Myklebustaac00a82007-07-05 19:02:21 -04001758 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001759 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001760 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001761 goto out;
1762 }
1763
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001765 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001766 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001767 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001769 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001770 goto err;
1771 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 state = nfs4_get_open_state(inode, data->owner);
1773 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001774 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001775 if (data->o_res.delegation_type != 0)
1776 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001777 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001778 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001779 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001780out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001781 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001783err_put_inode:
1784 iput(inode);
1785err:
1786 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001787}
1788
Andy Adamsone23008e2012-10-02 21:07:32 -04001789static struct nfs4_state *
1790nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1791{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001792 struct nfs4_state *ret;
1793
Andy Adamsone23008e2012-10-02 21:07:32 -04001794 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001795 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1796 else
1797 ret = _nfs4_opendata_to_nfs4_state(data);
1798 nfs4_sequence_free_slot(&data->o_res.seq_res);
1799 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001800}
1801
Trond Myklebust864472e2006-01-03 09:55:15 +01001802static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1803{
1804 struct nfs_inode *nfsi = NFS_I(state->inode);
1805 struct nfs_open_context *ctx;
1806
1807 spin_lock(&state->inode->i_lock);
1808 list_for_each_entry(ctx, &nfsi->open_files, list) {
1809 if (ctx->state != state)
1810 continue;
1811 get_nfs_open_context(ctx);
1812 spin_unlock(&state->inode->i_lock);
1813 return ctx;
1814 }
1815 spin_unlock(&state->inode->i_lock);
1816 return ERR_PTR(-ENOENT);
1817}
1818
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001819static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1820 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001821{
1822 struct nfs4_opendata *opendata;
1823
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001824 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001825 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001826 if (opendata == NULL)
1827 return ERR_PTR(-ENOMEM);
1828 opendata->state = state;
1829 atomic_inc(&state->count);
1830 return opendata;
1831}
1832
Trond Myklebust24311f82015-09-20 10:50:17 -04001833static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1834 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001835{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001836 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001837 int ret;
1838
Trond Myklebust24311f82015-09-20 10:50:17 -04001839 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001840 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001841 opendata->o_arg.open_flags = 0;
1842 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001843 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1844 NFS_SB(opendata->dentry->d_sb),
1845 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001846 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1847 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1848 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001849 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001850 if (ret != 0)
1851 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001852 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001853 if (IS_ERR(newstate))
1854 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001855 if (newstate != opendata->state)
1856 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001857 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001858 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001859}
1860
1861static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1862{
Trond Myklebust864472e2006-01-03 09:55:15 +01001863 int ret;
1864
Trond Myklebust4f14c192014-02-12 19:15:06 -05001865 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1866 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1867 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1868 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001869 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001870 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001871 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001872 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001873 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1874 if (ret != 0)
1875 return ret;
1876 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1877 if (ret != 0)
1878 return ret;
1879 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1880 if (ret != 0)
1881 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001882 /*
1883 * We may have performed cached opens for all three recoveries.
1884 * Check if we need to update the current stateid.
1885 */
1886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001887 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001888 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001889 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001890 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001891 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001892 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001893 return 0;
1894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/*
1897 * OPEN_RECLAIM:
1898 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001900static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001902 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001903 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001904 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int status;
1906
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001907 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1908 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001909 if (IS_ERR(opendata))
1910 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001911 rcu_read_lock();
1912 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001913 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001914 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001915 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001916 opendata->o_arg.u.delegation_type = delegation_type;
1917 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001918 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return status;
1920}
1921
Trond Myklebust539cd032007-06-05 11:46:42 -04001922static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
1924 struct nfs_server *server = NFS_SERVER(state->inode);
1925 struct nfs4_exception exception = { };
1926 int err;
1927 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001928 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001929 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001930 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1931 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001932 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001933 break;
1934 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } while (exception.retry);
1936 return err;
1937}
1938
Trond Myklebust864472e2006-01-03 09:55:15 +01001939static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1940{
1941 struct nfs_open_context *ctx;
1942 int ret;
1943
1944 ctx = nfs4_state_find_open_context(state);
1945 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001946 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001947 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001948 put_nfs_open_context(ctx);
1949 return ret;
1950}
1951
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001952static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001954 switch (err) {
1955 default:
1956 printk(KERN_ERR "NFS: %s: unhandled error "
1957 "%d.\n", __func__, err);
1958 case 0:
1959 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001960 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001961 case -ESTALE:
1962 break;
1963 case -NFS4ERR_BADSESSION:
1964 case -NFS4ERR_BADSLOT:
1965 case -NFS4ERR_BAD_HIGH_SLOT:
1966 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1967 case -NFS4ERR_DEADSESSION:
1968 set_bit(NFS_DELEGATED_STATE, &state->flags);
1969 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1970 return -EAGAIN;
1971 case -NFS4ERR_STALE_CLIENTID:
1972 case -NFS4ERR_STALE_STATEID:
1973 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001974 /* Don't recall a delegation if it was lost */
1975 nfs4_schedule_lease_recovery(server->nfs_client);
1976 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001977 case -NFS4ERR_MOVED:
1978 nfs4_schedule_migration_recovery(server);
1979 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001980 case -NFS4ERR_LEASE_MOVED:
1981 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1982 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001983 case -NFS4ERR_DELEG_REVOKED:
1984 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001985 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001986 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001987 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001988 nfs_inode_find_state_and_recover(state->inode,
1989 stateid);
1990 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001991 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001992 case -NFS4ERR_DELAY:
1993 case -NFS4ERR_GRACE:
1994 set_bit(NFS_DELEGATED_STATE, &state->flags);
1995 ssleep(1);
1996 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001997 case -ENOMEM:
1998 case -NFS4ERR_DENIED:
1999 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
2000 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return err;
2003}
2004
Trond Myklebust24311f82015-09-20 10:50:17 -04002005int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2006 struct nfs4_state *state, const nfs4_stateid *stateid,
2007 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002008{
2009 struct nfs_server *server = NFS_SERVER(state->inode);
2010 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002011 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002012
2013 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2014 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2015 if (IS_ERR(opendata))
2016 return PTR_ERR(opendata);
2017 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002018 write_seqlock(&state->seqlock);
2019 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2020 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002021 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2022 switch (type & (FMODE_READ|FMODE_WRITE)) {
2023 case FMODE_READ|FMODE_WRITE:
2024 case FMODE_WRITE:
2025 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2026 if (err)
2027 break;
2028 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2029 if (err)
2030 break;
2031 case FMODE_READ:
2032 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2033 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002034 nfs4_opendata_put(opendata);
2035 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2036}
2037
Chuck Leverbe05c862013-08-09 12:49:47 -04002038static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2039{
2040 struct nfs4_opendata *data = calldata;
2041
Peng Taocb04ad22014-06-11 05:24:15 +08002042 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
2043 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002044}
2045
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002046static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2047{
2048 struct nfs4_opendata *data = calldata;
2049
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002050 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002051
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002052 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002053 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002054 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002055 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002056 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002057 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002058 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002059}
2060
2061static void nfs4_open_confirm_release(void *calldata)
2062{
2063 struct nfs4_opendata *data = calldata;
2064 struct nfs4_state *state = NULL;
2065
2066 /* If this request hasn't been cancelled, do nothing */
2067 if (data->cancelled == 0)
2068 goto out_free;
2069 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002070 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002072 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002073 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002074 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002075out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002076 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002077}
2078
2079static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002080 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002081 .rpc_call_done = nfs4_open_confirm_done,
2082 .rpc_release = nfs4_open_confirm_release,
2083};
2084
2085/*
2086 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2087 */
2088static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2089{
David Howells2b0143b2015-03-17 22:25:59 +00002090 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002091 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002092 struct rpc_message msg = {
2093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2094 .rpc_argp = &data->c_arg,
2095 .rpc_resp = &data->c_res,
2096 .rpc_cred = data->owner->so_cred,
2097 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002098 struct rpc_task_setup task_setup_data = {
2099 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002100 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002101 .callback_ops = &nfs4_open_confirm_ops,
2102 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002103 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002104 .flags = RPC_TASK_ASYNC,
2105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 int status;
2107
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002108 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002109 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002110 data->rpc_done = 0;
2111 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002112 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002113 if (data->is_recover)
2114 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002115 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002116 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002117 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002118 status = nfs4_wait_for_completion_rpc_task(task);
2119 if (status != 0) {
2120 data->cancelled = 1;
2121 smp_wmb();
2122 } else
2123 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002124 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return status;
2126}
2127
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130 struct nfs4_opendata *data = calldata;
2131 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002132 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002133 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002134
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002135 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002136 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002137 /*
2138 * Check if we still need to send an OPEN call, or if we can use
2139 * a delegation instead.
2140 */
2141 if (data->state != NULL) {
2142 struct nfs_delegation *delegation;
2143
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002144 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002145 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002146 rcu_read_lock();
2147 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002148 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002149 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002150 rcu_read_unlock();
2151 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002152 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002153 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002154 switch (claim) {
2155 default:
2156 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002157 case NFS4_OPEN_CLAIM_PREVIOUS:
2158 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2159 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002160 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002161 case NFS4_OPEN_CLAIM_FH:
2162 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002163 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2164 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002165 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04002166 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002167 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002168 &data->o_res.seq_res,
2169 task) != 0)
2170 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002171
2172 /* Set the create mode (note dependency on the session type) */
2173 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2174 if (data->o_arg.open_flags & O_EXCL) {
2175 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2176 if (nfs4_has_persistent_session(clp))
2177 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2178 else if (clp->cl_mvops->minor_version > 0)
2179 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2180 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002181 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002182unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002183 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002184 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002185out_no_action:
2186 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002187out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002188 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002189}
2190
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002191static void nfs4_open_done(struct rpc_task *task, void *calldata)
2192{
2193 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002195 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002196
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002197 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002198 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002199
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002200 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002201 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2202 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002203 case S_IFREG:
2204 break;
2205 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002207 break;
2208 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002209 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002210 break;
2211 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002213 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002214 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002215 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002216 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2217 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002218 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002219 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002220}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002221
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002222static void nfs4_open_release(void *calldata)
2223{
2224 struct nfs4_opendata *data = calldata;
2225 struct nfs4_state *state = NULL;
2226
2227 /* If this request hasn't been cancelled, do nothing */
2228 if (data->cancelled == 0)
2229 goto out_free;
2230 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002231 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232 goto out_free;
2233 /* In case we need an open_confirm, no cleanup! */
2234 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2235 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002236 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002237 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002238 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002239out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002240 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002241}
2242
2243static const struct rpc_call_ops nfs4_open_ops = {
2244 .rpc_call_prepare = nfs4_open_prepare,
2245 .rpc_call_done = nfs4_open_done,
2246 .rpc_release = nfs4_open_release,
2247};
2248
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002249static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002250{
David Howells2b0143b2015-03-17 22:25:59 +00002251 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002252 struct nfs_server *server = NFS_SERVER(dir);
2253 struct nfs_openargs *o_arg = &data->o_arg;
2254 struct nfs_openres *o_res = &data->o_res;
2255 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002256 struct rpc_message msg = {
2257 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2258 .rpc_argp = o_arg,
2259 .rpc_resp = o_res,
2260 .rpc_cred = data->owner->so_cred,
2261 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002262 struct rpc_task_setup task_setup_data = {
2263 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002264 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002265 .callback_ops = &nfs4_open_ops,
2266 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002267 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002268 .flags = RPC_TASK_ASYNC,
2269 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002270 int status;
2271
Chuck Levera9c92d62013-08-09 12:48:18 -04002272 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002273 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002274 data->rpc_done = 0;
2275 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002276 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002277 data->is_recover = 0;
2278 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002279 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002280 data->is_recover = 1;
2281 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002282 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002283 if (IS_ERR(task))
2284 return PTR_ERR(task);
2285 status = nfs4_wait_for_completion_rpc_task(task);
2286 if (status != 0) {
2287 data->cancelled = 1;
2288 smp_wmb();
2289 } else
2290 status = data->rpc_status;
2291 rpc_put_task(task);
2292
2293 return status;
2294}
2295
2296static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2297{
David Howells2b0143b2015-03-17 22:25:59 +00002298 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002299 struct nfs_openres *o_res = &data->o_res;
2300 int status;
2301
2302 status = nfs4_run_open_task(data, 1);
2303 if (status != 0 || !data->rpc_done)
2304 return status;
2305
Trond Myklebust6926afd2012-01-07 13:22:46 -05002306 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2307
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002308 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2309 status = _nfs4_proc_open_confirm(data);
2310 if (status != 0)
2311 return status;
2312 }
2313
2314 return status;
2315}
2316
Trond Myklebustf3792d62014-07-10 08:54:32 -04002317/*
2318 * Additional permission checks in order to distinguish between an
2319 * open for read, and an open for execute. This works around the
2320 * fact that NFSv4 OPEN treats read and execute permissions as being
2321 * the same.
2322 * Note that in the non-execute case, we want to turn off permission
2323 * checking if we just created a new file (POSIX open() semantics).
2324 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002325static int nfs4_opendata_access(struct rpc_cred *cred,
2326 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002327 struct nfs4_state *state, fmode_t fmode,
2328 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002329{
2330 struct nfs_access_entry cache;
2331 u32 mask;
2332
2333 /* access call failed or for some reason the server doesn't
2334 * support any access modes -- defer access call until later */
2335 if (opendata->o_res.access_supported == 0)
2336 return 0;
2337
2338 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002339 /*
2340 * Use openflags to check for exec, because fmode won't
2341 * always have FMODE_EXEC set when file open for exec.
2342 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002343 if (openflags & __FMODE_EXEC) {
2344 /* ONLY check for exec rights */
2345 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002346 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002347 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002348
2349 cache.cred = cred;
2350 cache.jiffies = jiffies;
2351 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2352 nfs_access_add_cache(state->inode, &cache);
2353
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002354 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002355 return 0;
2356
2357 /* even though OPEN succeeded, access is denied. Close the file */
2358 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002359 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002360}
2361
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002362/*
2363 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2364 */
2365static int _nfs4_proc_open(struct nfs4_opendata *data)
2366{
David Howells2b0143b2015-03-17 22:25:59 +00002367 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002368 struct nfs_server *server = NFS_SERVER(dir);
2369 struct nfs_openargs *o_arg = &data->o_arg;
2370 struct nfs_openres *o_res = &data->o_res;
2371 int status;
2372
2373 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002374 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002375 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002376 if (status != 0) {
2377 if (status == -NFS4ERR_BADNAME &&
2378 !(o_arg->open_flags & O_CREAT))
2379 return -ENOENT;
2380 return status;
2381 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382
Trond Myklebust6926afd2012-01-07 13:22:46 -05002383 nfs_fattr_map_and_free_names(server, &data->f_attr);
2384
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002385 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002386 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002387 if (o_arg->open_flags & O_EXCL)
2388 data->file_created = 1;
2389 else if (o_res->cinfo.before != o_res->cinfo.after)
2390 data->file_created = 1;
2391 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002392 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2393 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002395 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002400 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Andy Adamsond83217c2011-03-01 01:34:17 +00002404static int nfs4_recover_expired_lease(struct nfs_server *server)
2405{
2406 return nfs4_client_recover_expired_lease(server->nfs_client);
2407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409/*
2410 * OPEN_EXPIRED:
2411 * reclaim state on the server after a network partition.
2412 * Assumes caller holds the appropriate lock
2413 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002414static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002416 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002417 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002419 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002420 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002421 if (IS_ERR(opendata))
2422 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002423 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002424 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002425 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002426 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002427 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428}
2429
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002430static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002431{
Trond Myklebust539cd032007-06-05 11:46:42 -04002432 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002433 struct nfs4_exception exception = { };
2434 int err;
2435
2436 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002437 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002438 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002439 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2440 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002441 switch (err) {
2442 default:
2443 goto out;
2444 case -NFS4ERR_GRACE:
2445 case -NFS4ERR_DELAY:
2446 nfs4_handle_exception(server, err, &exception);
2447 err = 0;
2448 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002449 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002450out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002451 return err;
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2455{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002457 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
Trond Myklebust864472e2006-01-03 09:55:15 +01002459 ctx = nfs4_state_find_open_context(state);
2460 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002461 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002462 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002463 put_nfs_open_context(ctx);
2464 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Trond Myklebust41020b62016-09-22 13:38:58 -04002467static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2468 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002469{
Trond Myklebust41020b62016-09-22 13:38:58 -04002470 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002471 write_seqlock(&state->seqlock);
2472 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2473 write_sequnlock(&state->seqlock);
2474 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2475}
2476
2477static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2478{
2479 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002480 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002481}
2482
2483static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2484{
2485 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2486 nfs40_clear_delegation_stateid(state);
2487 return nfs4_open_expired(sp, state);
2488}
2489
Trond Myklebust45870d62016-09-22 13:38:59 -04002490static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2491 nfs4_stateid *stateid,
2492 struct rpc_cred *cred)
2493{
2494 return -NFS4ERR_BAD_STATEID;
2495}
2496
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002497#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002498static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2499 nfs4_stateid *stateid,
2500 struct rpc_cred *cred)
2501{
2502 int status;
2503
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002504 switch (stateid->type) {
2505 default:
2506 break;
2507 case NFS4_INVALID_STATEID_TYPE:
2508 case NFS4_SPECIAL_STATEID_TYPE:
2509 return -NFS4ERR_BAD_STATEID;
2510 case NFS4_REVOKED_STATEID_TYPE:
2511 goto out_free;
2512 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002513
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002514 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002515 switch (status) {
2516 case -NFS4ERR_EXPIRED:
2517 case -NFS4ERR_ADMIN_REVOKED:
2518 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002519 break;
2520 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002521 return status;
2522 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002523out_free:
2524 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002525 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002526 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002527}
2528
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002529static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002530{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002531 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002532 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002534 struct rpc_cred *cred;
2535 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002536
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002537 /* Get the delegation credential for use by test/free_stateid */
2538 rcu_read_lock();
2539 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002540 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002541 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002542 return;
2543 }
2544
2545 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002546 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2547 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002548 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002549 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002550 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002551
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002552 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2553 rcu_read_unlock();
2554 return;
2555 }
2556
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002557 cred = get_rpccred(delegation->cred);
2558 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002559 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002560 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002561 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002562 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002563
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002564 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565}
2566
2567/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002568 * nfs41_check_expired_locks - possibly free a lock stateid
2569 *
2570 * @state: NFSv4 state for an inode
2571 *
2572 * Returns NFS_OK if recovery for this stateid is now finished.
2573 * Otherwise a negative NFS4ERR value is returned.
2574 */
2575static int nfs41_check_expired_locks(struct nfs4_state *state)
2576{
2577 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002578 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002579 struct nfs_server *server = NFS_SERVER(state->inode);
2580
2581 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2582 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002583
2584 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002585 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2586 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2587 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2588
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002589 atomic_inc(&lsp->ls_count);
2590 spin_unlock(&state->state_lock);
2591
2592 nfs4_put_lock_state(prev);
2593 prev = lsp;
2594
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002595 status = nfs41_test_and_free_expired_stateid(server,
2596 &lsp->ls_stateid,
2597 cred);
2598 trace_nfs4_test_lock_stateid(state, lsp, status);
2599 if (status == -NFS4ERR_EXPIRED ||
2600 status == -NFS4ERR_BAD_STATEID) {
2601 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002602 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603 if (!recover_lost_locks)
2604 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2605 } else if (status != NFS_OK) {
2606 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002607 nfs4_put_lock_state(prev);
2608 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002609 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002610 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002611 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002612 }
2613 spin_unlock(&state->state_lock);
2614 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002615out:
2616 return ret;
2617}
2618
2619/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002620 * nfs41_check_open_stateid - possibly free an open stateid
2621 *
2622 * @state: NFSv4 state for an inode
2623 *
2624 * Returns NFS_OK if recovery for this stateid is now finished.
2625 * Otherwise a negative NFS4ERR value is returned.
2626 */
2627static int nfs41_check_open_stateid(struct nfs4_state *state)
2628{
2629 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002630 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002631 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002632 int status;
2633
Trond Myklebustb134fc42016-09-22 13:39:16 -04002634 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002635 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2636 if (nfs4_have_delegation(state->inode, state->state))
2637 return NFS_OK;
2638 return -NFS4ERR_OPENMODE;
2639 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002640 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002641 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002642 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002643 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002644 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002645 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2646 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2647 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002648 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002649 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002650 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002651 if (status != NFS_OK)
2652 return status;
2653 if (nfs_open_stateid_recover_openmode(state))
2654 return -NFS4ERR_OPENMODE;
2655 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002656}
2657
2658static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2659{
Chuck Levereb64cf92012-07-11 16:30:05 -04002660 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002661
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002662 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002663 status = nfs41_check_expired_locks(state);
2664 if (status != NFS_OK)
2665 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002666 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002667 if (status != NFS_OK)
2668 status = nfs4_open_expired(sp, state);
2669 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002670}
2671#endif
2672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002674 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2675 * fields corresponding to attributes that were used to store the verifier.
2676 * Make sure we clobber those fields in the later setattr call
2677 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002678static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2679 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002681 const u32 *attrset = opendata->o_res.attrset;
2682
2683 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002684 !(sattr->ia_valid & ATTR_ATIME_SET))
2685 sattr->ia_valid |= ATTR_ATIME;
2686
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002687 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002688 !(sattr->ia_valid & ATTR_MTIME_SET))
2689 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002690
2691 /* Except MODE, it seems harmless of setting twice. */
2692 if ((attrset[1] & FATTR4_WORD1_MODE))
2693 sattr->ia_valid &= ~ATTR_MODE;
2694
2695 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2696 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002697}
2698
Trond Myklebustc21443c2013-02-07 14:26:21 -05002699static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2700 fmode_t fmode,
2701 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002702 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002703{
2704 struct nfs4_state_owner *sp = opendata->owner;
2705 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002706 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002707 struct nfs4_state *state;
2708 unsigned int seq;
2709 int ret;
2710
2711 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2712
2713 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002714 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002715 goto out;
2716
2717 state = nfs4_opendata_to_nfs4_state(opendata);
2718 ret = PTR_ERR(state);
2719 if (IS_ERR(state))
2720 goto out;
2721 if (server->caps & NFS_CAP_POSIX_LOCK)
2722 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002723 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2724 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002725
Trond Myklebust275bb302013-05-29 13:11:28 -04002726 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002727 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002728 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002729 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002730 alias = d_exact_alias(dentry, state->inode);
2731 if (!alias)
2732 alias = d_splice_alias(igrab(state->inode), dentry);
2733 /* d_splice_alias() can't fail here - it's a non-directory */
2734 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002735 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002736 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002737 }
2738 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002739 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002740 }
2741
Trond Myklebustc21443c2013-02-07 14:26:21 -05002742 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2743 if (ret != 0)
2744 goto out;
2745
Trond Myklebust3efb9722013-05-29 13:17:04 -04002746 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002747 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002748 nfs_inode_attach_open_context(ctx);
2749 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2750 nfs4_schedule_stateid_recovery(server, state);
2751 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002752out:
2753 return ret;
2754}
2755
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002756/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002757 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Andy Adamson82be4172012-05-23 05:02:35 -04002759static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002760 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002761 int flags,
2762 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002763 struct nfs4_label *label,
2764 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
2766 struct nfs4_state_owner *sp;
2767 struct nfs4_state *state = NULL;
2768 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002769 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002770 struct dentry *dentry = ctx->dentry;
2771 struct rpc_cred *cred = ctx->cred;
2772 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2773 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002774 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002775 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002776 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
2778 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002780 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2781 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2783 goto out_err;
2784 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002785 status = nfs4_recover_expired_lease(server);
2786 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002787 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002788 if (d_really_is_positive(dentry))
2789 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002790 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002791 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002792 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002793 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002794 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002795 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002796 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
David Quigley14c43f72013-05-22 12:50:43 -04002798 if (label) {
2799 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2800 if (IS_ERR(olabel)) {
2801 status = PTR_ERR(olabel);
2802 goto err_opendata_put;
2803 }
2804 }
2805
Trond Myklebuste911b812014-03-26 13:24:37 -07002806 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2807 if (!opendata->f_attr.mdsthreshold) {
2808 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2809 if (!opendata->f_attr.mdsthreshold)
2810 goto err_free_label;
2811 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002812 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002813 }
David Howells2b0143b2015-03-17 22:25:59 +00002814 if (d_really_is_positive(dentry))
2815 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002816
Trond Myklebust3efb9722013-05-29 13:17:04 -04002817 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002818 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002819 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002820 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002821
NeilBrownefcbc042015-07-30 13:00:56 +10002822 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002823 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002824 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002825 /*
2826 * send create attributes which was not set by open
2827 * with an extra setattr.
2828 */
2829 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2830 nfs_fattr_init(opendata->o_res.f_attr);
2831 status = nfs4_do_setattr(state->inode, cred,
2832 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002833 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002834 if (status == 0) {
2835 nfs_setattr_update_inode(state->inode, sattr,
2836 opendata->o_res.f_attr);
2837 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2838 }
David Quigley1775fd32013-05-22 12:50:42 -04002839 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002840 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002841 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002842 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002843
Trond Myklebuste911b812014-03-26 13:24:37 -07002844 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002845 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002846 opendata->f_attr.mdsthreshold = NULL;
2847 }
Andy Adamson82be4172012-05-23 05:02:35 -04002848
David Quigley14c43f72013-05-22 12:50:43 -04002849 nfs4_label_free(olabel);
2850
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002851 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002854err_free_label:
2855 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002856err_opendata_put:
2857 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002858err_put_state_owner:
2859 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
2864
Andy Adamson82be4172012-05-23 05:02:35 -04002865static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002866 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002867 int flags,
2868 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002869 struct nfs4_label *label,
2870 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002872 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 struct nfs4_exception exception = { };
2874 struct nfs4_state *res;
2875 int status;
2876
2877 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002878 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002879 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002880 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 if (status == 0)
2882 break;
2883 /* NOTE: BAD_SEQID means the server and client disagree about the
2884 * book-keeping w.r.t. state-changing operations
2885 * (OPEN/CLOSE/LOCK/LOCKU...)
2886 * It is actually a sign of a bug on the client or on the server.
2887 *
2888 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002889 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 * have unhashed the old state_owner for us, and that we can
2891 * therefore safely retry using a new one. We should still warn
2892 * the user though...
2893 */
2894 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002895 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002896 " returned a bad sequence-id error!\n",
2897 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 exception.retry = 1;
2899 continue;
2900 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002901 /*
2902 * BAD_STATEID on OPEN means that the server cancelled our
2903 * state before it received the OPEN_CONFIRM.
2904 * Recover by retrying the request as per the discussion
2905 * on Page 181 of RFC3530.
2906 */
2907 if (status == -NFS4ERR_BAD_STATEID) {
2908 exception.retry = 1;
2909 continue;
2910 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002911 if (status == -EAGAIN) {
2912 /* We must have found a delegation */
2913 exception.retry = 1;
2914 continue;
2915 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002916 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2917 continue;
2918 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 status, &exception));
2920 } while (exception.retry);
2921 return res;
2922}
2923
Trond Myklebust8487c472016-06-26 08:44:35 -04002924static int _nfs4_do_setattr(struct inode *inode,
2925 struct nfs_setattrargs *arg,
2926 struct nfs_setattrres *res,
2927 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002928 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002929{
2930 struct nfs_server *server = NFS_SERVER(inode);
2931 struct rpc_message msg = {
2932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2933 .rpc_argp = arg,
2934 .rpc_resp = res,
2935 .rpc_cred = cred,
2936 };
2937 struct rpc_cred *delegation_cred = NULL;
2938 unsigned long timestamp = jiffies;
2939 fmode_t fmode;
2940 bool truncate;
2941 int status;
2942
2943 nfs_fattr_init(res->fattr);
2944
2945 /* Servers should only apply open mode checks for file size changes */
2946 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2947 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2948
2949 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2950 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002951 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002952 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002953 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002954 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002955 l_ctx = nfs_get_lock_context(ctx);
2956 if (IS_ERR(l_ctx))
2957 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002958 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2959 &arg->stateid, &delegation_cred);
2960 nfs_put_lock_context(l_ctx);
2961 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002962 return -EBADF;
2963 } else
2964 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2965 if (delegation_cred)
2966 msg.rpc_cred = delegation_cred;
2967
2968 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2969
2970 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002971 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002972 renew_lease(server, timestamp);
2973 trace_nfs4_setattr(inode, &arg->stateid, status);
2974 return status;
2975}
2976
2977static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2978 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002979 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002980 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002982 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002983 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002985 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .iap = sattr,
2987 .server = server,
2988 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002989 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 };
2991 struct nfs_setattrres res = {
2992 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002993 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 .server = server,
2995 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002996 struct nfs4_exception exception = {
2997 .state = state,
2998 .inode = inode,
2999 .stateid = &arg.stateid,
3000 };
3001 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
David Quigleyaa9c2662013-05-22 12:50:44 -04003003 arg.bitmask = nfs4_bitmask(server, ilabel);
3004 if (ilabel)
3005 arg.bitmask = nfs4_bitmask(server, olabel);
3006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003008 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003009 switch (err) {
3010 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003011 if (!(sattr->ia_valid & ATTR_SIZE)) {
3012 pr_warn_once("NFSv4: server %s is incorrectly "
3013 "applying open mode checks to "
3014 "a SETATTR that is not "
3015 "changing file size.\n",
3016 server->nfs_client->cl_hostname);
3017 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003018 if (state && !(state->state & FMODE_WRITE)) {
3019 err = -EBADF;
3020 if (sattr->ia_valid & ATTR_OPEN)
3021 err = -EACCES;
3022 goto out;
3023 }
3024 }
3025 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003027out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return err;
3029}
3030
Peng Tao500d7012015-09-22 11:35:22 +08003031static bool
3032nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3033{
3034 if (inode == NULL || !nfs_have_layout(inode))
3035 return false;
3036
3037 return pnfs_wait_on_layoutreturn(inode, task);
3038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040struct nfs4_closedata {
3041 struct inode *inode;
3042 struct nfs4_state *state;
3043 struct nfs_closeargs arg;
3044 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003045 struct {
3046 struct nfs4_layoutreturn_args arg;
3047 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003048 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003049 u32 roc_barrier;
3050 bool roc;
3051 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003052 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003053 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054};
3055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003056static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003057{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003058 struct nfs4_closedata *calldata = data;
3059 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003060 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003061
Trond Myklebustcf805162016-11-15 14:56:07 -05003062 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003063 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3064 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003065 nfs4_put_open_state(calldata->state);
3066 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003067 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003068 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003069 kfree(calldata);
3070}
3071
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003072static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003074 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003077 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Chuck Levera3ca5652012-03-01 17:00:40 -05003079 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003080 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3081 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003082 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003083
3084 /* Handle Layoutreturn errors */
3085 if (calldata->arg.lr_args && task->tk_status != 0) {
3086 switch (calldata->res.lr_ret) {
3087 default:
3088 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3089 break;
3090 case 0:
3091 calldata->arg.lr_args = NULL;
3092 calldata->res.lr_res = NULL;
3093 break;
3094 case -NFS4ERR_ADMIN_REVOKED:
3095 case -NFS4ERR_DELEG_REVOKED:
3096 case -NFS4ERR_EXPIRED:
3097 case -NFS4ERR_BAD_STATEID:
3098 case -NFS4ERR_OLD_STATEID:
3099 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3100 case -NFS4ERR_WRONG_CRED:
3101 calldata->arg.lr_args = NULL;
3102 calldata->res.lr_res = NULL;
3103 calldata->res.lr_ret = 0;
3104 rpc_restart_call_prepare(task);
3105 return;
3106 }
3107 }
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 /* hmm. we are done with the inode, and in the process of freeing
3110 * the state_owner. we keep this around to process errors
3111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 switch (task->tk_status) {
3113 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003114 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003115 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003116 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003117 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003119 case -NFS4ERR_EXPIRED:
3120 nfs4_free_revoked_stateid(server,
3121 &calldata->arg.stateid,
3122 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003123 case -NFS4ERR_OLD_STATEID:
3124 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003125 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003126 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003127 rpc_restart_call_prepare(task);
3128 goto out_release;
3129 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003130 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003133 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003134 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003135 goto out_release;
3136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003138 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3139 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003140out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003141 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04003142 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003143 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003146static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003148 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003149 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003150 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003151 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003152 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003153
Chuck Levera3ca5652012-03-01 17:00:40 -05003154 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003156 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003157
Trond Myklebust88069f72009-12-08 08:33:16 -05003158 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003159 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003160 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3161 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3162 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003163 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003164 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003165 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003166 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003167 if (state->n_rdonly == 0)
3168 call_close |= is_rdonly;
3169 else if (is_rdonly)
3170 calldata->arg.fmode |= FMODE_READ;
3171 if (state->n_wronly == 0)
3172 call_close |= is_wronly;
3173 else if (is_wronly)
3174 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003175 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3176 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003177 } else if (is_rdwr)
3178 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3179
Trond Myklebust5cc78612016-11-14 11:19:56 -05003180 if (!nfs4_valid_open_stateid(state) ||
3181 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003182 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003183 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003184
3185 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003186 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003187 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003188 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003189
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003190 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003191 nfs_release_seqid(calldata->arg.seqid);
3192 goto out_wait;
3193 }
3194
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003195 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05003196 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003197
3198 /* Close-to-open cache consistency revalidation */
3199 if (!nfs4_have_delegation(inode, FMODE_READ))
3200 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3201 else
3202 calldata->arg.bitmask = NULL;
3203 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003204
Trond Myklebust6ae37332015-01-30 14:21:14 -05003205 calldata->arg.share_access =
3206 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3207 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003208
Trond Myklebust516a6af2005-10-27 22:12:41 -04003209 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003210 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003211 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05003212 &calldata->arg.seq_args,
3213 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003214 task) != 0)
3215 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003216 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003217 return;
3218out_no_action:
3219 task->tk_action = NULL;
3220out_wait:
3221 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003225 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003226 .rpc_call_done = nfs4_close_done,
3227 .rpc_release = nfs4_free_closedata,
3228};
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230/*
3231 * It is possible for data to be read/written from a mem-mapped file
3232 * after the sys_close call (which hits the vfs layer as a flush).
3233 * This means that we can't safely call nfsv4 close on a file until
3234 * the inode is cleared. This in turn means that we are not good
3235 * NFSv4 citizens - we do not indicate to the server to update the file's
3236 * share state even when we are done with one of the three share
3237 * stateid's in the inode.
3238 *
3239 * NOTE: Caller must be holding the sp->so_owner semaphore!
3240 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003241int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003243 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003244 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003246 struct nfs4_state_owner *sp = state->owner;
3247 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003248 struct rpc_message msg = {
3249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3250 .rpc_cred = state->owner->so_cred,
3251 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003252 struct rpc_task_setup task_setup_data = {
3253 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003254 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003255 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003256 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003257 .flags = RPC_TASK_ASYNC,
3258 };
Trond Myklebust95121352005-10-18 14:20:12 -07003259 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003261 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3262 &task_setup_data.rpc_client, &msg);
3263
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003264 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003266 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003267 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003268 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003270 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003272 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3273 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003274 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003275 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003276 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003277 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003278 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003279 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003280 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003281 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003282 calldata->lr.roc = pnfs_roc(state->inode,
3283 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3284 if (calldata->lr.roc) {
3285 calldata->arg.lr_args = &calldata->lr.arg;
3286 calldata->res.lr_res = &calldata->lr.res;
3287 }
Al Viro643168c2011-06-22 18:20:23 -04003288 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003289
Trond Myklebust1174dd12010-12-21 10:52:24 -05003290 msg.rpc_argp = &calldata->arg;
3291 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003292 task_setup_data.callback_data = calldata;
3293 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003294 if (IS_ERR(task))
3295 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003296 status = 0;
3297 if (wait)
3298 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003299 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003300 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003301out_free_calldata:
3302 kfree(calldata);
3303out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003304 nfs4_put_open_state(state);
3305 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Trond Myklebust2b484292010-09-17 10:56:51 -04003309static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003310nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3311 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003314 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3315
3316 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
Trond Myklebust565277f2007-10-15 18:17:53 -04003318 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003319 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003320
3321 nfs4_label_release_security(label);
3322
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003323 if (IS_ERR(state))
3324 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003325 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Trond Myklebust1185a552009-12-03 15:54:02 -05003328static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003329{
3330 if (ctx->state == NULL)
3331 return;
3332 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003333 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003334 else
Al Viro643168c2011-06-22 18:20:23 -04003335 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003336}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Trond Myklebustb944dba2013-11-04 15:20:20 -05003338#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3339#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003340#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3343{
Kinglong Mee8c612822015-08-26 21:12:58 +08003344 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003345 struct nfs4_server_caps_arg args = {
3346 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003347 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 struct nfs4_server_caps_res res = {};
3350 struct rpc_message msg = {
3351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003352 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 .rpc_resp = &res,
3354 };
3355 int status;
3356
Kinglong Mee8c612822015-08-26 21:12:58 +08003357 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3358 FATTR4_WORD0_FH_EXPIRE_TYPE |
3359 FATTR4_WORD0_LINK_SUPPORT |
3360 FATTR4_WORD0_SYMLINK_SUPPORT |
3361 FATTR4_WORD0_ACLSUPPORT;
3362 if (minorversion)
3363 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3364
Bryan Schumaker7c513052011-03-24 17:12:24 +00003365 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003367 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003368 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003369 case 0:
3370 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3371 res.attr_bitmask[2] = 0;
3372 break;
3373 case 1:
3374 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3375 break;
3376 case 2:
3377 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003380 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3381 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3382 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3383 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003384 NFS_CAP_CTIME|NFS_CAP_MTIME|
3385 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003386 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3387 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 server->caps |= NFS_CAP_ACLS;
3389 if (res.has_links != 0)
3390 server->caps |= NFS_CAP_HARDLINKS;
3391 if (res.has_symlinks != 0)
3392 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003393 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3394 server->caps |= NFS_CAP_FILEID;
3395 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3396 server->caps |= NFS_CAP_MODE;
3397 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3398 server->caps |= NFS_CAP_NLINK;
3399 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3400 server->caps |= NFS_CAP_OWNER;
3401 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3402 server->caps |= NFS_CAP_OWNER_GROUP;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3404 server->caps |= NFS_CAP_ATIME;
3405 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3406 server->caps |= NFS_CAP_CTIME;
3407 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3408 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003409#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3410 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3411 server->caps |= NFS_CAP_SECURITY_LABEL;
3412#endif
3413 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3414 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003415 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003416
Trond Myklebusta65318b2009-03-11 14:10:28 -04003417 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3418 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3419 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003420 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003421 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3422 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003424 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return status;
3428}
3429
Trond Myklebust55a97592006-06-09 09:34:19 -04003430int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
3432 struct nfs4_exception exception = { };
3433 int err;
3434 do {
3435 err = nfs4_handle_exception(server,
3436 _nfs4_server_capabilities(server, fhandle),
3437 &exception);
3438 } while (exception.retry);
3439 return err;
3440}
3441
3442static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3443 struct nfs_fsinfo *info)
3444{
David Quigleyaa9c2662013-05-22 12:50:44 -04003445 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003447 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 };
3449 struct nfs4_lookup_res res = {
3450 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003451 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 .fh = fhandle,
3453 };
3454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3456 .rpc_argp = &args,
3457 .rpc_resp = &res,
3458 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003459
David Quigleyaa9c2662013-05-22 12:50:44 -04003460 bitmask[0] = nfs4_fattr_bitmap[0];
3461 bitmask[1] = nfs4_fattr_bitmap[1];
3462 /*
3463 * Process the label in the upcoming getfattr
3464 */
3465 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3466
Trond Myklebust0e574af2005-10-27 22:12:38 -04003467 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003468 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469}
3470
3471static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3472 struct nfs_fsinfo *info)
3473{
3474 struct nfs4_exception exception = { };
3475 int err;
3476 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003477 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003478 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003479 switch (err) {
3480 case 0:
3481 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003482 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003483 default:
3484 err = nfs4_handle_exception(server, err, &exception);
3485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003487out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return err;
3489}
3490
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003491static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3492 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3493{
Trond Myklebustc2190662013-08-26 19:23:04 -04003494 struct rpc_auth_create_args auth_args = {
3495 .pseudoflavor = flavor,
3496 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003497 struct rpc_auth *auth;
3498 int ret;
3499
Trond Myklebustc2190662013-08-26 19:23:04 -04003500 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003501 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003502 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003503 goto out;
3504 }
3505 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003506out:
3507 return ret;
3508}
3509
Chuck Lever9a744ba2013-03-16 15:56:02 -04003510/*
3511 * Retry pseudoroot lookup with various security flavors. We do this when:
3512 *
3513 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3514 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3515 *
3516 * Returns zero on success, or a negative NFS4ERR value, or a
3517 * negative errno value.
3518 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003519static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003520 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003522 /* Per 3530bis 15.33.5 */
3523 static const rpc_authflavor_t flav_array[] = {
3524 RPC_AUTH_GSS_KRB5P,
3525 RPC_AUTH_GSS_KRB5I,
3526 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003527 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003528 RPC_AUTH_NULL,
3529 };
3530 int status = -EPERM;
3531 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003533 if (server->auth_info.flavor_len > 0) {
3534 /* try each flavor specified by user */
3535 for (i = 0; i < server->auth_info.flavor_len; i++) {
3536 status = nfs4_lookup_root_sec(server, fhandle, info,
3537 server->auth_info.flavors[i]);
3538 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3539 continue;
3540 break;
3541 }
3542 } else {
3543 /* no flavors specified by user, try default list */
3544 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3545 status = nfs4_lookup_root_sec(server, fhandle, info,
3546 flav_array[i]);
3547 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3548 continue;
3549 break;
3550 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003551 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003552
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003553 /*
3554 * -EACCESS could mean that the user doesn't have correct permissions
3555 * to access the mount. It could also mean that we tried to mount
3556 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3557 * existing mount programs don't handle -EACCES very well so it should
3558 * be mapped to -EPERM instead.
3559 */
3560 if (status == -EACCES)
3561 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003562 return status;
3563}
3564
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003565/**
3566 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3567 * @server: initialized nfs_server handle
3568 * @fhandle: we fill in the pseudo-fs root file handle
3569 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003570 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003571 *
3572 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003573 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003574int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003575 struct nfs_fsinfo *info,
3576 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003577{
Andre Przywarac7757072015-04-23 17:17:40 +01003578 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003579
Andre Przywarac7757072015-04-23 17:17:40 +01003580 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003581 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003582
3583 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003584 status = server->nfs_client->cl_mvops->find_root_sec(server,
3585 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003586
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 if (status == 0)
3588 status = nfs4_server_capabilities(server, fhandle);
3589 if (status == 0)
3590 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003591
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003592 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Bryan Schumakerbae36242012-05-10 15:07:31 -04003595static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3596 struct nfs_fsinfo *info)
3597{
3598 int error;
3599 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003600 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003601
3602 error = nfs4_server_capabilities(server, mntfh);
3603 if (error < 0) {
3604 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3605 return error;
3606 }
3607
David Quigley14c43f72013-05-22 12:50:43 -04003608 label = nfs4_label_alloc(server, GFP_KERNEL);
3609 if (IS_ERR(label))
3610 return PTR_ERR(label);
3611
David Quigley1775fd32013-05-22 12:50:42 -04003612 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003613 if (error < 0) {
3614 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003615 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003616 }
3617
3618 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3619 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3620 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3621
David Quigley14c43f72013-05-22 12:50:43 -04003622err_free_label:
3623 nfs4_label_free(label);
3624
Bryan Schumakerbae36242012-05-10 15:07:31 -04003625 return error;
3626}
3627
Manoj Naik6b97fd32006-06-09 09:34:29 -04003628/*
3629 * Get locations and (maybe) other attributes of a referral.
3630 * Note that we'll actually follow the referral later when
3631 * we detect fsid mismatch in inode revalidation
3632 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003633static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3634 const struct qstr *name, struct nfs_fattr *fattr,
3635 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003636{
3637 int status = -ENOMEM;
3638 struct page *page = NULL;
3639 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003640
3641 page = alloc_page(GFP_KERNEL);
3642 if (page == NULL)
3643 goto out;
3644 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3645 if (locations == NULL)
3646 goto out;
3647
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003648 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003649 if (status != 0)
3650 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003651
3652 /*
3653 * If the fsid didn't change, this is a migration event, not a
3654 * referral. Cause us to drop into the exception handler, which
3655 * will kick off migration recovery.
3656 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003657 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003658 dprintk("%s: server did not return a different fsid for"
3659 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003660 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003661 goto out;
3662 }
Andy Adamson533eb462011-06-13 18:25:56 -04003663 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3664 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003665
Andy Adamson533eb462011-06-13 18:25:56 -04003666 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003667 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003668 memset(fhandle, 0, sizeof(struct nfs_fh));
3669out:
3670 if (page)
3671 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003672 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003673 return status;
3674}
3675
David Quigley1775fd32013-05-22 12:50:42 -04003676static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3677 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
3679 struct nfs4_getattr_arg args = {
3680 .fh = fhandle,
3681 .bitmask = server->attr_bitmask,
3682 };
3683 struct nfs4_getattr_res res = {
3684 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003685 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 .server = server,
3687 };
3688 struct rpc_message msg = {
3689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3690 .rpc_argp = &args,
3691 .rpc_resp = &res,
3692 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003693
3694 args.bitmask = nfs4_bitmask(server, label);
3695
Trond Myklebust0e574af2005-10-27 22:12:38 -04003696 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003697 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698}
3699
David Quigley1775fd32013-05-22 12:50:42 -04003700static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3701 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702{
3703 struct nfs4_exception exception = { };
3704 int err;
3705 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003706 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3707 trace_nfs4_getattr(server, fhandle, fattr, err);
3708 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 &exception);
3710 } while (exception.retry);
3711 return err;
3712}
3713
3714/*
3715 * The file is not closed if it is opened due to the a request to change
3716 * the size of the file. The open call will not be needed once the
3717 * VFS layer lookup-intents are implemented.
3718 *
3719 * Close is called when the inode is destroyed.
3720 * If we haven't opened the file for O_WRONLY, we
3721 * need to in the size_change case to obtain a stateid.
3722 *
3723 * Got race?
3724 * Because OPEN is always done by name in nfsv4, it is
3725 * possible that we opened a different file by the same
3726 * name. We can recognize this race condition, but we
3727 * can't do anything about it besides returning an error.
3728 *
3729 * This will be fixed with VFS changes (lookup-intent).
3730 */
3731static int
3732nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3733 struct iattr *sattr)
3734{
David Howells2b0143b2015-03-17 22:25:59 +00003735 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003736 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003737 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003738 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 int status;
3740
Peng Tao88ac8152014-09-12 11:04:10 +08003741 if (pnfs_ld_layoutret_on_setattr(inode) &&
3742 sattr->ia_valid & ATTR_SIZE &&
3743 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003744 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003745
Trond Myklebust0e574af2005-10-27 22:12:38 -04003746 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Andy Adamson26699402012-05-30 16:12:24 -04003748 /* Deal with open(O_TRUNC) */
3749 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003750 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003751
3752 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003753 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003754 return 0;
3755
Trond Myklebustd5308382005-11-04 15:33:38 -05003756 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003757 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003758
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003759 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003760 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003761 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003762 }
3763
David Quigley14c43f72013-05-22 12:50:43 -04003764 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3765 if (IS_ERR(label))
3766 return PTR_ERR(label);
3767
NeilBrown29b59f92016-10-13 15:26:47 +11003768 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003769 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003770 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003771 nfs_setsecurity(inode, fattr, label);
3772 }
David Quigley14c43f72013-05-22 12:50:43 -04003773 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 return status;
3775}
3776
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003777static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3778 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003779 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003780{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003781 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003782 int status;
3783 struct nfs4_lookup_arg args = {
3784 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003785 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003786 .name = name,
3787 };
3788 struct nfs4_lookup_res res = {
3789 .server = server,
3790 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003791 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003792 .fh = fhandle,
3793 };
3794 struct rpc_message msg = {
3795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3796 .rpc_argp = &args,
3797 .rpc_resp = &res,
3798 };
3799
David Quigleyaa9c2662013-05-22 12:50:44 -04003800 args.bitmask = nfs4_bitmask(server, label);
3801
David Howells2b3de442006-08-22 20:06:09 -04003802 nfs_fattr_init(fattr);
3803
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003805 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 dprintk("NFS reply lookup: %d\n", status);
3807 return status;
3808}
3809
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003810static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003811{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003812 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003813 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003814 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3815 fattr->nlink = 2;
3816}
3817
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003818static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003819 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003820 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003821{
3822 struct nfs4_exception exception = { };
3823 struct rpc_clnt *client = *clnt;
3824 int err;
3825 do {
David Quigley1775fd32013-05-22 12:50:42 -04003826 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003827 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003828 switch (err) {
3829 case -NFS4ERR_BADNAME:
3830 err = -ENOENT;
3831 goto out;
3832 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003833 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003834 if (err == -NFS4ERR_MOVED)
3835 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003836 goto out;
3837 case -NFS4ERR_WRONGSEC:
3838 err = -EPERM;
3839 if (client != *clnt)
3840 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003841 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003842 if (IS_ERR(client))
3843 return PTR_ERR(client);
3844
3845 exception.retry = 1;
3846 break;
3847 default:
3848 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3849 }
3850 } while (exception.retry);
3851
3852out:
3853 if (err == 0)
3854 *clnt = client;
3855 else if (client != *clnt)
3856 rpc_shutdown_client(client);
3857
3858 return err;
3859}
3860
Al Virobeffb8f2016-07-20 16:34:42 -04003861static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003862 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3863 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003865 int status;
3866 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003867
David Quigley1775fd32013-05-22 12:50:42 -04003868 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003869 if (client != NFS_CLIENT(dir)) {
3870 rpc_shutdown_client(client);
3871 nfs_fixup_secinfo_attributes(fattr);
3872 }
3873 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003876struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003877nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003878 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3879{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003880 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003881 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003882
David Quigley1775fd32013-05-22 12:50:42 -04003883 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003884 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003885 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003886 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003887}
3888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3890{
Trond Myklebust76b32992007-08-10 17:45:11 -04003891 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 struct nfs4_accessargs args = {
3893 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003894 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003896 struct nfs4_accessres res = {
3897 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003898 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 struct rpc_message msg = {
3900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3901 .rpc_argp = &args,
3902 .rpc_resp = &res,
3903 .rpc_cred = entry->cred,
3904 };
3905 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003906 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907
3908 /*
3909 * Determine which access bits we want to ask for...
3910 */
3911 if (mode & MAY_READ)
3912 args.access |= NFS4_ACCESS_READ;
3913 if (S_ISDIR(inode->i_mode)) {
3914 if (mode & MAY_WRITE)
3915 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3916 if (mode & MAY_EXEC)
3917 args.access |= NFS4_ACCESS_LOOKUP;
3918 } else {
3919 if (mode & MAY_WRITE)
3920 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3921 if (mode & MAY_EXEC)
3922 args.access |= NFS4_ACCESS_EXECUTE;
3923 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003924
3925 res.fattr = nfs_alloc_fattr();
3926 if (res.fattr == NULL)
3927 return -ENOMEM;
3928
Bryan Schumaker7c513052011-03-24 17:12:24 +00003929 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003931 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003932 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003934 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 return status;
3936}
3937
3938static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3939{
3940 struct nfs4_exception exception = { };
3941 int err;
3942 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003943 err = _nfs4_proc_access(inode, entry);
3944 trace_nfs4_access(inode, err);
3945 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 &exception);
3947 } while (exception.retry);
3948 return err;
3949}
3950
3951/*
3952 * TODO: For the time being, we don't try to get any attributes
3953 * along with any of the zero-copy operations READ, READDIR,
3954 * READLINK, WRITE.
3955 *
3956 * In the case of the first three, we want to put the GETATTR
3957 * after the read-type operation -- this is because it is hard
3958 * to predict the length of a GETATTR response in v4, and thus
3959 * align the READ data correctly. This means that the GETATTR
3960 * may end up partially falling into the page cache, and we should
3961 * shift it into the 'tail' of the xdr_buf before processing.
3962 * To do this efficiently, we need to know the total length
3963 * of data received, which doesn't seem to be available outside
3964 * of the RPC layer.
3965 *
3966 * In the case of WRITE, we also want to put the GETATTR after
3967 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003968 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 *
3970 * Both of these changes to the XDR layer would in fact be quite
3971 * minor, but I decided to leave them for a subsequent patch.
3972 */
3973static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3974 unsigned int pgbase, unsigned int pglen)
3975{
3976 struct nfs4_readlink args = {
3977 .fh = NFS_FH(inode),
3978 .pgbase = pgbase,
3979 .pglen = pglen,
3980 .pages = &page,
3981 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003982 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 struct rpc_message msg = {
3984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3985 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003986 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 };
3988
Bryan Schumaker7c513052011-03-24 17:12:24 +00003989 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 -07003990}
3991
3992static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3993 unsigned int pgbase, unsigned int pglen)
3994{
3995 struct nfs4_exception exception = { };
3996 int err;
3997 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003998 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3999 trace_nfs4_readlink(inode, err);
4000 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 &exception);
4002 } while (exception.retry);
4003 return err;
4004}
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004007 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009static int
4010nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004011 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
David Quigleyaa9c2662013-05-22 12:50:44 -04004013 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004014 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 int status = 0;
4017
NeilBrown532d4de2016-10-13 15:26:47 +11004018 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004019 if (IS_ERR(ctx))
4020 return PTR_ERR(ctx);
4021
David Quigleyaa9c2662013-05-22 12:50:44 -04004022 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4023
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004024 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004025 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 if (IS_ERR(state)) {
4027 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004031 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004032 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 return status;
4034}
4035
Al Virobeffb8f2016-07-20 16:34:42 -04004036static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037{
Trond Myklebust16e42952005-10-27 22:12:44 -04004038 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004039 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004041 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004043 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004044 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4048 .rpc_argp = &args,
4049 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 };
Trond Myklebust778d2812012-04-27 13:48:19 -04004051 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004052
Bryan Schumaker7c513052011-03-24 17:12:24 +00004053 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004054 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04004055 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 return status;
4057}
4058
Al Virobeffb8f2016-07-20 16:34:42 -04004059static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
4061 struct nfs4_exception exception = { };
4062 int err;
4063 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004064 err = _nfs4_proc_remove(dir, name);
4065 trace_nfs4_remove(dir, name, err);
4066 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 &exception);
4068 } while (exception.retry);
4069 return err;
4070}
4071
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004072static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004074 struct nfs_server *server = NFS_SERVER(dir);
4075 struct nfs_removeargs *args = msg->rpc_argp;
4076 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004078 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004080 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004081
4082 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083}
4084
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004085static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4086{
Al Viro884be172016-04-28 23:56:31 -04004087 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004088 &data->args.seq_args,
4089 &data->res.seq_res,
4090 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004093static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004095 struct nfs_unlinkdata *data = task->tk_calldata;
4096 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004097
Trond Myklebust14516c32010-07-31 14:29:06 -04004098 if (!nfs4_sequence_done(task, &res->seq_res))
4099 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004100 if (nfs4_async_handle_error(task, res->server, NULL,
4101 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004102 return 0;
4103 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004104 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
Jeff Laytond3d41522010-09-17 17:31:57 -04004107static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4108{
4109 struct nfs_server *server = NFS_SERVER(dir);
4110 struct nfs_renameargs *arg = msg->rpc_argp;
4111 struct nfs_renameres *res = msg->rpc_resp;
4112
4113 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004114 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004115 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004116}
4117
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004118static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4119{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004120 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
4121 &data->args.seq_args,
4122 &data->res.seq_res,
4123 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004124}
4125
4126static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4127 struct inode *new_dir)
4128{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004129 struct nfs_renamedata *data = task->tk_calldata;
4130 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004131
4132 if (!nfs4_sequence_done(task, &res->seq_res))
4133 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004134 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004135 return 0;
4136
4137 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004138 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04004139 return 1;
4140}
4141
Al Virobeffb8f2016-07-20 16:34:42 -04004142static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004144 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 struct nfs4_link_arg arg = {
4146 .fh = NFS_FH(inode),
4147 .dir_fh = NFS_FH(dir),
4148 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004149 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004151 struct nfs4_link_res res = {
4152 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004153 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004154 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 struct rpc_message msg = {
4156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4157 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004158 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004160 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Trond Myklebust136f2622010-04-16 16:22:49 -04004162 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004163 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004164 goto out;
4165
David Quigley14c43f72013-05-22 12:50:43 -04004166 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4167 if (IS_ERR(res.label)) {
4168 status = PTR_ERR(res.label);
4169 goto out;
4170 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004171 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004172
Bryan Schumaker7c513052011-03-24 17:12:24 +00004173 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004174 if (!status) {
4175 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04004176 status = nfs_post_op_update_inode(inode, res.fattr);
4177 if (!status)
4178 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004179 }
David Quigley14c43f72013-05-22 12:50:43 -04004180
4181
4182 nfs4_label_free(res.label);
4183
Trond Myklebust136f2622010-04-16 16:22:49 -04004184out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004185 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return status;
4187}
4188
Al Virobeffb8f2016-07-20 16:34:42 -04004189static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190{
4191 struct nfs4_exception exception = { };
4192 int err;
4193 do {
4194 err = nfs4_handle_exception(NFS_SERVER(inode),
4195 _nfs4_proc_link(inode, dir, name),
4196 &exception);
4197 } while (exception.retry);
4198 return err;
4199}
4200
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004201struct nfs4_createdata {
4202 struct rpc_message msg;
4203 struct nfs4_create_arg arg;
4204 struct nfs4_create_res res;
4205 struct nfs_fh fh;
4206 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004207 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004208};
4209
4210static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004211 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004212{
4213 struct nfs4_createdata *data;
4214
4215 data = kzalloc(sizeof(*data), GFP_KERNEL);
4216 if (data != NULL) {
4217 struct nfs_server *server = NFS_SERVER(dir);
4218
David Quigley14c43f72013-05-22 12:50:43 -04004219 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4220 if (IS_ERR(data->label))
4221 goto out_free;
4222
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4224 data->msg.rpc_argp = &data->arg;
4225 data->msg.rpc_resp = &data->res;
4226 data->arg.dir_fh = NFS_FH(dir);
4227 data->arg.server = server;
4228 data->arg.name = name;
4229 data->arg.attrs = sattr;
4230 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004231 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004232 data->res.server = server;
4233 data->res.fh = &data->fh;
4234 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004235 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004236 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004237 }
4238 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004239out_free:
4240 kfree(data);
4241 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004242}
4243
4244static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4245{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004246 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004247 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004248 if (status == 0) {
4249 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04004250 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004251 }
4252 return status;
4253}
4254
4255static void nfs4_free_createdata(struct nfs4_createdata *data)
4256{
David Quigley14c43f72013-05-22 12:50:43 -04004257 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004258 kfree(data);
4259}
4260
Chuck Lever4f390c12006-08-22 20:06:22 -04004261static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004262 struct page *page, unsigned int len, struct iattr *sattr,
4263 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004265 struct nfs4_createdata *data;
4266 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Chuck Lever94a6d752006-08-22 20:06:23 -04004268 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004269 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004270
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004271 status = -ENOMEM;
4272 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4273 if (data == NULL)
4274 goto out;
4275
4276 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4277 data->arg.u.symlink.pages = &page;
4278 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004279 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004281 status = nfs4_do_create(dir, dentry, data);
4282
4283 nfs4_free_createdata(data);
4284out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return status;
4286}
4287
Chuck Lever4f390c12006-08-22 20:06:22 -04004288static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004289 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290{
4291 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004292 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004294
4295 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004298 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4299 trace_nfs4_symlink(dir, &dentry->d_name, err);
4300 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 &exception);
4302 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004303
4304 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return err;
4306}
4307
4308static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004309 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004311 struct nfs4_createdata *data;
4312 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004314 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4315 if (data == NULL)
4316 goto out;
4317
David Quigley1775fd32013-05-22 12:50:42 -04004318 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004319 status = nfs4_do_create(dir, dentry, data);
4320
4321 nfs4_free_createdata(data);
4322out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return status;
4324}
4325
4326static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4327 struct iattr *sattr)
4328{
4329 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004330 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004332
David Quigleyaa9c2662013-05-22 12:50:44 -04004333 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4334
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004335 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004337 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4338 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4339 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 &exception);
4341 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004342 nfs4_label_release_security(label);
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 return err;
4345}
4346
4347static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004348 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349{
David Howells2b0143b2015-03-17 22:25:59 +00004350 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 struct nfs4_readdir_arg args = {
4352 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004353 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 .pgbase = 0,
4355 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004356 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004357 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 };
4359 struct nfs4_readdir_res res;
4360 struct rpc_message msg = {
4361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4362 .rpc_argp = &args,
4363 .rpc_resp = &res,
4364 .rpc_cred = cred,
4365 };
4366 int status;
4367
Al Viro6de14722013-09-16 10:53:17 -04004368 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4369 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004370 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004371 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004373 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 -05004374 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004375 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004376 status += args.pgbase;
4377 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004378
4379 nfs_invalidate_atime(dir);
4380
Harvey Harrison3110ff82008-05-02 13:42:44 -07004381 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return status;
4383}
4384
4385static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004386 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387{
4388 struct nfs4_exception exception = { };
4389 int err;
4390 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004391 err = _nfs4_proc_readdir(dentry, cred, cookie,
4392 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004393 trace_nfs4_readdir(d_inode(dentry), err);
4394 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 &exception);
4396 } while (exception.retry);
4397 return err;
4398}
4399
4400static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004401 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004403 struct nfs4_createdata *data;
4404 int mode = sattr->ia_mode;
4405 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004407 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4408 if (data == NULL)
4409 goto out;
4410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004414 data->arg.ftype = NF4BLK;
4415 data->arg.u.device.specdata1 = MAJOR(rdev);
4416 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 data->arg.ftype = NF4CHR;
4420 data->arg.u.device.specdata1 = MAJOR(rdev);
4421 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004422 } else if (!S_ISSOCK(mode)) {
4423 status = -EINVAL;
4424 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
David Quigley1775fd32013-05-22 12:50:42 -04004426
David Quigleyaa9c2662013-05-22 12:50:44 -04004427 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004428 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004429out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004430 nfs4_free_createdata(data);
4431out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 return status;
4433}
4434
4435static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4436 struct iattr *sattr, dev_t rdev)
4437{
4438 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004439 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004441
David Quigleyaa9c2662013-05-22 12:50:44 -04004442 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4443
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004444 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004446 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4447 trace_nfs4_mknod(dir, &dentry->d_name, err);
4448 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 &exception);
4450 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004451
4452 nfs4_label_release_security(label);
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 return err;
4455}
4456
4457static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4458 struct nfs_fsstat *fsstat)
4459{
4460 struct nfs4_statfs_arg args = {
4461 .fh = fhandle,
4462 .bitmask = server->attr_bitmask,
4463 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004464 struct nfs4_statfs_res res = {
4465 .fsstat = fsstat,
4466 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 struct rpc_message msg = {
4468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4469 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004470 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 };
4472
Trond Myklebust0e574af2005-10-27 22:12:38 -04004473 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004474 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475}
4476
4477static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4478{
4479 struct nfs4_exception exception = { };
4480 int err;
4481 do {
4482 err = nfs4_handle_exception(server,
4483 _nfs4_proc_statfs(server, fhandle, fsstat),
4484 &exception);
4485 } while (exception.retry);
4486 return err;
4487}
4488
4489static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4490 struct nfs_fsinfo *fsinfo)
4491{
4492 struct nfs4_fsinfo_arg args = {
4493 .fh = fhandle,
4494 .bitmask = server->attr_bitmask,
4495 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004496 struct nfs4_fsinfo_res res = {
4497 .fsinfo = fsinfo,
4498 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 struct rpc_message msg = {
4500 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4501 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004502 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 };
4504
Bryan Schumaker7c513052011-03-24 17:12:24 +00004505 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
4508static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4509{
4510 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004511 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 int err;
4513
4514 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004515 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004516 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004517 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004518 nfs4_set_lease_period(server->nfs_client,
4519 fsinfo->lease_time * HZ,
4520 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004521 break;
4522 }
4523 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 } while (exception.retry);
4525 return err;
4526}
4527
4528static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4529{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004530 int error;
4531
Trond Myklebust0e574af2005-10-27 22:12:38 -04004532 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004533 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004534 if (error == 0) {
4535 /* block layout checks this! */
4536 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004537 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004538 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004539
4540 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541}
4542
4543static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4544 struct nfs_pathconf *pathconf)
4545{
4546 struct nfs4_pathconf_arg args = {
4547 .fh = fhandle,
4548 .bitmask = server->attr_bitmask,
4549 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004550 struct nfs4_pathconf_res res = {
4551 .pathconf = pathconf,
4552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 struct rpc_message msg = {
4554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4555 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004556 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 };
4558
4559 /* None of the pathconf attributes are mandatory to implement */
4560 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4561 memset(pathconf, 0, sizeof(*pathconf));
4562 return 0;
4563 }
4564
Trond Myklebust0e574af2005-10-27 22:12:38 -04004565 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004566 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
4568
4569static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4570 struct nfs_pathconf *pathconf)
4571{
4572 struct nfs4_exception exception = { };
4573 int err;
4574
4575 do {
4576 err = nfs4_handle_exception(server,
4577 _nfs4_proc_pathconf(server, fhandle, pathconf),
4578 &exception);
4579 } while (exception.retry);
4580 return err;
4581}
4582
Trond Myklebust5521abf2013-03-16 20:54:34 -04004583int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004584 const struct nfs_open_context *ctx,
4585 const struct nfs_lock_context *l_ctx,
4586 fmode_t fmode)
4587{
NeilBrown17393472016-10-13 15:26:47 +11004588 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004589}
4590EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4591
Trond Myklebust5521abf2013-03-16 20:54:34 -04004592static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4593 const struct nfs_open_context *ctx,
4594 const struct nfs_lock_context *l_ctx,
4595 fmode_t fmode)
4596{
4597 nfs4_stateid current_stateid;
4598
Trond Myklebuste1253be2014-03-05 08:44:23 -05004599 /* If the current stateid represents a lost lock, then exit */
4600 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4601 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004602 return nfs4_stateid_match(stateid, &current_stateid);
4603}
4604
4605static bool nfs4_error_stateid_expired(int err)
4606{
4607 switch (err) {
4608 case -NFS4ERR_DELEG_REVOKED:
4609 case -NFS4ERR_ADMIN_REVOKED:
4610 case -NFS4ERR_BAD_STATEID:
4611 case -NFS4ERR_STALE_STATEID:
4612 case -NFS4ERR_OLD_STATEID:
4613 case -NFS4ERR_OPENMODE:
4614 case -NFS4ERR_EXPIRED:
4615 return true;
4616 }
4617 return false;
4618}
4619
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004620static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004622 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004624 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004625 if (task->tk_status < 0) {
4626 struct nfs4_exception exception = {
4627 .inode = hdr->inode,
4628 .state = hdr->args.context->state,
4629 .stateid = &hdr->args.stateid,
4630 };
4631 task->tk_status = nfs4_async_handle_exception(task,
4632 server, task->tk_status, &exception);
4633 if (exception.retry) {
4634 rpc_restart_call_prepare(task);
4635 return -EAGAIN;
4636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004638
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004640 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
Trond Myklebust5521abf2013-03-16 20:54:34 -04004644static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004645 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004646{
4647
4648 if (!nfs4_error_stateid_expired(task->tk_status) ||
4649 nfs4_stateid_is_current(&args->stateid,
4650 args->context,
4651 args->lock_context,
4652 FMODE_READ))
4653 return false;
4654 rpc_restart_call_prepare(task);
4655 return true;
4656}
4657
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004658static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004659{
4660
4661 dprintk("--> %s\n", __func__);
4662
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004663 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004664 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004665 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004666 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004667 if (task->tk_status > 0)
4668 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004669 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4670 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004671}
4672
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004673static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4674 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004676 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004677 if (!hdr->pgio_done_cb)
4678 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004679 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004680 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004683static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4684 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004685{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4687 &hdr->args.seq_args,
4688 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004689 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004690 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4692 hdr->args.lock_context,
4693 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004694 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004696 return -EIO;
4697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698}
4699
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004700static int nfs4_write_done_cb(struct rpc_task *task,
4701 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004704
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004705 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004706 if (task->tk_status < 0) {
4707 struct nfs4_exception exception = {
4708 .inode = hdr->inode,
4709 .state = hdr->args.context->state,
4710 .stateid = &hdr->args.stateid,
4711 };
4712 task->tk_status = nfs4_async_handle_exception(task,
4713 NFS_SERVER(inode), task->tk_status,
4714 &exception);
4715 if (exception.retry) {
4716 rpc_restart_call_prepare(task);
4717 return -EAGAIN;
4718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004720 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004722 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004723 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Trond Myklebust5521abf2013-03-16 20:54:34 -04004727static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004728 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004729{
4730
4731 if (!nfs4_error_stateid_expired(task->tk_status) ||
4732 nfs4_stateid_is_current(&args->stateid,
4733 args->context,
4734 args->lock_context,
4735 FMODE_WRITE))
4736 return false;
4737 rpc_restart_call_prepare(task);
4738 return true;
4739}
4740
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004741static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004742{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004743 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004744 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004745 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004746 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004747 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4748 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004749}
4750
Trond Myklebust5a37f852012-04-28 14:55:16 -04004751static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004752bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004753{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004754 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004755 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004756 return false;
4757 /* Otherwise, request attributes if and only if we don't hold
4758 * a delegation
4759 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004760 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004761}
Fred Isamana69aef12011-03-03 15:13:47 +00004762
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004763static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4764 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004767
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004768 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4769 hdr->args.bitmask = NULL;
4770 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004771 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004772 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004773
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004774 if (!hdr->pgio_done_cb)
4775 hdr->pgio_done_cb = nfs4_write_done_cb;
4776 hdr->res.server = server;
4777 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004779 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004780 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
4782
Fred Isaman0b7c0152012-04-20 14:47:39 -04004783static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4784{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004785 nfs4_setup_sequence(NFS_SERVER(data->inode),
4786 &data->args.seq_args,
4787 &data->res.seq_res,
4788 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Fred Isaman0b7c0152012-04-20 14:47:39 -04004791static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004794
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004795 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004796 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4797 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004798 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004799 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
Fred Isaman0b7c0152012-04-20 14:47:39 -04004804static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004805{
4806 if (!nfs4_sequence_done(task, &data->res.seq_res))
4807 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004808 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004809}
4810
Fred Isaman0b7c0152012-04-20 14:47:39 -04004811static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004813 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004814
Fred Isaman0b7c0152012-04-20 14:47:39 -04004815 if (data->commit_done_cb == NULL)
4816 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004817 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004818 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004819 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820}
4821
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004822struct nfs4_renewdata {
4823 struct nfs_client *client;
4824 unsigned long timestamp;
4825};
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827/*
4828 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4829 * standalone procedure for queueing an asynchronous RENEW.
4830 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004831static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004832{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004833 struct nfs4_renewdata *data = calldata;
4834 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004835
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004836 if (atomic_read(&clp->cl_count) > 1)
4837 nfs4_schedule_state_renewal(clp);
4838 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004839 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004840}
4841
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004842static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004844 struct nfs4_renewdata *data = calldata;
4845 struct nfs_client *clp = data->client;
4846 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004848 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004849 switch (task->tk_status) {
4850 case 0:
4851 break;
4852 case -NFS4ERR_LEASE_MOVED:
4853 nfs4_schedule_lease_moved_recovery(clp);
4854 break;
4855 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004856 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004857 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4858 return;
4859 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004860 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004861 return;
4862 }
4863 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004865 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
4867
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004868static const struct rpc_call_ops nfs4_renew_ops = {
4869 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004870 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004871};
4872
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004873static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874{
4875 struct rpc_message msg = {
4876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4877 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004878 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004880 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004882 if (renew_flags == 0)
4883 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004884 if (!atomic_inc_not_zero(&clp->cl_count))
4885 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004886 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004887 if (data == NULL)
4888 return -ENOMEM;
4889 data->client = clp;
4890 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004891 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004892 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893}
4894
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004895static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
4897 struct rpc_message msg = {
4898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4899 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004900 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 };
4902 unsigned long now = jiffies;
4903 int status;
4904
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004905 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 if (status < 0)
4907 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004908 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 return 0;
4910}
4911
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004912static inline int nfs4_server_supports_acls(struct nfs_server *server)
4913{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004914 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004915}
4916
Trond Myklebust21f498c2012-08-24 10:59:25 -04004917/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4918 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004919 * the stack.
4920 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004921#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004922
Neil Hormane9e3d722011-03-04 19:26:03 -05004923static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004924 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004925{
4926 struct page *newpage, **spages;
4927 int rc = 0;
4928 size_t len;
4929 spages = pages;
4930
4931 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004932 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004933 newpage = alloc_page(GFP_KERNEL);
4934
4935 if (newpage == NULL)
4936 goto unwind;
4937 memcpy(page_address(newpage), buf, len);
4938 buf += len;
4939 buflen -= len;
4940 *pages++ = newpage;
4941 rc++;
4942 } while (buflen != 0);
4943
4944 return rc;
4945
4946unwind:
4947 for(; rc > 0; rc--)
4948 __free_page(spages[rc-1]);
4949 return -ENOMEM;
4950}
4951
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004952struct nfs4_cached_acl {
4953 int cached;
4954 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004955 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956};
4957
4958static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004959{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004960 struct nfs_inode *nfsi = NFS_I(inode);
4961
4962 spin_lock(&inode->i_lock);
4963 kfree(nfsi->nfs4_acl);
4964 nfsi->nfs4_acl = acl;
4965 spin_unlock(&inode->i_lock);
4966}
4967
4968static void nfs4_zap_acl_attr(struct inode *inode)
4969{
4970 nfs4_set_cached_acl(inode, NULL);
4971}
4972
4973static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4974{
4975 struct nfs_inode *nfsi = NFS_I(inode);
4976 struct nfs4_cached_acl *acl;
4977 int ret = -ENOENT;
4978
4979 spin_lock(&inode->i_lock);
4980 acl = nfsi->nfs4_acl;
4981 if (acl == NULL)
4982 goto out;
4983 if (buf == NULL) /* user is just asking for length */
4984 goto out_len;
4985 if (acl->cached == 0)
4986 goto out;
4987 ret = -ERANGE; /* see getxattr(2) man page */
4988 if (acl->len > buflen)
4989 goto out;
4990 memcpy(buf, acl->data, acl->len);
4991out_len:
4992 ret = acl->len;
4993out:
4994 spin_unlock(&inode->i_lock);
4995 return ret;
4996}
4997
Sachin Prabhu5794d212012-04-17 14:36:40 +01004998static 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 +00004999{
5000 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005001 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005003 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005004 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005005 if (acl == NULL)
5006 goto out;
5007 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005008 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005009 } else {
5010 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5011 if (acl == NULL)
5012 goto out;
5013 acl->cached = 0;
5014 }
5015 acl->len = acl_len;
5016out:
5017 nfs4_set_cached_acl(inode, acl);
5018}
5019
Andy Adamsonbf118a32011-12-07 11:55:27 -05005020/*
5021 * The getxattr API returns the required buffer length when called with a
5022 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5023 * the required buf. On a NULL buf, we send a page of data to the server
5024 * guessing that the ACL request can be serviced by a page. If so, we cache
5025 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5026 * the cache. If not so, we throw away the page, and cache the required
5027 * length. The next getxattr call will then produce another round trip to
5028 * the server, this time with the input buf of the required size.
5029 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005030static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005031{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005032 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005033 struct nfs_getaclargs args = {
5034 .fh = NFS_FH(inode),
5035 .acl_pages = pages,
5036 .acl_len = buflen,
5037 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005038 struct nfs_getaclres res = {
5039 .acl_len = buflen,
5040 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005041 struct rpc_message msg = {
5042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5043 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005044 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005045 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005046 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5047 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005048
Andy Adamsonbf118a32011-12-07 11:55:27 -05005049 /* As long as we're doing a round trip to the server anyway,
5050 * let's be prepared for a page of acl data. */
5051 if (npages == 0)
5052 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005053 if (npages > ARRAY_SIZE(pages))
5054 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005055
Andy Adamsonbf118a32011-12-07 11:55:27 -05005056 for (i = 0; i < npages; i++) {
5057 pages[i] = alloc_page(GFP_KERNEL);
5058 if (!pages[i])
5059 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005060 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005061
5062 /* for decoding across pages */
5063 res.acl_scratch = alloc_page(GFP_KERNEL);
5064 if (!res.acl_scratch)
5065 goto out_free;
5066
Andy Adamsonbf118a32011-12-07 11:55:27 -05005067 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005068
Peng Taode040be2012-01-10 22:42:47 +08005069 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005070 __func__, buf, buflen, npages, args.acl_len);
5071 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5072 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005073 if (ret)
5074 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005075
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005076 /* Handle the case where the passed-in buffer is too short */
5077 if (res.acl_flags & NFS4_ACL_TRUNC) {
5078 /* Did the user only issue a request for the acl length? */
5079 if (buf == NULL)
5080 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005082 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005083 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005084 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005085 if (buf) {
5086 if (res.acl_len > buflen) {
5087 ret = -ERANGE;
5088 goto out_free;
5089 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005090 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005091 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005092out_ok:
5093 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005095 for (i = 0; i < npages; i++)
5096 if (pages[i])
5097 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005098 if (res.acl_scratch)
5099 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005100 return ret;
5101}
5102
Trond Myklebust16b42892006-08-24 12:27:15 -04005103static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5104{
5105 struct nfs4_exception exception = { };
5106 ssize_t ret;
5107 do {
5108 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005109 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005110 if (ret >= 0)
5111 break;
5112 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5113 } while (exception.retry);
5114 return ret;
5115}
5116
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005117static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5118{
5119 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005120 int ret;
5121
5122 if (!nfs4_server_supports_acls(server))
5123 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005124 ret = nfs_revalidate_inode(server, inode);
5125 if (ret < 0)
5126 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005127 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5128 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005129 ret = nfs4_read_cached_acl(inode, buf, buflen);
5130 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005131 /* -ENOENT is returned if there is no ACL or if there is an ACL
5132 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005133 return ret;
5134 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005135}
5136
Trond Myklebust16b42892006-08-24 12:27:15 -04005137static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005138{
5139 struct nfs_server *server = NFS_SERVER(inode);
5140 struct page *pages[NFS4ACL_MAXPAGES];
5141 struct nfs_setaclargs arg = {
5142 .fh = NFS_FH(inode),
5143 .acl_pages = pages,
5144 .acl_len = buflen,
5145 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005146 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005147 struct rpc_message msg = {
5148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5149 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005150 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005151 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005152 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005153 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005154
5155 if (!nfs4_server_supports_acls(server))
5156 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005157 if (npages > ARRAY_SIZE(pages))
5158 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005159 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005160 if (i < 0)
5161 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005162 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005163 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005164
5165 /*
5166 * Free each page after tx, so the only ref left is
5167 * held by the network stack
5168 */
5169 for (; i > 0; i--)
5170 put_page(pages[i-1]);
5171
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005172 /*
5173 * Acl update can result in inode attribute update.
5174 * so mark the attribute cache invalid.
5175 */
5176 spin_lock(&inode->i_lock);
5177 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5178 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005179 nfs_access_zap_cache(inode);
5180 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005181 return ret;
5182}
5183
Trond Myklebust16b42892006-08-24 12:27:15 -04005184static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5185{
5186 struct nfs4_exception exception = { };
5187 int err;
5188 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005189 err = __nfs4_proc_set_acl(inode, buf, buflen);
5190 trace_nfs4_set_acl(inode, err);
5191 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005192 &exception);
5193 } while (exception.retry);
5194 return err;
5195}
5196
David Quigleyaa9c2662013-05-22 12:50:44 -04005197#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5198static int _nfs4_get_security_label(struct inode *inode, void *buf,
5199 size_t buflen)
5200{
5201 struct nfs_server *server = NFS_SERVER(inode);
5202 struct nfs_fattr fattr;
5203 struct nfs4_label label = {0, 0, buflen, buf};
5204
5205 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005206 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005207 .fh = NFS_FH(inode),
5208 .bitmask = bitmask,
5209 };
5210 struct nfs4_getattr_res res = {
5211 .fattr = &fattr,
5212 .label = &label,
5213 .server = server,
5214 };
5215 struct rpc_message msg = {
5216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005217 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005218 .rpc_resp = &res,
5219 };
5220 int ret;
5221
5222 nfs_fattr_init(&fattr);
5223
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005224 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005225 if (ret)
5226 return ret;
5227 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5228 return -ENOENT;
5229 if (buflen < label.len)
5230 return -ERANGE;
5231 return 0;
5232}
5233
5234static int nfs4_get_security_label(struct inode *inode, void *buf,
5235 size_t buflen)
5236{
5237 struct nfs4_exception exception = { };
5238 int err;
5239
5240 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5241 return -EOPNOTSUPP;
5242
5243 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005244 err = _nfs4_get_security_label(inode, buf, buflen);
5245 trace_nfs4_get_security_label(inode, err);
5246 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005247 &exception);
5248 } while (exception.retry);
5249 return err;
5250}
5251
5252static int _nfs4_do_set_security_label(struct inode *inode,
5253 struct nfs4_label *ilabel,
5254 struct nfs_fattr *fattr,
5255 struct nfs4_label *olabel)
5256{
5257
5258 struct iattr sattr = {0};
5259 struct nfs_server *server = NFS_SERVER(inode);
5260 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005261 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005262 .fh = NFS_FH(inode),
5263 .iap = &sattr,
5264 .server = server,
5265 .bitmask = bitmask,
5266 .label = ilabel,
5267 };
5268 struct nfs_setattrres res = {
5269 .fattr = fattr,
5270 .label = olabel,
5271 .server = server,
5272 };
5273 struct rpc_message msg = {
5274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005275 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005276 .rpc_resp = &res,
5277 };
5278 int status;
5279
Jeff Layton12207f62013-11-01 10:49:32 -04005280 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005281
Jeff Layton12207f62013-11-01 10:49:32 -04005282 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005283 if (status)
5284 dprintk("%s failed: %d\n", __func__, status);
5285
5286 return status;
5287}
5288
5289static int nfs4_do_set_security_label(struct inode *inode,
5290 struct nfs4_label *ilabel,
5291 struct nfs_fattr *fattr,
5292 struct nfs4_label *olabel)
5293{
5294 struct nfs4_exception exception = { };
5295 int err;
5296
5297 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005298 err = _nfs4_do_set_security_label(inode, ilabel,
5299 fattr, olabel);
5300 trace_nfs4_set_security_label(inode, err);
5301 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005302 &exception);
5303 } while (exception.retry);
5304 return err;
5305}
5306
5307static int
Al Viro59301222016-05-27 10:19:30 -04005308nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005309{
5310 struct nfs4_label ilabel, *olabel = NULL;
5311 struct nfs_fattr fattr;
5312 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005313 int status;
5314
5315 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5316 return -EOPNOTSUPP;
5317
5318 nfs_fattr_init(&fattr);
5319
5320 ilabel.pi = 0;
5321 ilabel.lfs = 0;
5322 ilabel.label = (char *)buf;
5323 ilabel.len = buflen;
5324
5325 cred = rpc_lookup_cred();
5326 if (IS_ERR(cred))
5327 return PTR_ERR(cred);
5328
5329 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5330 if (IS_ERR(olabel)) {
5331 status = -PTR_ERR(olabel);
5332 goto out;
5333 }
5334
5335 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5336 if (status == 0)
5337 nfs_setsecurity(inode, &fattr, olabel);
5338
5339 nfs4_label_free(olabel);
5340out:
5341 put_rpccred(cred);
5342 return status;
5343}
5344#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5345
5346
Chuck Leverf0920752012-05-21 22:45:41 -04005347static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5348 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005349{
5350 __be32 verf[2];
5351
Chuck Lever2c820d92012-05-21 22:45:33 -04005352 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5353 /* An impossible timestamp guarantees this value
5354 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005355 verf[0] = cpu_to_be32(U32_MAX);
5356 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005357 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005358 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005359 u64 ns = ktime_to_ns(nn->boot_time);
5360
5361 verf[0] = cpu_to_be32(ns >> 32);
5362 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005363 }
Chuck Levercd937102012-03-02 17:14:31 -05005364 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5365}
5366
Jeff Laytona3192682015-06-09 19:43:59 -04005367static int
5368nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005369{
Jeff Laytona3192682015-06-09 19:43:59 -04005370 size_t len;
5371 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005372
Trond Myklebustceb3a162015-01-03 15:16:04 -05005373 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005374 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005375
Jeff Laytona3192682015-06-09 19:43:59 -04005376 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005377 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005378 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5379 1 +
5380 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5381 1;
5382 rcu_read_unlock();
5383
5384 if (len > NFS4_OPAQUE_LIMIT + 1)
5385 return -EINVAL;
5386
5387 /*
5388 * Since this string is allocated at mount time, and held until the
5389 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5390 * about a memory-reclaim deadlock.
5391 */
5392 str = kmalloc(len, GFP_KERNEL);
5393 if (!str)
5394 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005395
Chuck Levere984a552012-09-14 17:24:21 -04005396 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005397 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005398 clp->cl_ipaddr,
5399 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5400 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005401 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005402
Jeff Laytona3192682015-06-09 19:43:59 -04005403 clp->cl_owner_id = str;
5404 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005405}
5406
Jeff Layton873e3852015-06-09 19:44:00 -04005407static int
5408nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005409{
Jeff Layton873e3852015-06-09 19:44:00 -04005410 size_t len;
5411 char *str;
5412
5413 len = 10 + 10 + 1 + 10 + 1 +
5414 strlen(nfs4_client_id_uniquifier) + 1 +
5415 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5416
5417 if (len > NFS4_OPAQUE_LIMIT + 1)
5418 return -EINVAL;
5419
5420 /*
5421 * Since this string is allocated at mount time, and held until the
5422 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5423 * about a memory-reclaim deadlock.
5424 */
5425 str = kmalloc(len, GFP_KERNEL);
5426 if (!str)
5427 return -ENOMEM;
5428
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005429 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005430 clp->rpc_ops->version, clp->cl_minorversion,
5431 nfs4_client_id_uniquifier,
5432 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005433 clp->cl_owner_id = str;
5434 return 0;
5435}
5436
5437static int
5438nfs4_init_uniform_client_string(struct nfs_client *clp)
5439{
Jeff Layton873e3852015-06-09 19:44:00 -04005440 size_t len;
5441 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005442
5443 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005444 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005445
5446 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005447 return nfs4_init_uniquifier_client_string(clp);
5448
5449 len = 10 + 10 + 1 + 10 + 1 +
5450 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5451
5452 if (len > NFS4_OPAQUE_LIMIT + 1)
5453 return -EINVAL;
5454
5455 /*
5456 * Since this string is allocated at mount time, and held until the
5457 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5458 * about a memory-reclaim deadlock.
5459 */
5460 str = kmalloc(len, GFP_KERNEL);
5461 if (!str)
5462 return -ENOMEM;
5463
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005464 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005465 clp->rpc_ops->version, clp->cl_minorversion,
5466 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005467 clp->cl_owner_id = str;
5468 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005469}
5470
Chuck Lever706cb8d2014-03-12 12:51:47 -04005471/*
5472 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5473 * services. Advertise one based on the address family of the
5474 * clientaddr.
5475 */
5476static unsigned int
5477nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5478{
5479 if (strchr(clp->cl_ipaddr, ':') != NULL)
5480 return scnprintf(buf, len, "tcp6");
5481 else
5482 return scnprintf(buf, len, "tcp");
5483}
5484
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005485static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5486{
5487 struct nfs4_setclientid *sc = calldata;
5488
5489 if (task->tk_status == 0)
5490 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5491}
5492
5493static const struct rpc_call_ops nfs4_setclientid_ops = {
5494 .rpc_call_done = nfs4_setclientid_done,
5495};
5496
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005497/**
5498 * nfs4_proc_setclientid - Negotiate client ID
5499 * @clp: state data structure
5500 * @program: RPC program for NFSv4 callback service
5501 * @port: IP port number for NFS4 callback service
5502 * @cred: RPC credential to use for this call
5503 * @res: where to place the result
5504 *
5505 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5506 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005507int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5508 unsigned short port, struct rpc_cred *cred,
5509 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
5511 nfs4_verifier sc_verifier;
5512 struct nfs4_setclientid setclientid = {
5513 .sc_verifier = &sc_verifier,
5514 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005515 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 };
5517 struct rpc_message msg = {
5518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5519 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005520 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005521 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005523 struct rpc_task *task;
5524 struct rpc_task_setup task_setup_data = {
5525 .rpc_client = clp->cl_rpcclient,
5526 .rpc_message = &msg,
5527 .callback_ops = &nfs4_setclientid_ops,
5528 .callback_data = &setclientid,
5529 .flags = RPC_TASK_TIMEOUT,
5530 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005531 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532
Chuck Leverde734832012-07-11 16:30:50 -04005533 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005534 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005535
5536 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5537 status = nfs4_init_uniform_client_string(clp);
5538 else
Jeff Laytona3192682015-06-09 19:43:59 -04005539 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005540
5541 if (status)
5542 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005543
Chuck Leverde734832012-07-11 16:30:50 -04005544 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005545 setclientid.sc_netid_len =
5546 nfs4_init_callback_netid(clp,
5547 setclientid.sc_netid,
5548 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005549 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005550 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 clp->cl_ipaddr, port >> 8, port & 255);
5552
Jeff Layton3a6bb732015-06-09 19:43:57 -04005553 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005554 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005555 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005556 task = rpc_run_task(&task_setup_data);
5557 if (IS_ERR(task)) {
5558 status = PTR_ERR(task);
5559 goto out;
5560 }
5561 status = task->tk_status;
5562 if (setclientid.sc_cred) {
5563 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5564 put_rpccred(setclientid.sc_cred);
5565 }
5566 rpc_put_task(task);
5567out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005568 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005569 dprintk("NFS reply setclientid: %d\n", status);
5570 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571}
5572
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005573/**
5574 * nfs4_proc_setclientid_confirm - Confirm client ID
5575 * @clp: state data structure
5576 * @res: result of a previous SETCLIENTID
5577 * @cred: RPC credential to use for this call
5578 *
5579 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5580 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005581int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005582 struct nfs4_setclientid_res *arg,
5583 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 struct rpc_message msg = {
5586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005587 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005588 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 int status;
5591
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005592 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5593 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5594 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005595 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005596 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005597 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 return status;
5599}
5600
Trond Myklebustfe650402006-01-03 09:55:18 +01005601struct nfs4_delegreturndata {
5602 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005603 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005604 struct nfs_fh fh;
5605 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005606 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005607 struct {
5608 struct nfs4_layoutreturn_args arg;
5609 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005610 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005611 u32 roc_barrier;
5612 bool roc;
5613 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005614 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005615 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005616 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005617};
5618
Trond Myklebustfe650402006-01-03 09:55:18 +01005619static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5620{
5621 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005622
Trond Myklebust14516c32010-07-31 14:29:06 -04005623 if (!nfs4_sequence_done(task, &data->res.seq_res))
5624 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005625
Trond Myklebustca8acf82013-08-13 10:36:56 -04005626 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005627
5628 /* Handle Layoutreturn errors */
5629 if (data->args.lr_args && task->tk_status != 0) {
5630 switch(data->res.lr_ret) {
5631 default:
5632 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5633 break;
5634 case 0:
5635 data->args.lr_args = NULL;
5636 data->res.lr_res = NULL;
5637 break;
5638 case -NFS4ERR_ADMIN_REVOKED:
5639 case -NFS4ERR_DELEG_REVOKED:
5640 case -NFS4ERR_EXPIRED:
5641 case -NFS4ERR_BAD_STATEID:
5642 case -NFS4ERR_OLD_STATEID:
5643 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5644 case -NFS4ERR_WRONG_CRED:
5645 data->args.lr_args = NULL;
5646 data->res.lr_res = NULL;
5647 data->res.lr_ret = 0;
5648 rpc_restart_call_prepare(task);
5649 return;
5650 }
5651 }
5652
Ricardo Labiaga79708862009-12-07 09:23:21 -05005653 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005654 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005655 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005656 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005657 case -NFS4ERR_ADMIN_REVOKED:
5658 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005659 case -NFS4ERR_EXPIRED:
5660 nfs4_free_revoked_stateid(data->res.server,
5661 data->args.stateid,
5662 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005663 case -NFS4ERR_BAD_STATEID:
5664 case -NFS4ERR_OLD_STATEID:
5665 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005666 task->tk_status = 0;
5667 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005668 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005669 if (nfs4_async_handle_error(task, data->res.server,
5670 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005671 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005672 return;
5673 }
5674 }
5675 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005676}
5677
5678static void nfs4_delegreturn_release(void *calldata)
5679{
Peng Tao039b7562014-07-03 13:05:02 +08005680 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005681 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005682
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005683 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005684 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005685 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5686 data->res.lr_ret);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005687 nfs_iput_and_deactive(inode);
5688 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005689 kfree(calldata);
5690}
5691
Andy Adamson938e1012009-04-01 09:22:28 -04005692static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5693{
5694 struct nfs4_delegreturndata *d_data;
5695
5696 d_data = (struct nfs4_delegreturndata *)data;
5697
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005698 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005699 return;
5700
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005701 nfs4_setup_sequence(d_data->res.server,
5702 &d_data->args.seq_args,
5703 &d_data->res.seq_res,
5704 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005705}
Andy Adamson938e1012009-04-01 09:22:28 -04005706
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005707static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005708 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005709 .rpc_call_done = nfs4_delegreturn_done,
5710 .rpc_release = nfs4_delegreturn_release,
5711};
5712
Trond Myklebuste6f81072008-01-24 18:14:34 -05005713static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005714{
5715 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005716 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005717 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005718 struct rpc_message msg = {
5719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5720 .rpc_cred = cred,
5721 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005722 struct rpc_task_setup task_setup_data = {
5723 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005724 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005725 .callback_ops = &nfs4_delegreturn_ops,
5726 .flags = RPC_TASK_ASYNC,
5727 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005728 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005729
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005730 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005731 if (data == NULL)
5732 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005733 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005734
5735 nfs4_state_protect(server->nfs_client,
5736 NFS_SP4_MACH_CRED_CLEANUP,
5737 &task_setup_data.rpc_client, &msg);
5738
Trond Myklebustfe650402006-01-03 09:55:18 +01005739 data->args.fhandle = &data->fh;
5740 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005741 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005742 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005743 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005744 data->res.fattr = &data->fattr;
5745 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005746 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005747 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005748 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005749 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005750 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005751 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005752 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005753 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005754 if (data->lr.roc) {
5755 data->args.lr_args = &data->lr.arg;
5756 data->res.lr_res = &data->lr.res;
5757 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005758 } else if (data->lr.roc) {
5759 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5760 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005761 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005762
Trond Myklebustc970aa82007-07-14 15:39:59 -04005763 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005764 msg.rpc_argp = &data->args;
5765 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005766 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005767 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005768 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005769 if (!issync)
5770 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005771 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005772 if (status != 0)
5773 goto out;
5774 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005775 if (status == 0)
5776 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5777 else
5778 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005779out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005780 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005781 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782}
5783
Trond Myklebuste6f81072008-01-24 18:14:34 -05005784int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785{
5786 struct nfs_server *server = NFS_SERVER(inode);
5787 struct nfs4_exception exception = { };
5788 int err;
5789 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005790 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005791 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 switch (err) {
5793 case -NFS4ERR_STALE_STATEID:
5794 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 case 0:
5796 return 0;
5797 }
5798 err = nfs4_handle_exception(server, err, &exception);
5799 } while (exception.retry);
5800 return err;
5801}
5802
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5804{
5805 struct inode *inode = state->inode;
5806 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005807 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005808 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005810 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005812 struct nfs_lockt_res res = {
5813 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 };
5815 struct rpc_message msg = {
5816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5817 .rpc_argp = &arg,
5818 .rpc_resp = &res,
5819 .rpc_cred = state->owner->so_cred,
5820 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 struct nfs4_lock_state *lsp;
5822 int status;
5823
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005824 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005825 status = nfs4_set_lock_state(state, request);
5826 if (status != 0)
5827 goto out;
5828 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005829 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005830 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005831 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005832 switch (status) {
5833 case 0:
5834 request->fl_type = F_UNLCK;
5835 break;
5836 case -NFS4ERR_DENIED:
5837 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005839 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005840 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005841out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 return status;
5843}
5844
5845static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5846{
5847 struct nfs4_exception exception = { };
5848 int err;
5849
5850 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005851 err = _nfs4_proc_getlk(state, cmd, request);
5852 trace_nfs4_get_lock(request, state, cmd, err);
5853 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 &exception);
5855 } while (exception.retry);
5856 return err;
5857}
5858
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005859struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005860 struct nfs_locku_args arg;
5861 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862 struct nfs4_lock_state *lsp;
5863 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005864 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005865 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005866 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005867};
5868
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005869static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5870 struct nfs_open_context *ctx,
5871 struct nfs4_lock_state *lsp,
5872 struct nfs_seqid *seqid)
5873{
5874 struct nfs4_unlockdata *p;
5875 struct inode *inode = lsp->ls_state->inode;
5876
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005877 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005878 if (p == NULL)
5879 return NULL;
5880 p->arg.fh = NFS_FH(inode);
5881 p->arg.fl = &p->fl;
5882 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005883 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005884 p->lsp = lsp;
5885 atomic_inc(&lsp->ls_count);
5886 /* Ensure we don't close file until we're done freeing locks! */
5887 p->ctx = get_nfs_open_context(ctx);
5888 memcpy(&p->fl, fl, sizeof(p->fl));
5889 p->server = NFS_SERVER(inode);
5890 return p;
5891}
5892
Trond Myklebust06f814a2006-01-03 09:55:07 +01005893static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005894{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005895 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005896 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005897 nfs4_put_lock_state(calldata->lsp);
5898 put_nfs_open_context(calldata->ctx);
5899 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005900}
5901
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005902static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005903{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005904 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005905
Trond Myklebust14516c32010-07-31 14:29:06 -04005906 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5907 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005908 switch (task->tk_status) {
5909 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005910 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005911 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005912 if (nfs4_update_lock_stateid(calldata->lsp,
5913 &calldata->res.stateid))
5914 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005915 case -NFS4ERR_ADMIN_REVOKED:
5916 case -NFS4ERR_EXPIRED:
5917 nfs4_free_revoked_stateid(calldata->server,
5918 &calldata->arg.stateid,
5919 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005920 case -NFS4ERR_BAD_STATEID:
5921 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005922 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005923 if (!nfs4_stateid_match(&calldata->arg.stateid,
5924 &calldata->lsp->ls_stateid))
5925 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005926 break;
5927 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005928 if (nfs4_async_handle_error(task, calldata->server,
5929 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005930 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005931 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005932 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005933}
5934
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005935static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005936{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005937 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005939 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005940 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005941 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005942 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005943 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005944 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005946 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04005947 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005948 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005949 &calldata->res.seq_res,
5950 task) != 0)
5951 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005952 return;
5953out_no_action:
5954 task->tk_action = NULL;
5955out_wait:
5956 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957}
5958
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005959static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005960 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005961 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005962 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005963};
5964
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005965static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5966 struct nfs_open_context *ctx,
5967 struct nfs4_lock_state *lsp,
5968 struct nfs_seqid *seqid)
5969{
5970 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5973 .rpc_cred = ctx->cred,
5974 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005975 struct rpc_task_setup task_setup_data = {
5976 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005977 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005978 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005979 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005980 .flags = RPC_TASK_ASYNC,
5981 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005982
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005983 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5984 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5985
Frank Filz137d6ac2007-07-09 15:32:29 -07005986 /* Ensure this is an unlock - when canceling a lock, the
5987 * canceled lock is passed in, and it won't be an unlock.
5988 */
5989 fl->fl_type = F_UNLCK;
5990
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5992 if (data == NULL) {
5993 nfs_free_seqid(seqid);
5994 return ERR_PTR(-ENOMEM);
5995 }
5996
Chuck Levera9c92d62013-08-09 12:48:18 -04005997 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005998 msg.rpc_argp = &data->arg;
5999 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006000 task_setup_data.callback_data = data;
6001 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006002}
6003
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6005{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006006 struct inode *inode = state->inode;
6007 struct nfs4_state_owner *sp = state->owner;
6008 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006009 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006010 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006011 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006012 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006013 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006014 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
Trond Myklebust9b073572006-06-29 16:38:34 -04006016 status = nfs4_set_lock_state(state, request);
6017 /* Unlock _before_ we do the RPC call */
6018 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006019 /* Exclude nfs_delegation_claim_locks() */
6020 mutex_lock(&sp->so_delegreturn_mutex);
6021 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006022 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006023 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006024 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006025 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006026 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006027 }
6028 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006029 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006030 if (status != 0)
6031 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006032 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006033 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006034 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6035 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006036 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6037 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006038 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006039 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006040 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006041 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006042 status = PTR_ERR(task);
6043 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006044 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006045 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006046 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006047out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006048 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006049 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006050 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051}
6052
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006053struct nfs4_lockdata {
6054 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006055 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006056 struct nfs4_lock_state *lsp;
6057 struct nfs_open_context *ctx;
6058 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006059 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006060 int rpc_status;
6061 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006062 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006063};
6064
6065static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006066 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6067 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006068{
6069 struct nfs4_lockdata *p;
6070 struct inode *inode = lsp->ls_state->inode;
6071 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006072 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006073
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006074 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006075 if (p == NULL)
6076 return NULL;
6077
6078 p->arg.fh = NFS_FH(inode);
6079 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006080 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006081 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006082 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006083 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6084 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006085 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006086 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006087 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006088 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006089 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006090 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006092 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006093 atomic_inc(&lsp->ls_count);
6094 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006095 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006096 memcpy(&p->fl, fl, sizeof(p->fl));
6097 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006098out_free_seqid:
6099 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006100out_free:
6101 kfree(p);
6102 return NULL;
6103}
6104
6105static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6106{
6107 struct nfs4_lockdata *data = calldata;
6108 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
Harvey Harrison3110ff82008-05-02 13:42:44 -07006110 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006111 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006112 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006113 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006114 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006115 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006116 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006117 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006118 nfs4_stateid_copy(&data->arg.open_stateid,
6119 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006120 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006121 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006122 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006123 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006124 nfs4_stateid_copy(&data->arg.lock_stateid,
6125 &data->lsp->ls_stateid);
6126 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006127 if (!nfs4_valid_open_stateid(state)) {
6128 data->rpc_status = -EBADF;
6129 task->tk_action = NULL;
6130 goto out_release_open_seqid;
6131 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006132 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04006133 if (nfs4_setup_sequence(data->server,
6134 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006135 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006136 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006137 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006138out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006139 nfs_release_seqid(data->arg.open_seqid);
6140out_release_lock_seqid:
6141 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006142out_wait:
6143 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006144 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006145}
6146
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006147static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6148{
6149 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006150 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151
Harvey Harrison3110ff82008-05-02 13:42:44 -07006152 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006153
Trond Myklebust14516c32010-07-31 14:29:06 -04006154 if (!nfs4_sequence_done(task, &data->res.seq_res))
6155 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006156
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006157 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006158 switch (task->tk_status) {
6159 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006160 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006161 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006162 if (data->arg.new_lock) {
6163 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006164 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006165 rpc_restart_call_prepare(task);
6166 break;
6167 }
6168 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006169 if (data->arg.new_lock_owner != 0) {
6170 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6171 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6172 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6173 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6174 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006175 break;
6176 case -NFS4ERR_BAD_STATEID:
6177 case -NFS4ERR_OLD_STATEID:
6178 case -NFS4ERR_STALE_STATEID:
6179 case -NFS4ERR_EXPIRED:
6180 if (data->arg.new_lock_owner != 0) {
6181 if (!nfs4_stateid_match(&data->arg.open_stateid,
6182 &lsp->ls_state->open_stateid))
6183 rpc_restart_call_prepare(task);
6184 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6185 &lsp->ls_stateid))
6186 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006187 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006188 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006189}
6190
6191static void nfs4_lock_release(void *calldata)
6192{
6193 struct nfs4_lockdata *data = calldata;
6194
Harvey Harrison3110ff82008-05-02 13:42:44 -07006195 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006196 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006197 if (data->cancelled != 0) {
6198 struct rpc_task *task;
6199 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6200 data->arg.lock_seqid);
6201 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006202 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006203 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006204 } else
6205 nfs_free_seqid(data->arg.lock_seqid);
6206 nfs4_put_lock_state(data->lsp);
6207 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04006208 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006210 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006211}
6212
6213static const struct rpc_call_ops nfs4_lock_ops = {
6214 .rpc_call_prepare = nfs4_lock_prepare,
6215 .rpc_call_done = nfs4_lock_done,
6216 .rpc_release = nfs4_lock_release,
6217};
6218
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006219static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6220{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006221 switch (error) {
6222 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006223 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006224 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006225 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006226 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006227 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006228 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006229 break;
6230 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006231 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006232 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006233 };
6234}
6235
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006236static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006237{
6238 struct nfs4_lockdata *data;
6239 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006240 struct rpc_message msg = {
6241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6242 .rpc_cred = state->owner->so_cred,
6243 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006244 struct rpc_task_setup task_setup_data = {
6245 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006246 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006247 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006248 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006249 .flags = RPC_TASK_ASYNC,
6250 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006251 int ret;
6252
Harvey Harrison3110ff82008-05-02 13:42:44 -07006253 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006254 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006255 fl->fl_u.nfs4_fl.owner,
6256 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257 if (data == NULL)
6258 return -ENOMEM;
6259 if (IS_SETLKW(cmd))
6260 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006261 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006262 msg.rpc_argp = &data->arg;
6263 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006264 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006265 if (recovery_type > NFS_LOCK_NEW) {
6266 if (recovery_type == NFS_LOCK_RECLAIM)
6267 data->arg.reclaim = NFS_LOCK_RECLAIM;
6268 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006269 } else
6270 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006271 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006272 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006273 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274 ret = nfs4_wait_for_completion_rpc_task(task);
6275 if (ret == 0) {
6276 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006277 if (ret)
6278 nfs4_handle_setlk_error(data->server, data->lsp,
6279 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006280 } else
6281 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006282 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006283 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006284 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286}
6287
6288static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6289{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006290 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006291 struct nfs4_exception exception = {
6292 .inode = state->inode,
6293 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006294 int err;
6295
6296 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006297 /* Cache the lock if possible... */
6298 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6299 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006300 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006301 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006302 break;
6303 nfs4_handle_exception(server, err, &exception);
6304 } while (exception.retry);
6305 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
6308static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6309{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006310 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006311 struct nfs4_exception exception = {
6312 .inode = state->inode,
6313 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006314 int err;
6315
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006316 err = nfs4_set_lock_state(state, request);
6317 if (err != 0)
6318 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006319 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006320 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6321 return 0;
6322 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006323 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006324 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6325 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006326 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006327 switch (err) {
6328 default:
6329 goto out;
6330 case -NFS4ERR_GRACE:
6331 case -NFS4ERR_DELAY:
6332 nfs4_handle_exception(server, err, &exception);
6333 err = 0;
6334 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006335 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006336out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006337 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338}
6339
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006340#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006341static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6342{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006343 struct nfs4_lock_state *lsp;
6344 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006345
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006346 status = nfs4_set_lock_state(state, request);
6347 if (status != 0)
6348 return status;
6349 lsp = request->fl_u.nfs4_fl.owner;
6350 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6351 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6352 return 0;
6353 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006354 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006355}
6356#endif
6357
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6359{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006360 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006361 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006362 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006363 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Trond Myklebust01c3b862006-06-29 16:38:39 -04006365 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006366 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006367 if (status < 0)
6368 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006369 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006370 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006371 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006372 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006373 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006374 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006375 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006376 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006377 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006378 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006379 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006380 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006381 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006382 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006383out:
6384 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 return status;
6386}
6387
6388static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6389{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006390 struct nfs4_exception exception = {
6391 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006392 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006393 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 int err;
6395
6396 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006397 err = _nfs4_proc_setlk(state, cmd, request);
6398 if (err == -NFS4ERR_DENIED)
6399 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006401 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 } while (exception.retry);
6403 return err;
6404}
6405
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006406#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6407#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6408
6409static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006410nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6411 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006412{
6413 int status = -ERESTARTSYS;
6414 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6415
6416 while(!signalled()) {
6417 status = nfs4_proc_setlk(state, cmd, request);
6418 if ((status != -EAGAIN) || IS_SETLK(cmd))
6419 break;
6420 freezable_schedule_timeout_interruptible(timeout);
6421 timeout *= 2;
6422 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6423 status = -ERESTARTSYS;
6424 }
6425 return status;
6426}
6427
Jeff Laytona1d617d82016-09-17 18:17:39 -04006428#ifdef CONFIG_NFS_V4_1
6429struct nfs4_lock_waiter {
6430 struct task_struct *task;
6431 struct inode *inode;
6432 struct nfs_lowner *owner;
6433 bool notified;
6434};
6435
6436static int
6437nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6438{
6439 int ret;
6440 struct cb_notify_lock_args *cbnl = key;
6441 struct nfs4_lock_waiter *waiter = wait->private;
6442 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6443 *wowner = waiter->owner;
6444
6445 /* Only wake if the callback was for the same owner */
6446 if (lowner->clientid != wowner->clientid ||
6447 lowner->id != wowner->id ||
6448 lowner->s_dev != wowner->s_dev)
6449 return 0;
6450
6451 /* Make sure it's for the right inode */
6452 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6453 return 0;
6454
6455 waiter->notified = true;
6456
6457 /* override "private" so we can use default_wake_function */
6458 wait->private = waiter->task;
6459 ret = autoremove_wake_function(wait, mode, flags, key);
6460 wait->private = waiter;
6461 return ret;
6462}
6463
6464static int
6465nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6466{
6467 int status = -ERESTARTSYS;
6468 unsigned long flags;
6469 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6470 struct nfs_server *server = NFS_SERVER(state->inode);
6471 struct nfs_client *clp = server->nfs_client;
6472 wait_queue_head_t *q = &clp->cl_lock_waitq;
6473 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6474 .id = lsp->ls_seqid.owner_id,
6475 .s_dev = server->s_dev };
6476 struct nfs4_lock_waiter waiter = { .task = current,
6477 .inode = state->inode,
6478 .owner = &owner,
6479 .notified = false };
6480 wait_queue_t wait;
6481
6482 /* Don't bother with waitqueue if we don't expect a callback */
6483 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6484 return nfs4_retry_setlk_simple(state, cmd, request);
6485
6486 init_wait(&wait);
6487 wait.private = &waiter;
6488 wait.func = nfs4_wake_lock_waiter;
6489 add_wait_queue(q, &wait);
6490
6491 while(!signalled()) {
6492 status = nfs4_proc_setlk(state, cmd, request);
6493 if ((status != -EAGAIN) || IS_SETLK(cmd))
6494 break;
6495
6496 status = -ERESTARTSYS;
6497 spin_lock_irqsave(&q->lock, flags);
6498 if (waiter.notified) {
6499 spin_unlock_irqrestore(&q->lock, flags);
6500 continue;
6501 }
6502 set_current_state(TASK_INTERRUPTIBLE);
6503 spin_unlock_irqrestore(&q->lock, flags);
6504
6505 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6506 }
6507
6508 finish_wait(q, &wait);
6509 return status;
6510}
6511#else /* !CONFIG_NFS_V4_1 */
6512static inline int
6513nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6514{
6515 return nfs4_retry_setlk_simple(state, cmd, request);
6516}
6517#endif
6518
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519static int
6520nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6521{
6522 struct nfs_open_context *ctx;
6523 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524 int status;
6525
6526 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006527 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 state = ctx->state;
6529
6530 if (request->fl_start < 0 || request->fl_end < 0)
6531 return -EINVAL;
6532
Trond Myklebustd9531262009-07-21 19:22:38 -04006533 if (IS_GETLK(cmd)) {
6534 if (state != NULL)
6535 return nfs4_proc_getlk(state, F_GETLK, request);
6536 return 0;
6537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538
6539 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6540 return -EINVAL;
6541
Trond Myklebustd9531262009-07-21 19:22:38 -04006542 if (request->fl_type == F_UNLCK) {
6543 if (state != NULL)
6544 return nfs4_proc_unlck(state, cmd, request);
6545 return 0;
6546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547
Trond Myklebustd9531262009-07-21 19:22:38 -04006548 if (state == NULL)
6549 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006550
6551 if ((request->fl_flags & FL_POSIX) &&
6552 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6553 return -ENOLCK;
6554
Trond Myklebust55725512012-04-18 12:48:35 -04006555 /*
6556 * Don't rely on the VFS having checked the file open mode,
6557 * since it won't do this for flock() locks.
6558 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006559 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006560 case F_RDLCK:
6561 if (!(filp->f_mode & FMODE_READ))
6562 return -EBADF;
6563 break;
6564 case F_WRLCK:
6565 if (!(filp->f_mode & FMODE_WRITE))
6566 return -EBADF;
6567 }
6568
Jeff Layton1ea67db2016-09-17 18:17:37 -04006569 status = nfs4_set_lock_state(state, request);
6570 if (status != 0)
6571 return status;
6572
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006573 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574}
6575
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006576int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006577{
6578 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006579 int err;
6580
6581 err = nfs4_set_lock_state(state, fl);
6582 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006583 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006584 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006585 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006586}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006587
Trond Myklebustcf470c32012-03-07 13:49:12 -05006588struct nfs_release_lockowner_data {
6589 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006590 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006591 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006592 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006593 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006594};
6595
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006596static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6597{
6598 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006599 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006600 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6601 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006602 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006603 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006604}
6605
6606static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6607{
6608 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006609 struct nfs_server *server = data->server;
6610
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006611 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006612
6613 switch (task->tk_status) {
6614 case 0:
6615 renew_lease(server, data->timestamp);
6616 break;
6617 case -NFS4ERR_STALE_CLIENTID:
6618 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006619 nfs4_schedule_lease_recovery(server->nfs_client);
6620 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006621 case -NFS4ERR_LEASE_MOVED:
6622 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006623 if (nfs4_async_handle_error(task, server,
6624 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006625 rpc_restart_call_prepare(task);
6626 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006627}
6628
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006629static void nfs4_release_lockowner_release(void *calldata)
6630{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006631 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006632 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006633 kfree(calldata);
6634}
6635
Trond Myklebust17280172012-03-11 13:11:00 -04006636static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006637 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6638 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006639 .rpc_release = nfs4_release_lockowner_release,
6640};
6641
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006642static void
6643nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006644{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006645 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006646 struct rpc_message msg = {
6647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6648 };
6649
6650 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006651 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006652
Trond Myklebustcf470c32012-03-07 13:49:12 -05006653 data = kmalloc(sizeof(*data), GFP_NOFS);
6654 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006655 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006656 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006657 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006658 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6659 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6660 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006661
Trond Myklebustcf470c32012-03-07 13:49:12 -05006662 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006663 msg.rpc_resp = &data->res;
6664 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006665 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006666}
6667
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006668#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6669
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006670static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006671 struct dentry *unused, struct inode *inode,
6672 const char *key, const void *buf,
6673 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006674{
Al Viro59301222016-05-27 10:19:30 -04006675 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006676}
6677
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006678static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006679 struct dentry *unused, struct inode *inode,
6680 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006681{
Al Virob2968212016-04-10 20:48:24 -04006682 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006683}
6684
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006685static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006686{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006687 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006688}
6689
David Quigleyc9bccef2013-05-22 12:50:45 -04006690#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006691
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006692static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006693 struct dentry *unused, struct inode *inode,
6694 const char *key, const void *buf,
6695 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006696{
6697 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006698 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006699
6700 return -EOPNOTSUPP;
6701}
6702
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006703static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006704 struct dentry *unused, struct inode *inode,
6705 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006706{
6707 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006708 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006709 return -EOPNOTSUPP;
6710}
6711
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006712static ssize_t
6713nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006714{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006715 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006716
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006717 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6718 len = security_inode_listsecurity(inode, list, list_len);
6719 if (list_len && len > list_len)
6720 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006721 }
6722 return len;
6723}
6724
6725static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6726 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006727 .get = nfs4_xattr_get_nfs4_label,
6728 .set = nfs4_xattr_set_nfs4_label,
6729};
David Quigleyc9bccef2013-05-22 12:50:45 -04006730
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006731#else
6732
6733static ssize_t
6734nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6735{
6736 return 0;
6737}
6738
6739#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006740
Andy Adamson533eb462011-06-13 18:25:56 -04006741/*
6742 * nfs_fhget will use either the mounted_on_fileid or the fileid
6743 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006744static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6745{
Andy Adamson533eb462011-06-13 18:25:56 -04006746 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6747 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6748 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006749 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006750 return;
6751
6752 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006753 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006754 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6755 fattr->nlink = 2;
6756}
6757
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006758static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6759 const struct qstr *name,
6760 struct nfs4_fs_locations *fs_locations,
6761 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006762{
6763 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006764 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006765 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006766 };
6767 struct nfs4_fs_locations_arg args = {
6768 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006769 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006770 .page = page,
6771 .bitmask = bitmask,
6772 };
Benny Halevy22958462009-04-01 09:22:02 -04006773 struct nfs4_fs_locations_res res = {
6774 .fs_locations = fs_locations,
6775 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006776 struct rpc_message msg = {
6777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6778 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006779 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006780 };
6781 int status;
6782
Harvey Harrison3110ff82008-05-02 13:42:44 -07006783 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006784
6785 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6786 * is not supported */
6787 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6788 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6789 else
6790 bitmask[0] |= FATTR4_WORD0_FILEID;
6791
Trond Myklebustc228fd32007-01-13 02:28:11 -05006792 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006793 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006794 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006795 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006796 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006797 return status;
6798}
6799
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006800int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6801 const struct qstr *name,
6802 struct nfs4_fs_locations *fs_locations,
6803 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006804{
6805 struct nfs4_exception exception = { };
6806 int err;
6807 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006808 err = _nfs4_proc_fs_locations(client, dir, name,
6809 fs_locations, page);
6810 trace_nfs4_get_fs_locations(dir, name, err);
6811 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006812 &exception);
6813 } while (exception.retry);
6814 return err;
6815}
6816
Chuck Leverb03d7352013-10-17 14:12:50 -04006817/*
6818 * This operation also signals the server that this client is
6819 * performing migration recovery. The server can stop returning
6820 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6821 * appended to this compound to identify the client ID which is
6822 * performing recovery.
6823 */
6824static int _nfs40_proc_get_locations(struct inode *inode,
6825 struct nfs4_fs_locations *locations,
6826 struct page *page, struct rpc_cred *cred)
6827{
6828 struct nfs_server *server = NFS_SERVER(inode);
6829 struct rpc_clnt *clnt = server->client;
6830 u32 bitmask[2] = {
6831 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6832 };
6833 struct nfs4_fs_locations_arg args = {
6834 .clientid = server->nfs_client->cl_clientid,
6835 .fh = NFS_FH(inode),
6836 .page = page,
6837 .bitmask = bitmask,
6838 .migration = 1, /* skip LOOKUP */
6839 .renew = 1, /* append RENEW */
6840 };
6841 struct nfs4_fs_locations_res res = {
6842 .fs_locations = locations,
6843 .migration = 1,
6844 .renew = 1,
6845 };
6846 struct rpc_message msg = {
6847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6848 .rpc_argp = &args,
6849 .rpc_resp = &res,
6850 .rpc_cred = cred,
6851 };
6852 unsigned long now = jiffies;
6853 int status;
6854
6855 nfs_fattr_init(&locations->fattr);
6856 locations->server = server;
6857 locations->nlocations = 0;
6858
6859 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6860 nfs4_set_sequence_privileged(&args.seq_args);
6861 status = nfs4_call_sync_sequence(clnt, server, &msg,
6862 &args.seq_args, &res.seq_res);
6863 if (status)
6864 return status;
6865
6866 renew_lease(server, now);
6867 return 0;
6868}
6869
6870#ifdef CONFIG_NFS_V4_1
6871
6872/*
6873 * This operation also signals the server that this client is
6874 * performing migration recovery. The server can stop asserting
6875 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6876 * performing this operation is identified in the SEQUENCE
6877 * operation in this compound.
6878 *
6879 * When the client supports GETATTR(fs_locations_info), it can
6880 * be plumbed in here.
6881 */
6882static int _nfs41_proc_get_locations(struct inode *inode,
6883 struct nfs4_fs_locations *locations,
6884 struct page *page, struct rpc_cred *cred)
6885{
6886 struct nfs_server *server = NFS_SERVER(inode);
6887 struct rpc_clnt *clnt = server->client;
6888 u32 bitmask[2] = {
6889 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6890 };
6891 struct nfs4_fs_locations_arg args = {
6892 .fh = NFS_FH(inode),
6893 .page = page,
6894 .bitmask = bitmask,
6895 .migration = 1, /* skip LOOKUP */
6896 };
6897 struct nfs4_fs_locations_res res = {
6898 .fs_locations = locations,
6899 .migration = 1,
6900 };
6901 struct rpc_message msg = {
6902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6903 .rpc_argp = &args,
6904 .rpc_resp = &res,
6905 .rpc_cred = cred,
6906 };
6907 int status;
6908
6909 nfs_fattr_init(&locations->fattr);
6910 locations->server = server;
6911 locations->nlocations = 0;
6912
6913 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6914 nfs4_set_sequence_privileged(&args.seq_args);
6915 status = nfs4_call_sync_sequence(clnt, server, &msg,
6916 &args.seq_args, &res.seq_res);
6917 if (status == NFS4_OK &&
6918 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6919 status = -NFS4ERR_LEASE_MOVED;
6920 return status;
6921}
6922
6923#endif /* CONFIG_NFS_V4_1 */
6924
6925/**
6926 * nfs4_proc_get_locations - discover locations for a migrated FSID
6927 * @inode: inode on FSID that is migrating
6928 * @locations: result of query
6929 * @page: buffer
6930 * @cred: credential to use for this operation
6931 *
6932 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6933 * operation failed, or a negative errno if a local error occurred.
6934 *
6935 * On success, "locations" is filled in, but if the server has
6936 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6937 * asserted.
6938 *
6939 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6940 * from this client that require migration recovery.
6941 */
6942int nfs4_proc_get_locations(struct inode *inode,
6943 struct nfs4_fs_locations *locations,
6944 struct page *page, struct rpc_cred *cred)
6945{
6946 struct nfs_server *server = NFS_SERVER(inode);
6947 struct nfs_client *clp = server->nfs_client;
6948 const struct nfs4_mig_recovery_ops *ops =
6949 clp->cl_mvops->mig_recovery_ops;
6950 struct nfs4_exception exception = { };
6951 int status;
6952
6953 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6954 (unsigned long long)server->fsid.major,
6955 (unsigned long long)server->fsid.minor,
6956 clp->cl_hostname);
6957 nfs_display_fhandle(NFS_FH(inode), __func__);
6958
6959 do {
6960 status = ops->get_locations(inode, locations, page, cred);
6961 if (status != -NFS4ERR_DELAY)
6962 break;
6963 nfs4_handle_exception(server, status, &exception);
6964 } while (exception.retry);
6965 return status;
6966}
6967
Chuck Lever44c99932013-10-17 14:13:30 -04006968/*
6969 * This operation also signals the server that this client is
6970 * performing "lease moved" recovery. The server can stop
6971 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6972 * is appended to this compound to identify the client ID which is
6973 * performing recovery.
6974 */
6975static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6976{
6977 struct nfs_server *server = NFS_SERVER(inode);
6978 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6979 struct rpc_clnt *clnt = server->client;
6980 struct nfs4_fsid_present_arg args = {
6981 .fh = NFS_FH(inode),
6982 .clientid = clp->cl_clientid,
6983 .renew = 1, /* append RENEW */
6984 };
6985 struct nfs4_fsid_present_res res = {
6986 .renew = 1,
6987 };
6988 struct rpc_message msg = {
6989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6990 .rpc_argp = &args,
6991 .rpc_resp = &res,
6992 .rpc_cred = cred,
6993 };
6994 unsigned long now = jiffies;
6995 int status;
6996
6997 res.fh = nfs_alloc_fhandle();
6998 if (res.fh == NULL)
6999 return -ENOMEM;
7000
7001 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7002 nfs4_set_sequence_privileged(&args.seq_args);
7003 status = nfs4_call_sync_sequence(clnt, server, &msg,
7004 &args.seq_args, &res.seq_res);
7005 nfs_free_fhandle(res.fh);
7006 if (status)
7007 return status;
7008
7009 do_renew_lease(clp, now);
7010 return 0;
7011}
7012
7013#ifdef CONFIG_NFS_V4_1
7014
7015/*
7016 * This operation also signals the server that this client is
7017 * performing "lease moved" recovery. The server can stop asserting
7018 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7019 * this operation is identified in the SEQUENCE operation in this
7020 * compound.
7021 */
7022static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7023{
7024 struct nfs_server *server = NFS_SERVER(inode);
7025 struct rpc_clnt *clnt = server->client;
7026 struct nfs4_fsid_present_arg args = {
7027 .fh = NFS_FH(inode),
7028 };
7029 struct nfs4_fsid_present_res res = {
7030 };
7031 struct rpc_message msg = {
7032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7033 .rpc_argp = &args,
7034 .rpc_resp = &res,
7035 .rpc_cred = cred,
7036 };
7037 int status;
7038
7039 res.fh = nfs_alloc_fhandle();
7040 if (res.fh == NULL)
7041 return -ENOMEM;
7042
7043 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7044 nfs4_set_sequence_privileged(&args.seq_args);
7045 status = nfs4_call_sync_sequence(clnt, server, &msg,
7046 &args.seq_args, &res.seq_res);
7047 nfs_free_fhandle(res.fh);
7048 if (status == NFS4_OK &&
7049 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7050 status = -NFS4ERR_LEASE_MOVED;
7051 return status;
7052}
7053
7054#endif /* CONFIG_NFS_V4_1 */
7055
7056/**
7057 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7058 * @inode: inode on FSID to check
7059 * @cred: credential to use for this operation
7060 *
7061 * Server indicates whether the FSID is present, moved, or not
7062 * recognized. This operation is necessary to clear a LEASE_MOVED
7063 * condition for this client ID.
7064 *
7065 * Returns NFS4_OK if the FSID is present on this server,
7066 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7067 * NFS4ERR code if some error occurred on the server, or a
7068 * negative errno if a local failure occurred.
7069 */
7070int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7071{
7072 struct nfs_server *server = NFS_SERVER(inode);
7073 struct nfs_client *clp = server->nfs_client;
7074 const struct nfs4_mig_recovery_ops *ops =
7075 clp->cl_mvops->mig_recovery_ops;
7076 struct nfs4_exception exception = { };
7077 int status;
7078
7079 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7080 (unsigned long long)server->fsid.major,
7081 (unsigned long long)server->fsid.minor,
7082 clp->cl_hostname);
7083 nfs_display_fhandle(NFS_FH(inode), __func__);
7084
7085 do {
7086 status = ops->fsid_present(inode, cred);
7087 if (status != -NFS4ERR_DELAY)
7088 break;
7089 nfs4_handle_exception(server, status, &exception);
7090 } while (exception.retry);
7091 return status;
7092}
7093
Andy Adamson5ec16a82013-08-08 10:57:55 -04007094/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007095 * If 'use_integrity' is true and the state managment nfs_client
7096 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7097 * and the machine credential as per RFC3530bis and RFC5661 Security
7098 * Considerations sections. Otherwise, just use the user cred with the
7099 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007100 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007101static 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 +00007102{
7103 int status;
7104 struct nfs4_secinfo_arg args = {
7105 .dir_fh = NFS_FH(dir),
7106 .name = name,
7107 };
7108 struct nfs4_secinfo_res res = {
7109 .flavors = flavors,
7110 };
7111 struct rpc_message msg = {
7112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7113 .rpc_argp = &args,
7114 .rpc_resp = &res,
7115 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007116 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007117 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007118
7119 if (use_integrity) {
7120 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007121 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7122 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007123 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007124
7125 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007126
7127 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7128 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7129
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007130 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7131 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007132 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007133
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007134 if (cred)
7135 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007136
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007137 return status;
7138}
7139
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007140int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7141 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007142{
7143 struct nfs4_exception exception = { };
7144 int err;
7145 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007146 err = -NFS4ERR_WRONGSEC;
7147
7148 /* try to use integrity protection with machine cred */
7149 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7150 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7151
7152 /*
7153 * if unable to use integrity protection, or SECINFO with
7154 * integrity protection returns NFS4ERR_WRONGSEC (which is
7155 * disallowed by spec, but exists in deployed servers) use
7156 * the current filesystem's rpc_client and the user cred.
7157 */
7158 if (err == -NFS4ERR_WRONGSEC)
7159 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7160
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007161 trace_nfs4_secinfo(dir, name, err);
7162 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007163 &exception);
7164 } while (exception.retry);
7165 return err;
7166}
7167
Andy Adamson557134a2009-04-01 09:21:53 -04007168#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007169/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007170 * Check the exchange flags returned by the server for invalid flags, having
7171 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7172 * DS flags set.
7173 */
7174static int nfs4_check_cl_exchange_flags(u32 flags)
7175{
7176 if (flags & ~EXCHGID4_FLAG_MASK_R)
7177 goto out_inval;
7178 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7179 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7180 goto out_inval;
7181 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7182 goto out_inval;
7183 return NFS_OK;
7184out_inval:
7185 return -NFS4ERR_INVAL;
7186}
7187
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007188static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007189nfs41_same_server_scope(struct nfs41_server_scope *a,
7190 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007191{
7192 if (a->server_scope_sz == b->server_scope_sz &&
7193 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7194 return true;
7195
7196 return false;
7197}
7198
Andy Adamson02a95de2016-02-05 16:08:37 -05007199static void
7200nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7201{
7202}
7203
7204static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7205 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7206};
7207
Andy Adamson357f54d2010-12-14 10:11:57 -05007208/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007209 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007210 *
7211 * The 4.1 client currently uses the same TCP connection for the
7212 * fore and backchannel.
7213 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007214static
7215int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7216 struct rpc_xprt *xprt,
7217 struct nfs_client *clp,
7218 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007219{
7220 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007221 struct nfs41_bind_conn_to_session_args args = {
7222 .client = clp,
7223 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7224 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007225 struct nfs41_bind_conn_to_session_res res;
7226 struct rpc_message msg = {
7227 .rpc_proc =
7228 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007229 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007230 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007231 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007232 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007233 struct rpc_task_setup task_setup_data = {
7234 .rpc_client = clnt,
7235 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007236 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007237 .rpc_message = &msg,
7238 .flags = RPC_TASK_TIMEOUT,
7239 };
7240 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007241
7242 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243
Trond Myklebust71a097c2015-02-18 09:27:18 -08007244 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7245 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7246 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007247
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007248 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7249 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7250 args.dir = NFS4_CDFC4_FORE;
7251
7252 task = rpc_run_task(&task_setup_data);
7253 if (!IS_ERR(task)) {
7254 status = task->tk_status;
7255 rpc_put_task(task);
7256 } else
7257 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007258 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007259 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007260 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007261 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7262 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7263 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007264 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007265 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007266 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267 dprintk("NFS: %s: Unexpected direction from server\n",
7268 __func__);
7269 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007270 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007271 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007272 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007273 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7274 __func__);
7275 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007276 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007277 }
7278 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007279out:
7280 dprintk("<-- %s status= %d\n", __func__, status);
7281 return status;
7282}
7283
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007284struct rpc_bind_conn_calldata {
7285 struct nfs_client *clp;
7286 struct rpc_cred *cred;
7287};
7288
7289static int
7290nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7291 struct rpc_xprt *xprt,
7292 void *calldata)
7293{
7294 struct rpc_bind_conn_calldata *p = calldata;
7295
7296 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7297}
7298
7299int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7300{
7301 struct rpc_bind_conn_calldata data = {
7302 .clp = clp,
7303 .cred = cred,
7304 };
7305 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7306 nfs4_proc_bind_conn_to_session_callback, &data);
7307}
7308
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007309/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007310 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7311 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007312 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007313static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7314 .how = SP4_MACH_CRED,
7315 .enforce.u.words = {
7316 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7317 1 << (OP_EXCHANGE_ID - 32) |
7318 1 << (OP_CREATE_SESSION - 32) |
7319 1 << (OP_DESTROY_SESSION - 32) |
7320 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007321 },
7322 .allow.u.words = {
7323 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007324 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007325 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007326 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007327 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007328 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007329 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007330 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007331 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007332 1 << (OP_FREE_STATEID - 32) |
7333 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007334 }
7335};
7336
7337/*
7338 * Select the state protection mode for client `clp' given the server results
7339 * from exchange_id in `sp'.
7340 *
7341 * Returns 0 on success, negative errno otherwise.
7342 */
7343static int nfs4_sp4_select_mode(struct nfs_client *clp,
7344 struct nfs41_state_protection *sp)
7345{
7346 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7347 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7348 1 << (OP_EXCHANGE_ID - 32) |
7349 1 << (OP_CREATE_SESSION - 32) |
7350 1 << (OP_DESTROY_SESSION - 32) |
7351 1 << (OP_DESTROY_CLIENTID - 32)
7352 };
7353 unsigned int i;
7354
7355 if (sp->how == SP4_MACH_CRED) {
7356 /* Print state protect result */
7357 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7358 for (i = 0; i <= LAST_NFS4_OP; i++) {
7359 if (test_bit(i, sp->enforce.u.longs))
7360 dfprintk(MOUNT, " enforce op %d\n", i);
7361 if (test_bit(i, sp->allow.u.longs))
7362 dfprintk(MOUNT, " allow op %d\n", i);
7363 }
7364
7365 /* make sure nothing is on enforce list that isn't supported */
7366 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7367 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7368 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7369 return -EINVAL;
7370 }
7371 }
7372
7373 /*
7374 * Minimal mode - state operations are allowed to use machine
7375 * credential. Note this already happens by default, so the
7376 * client doesn't have to do anything more than the negotiation.
7377 *
7378 * NOTE: we don't care if EXCHANGE_ID is in the list -
7379 * we're already using the machine cred for exchange_id
7380 * and will never use a different cred.
7381 */
7382 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7383 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7384 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7385 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7386 dfprintk(MOUNT, "sp4_mach_cred:\n");
7387 dfprintk(MOUNT, " minimal mode enabled\n");
7388 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7389 } else {
7390 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7391 return -EINVAL;
7392 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007393
7394 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007395 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7396 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007397 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7398 dfprintk(MOUNT, " cleanup mode enabled\n");
7399 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7400 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007401
Andrew Elble99ade3c2015-12-02 09:39:51 -05007402 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7403 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7404 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7405 &clp->cl_sp4_flags);
7406 }
7407
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007408 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7409 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7410 dfprintk(MOUNT, " secinfo mode enabled\n");
7411 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7412 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007413
7414 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7415 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7416 dfprintk(MOUNT, " stateid mode enabled\n");
7417 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7418 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007419
7420 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7421 dfprintk(MOUNT, " write mode enabled\n");
7422 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7423 }
7424
7425 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7426 dfprintk(MOUNT, " commit mode enabled\n");
7427 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7428 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007429 }
7430
7431 return 0;
7432}
7433
Andy Adamson8d89bd72016-09-09 09:22:18 -04007434struct nfs41_exchange_id_data {
7435 struct nfs41_exchange_id_res res;
7436 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007437 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007438 int rpc_status;
7439};
7440
7441static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007442{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007443 struct nfs41_exchange_id_data *cdata =
7444 (struct nfs41_exchange_id_data *)data;
7445 struct nfs_client *clp = cdata->args.client;
7446 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007447
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007448 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007449
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007450 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007451 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007452
7453 if (cdata->xprt && status == 0) {
7454 status = nfs4_detect_session_trunking(clp, &cdata->res,
7455 cdata->xprt);
7456 goto out;
7457 }
7458
Andy Adamson8d89bd72016-09-09 09:22:18 -04007459 if (status == 0)
7460 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007461
Chuck Lever177313f2012-05-21 22:44:58 -04007462 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007463 clp->cl_clientid = cdata->res.clientid;
7464 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007465 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007466 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007467 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007468 &clp->cl_session->session_state);
7469 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007470 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007471
Chuck Leveracdeb692012-05-21 22:46:16 -04007472 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007473 clp->cl_serverowner = cdata->res.server_owner;
7474 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007475
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007476 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007477 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007478 clp->cl_implid = cdata->res.impl_id;
7479 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007480
Chuck Lever177313f2012-05-21 22:44:58 -04007481 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007482 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007484 dprintk("%s: server_scope mismatch detected\n",
7485 __func__);
7486 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007487 kfree(clp->cl_serverscope);
7488 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007489 }
7490
Chuck Lever177313f2012-05-21 22:44:58 -04007491 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 clp->cl_serverscope = cdata->res.server_scope;
7493 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007494 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007495 /* Save the EXCHANGE_ID verifier session trunk tests */
7496 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7497 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007498 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007499out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007500 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007501 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502}
7503
7504static void nfs4_exchange_id_release(void *data)
7505{
7506 struct nfs41_exchange_id_data *cdata =
7507 (struct nfs41_exchange_id_data *)data;
7508
7509 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007510 if (cdata->xprt) {
7511 xprt_put(cdata->xprt);
7512 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7513 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007514 kfree(cdata->res.impl_id);
7515 kfree(cdata->res.server_scope);
7516 kfree(cdata->res.server_owner);
7517 kfree(cdata);
7518}
7519
7520static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7521 .rpc_call_done = nfs4_exchange_id_done,
7522 .rpc_release = nfs4_exchange_id_release,
7523};
7524
Benny Halevy99fe60d2009-04-01 09:22:29 -04007525/*
7526 * _nfs4_proc_exchange_id()
7527 *
7528 * Wrapper for EXCHANGE_ID operation.
7529 */
7530static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007531 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007532{
7533 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007534 struct rpc_message msg = {
7535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007536 .rpc_cred = cred,
7537 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 struct rpc_task_setup task_setup_data = {
7539 .rpc_client = clp->cl_rpcclient,
7540 .callback_ops = &nfs4_exchange_id_call_ops,
7541 .rpc_message = &msg,
7542 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7543 };
7544 struct nfs41_exchange_id_data *calldata;
7545 struct rpc_task *task;
7546 int status = -EIO;
7547
7548 if (!atomic_inc_not_zero(&clp->cl_count))
7549 goto out;
7550
7551 status = -ENOMEM;
7552 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7553 if (!calldata)
7554 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007555
Andy Adamsonad0849a2016-09-09 09:22:28 -04007556 if (!xprt)
7557 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007558
7559 status = nfs4_init_uniform_client_string(clp);
7560 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007561 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007562
7563 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7564 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7565 clp->cl_owner_id);
7566
Andy Adamson8d89bd72016-09-09 09:22:18 -04007567 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7568 GFP_NOFS);
7569 status = -ENOMEM;
7570 if (unlikely(calldata->res.server_owner == NULL))
7571 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007572
Andy Adamson8d89bd72016-09-09 09:22:18 -04007573 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007574 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007575 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007576 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007577
Andy Adamson8d89bd72016-09-09 09:22:18 -04007578 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7579 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007580 goto out_server_scope;
7581
7582 switch (sp4_how) {
7583 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007584 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007585 break;
7586
7587 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007588 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007589 break;
7590
7591 default:
7592 /* unsupported! */
7593 WARN_ON_ONCE(1);
7594 status = -EINVAL;
7595 goto out_impl_id;
7596 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007597 if (xprt) {
7598 calldata->xprt = xprt;
7599 task_setup_data.rpc_xprt = xprt;
7600 task_setup_data.flags =
7601 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7602 calldata->args.verifier = &clp->cl_confirm;
7603 } else {
7604 calldata->args.verifier = &verifier;
7605 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007606 calldata->args.client = clp;
7607#ifdef CONFIG_NFS_V4_1_MIGRATION
7608 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7609 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7610 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7611#else
7612 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7613 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7614#endif
7615 msg.rpc_argp = &calldata->args;
7616 msg.rpc_resp = &calldata->res;
7617 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007618
Andy Adamson8d89bd72016-09-09 09:22:18 -04007619 task = rpc_run_task(&task_setup_data);
7620 if (IS_ERR(task)) {
7621 status = PTR_ERR(task);
7622 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007623 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007624
Andy Adamsonad0849a2016-09-09 09:22:28 -04007625 if (!xprt) {
7626 status = rpc_wait_for_completion_task(task);
7627 if (!status)
7628 status = calldata->rpc_status;
7629 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007631
Andy Adamson8d89bd72016-09-09 09:22:18 -04007632 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007633out:
Chuck Lever177313f2012-05-21 22:44:58 -04007634 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007635 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007636 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007637 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007638 clp->cl_implid->date.seconds,
7639 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007640 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007641 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007642
7643out_impl_id:
7644 kfree(calldata->res.impl_id);
7645out_server_scope:
7646 kfree(calldata->res.server_scope);
7647out_server_owner:
7648 kfree(calldata->res.server_owner);
7649out_calldata:
7650 kfree(calldata);
7651 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007652}
7653
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007654/*
7655 * nfs4_proc_exchange_id()
7656 *
7657 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7658 *
7659 * Since the clientid has expired, all compounds using sessions
7660 * associated with the stale clientid will be returning
7661 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7662 * be in some phase of session reset.
7663 *
7664 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7665 */
7666int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7667{
7668 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7669 int status;
7670
7671 /* try SP4_MACH_CRED if krb5i/p */
7672 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7673 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007674 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007675 if (!status)
7676 return 0;
7677 }
7678
7679 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007680 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007681}
7682
Andy Adamson04fa2c62016-09-09 09:22:29 -04007683/**
7684 * nfs4_test_session_trunk
7685 *
7686 * This is an add_xprt_test() test function called from
7687 * rpc_clnt_setup_test_and_add_xprt.
7688 *
7689 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7690 * and is dereferrenced in nfs4_exchange_id_release
7691 *
7692 * Upon success, add the new transport to the rpc_clnt
7693 *
7694 * @clnt: struct rpc_clnt to get new transport
7695 * @xprt: the rpc_xprt to test
7696 * @data: call data for _nfs4_proc_exchange_id.
7697 */
7698int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7699 void *data)
7700{
7701 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7702 u32 sp4_how;
7703
7704 dprintk("--> %s try %s\n", __func__,
7705 xprt->address_strings[RPC_DISPLAY_ADDR]);
7706
7707 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7708
7709 /* Test connection for session trunking. Async exchange_id call */
7710 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7711}
7712EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7713
Trond Myklebust66245532012-05-25 17:18:09 -04007714static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7715 struct rpc_cred *cred)
7716{
7717 struct rpc_message msg = {
7718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7719 .rpc_argp = clp,
7720 .rpc_cred = cred,
7721 };
7722 int status;
7723
7724 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007725 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007726 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007727 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007728 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7729 return status;
7730}
7731
7732static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7733 struct rpc_cred *cred)
7734{
7735 unsigned int loop;
7736 int ret;
7737
7738 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7739 ret = _nfs4_proc_destroy_clientid(clp, cred);
7740 switch (ret) {
7741 case -NFS4ERR_DELAY:
7742 case -NFS4ERR_CLIENTID_BUSY:
7743 ssleep(1);
7744 break;
7745 default:
7746 return ret;
7747 }
7748 }
7749 return 0;
7750}
7751
7752int nfs4_destroy_clientid(struct nfs_client *clp)
7753{
7754 struct rpc_cred *cred;
7755 int ret = 0;
7756
7757 if (clp->cl_mvops->minor_version < 1)
7758 goto out;
7759 if (clp->cl_exchange_flags == 0)
7760 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007761 if (clp->cl_preserve_clid)
7762 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007763 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007764 ret = nfs4_proc_destroy_clientid(clp, cred);
7765 if (cred)
7766 put_rpccred(cred);
7767 switch (ret) {
7768 case 0:
7769 case -NFS4ERR_STALE_CLIENTID:
7770 clp->cl_exchange_flags = 0;
7771 }
7772out:
7773 return ret;
7774}
7775
Andy Adamson2050f0c2009-04-01 09:22:30 -04007776struct nfs4_get_lease_time_data {
7777 struct nfs4_get_lease_time_args *args;
7778 struct nfs4_get_lease_time_res *res;
7779 struct nfs_client *clp;
7780};
7781
7782static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7783 void *calldata)
7784{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007785 struct nfs4_get_lease_time_data *data =
7786 (struct nfs4_get_lease_time_data *)calldata;
7787
7788 dprintk("--> %s\n", __func__);
7789 /* just setup sequence, do not trigger session recovery
7790 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007791 nfs41_setup_sequence(data->clp->cl_session,
7792 &data->args->la_seq_args,
7793 &data->res->lr_seq_res,
7794 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007795 dprintk("<-- %s\n", __func__);
7796}
7797
7798/*
7799 * Called from nfs4_state_manager thread for session setup, so don't recover
7800 * from sequence operation or clientid errors.
7801 */
7802static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7803{
7804 struct nfs4_get_lease_time_data *data =
7805 (struct nfs4_get_lease_time_data *)calldata;
7806
7807 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007808 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7809 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007810 switch (task->tk_status) {
7811 case -NFS4ERR_DELAY:
7812 case -NFS4ERR_GRACE:
7813 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7814 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7815 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007816 /* fall through */
7817 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007818 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007819 return;
7820 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007821 dprintk("<-- %s\n", __func__);
7822}
7823
Trond Myklebust17280172012-03-11 13:11:00 -04007824static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007825 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7826 .rpc_call_done = nfs4_get_lease_time_done,
7827};
7828
7829int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7830{
7831 struct rpc_task *task;
7832 struct nfs4_get_lease_time_args args;
7833 struct nfs4_get_lease_time_res res = {
7834 .lr_fsinfo = fsinfo,
7835 };
7836 struct nfs4_get_lease_time_data data = {
7837 .args = &args,
7838 .res = &res,
7839 .clp = clp,
7840 };
7841 struct rpc_message msg = {
7842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7843 .rpc_argp = &args,
7844 .rpc_resp = &res,
7845 };
7846 struct rpc_task_setup task_setup = {
7847 .rpc_client = clp->cl_rpcclient,
7848 .rpc_message = &msg,
7849 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007850 .callback_data = &data,
7851 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007852 };
7853 int status;
7854
Chuck Levera9c92d62013-08-09 12:48:18 -04007855 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007856 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007857 dprintk("--> %s\n", __func__);
7858 task = rpc_run_task(&task_setup);
7859
7860 if (IS_ERR(task))
7861 status = PTR_ERR(task);
7862 else {
7863 status = task->tk_status;
7864 rpc_put_task(task);
7865 }
7866 dprintk("<-- %s return %d\n", __func__, status);
7867
7868 return status;
7869}
7870
Andy Adamsonfc931582009-04-01 09:22:31 -04007871/*
7872 * Initialize the values to be used by the client in CREATE_SESSION
7873 * If nfs4_init_session set the fore channel request and response sizes,
7874 * use them.
7875 *
7876 * Set the back channel max_resp_sz_cached to zero to force the client to
7877 * always set csa_cachethis to FALSE because the current implementation
7878 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7879 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007880static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7881 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007882{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007883 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007884 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007885
Andy Adamson18aad3d2013-06-26 12:21:49 -04007886 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7887 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7888
Andy Adamsonfc931582009-04-01 09:22:31 -04007889 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007890 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7891 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007892 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007893 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007894
7895 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007896 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007897 __func__,
7898 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007899 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007900
7901 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007902 args->bc_attrs.max_rqst_sz = max_bc_payload;
7903 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007904 args->bc_attrs.max_resp_sz_cached = 0;
7905 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007906 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007907
7908 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7909 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7910 __func__,
7911 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7912 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7913 args->bc_attrs.max_reqs);
7914}
7915
Trond Myklebust79969dd2015-02-18 11:30:18 -08007916static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7917 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007918{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007919 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007920 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007921
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007922 if (rcvd->max_resp_sz > sent->max_resp_sz)
7923 return -EINVAL;
7924 /*
7925 * Our requested max_ops is the minimum we need; we're not
7926 * prepared to break up compounds into smaller pieces than that.
7927 * So, no point even trying to continue if the server won't
7928 * cooperate:
7929 */
7930 if (rcvd->max_ops < sent->max_ops)
7931 return -EINVAL;
7932 if (rcvd->max_reqs == 0)
7933 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007934 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7935 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007936 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007937}
7938
Trond Myklebust79969dd2015-02-18 11:30:18 -08007939static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7940 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007941{
7942 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007943 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007944
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007945 if (!(res->flags & SESSION4_BACK_CHAN))
7946 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007947 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7948 return -EINVAL;
7949 if (rcvd->max_resp_sz < sent->max_resp_sz)
7950 return -EINVAL;
7951 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7952 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007953 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007954 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007955 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007956 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007957out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007958 return 0;
7959}
Andy Adamson8d353012009-04-01 09:22:32 -04007960
Andy Adamson8d353012009-04-01 09:22:32 -04007961static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007962 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007963{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007964 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007965
Trond Myklebust79969dd2015-02-18 11:30:18 -08007966 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007967 if (ret)
7968 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007969 return nfs4_verify_back_channel_attrs(args, res);
7970}
7971
7972static void nfs4_update_session(struct nfs4_session *session,
7973 struct nfs41_create_session_res *res)
7974{
7975 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007976 /* Mark client id and session as being confirmed */
7977 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7978 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007979 session->flags = res->flags;
7980 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007981 if (res->flags & SESSION4_BACK_CHAN)
7982 memcpy(&session->bc_attrs, &res->bc_attrs,
7983 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007984}
7985
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007986static int _nfs4_proc_create_session(struct nfs_client *clp,
7987 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007988{
7989 struct nfs4_session *session = clp->cl_session;
7990 struct nfs41_create_session_args args = {
7991 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007992 .clientid = clp->cl_clientid,
7993 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007994 .cb_program = NFS4_CALLBACK,
7995 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007996 struct nfs41_create_session_res res;
7997
Andy Adamsonfc931582009-04-01 09:22:31 -04007998 struct rpc_message msg = {
7999 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8000 .rpc_argp = &args,
8001 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008002 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008003 };
8004 int status;
8005
Chuck Lever6b26cc82016-05-02 14:40:40 -04008006 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008007 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008008
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008009 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008010 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008011
Trond Myklebustb519d402016-09-11 14:50:01 -04008012 switch (status) {
8013 case -NFS4ERR_STALE_CLIENTID:
8014 case -NFS4ERR_DELAY:
8015 case -ETIMEDOUT:
8016 case -EACCES:
8017 case -EAGAIN:
8018 goto out;
8019 };
8020
8021 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008022 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008023 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008024 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008025 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008026 if (status)
8027 goto out;
8028 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008029 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008030out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008031 return status;
8032}
8033
8034/*
8035 * Issues a CREATE_SESSION operation to the server.
8036 * It is the responsibility of the caller to verify the session is
8037 * expired before calling this routine.
8038 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008039int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008040{
8041 int status;
8042 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008043 struct nfs4_session *session = clp->cl_session;
8044
8045 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8046
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008047 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008048 if (status)
8049 goto out;
8050
Andy Adamsonaacd5532011-11-09 13:58:21 -05008051 /* Init or reset the session slot tables */
8052 status = nfs4_setup_session_slot_tables(session);
8053 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008054 if (status)
8055 goto out;
8056
8057 ptr = (unsigned *)&session->sess_id.data[0];
8058 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8059 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008060out:
8061 dprintk("<-- %s\n", __func__);
8062 return status;
8063}
8064
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008065/*
8066 * Issue the over-the-wire RPC DESTROY_SESSION.
8067 * The caller must serialize access to this routine.
8068 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008069int nfs4_proc_destroy_session(struct nfs4_session *session,
8070 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008071{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008072 struct rpc_message msg = {
8073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8074 .rpc_argp = session,
8075 .rpc_cred = cred,
8076 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008077 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008078
8079 dprintk("--> nfs4_proc_destroy_session\n");
8080
8081 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008082 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8083 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008084
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008085 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008086 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008087
8088 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008089 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008090 "Session has been destroyed regardless...\n", status);
8091
8092 dprintk("<-- nfs4_proc_destroy_session\n");
8093 return status;
8094}
8095
Trond Myklebust7b38c362012-05-23 13:23:31 -04008096/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008097 * Renew the cl_session lease.
8098 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008099struct nfs4_sequence_data {
8100 struct nfs_client *clp;
8101 struct nfs4_sequence_args args;
8102 struct nfs4_sequence_res res;
8103};
8104
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008105static void nfs41_sequence_release(void *data)
8106{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008107 struct nfs4_sequence_data *calldata = data;
8108 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008109
Alexandros Batsakis71358402010-02-05 03:45:05 -08008110 if (atomic_read(&clp->cl_count) > 1)
8111 nfs4_schedule_state_renewal(clp);
8112 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008113 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008114}
8115
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008116static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8117{
8118 switch(task->tk_status) {
8119 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008120 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8121 return -EAGAIN;
8122 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008123 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008124 }
8125 return 0;
8126}
8127
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008128static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008129{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008130 struct nfs4_sequence_data *calldata = data;
8131 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008132
Trond Myklebust14516c32010-07-31 14:29:06 -04008133 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8134 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008135
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008136 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008137 if (task->tk_status < 0) {
8138 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008139 if (atomic_read(&clp->cl_count) == 1)
8140 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008141
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008142 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8143 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008144 return;
8145 }
8146 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008147 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008148out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008149 dprintk("<-- %s\n", __func__);
8150}
8151
8152static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8153{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008154 struct nfs4_sequence_data *calldata = data;
8155 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008156 struct nfs4_sequence_args *args;
8157 struct nfs4_sequence_res *res;
8158
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008159 args = task->tk_msg.rpc_argp;
8160 res = task->tk_msg.rpc_resp;
8161
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008162 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008163}
8164
8165static const struct rpc_call_ops nfs41_sequence_ops = {
8166 .rpc_call_done = nfs41_sequence_call_done,
8167 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008168 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008169};
8170
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008171static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8172 struct rpc_cred *cred,
8173 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008174{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008175 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008176 struct rpc_message msg = {
8177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8178 .rpc_cred = cred,
8179 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008180 struct rpc_task_setup task_setup_data = {
8181 .rpc_client = clp->cl_rpcclient,
8182 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008183 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008184 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008185 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008186
Alexandros Batsakis71358402010-02-05 03:45:05 -08008187 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008188 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008189 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008190 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008191 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008192 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008193 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008194 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008195 if (is_privileged)
8196 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008197 msg.rpc_argp = &calldata->args;
8198 msg.rpc_resp = &calldata->res;
8199 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008200 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008201
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008202 return rpc_run_task(&task_setup_data);
8203}
8204
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008205static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008206{
8207 struct rpc_task *task;
8208 int ret = 0;
8209
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008210 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008211 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008212 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008213 if (IS_ERR(task))
8214 ret = PTR_ERR(task);
8215 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008216 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008217 dprintk("<-- %s status=%d\n", __func__, ret);
8218 return ret;
8219}
8220
8221static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8222{
8223 struct rpc_task *task;
8224 int ret;
8225
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008226 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008227 if (IS_ERR(task)) {
8228 ret = PTR_ERR(task);
8229 goto out;
8230 }
8231 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008232 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008233 ret = task->tk_status;
8234 rpc_put_task(task);
8235out:
8236 dprintk("<-- %s status=%d\n", __func__, ret);
8237 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008238}
8239
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008240struct nfs4_reclaim_complete_data {
8241 struct nfs_client *clp;
8242 struct nfs41_reclaim_complete_args arg;
8243 struct nfs41_reclaim_complete_res res;
8244};
8245
8246static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8247{
8248 struct nfs4_reclaim_complete_data *calldata = data;
8249
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008250 nfs41_setup_sequence(calldata->clp->cl_session,
8251 &calldata->arg.seq_args,
8252 &calldata->res.seq_res,
8253 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008254}
8255
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008256static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8257{
8258 switch(task->tk_status) {
8259 case 0:
8260 case -NFS4ERR_COMPLETE_ALREADY:
8261 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8262 break;
8263 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008264 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008265 /* fall through */
8266 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008267 return -EAGAIN;
8268 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008269 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008270 }
8271 return 0;
8272}
8273
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008274static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8275{
8276 struct nfs4_reclaim_complete_data *calldata = data;
8277 struct nfs_client *clp = calldata->clp;
8278 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8279
8280 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008281 if (!nfs41_sequence_done(task, res))
8282 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008283
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008284 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008285 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8286 rpc_restart_call_prepare(task);
8287 return;
8288 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008289 dprintk("<-- %s\n", __func__);
8290}
8291
8292static void nfs4_free_reclaim_complete_data(void *data)
8293{
8294 struct nfs4_reclaim_complete_data *calldata = data;
8295
8296 kfree(calldata);
8297}
8298
8299static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8300 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8301 .rpc_call_done = nfs4_reclaim_complete_done,
8302 .rpc_release = nfs4_free_reclaim_complete_data,
8303};
8304
8305/*
8306 * Issue a global reclaim complete.
8307 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008308static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8309 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008310{
8311 struct nfs4_reclaim_complete_data *calldata;
8312 struct rpc_task *task;
8313 struct rpc_message msg = {
8314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008315 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008316 };
8317 struct rpc_task_setup task_setup_data = {
8318 .rpc_client = clp->cl_rpcclient,
8319 .rpc_message = &msg,
8320 .callback_ops = &nfs4_reclaim_complete_call_ops,
8321 .flags = RPC_TASK_ASYNC,
8322 };
8323 int status = -ENOMEM;
8324
8325 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008326 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008327 if (calldata == NULL)
8328 goto out;
8329 calldata->clp = clp;
8330 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008331
Chuck Levera9c92d62013-08-09 12:48:18 -04008332 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008333 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008334 msg.rpc_argp = &calldata->arg;
8335 msg.rpc_resp = &calldata->res;
8336 task_setup_data.callback_data = calldata;
8337 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008338 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008339 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008340 goto out;
8341 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008342 status = nfs4_wait_for_completion_rpc_task(task);
8343 if (status == 0)
8344 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008345 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008346 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008347out:
8348 dprintk("<-- %s status=%d\n", __func__, status);
8349 return status;
8350}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008351
8352static void
8353nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8354{
8355 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008356 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008357 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008358
8359 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008360 nfs41_setup_sequence(session, &lgp->args.seq_args,
8361 &lgp->res.seq_res, task);
8362 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008363}
8364
8365static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8366{
8367 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008368
8369 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008370 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008371 dprintk("<-- %s\n", __func__);
8372}
8373
8374static int
8375nfs4_layoutget_handle_exception(struct rpc_task *task,
8376 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8377{
Trond Myklebustee314c22012-10-01 17:25:48 -07008378 struct inode *inode = lgp->args.inode;
8379 struct nfs_server *server = NFS_SERVER(inode);
8380 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008381 int nfs4err = task->tk_status;
8382 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008383 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008384
Boaz Harroshed7e5422014-01-22 20:34:54 +02008385 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008386
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008387 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008388 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008389 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008390
8391 /*
8392 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8393 * on the file. set tk_status to -ENODATA to tell upper layer to
8394 * retry go inband.
8395 */
8396 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008397 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008398 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008399 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008400 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8401 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8402 */
8403 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008404 status = -EOVERFLOW;
8405 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008406 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008407 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008408 * (or clients) writing to the same RAID stripe except when
8409 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008410 *
8411 * Treat it like we would RECALLCONFLICT -- we retry for a little
8412 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008413 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008414 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008415 if (lgp->args.minlength == 0) {
8416 status = -EOVERFLOW;
8417 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008418 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008419 status = -EBUSY;
8420 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008421 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008422 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008423 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008424 case -NFS4ERR_DELEG_REVOKED:
8425 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008426 case -NFS4ERR_EXPIRED:
8427 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008428 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008429 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008430 lo = NFS_I(inode)->layout;
8431 /* If the open stateid was bad, then recover it. */
8432 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8433 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008434 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008435 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008436 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008437 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008438 break;
8439 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008440
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008441 /*
8442 * Mark the bad layout state as invalid, then retry
8443 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008444 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008445 spin_unlock(&inode->i_lock);
8446 pnfs_free_lseg_list(&head);
8447 status = -EAGAIN;
8448 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008449 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008450
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008451 err = nfs4_handle_exception(server, nfs4err, exception);
8452 if (!status) {
8453 if (exception->retry)
8454 status = -EAGAIN;
8455 else
8456 status = err;
8457 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008458out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008459 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008460 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008461}
8462
Idan Kedar85541162012-08-02 11:47:10 +03008463static size_t max_response_pages(struct nfs_server *server)
8464{
8465 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8466 return nfs_page_array_len(0, max_resp_sz);
8467}
8468
8469static void nfs4_free_pages(struct page **pages, size_t size)
8470{
8471 int i;
8472
8473 if (!pages)
8474 return;
8475
8476 for (i = 0; i < size; i++) {
8477 if (!pages[i])
8478 break;
8479 __free_page(pages[i]);
8480 }
8481 kfree(pages);
8482}
8483
8484static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8485{
8486 struct page **pages;
8487 int i;
8488
8489 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8490 if (!pages) {
8491 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8492 return NULL;
8493 }
8494
8495 for (i = 0; i < size; i++) {
8496 pages[i] = alloc_page(gfp_flags);
8497 if (!pages[i]) {
8498 dprintk("%s: failed to allocate page\n", __func__);
8499 nfs4_free_pages(pages, size);
8500 return NULL;
8501 }
8502 }
8503
8504 return pages;
8505}
8506
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008507static void nfs4_layoutget_release(void *calldata)
8508{
8509 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008510 struct inode *inode = lgp->args.inode;
8511 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008512 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008513
8514 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008515 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008516 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008517 put_nfs_open_context(lgp->args.ctx);
8518 kfree(calldata);
8519 dprintk("<-- %s\n", __func__);
8520}
8521
8522static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8523 .rpc_call_prepare = nfs4_layoutget_prepare,
8524 .rpc_call_done = nfs4_layoutget_done,
8525 .rpc_release = nfs4_layoutget_release,
8526};
8527
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008528struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008529nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008530{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008531 struct inode *inode = lgp->args.inode;
8532 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008533 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008534 struct rpc_task *task;
8535 struct rpc_message msg = {
8536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8537 .rpc_argp = &lgp->args,
8538 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008539 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008540 };
8541 struct rpc_task_setup task_setup_data = {
8542 .rpc_client = server->client,
8543 .rpc_message = &msg,
8544 .callback_ops = &nfs4_layoutget_call_ops,
8545 .callback_data = lgp,
8546 .flags = RPC_TASK_ASYNC,
8547 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008548 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008549 struct nfs4_exception exception = {
8550 .inode = inode,
8551 .timeout = *timeout,
8552 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553 int status = 0;
8554
8555 dprintk("--> %s\n", __func__);
8556
Peng Tao4bd5a982014-11-17 11:05:17 +08008557 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8558 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8559
Idan Kedar85541162012-08-02 11:47:10 +03008560 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8561 if (!lgp->args.layout.pages) {
8562 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008563 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008564 }
8565 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8566
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008567 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008568 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008569 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008570
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008571 task = rpc_run_task(&task_setup_data);
8572 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008573 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008574 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008575 if (status == 0) {
8576 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8577 *timeout = exception.timeout;
8578 }
8579
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008580 trace_nfs4_layoutget(lgp->args.ctx,
8581 &lgp->args.range,
8582 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008583 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008584 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008585
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008586 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8587 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008588 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008589 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008590 rpc_put_task(task);
8591 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008592 if (status)
8593 return ERR_PTR(status);
8594 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008595}
8596
Benny Halevycbe82602011-05-22 19:52:37 +03008597static void
8598nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8599{
8600 struct nfs4_layoutreturn *lrp = calldata;
8601
8602 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008603 nfs41_setup_sequence(lrp->clp->cl_session,
8604 &lrp->args.seq_args,
8605 &lrp->res.seq_res,
8606 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008607}
8608
8609static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8610{
8611 struct nfs4_layoutreturn *lrp = calldata;
8612 struct nfs_server *server;
8613
8614 dprintk("--> %s\n", __func__);
8615
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008616 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008617 return;
8618
8619 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008620 switch (task->tk_status) {
8621 default:
8622 task->tk_status = 0;
8623 case 0:
8624 break;
8625 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008626 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008627 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008628 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008629 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008630 return;
8631 }
Benny Halevycbe82602011-05-22 19:52:37 +03008632 dprintk("<-- %s\n", __func__);
8633}
8634
8635static void nfs4_layoutreturn_release(void *calldata)
8636{
8637 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008638 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008639
8640 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008641 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008642 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008643 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008644 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8645 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008646 pnfs_put_layout_hdr(lrp->args.layout);
8647 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008648 kfree(calldata);
8649 dprintk("<-- %s\n", __func__);
8650}
8651
8652static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8653 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8654 .rpc_call_done = nfs4_layoutreturn_done,
8655 .rpc_release = nfs4_layoutreturn_release,
8656};
8657
Peng Tao6c166052014-11-17 09:30:40 +08008658int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008659{
8660 struct rpc_task *task;
8661 struct rpc_message msg = {
8662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8663 .rpc_argp = &lrp->args,
8664 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008665 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008666 };
8667 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008668 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008669 .rpc_message = &msg,
8670 .callback_ops = &nfs4_layoutreturn_call_ops,
8671 .callback_data = lrp,
8672 };
Peng Tao6c166052014-11-17 09:30:40 +08008673 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008674
Andrew Elble99ade3c2015-12-02 09:39:51 -05008675 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8676 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8677 &task_setup_data.rpc_client, &msg);
8678
Benny Halevycbe82602011-05-22 19:52:37 +03008679 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008680 if (!sync) {
8681 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8682 if (!lrp->inode) {
8683 nfs4_layoutreturn_release(lrp);
8684 return -EAGAIN;
8685 }
8686 task_setup_data.flags |= RPC_TASK_ASYNC;
8687 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008688 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008689 task = rpc_run_task(&task_setup_data);
8690 if (IS_ERR(task))
8691 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008692 if (sync)
8693 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008694 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008695 dprintk("<-- %s status=%d\n", __func__, status);
8696 rpc_put_task(task);
8697 return status;
8698}
8699
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008700static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008701_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8702 struct pnfs_device *pdev,
8703 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008704{
8705 struct nfs4_getdeviceinfo_args args = {
8706 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008707 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8708 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008709 };
8710 struct nfs4_getdeviceinfo_res res = {
8711 .pdev = pdev,
8712 };
8713 struct rpc_message msg = {
8714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8715 .rpc_argp = &args,
8716 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008717 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008718 };
8719 int status;
8720
8721 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008722 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008723 if (res.notification & ~args.notify_types)
8724 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008725 if (res.notification != args.notify_types)
8726 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008727
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008728 dprintk("<-- %s status=%d\n", __func__, status);
8729
8730 return status;
8731}
8732
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008733int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8734 struct pnfs_device *pdev,
8735 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008736{
8737 struct nfs4_exception exception = { };
8738 int err;
8739
8740 do {
8741 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008742 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008743 &exception);
8744 } while (exception.retry);
8745 return err;
8746}
8747EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8748
Andy Adamson863a3c62011-03-23 13:27:54 +00008749static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8750{
8751 struct nfs4_layoutcommit_data *data = calldata;
8752 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008753 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008754
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008755 nfs41_setup_sequence(session,
8756 &data->args.seq_args,
8757 &data->res.seq_res,
8758 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008759}
8760
8761static void
8762nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8763{
8764 struct nfs4_layoutcommit_data *data = calldata;
8765 struct nfs_server *server = NFS_SERVER(data->args.inode);
8766
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008767 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008768 return;
8769
8770 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008771 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8772 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8773 case -NFS4ERR_BADLAYOUT: /* no layout */
8774 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008775 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008776 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008777 break;
8778 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008779 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008780 rpc_restart_call_prepare(task);
8781 return;
8782 }
8783 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008784}
8785
8786static void nfs4_layoutcommit_release(void *calldata)
8787{
8788 struct nfs4_layoutcommit_data *data = calldata;
8789
Andy Adamsondb29c082011-07-30 20:52:38 -04008790 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008791 nfs_post_op_update_inode_force_wcc(data->args.inode,
8792 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008793 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008794 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008795 kfree(data);
8796}
8797
8798static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8799 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8800 .rpc_call_done = nfs4_layoutcommit_done,
8801 .rpc_release = nfs4_layoutcommit_release,
8802};
8803
8804int
Andy Adamsonef311532011-03-12 02:58:10 -05008805nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008806{
8807 struct rpc_message msg = {
8808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8809 .rpc_argp = &data->args,
8810 .rpc_resp = &data->res,
8811 .rpc_cred = data->cred,
8812 };
8813 struct rpc_task_setup task_setup_data = {
8814 .task = &data->task,
8815 .rpc_client = NFS_CLIENT(data->args.inode),
8816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_layoutcommit_ops,
8818 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008819 };
8820 struct rpc_task *task;
8821 int status = 0;
8822
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008823 dprintk("NFS: initiating layoutcommit call. sync %d "
8824 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008825 data->args.lastbytewritten,
8826 data->args.inode->i_ino);
8827
Trond Myklebust472e2592015-02-05 16:50:30 -05008828 if (!sync) {
8829 data->inode = nfs_igrab_and_active(data->args.inode);
8830 if (data->inode == NULL) {
8831 nfs4_layoutcommit_release(data);
8832 return -EAGAIN;
8833 }
8834 task_setup_data.flags = RPC_TASK_ASYNC;
8835 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008836 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008837 task = rpc_run_task(&task_setup_data);
8838 if (IS_ERR(task))
8839 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008840 if (sync)
8841 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008842 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008843 dprintk("%s: status %d\n", __func__, status);
8844 rpc_put_task(task);
8845 return status;
8846}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008847
Andy Adamson97431202013-08-08 10:57:56 -04008848/**
8849 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8850 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8851 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008852static int
8853_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008854 struct nfs_fsinfo *info,
8855 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008856{
8857 struct nfs41_secinfo_no_name_args args = {
8858 .style = SECINFO_STYLE_CURRENT_FH,
8859 };
8860 struct nfs4_secinfo_res res = {
8861 .flavors = flavors,
8862 };
8863 struct rpc_message msg = {
8864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8865 .rpc_argp = &args,
8866 .rpc_resp = &res,
8867 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008868 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008869 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008870 int status;
8871
8872 if (use_integrity) {
8873 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008874 cred = nfs4_get_clid_cred(server->nfs_client);
8875 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008876 }
8877
8878 dprintk("--> %s\n", __func__);
8879 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8880 &res.seq_res, 0);
8881 dprintk("<-- %s status=%d\n", __func__, status);
8882
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008883 if (cred)
8884 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008885
8886 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008887}
8888
8889static int
8890nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8891 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8892{
8893 struct nfs4_exception exception = { };
8894 int err;
8895 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008896 /* first try using integrity protection */
8897 err = -NFS4ERR_WRONGSEC;
8898
8899 /* try to use integrity protection with machine cred */
8900 if (_nfs4_is_integrity_protected(server->nfs_client))
8901 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8902 flavors, true);
8903
8904 /*
8905 * if unable to use integrity protection, or SECINFO with
8906 * integrity protection returns NFS4ERR_WRONGSEC (which is
8907 * disallowed by spec, but exists in deployed servers) use
8908 * the current filesystem's rpc_client and the user cred.
8909 */
8910 if (err == -NFS4ERR_WRONGSEC)
8911 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8912 flavors, false);
8913
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008914 switch (err) {
8915 case 0:
8916 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008917 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008918 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008919 default:
8920 err = nfs4_handle_exception(server, err, &exception);
8921 }
8922 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008923out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008924 return err;
8925}
8926
8927static int
8928nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8929 struct nfs_fsinfo *info)
8930{
8931 int err;
8932 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008933 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008934 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008935 struct nfs4_secinfo4 *secinfo;
8936 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008937
8938 page = alloc_page(GFP_KERNEL);
8939 if (!page) {
8940 err = -ENOMEM;
8941 goto out;
8942 }
8943
8944 flavors = page_address(page);
8945 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8946
8947 /*
8948 * Fall back on "guess and check" method if
8949 * the server doesn't support SECINFO_NO_NAME
8950 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008951 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008952 err = nfs4_find_root_sec(server, fhandle, info);
8953 goto out_freepage;
8954 }
8955 if (err)
8956 goto out_freepage;
8957
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008958 for (i = 0; i < flavors->num_flavors; i++) {
8959 secinfo = &flavors->flavors[i];
8960
8961 switch (secinfo->flavor) {
8962 case RPC_AUTH_NULL:
8963 case RPC_AUTH_UNIX:
8964 case RPC_AUTH_GSS:
8965 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8966 &secinfo->flavor_info);
8967 break;
8968 default:
8969 flavor = RPC_AUTH_MAXFLAVOR;
8970 break;
8971 }
8972
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008973 if (!nfs_auth_info_match(&server->auth_info, flavor))
8974 flavor = RPC_AUTH_MAXFLAVOR;
8975
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008976 if (flavor != RPC_AUTH_MAXFLAVOR) {
8977 err = nfs4_lookup_root_sec(server, fhandle,
8978 info, flavor);
8979 if (!err)
8980 break;
8981 }
8982 }
8983
8984 if (flavor == RPC_AUTH_MAXFLAVOR)
8985 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008986
8987out_freepage:
8988 put_page(page);
8989 if (err == -EACCES)
8990 return -EPERM;
8991out:
8992 return err;
8993}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008994
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008995static int _nfs41_test_stateid(struct nfs_server *server,
8996 nfs4_stateid *stateid,
8997 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008998{
8999 int status;
9000 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009001 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009002 };
9003 struct nfs41_test_stateid_res res;
9004 struct rpc_message msg = {
9005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9006 .rpc_argp = &args,
9007 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009008 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009009 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009010 struct rpc_clnt *rpc_client = server->client;
9011
9012 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9013 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009014
Chuck Lever38527b12012-07-11 16:30:23 -04009015 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009016 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009017 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009018 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009019 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009020 if (status != NFS_OK) {
9021 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009022 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009023 }
9024 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009025 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009026}
9027
Trond Myklebust43912bb2016-09-22 13:38:56 -04009028static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9029 int err, struct nfs4_exception *exception)
9030{
9031 exception->retry = 0;
9032 switch(err) {
9033 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009034 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009035 nfs4_handle_exception(server, err, exception);
9036 break;
9037 case -NFS4ERR_BADSESSION:
9038 case -NFS4ERR_BADSLOT:
9039 case -NFS4ERR_BAD_HIGH_SLOT:
9040 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9041 case -NFS4ERR_DEADSESSION:
9042 nfs4_do_handle_exception(server, err, exception);
9043 }
9044}
9045
Chuck Lever38527b12012-07-11 16:30:23 -04009046/**
9047 * nfs41_test_stateid - perform a TEST_STATEID operation
9048 *
9049 * @server: server / transport on which to perform the operation
9050 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009051 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009052 *
9053 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9054 * Otherwise a negative NFS4ERR value is returned if the operation
9055 * failed or the state ID is not currently valid.
9056 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009057static int nfs41_test_stateid(struct nfs_server *server,
9058 nfs4_stateid *stateid,
9059 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009060{
9061 struct nfs4_exception exception = { };
9062 int err;
9063 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009064 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009065 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009066 } while (exception.retry);
9067 return err;
9068}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009069
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009070struct nfs_free_stateid_data {
9071 struct nfs_server *server;
9072 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009073 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009074};
9075
9076static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9077{
9078 struct nfs_free_stateid_data *data = calldata;
9079 nfs41_setup_sequence(nfs4_get_session(data->server),
9080 &data->args.seq_args,
9081 &data->res.seq_res,
9082 task);
9083}
9084
9085static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9086{
9087 struct nfs_free_stateid_data *data = calldata;
9088
9089 nfs41_sequence_done(task, &data->res.seq_res);
9090
9091 switch (task->tk_status) {
9092 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009093 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009094 rpc_restart_call_prepare(task);
9095 }
9096}
9097
9098static void nfs41_free_stateid_release(void *calldata)
9099{
9100 kfree(calldata);
9101}
9102
Trond Myklebust17f26b12013-08-21 15:48:42 -04009103static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009104 .rpc_call_prepare = nfs41_free_stateid_prepare,
9105 .rpc_call_done = nfs41_free_stateid_done,
9106 .rpc_release = nfs41_free_stateid_release,
9107};
9108
9109static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009110 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009111 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009112 bool privileged)
9113{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009114 struct rpc_message msg = {
9115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009116 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009117 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009118 struct rpc_task_setup task_setup = {
9119 .rpc_client = server->client,
9120 .rpc_message = &msg,
9121 .callback_ops = &nfs41_free_stateid_ops,
9122 .flags = RPC_TASK_ASYNC,
9123 };
9124 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009125
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009126 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9127 &task_setup.rpc_client, &msg);
9128
Chuck Lever38527b12012-07-11 16:30:23 -04009129 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009130 data = kmalloc(sizeof(*data), GFP_NOFS);
9131 if (!data)
9132 return ERR_PTR(-ENOMEM);
9133 data->server = server;
9134 nfs4_stateid_copy(&data->args.stateid, stateid);
9135
9136 task_setup.callback_data = data;
9137
9138 msg.rpc_argp = &data->args;
9139 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009140 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009141 if (privileged)
9142 nfs4_set_sequence_privileged(&data->args.seq_args);
9143
9144 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009145}
9146
Chuck Lever38527b12012-07-11 16:30:23 -04009147/**
9148 * nfs41_free_stateid - perform a FREE_STATEID operation
9149 *
9150 * @server: server / transport on which to perform the operation
9151 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009152 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009153 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009154 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009155 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009156 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009157static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009158 const nfs4_stateid *stateid,
9159 struct rpc_cred *cred,
9160 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009161{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009162 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009163
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009164 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009165 if (IS_ERR(task))
9166 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009167 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009168 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009169}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009170
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009171static void
9172nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009173{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009174 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009175
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009176 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009177 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009178}
9179
Trond Myklebust36281ca2012-03-04 18:13:56 -05009180static bool nfs41_match_stateid(const nfs4_stateid *s1,
9181 const nfs4_stateid *s2)
9182{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009183 if (s1->type != s2->type)
9184 return false;
9185
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009186 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009187 return false;
9188
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009189 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009190 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009191 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009192 return true;
9193
9194 return false;
9195}
9196
Andy Adamson557134a2009-04-01 09:21:53 -04009197#endif /* CONFIG_NFS_V4_1 */
9198
Trond Myklebust36281ca2012-03-04 18:13:56 -05009199static bool nfs4_match_stateid(const nfs4_stateid *s1,
9200 const nfs4_stateid *s2)
9201{
Trond Myklebustf597c532012-03-04 18:13:56 -05009202 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009203}
9204
9205
Trond Myklebust17280172012-03-11 13:11:00 -04009206static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009207 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009208 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009209 .recover_open = nfs4_open_reclaim,
9210 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009211 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009212 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009213};
9214
Andy Adamson591d71c2009-04-01 09:22:47 -04009215#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009216static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009217 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9218 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9219 .recover_open = nfs4_open_reclaim,
9220 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009221 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009222 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009223 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009224};
9225#endif /* CONFIG_NFS_V4_1 */
9226
Trond Myklebust17280172012-03-11 13:11:00 -04009227static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009228 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009229 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009230 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009231 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009232 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009233};
9234
Andy Adamson591d71c2009-04-01 09:22:47 -04009235#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009236static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009237 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9238 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009239 .recover_open = nfs41_open_expired,
9240 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009241 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009242};
9243#endif /* CONFIG_NFS_V4_1 */
9244
Trond Myklebust17280172012-03-11 13:11:00 -04009245static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009246 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009247 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009248 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009249};
9250
9251#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009252static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009253 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009254 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009255 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009256};
9257#endif
9258
Chuck Leverec011fe2013-10-17 14:12:39 -04009259static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009260 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009261 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009262};
9263
9264#if defined(CONFIG_NFS_V4_1)
9265static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009266 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009267 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009268};
9269#endif /* CONFIG_NFS_V4_1 */
9270
Trond Myklebust97dc1352010-06-16 09:52:26 -04009271static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9272 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009273 .init_caps = NFS_CAP_READDIRPLUS
9274 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009275 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009276 .init_client = nfs40_init_client,
9277 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009278 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009279 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009280 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009281 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009282 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009283 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009284 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9285 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9286 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009287 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009288};
9289
9290#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009291static struct nfs_seqid *
9292nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9293{
9294 return NULL;
9295}
9296
Trond Myklebust97dc1352010-06-16 09:52:26 -04009297static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9298 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009299 .init_caps = NFS_CAP_READDIRPLUS
9300 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009301 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009302 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009303 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009304 .init_client = nfs41_init_client,
9305 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009306 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009307 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009308 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009309 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009310 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009311 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009312 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009313 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9314 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9315 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009316 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009317};
9318#endif
9319
Steve Dickson42c2c422013-05-22 12:50:38 -04009320#if defined(CONFIG_NFS_V4_2)
9321static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9322 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009323 .init_caps = NFS_CAP_READDIRPLUS
9324 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009325 | NFS_CAP_POSIX_LOCK
9326 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009327 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009328 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009329 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009330 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009331 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009332 | NFS_CAP_LAYOUTSTATS
9333 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009334 .init_client = nfs41_init_client,
9335 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009336 .match_stateid = nfs41_match_stateid,
9337 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009338 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009339 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009340 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009341 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009342 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009343 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9344 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9345 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009346 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009347};
9348#endif
9349
Trond Myklebust97dc1352010-06-16 09:52:26 -04009350const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9351 [0] = &nfs_v4_0_minor_ops,
9352#if defined(CONFIG_NFS_V4_1)
9353 [1] = &nfs_v4_1_minor_ops,
9354#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009355#if defined(CONFIG_NFS_V4_2)
9356 [2] = &nfs_v4_2_minor_ops,
9357#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009358};
9359
Trond Myklebust13997822016-07-24 17:10:52 -04009360static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009361{
9362 ssize_t error, error2;
9363
9364 error = generic_listxattr(dentry, list, size);
9365 if (error < 0)
9366 return error;
9367 if (list) {
9368 list += error;
9369 size -= error;
9370 }
9371
9372 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9373 if (error2 < 0)
9374 return error2;
9375 return error + error2;
9376}
9377
Trond Myklebust17f26b12013-08-21 15:48:42 -04009378static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009379 .create = nfs_create,
9380 .lookup = nfs_lookup,
9381 .atomic_open = nfs_atomic_open,
9382 .link = nfs_link,
9383 .unlink = nfs_unlink,
9384 .symlink = nfs_symlink,
9385 .mkdir = nfs_mkdir,
9386 .rmdir = nfs_rmdir,
9387 .mknod = nfs_mknod,
9388 .rename = nfs_rename,
9389 .permission = nfs_permission,
9390 .getattr = nfs_getattr,
9391 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009392 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009393};
9394
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009395static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009396 .permission = nfs_permission,
9397 .getattr = nfs_getattr,
9398 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009399 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009400};
9401
David Howells509de812006-08-22 20:06:11 -04009402const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009403 .version = 4, /* protocol version */
9404 .dentry_ops = &nfs4_dentry_operations,
9405 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009406 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009407 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009408 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009409 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009410 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009411 .getattr = nfs4_proc_getattr,
9412 .setattr = nfs4_proc_setattr,
9413 .lookup = nfs4_proc_lookup,
9414 .access = nfs4_proc_access,
9415 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009416 .create = nfs4_proc_create,
9417 .remove = nfs4_proc_remove,
9418 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009419 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009420 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009421 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009422 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009423 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .link = nfs4_proc_link,
9425 .symlink = nfs4_proc_symlink,
9426 .mkdir = nfs4_proc_mkdir,
9427 .rmdir = nfs4_proc_remove,
9428 .readdir = nfs4_proc_readdir,
9429 .mknod = nfs4_proc_mknod,
9430 .statfs = nfs4_proc_statfs,
9431 .fsinfo = nfs4_proc_fsinfo,
9432 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009433 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009435 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009436 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009437 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009438 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009439 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009440 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009441 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009442 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009444 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009445 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009446 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009447 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009448 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009449 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009450 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009451 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009452 .create_server = nfs4_create_server,
9453 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454};
9455
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009456static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009457 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009458 .list = nfs4_xattr_list_nfs4_acl,
9459 .get = nfs4_xattr_get_nfs4_acl,
9460 .set = nfs4_xattr_set_nfs4_acl,
9461};
9462
9463const struct xattr_handler *nfs4_xattr_handlers[] = {
9464 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009465#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9466 &nfs4_xattr_nfs4_label_handler,
9467#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009468 NULL
9469};
9470
Linus Torvalds1da177e2005-04-16 15:20:36 -07009471/*
9472 * Local variables:
9473 * c-basic-offset: 8
9474 * End:
9475 */