blob: 9795725d708c0d7b4e42be96e2aa67e97876b506 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
Jeff Laytonfeaff8e2015-05-12 15:48:10 -040041#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050043#include <linux/ratelimit.h>
44#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090045#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/sunrpc/clnt.h>
47#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000055#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050056#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010057#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040097 struct nfs4_state *state, struct nfs4_label *ilabel,
98 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400100static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
101 struct rpc_cred *);
102static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
103 struct rpc_cred *);
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
229 | FATTR4_WORD0_CHANGE
230 | FATTR4_WORD0_FILEID,
231};
232
David Quigleya09df2c2013-05-22 12:50:41 -0400233const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 FATTR4_WORD0_FILES_AVAIL
235 | FATTR4_WORD0_FILES_FREE
236 | FATTR4_WORD0_FILES_TOTAL,
237 FATTR4_WORD1_SPACE_AVAIL
238 | FATTR4_WORD1_SPACE_FREE
239 | FATTR4_WORD1_SPACE_TOTAL
240};
241
David Quigleya09df2c2013-05-22 12:50:41 -0400242const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 FATTR4_WORD0_MAXLINK
244 | FATTR4_WORD0_MAXNAME,
245 0
246};
247
Fred Isamandae100c2011-07-30 20:52:37 -0400248const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 | FATTR4_WORD0_MAXREAD
250 | FATTR4_WORD0_MAXWRITE
251 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700252 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400253 | FATTR4_WORD1_FS_LAYOUT_TYPES,
254 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800255 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256};
257
David Quigleya09df2c2013-05-22 12:50:41 -0400258const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400259 FATTR4_WORD0_TYPE
260 | FATTR4_WORD0_CHANGE
261 | FATTR4_WORD0_SIZE
262 | FATTR4_WORD0_FSID
263 | FATTR4_WORD0_FILEID
264 | FATTR4_WORD0_FS_LOCATIONS,
265 FATTR4_WORD1_MODE
266 | FATTR4_WORD1_NUMLINKS
267 | FATTR4_WORD1_OWNER
268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Al Viro0dbb4c62006-10-19 23:28:49 -0700280 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000283 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
285 return;
286 }
287
288 readdir->cookie = 0;
289 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
290 if (cookie == 2)
291 return;
292
293 /*
294 * NFSv4 servers do not return entries for '.' and '..'
295 * Therefore, we fake these entries here. We let '.'
296 * have cookie 0 and '..' have cookie 1. Note that
297 * when talking to the server, we always send cookie 0
298 * instead of 1 or 2.
299 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800300 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (cookie == 0) {
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_one; /* cookie, second word */
306 *p++ = xdr_one; /* entry len */
307 memcpy(p, ".\0\0\0", 4); /* entry */
308 p++;
309 *p++ = xdr_one; /* bitmap length */
310 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
311 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000312 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
315 *p++ = xdr_one; /* next */
316 *p++ = xdr_zero; /* cookie, first word */
317 *p++ = xdr_two; /* cookie, second word */
318 *p++ = xdr_two; /* entry len */
319 memcpy(p, "..\0\0", 4); /* entry */
320 p++;
321 *p++ = xdr_one; /* bitmap length */
322 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
323 *p++ = htonl(8); /* attribute buffer length */
David Howells2b0143b2015-03-17 22:25:59 +0000324 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 readdir->pgbase = (char *)p - (char *)start;
327 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800328 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
NeilBrown8478eaa2014-09-18 16:09:27 +1000331static long nfs4_update_delay(long *timeout)
332{
333 long ret;
334 if (!timeout)
335 return NFS4_POLL_RETRY_MAX;
336 if (*timeout <= 0)
337 *timeout = NFS4_POLL_RETRY_MIN;
338 if (*timeout > NFS4_POLL_RETRY_MAX)
339 *timeout = NFS4_POLL_RETRY_MAX;
340 ret = *timeout;
341 *timeout <<= 1;
342 return ret;
343}
344
Trond Myklebust65de8722008-12-23 15:21:44 -0500345static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
346{
347 int res = 0;
348
349 might_sleep();
350
NeilBrown8478eaa2014-09-18 16:09:27 +1000351 freezable_schedule_timeout_killable_unsafe(
352 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500353 if (fatal_signal_pending(current))
354 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500355 return res;
356}
357
358/* This is the error handling routine for processes that are allowed
359 * to sleep.
360 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400361static int nfs4_do_handle_exception(struct nfs_server *server,
362 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500363{
364 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500365 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500366 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500367 int ret = errorcode;
368
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400369 exception->delay = 0;
370 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500371 exception->retry = 0;
372 switch(errorcode) {
373 case 0:
374 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500375 case -NFS4ERR_OPENMODE:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400376 case -NFS4ERR_DELEG_REVOKED:
377 case -NFS4ERR_ADMIN_REVOKED:
378 case -NFS4ERR_BAD_STATEID:
Trond Myklebust4816fda2015-09-20 14:58:42 -0400379 if (inode && nfs_async_inode_return_delegation(inode,
380 NULL) == 0)
381 goto wait_on_recovery;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500382 if (state == NULL)
383 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400384 ret = nfs4_schedule_stateid_recovery(server, state);
385 if (ret < 0)
386 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500387 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400388 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400389 if (state != NULL) {
390 ret = nfs4_schedule_stateid_recovery(server, state);
391 if (ret < 0)
392 break;
393 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500394 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500395 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500396 nfs4_schedule_lease_recovery(clp);
397 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400398 case -NFS4ERR_MOVED:
399 ret = nfs4_schedule_migration_recovery(server);
400 if (ret < 0)
401 break;
402 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400403 case -NFS4ERR_LEASE_MOVED:
404 nfs4_schedule_lease_moved_recovery(clp);
405 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500406#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400407 case -NFS4ERR_BADSESSION:
408 case -NFS4ERR_BADSLOT:
409 case -NFS4ERR_BAD_HIGH_SLOT:
410 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
411 case -NFS4ERR_DEADSESSION:
412 case -NFS4ERR_SEQ_FALSE_RETRY:
413 case -NFS4ERR_SEQ_MISORDERED:
414 dprintk("%s ERROR: %d Reset session\n", __func__,
415 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400416 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400417 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500418#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500419 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500420 if (exception->timeout > HZ) {
421 /* We have retried a decent amount, time to
422 * fail
423 */
424 ret = -EBUSY;
425 break;
426 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500427 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400428 nfs_inc_server_stats(server, NFSIOS_DELAY);
429 case -NFS4ERR_GRACE:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400430 exception->delay = 1;
431 return 0;
432
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400433 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500434 case -NFS4ERR_OLD_STATEID:
435 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800436 break;
437 case -NFS4ERR_BADOWNER:
438 /* The following works around a Linux server bug! */
439 case -NFS4ERR_BADNAME:
440 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
441 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
442 exception->retry = 1;
443 printk(KERN_WARNING "NFS: v4 server %s "
444 "does not accept raw "
445 "uid/gids. "
446 "Reenabling the idmapper.\n",
447 server->nfs_client->cl_hostname);
448 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500449 }
450 /* We failed to handle the error */
451 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500452wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400453 exception->recovering = 1;
454 return 0;
455}
456
457/* This is the error handling routine for processes that are allowed
458 * to sleep.
459 */
460int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
461{
462 struct nfs_client *clp = server->nfs_client;
463 int ret;
464
465 ret = nfs4_do_handle_exception(server, errorcode, exception);
466 if (exception->delay) {
467 ret = nfs4_delay(server->client, &exception->timeout);
468 goto out_retry;
469 }
470 if (exception->recovering) {
471 ret = nfs4_wait_clnt_recover(clp);
472 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
473 return -EIO;
474 goto out_retry;
475 }
476 return ret;
477out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500478 if (ret == 0)
479 exception->retry = 1;
480 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500481}
482
Trond Myklebust037fc982015-09-20 15:51:00 -0400483static int
484nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
485 int errorcode, struct nfs4_exception *exception)
486{
487 struct nfs_client *clp = server->nfs_client;
488 int ret;
489
490 ret = nfs4_do_handle_exception(server, errorcode, exception);
491 if (exception->delay) {
492 rpc_delay(task, nfs4_update_delay(&exception->timeout));
493 goto out_retry;
494 }
495 if (exception->recovering) {
496 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
497 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
498 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
499 goto out_retry;
500 }
501 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
502 ret = -EIO;
503 return ret;
504out_retry:
505 if (ret == 0)
506 exception->retry = 1;
507 return ret;
508}
509
510static int
511nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
512 struct nfs4_state *state, long *timeout)
513{
514 struct nfs4_exception exception = {
515 .state = state,
516 };
517
518 if (task->tk_status >= 0)
519 return 0;
520 if (timeout)
521 exception.timeout = *timeout;
522 task->tk_status = nfs4_async_handle_exception(task, server,
523 task->tk_status,
524 &exception);
525 if (exception.delay && timeout)
526 *timeout = exception.timeout;
527 if (exception.retry)
528 return -EAGAIN;
529 return 0;
530}
531
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400532/*
533 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
534 * or 'false' otherwise.
535 */
536static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
537{
538 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
539
540 if (flavor == RPC_AUTH_GSS_KRB5I ||
541 flavor == RPC_AUTH_GSS_KRB5P)
542 return true;
543
544 return false;
545}
Trond Myklebust65de8722008-12-23 15:21:44 -0500546
Trond Myklebust452e9352010-07-31 14:29:06 -0400547static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 spin_lock(&clp->cl_lock);
550 if (time_before(clp->cl_last_renewal,timestamp))
551 clp->cl_last_renewal = timestamp;
552 spin_unlock(&clp->cl_lock);
553}
554
Trond Myklebust452e9352010-07-31 14:29:06 -0400555static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
556{
Trond Myklebustbe824162015-07-05 14:50:46 -0400557 struct nfs_client *clp = server->nfs_client;
558
559 if (!nfs4_has_session(clp))
560 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400561}
562
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400563struct nfs4_call_sync_data {
564 const struct nfs_server *seq_server;
565 struct nfs4_sequence_args *seq_args;
566 struct nfs4_sequence_res *seq_res;
567};
568
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800569void nfs4_init_sequence(struct nfs4_sequence_args *args,
570 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400571{
572 args->sa_slot = NULL;
573 args->sa_cache_this = cache_reply;
574 args->sa_privileged = 0;
575
576 res->sr_slot = NULL;
577}
578
579static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
580{
581 args->sa_privileged = 1;
582}
583
Peng Taocb04ad22014-06-11 05:24:15 +0800584int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
585 struct nfs4_sequence_args *args,
586 struct nfs4_sequence_res *res,
587 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400588{
Chuck Lever3bd23842013-08-09 12:49:19 -0400589 struct nfs4_slot *slot;
590
591 /* slot already allocated? */
592 if (res->sr_slot != NULL)
593 goto out_start;
594
595 spin_lock(&tbl->slot_tbl_lock);
596 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
597 goto out_sleep;
598
599 slot = nfs4_alloc_slot(tbl);
600 if (IS_ERR(slot)) {
601 if (slot == ERR_PTR(-ENOMEM))
602 task->tk_timeout = HZ >> 2;
603 goto out_sleep;
604 }
605 spin_unlock(&tbl->slot_tbl_lock);
606
607 args->sa_slot = slot;
608 res->sr_slot = slot;
609
610out_start:
611 rpc_call_start(task);
612 return 0;
613
614out_sleep:
615 if (args->sa_privileged)
616 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
617 NULL, RPC_PRIORITY_PRIVILEGED);
618 else
619 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
620 spin_unlock(&tbl->slot_tbl_lock);
621 return -EAGAIN;
622}
Peng Taocb04ad22014-06-11 05:24:15 +0800623EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400624
625static int nfs40_sequence_done(struct rpc_task *task,
626 struct nfs4_sequence_res *res)
627{
628 struct nfs4_slot *slot = res->sr_slot;
629 struct nfs4_slot_table *tbl;
630
Trond Myklebustcab92c12014-01-29 12:12:15 -0500631 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400632 goto out;
633
634 tbl = slot->table;
635 spin_lock(&tbl->slot_tbl_lock);
636 if (!nfs41_wake_and_assign_slot(tbl, slot))
637 nfs4_free_slot(tbl, slot);
638 spin_unlock(&tbl->slot_tbl_lock);
639
640 res->sr_slot = NULL;
641out:
642 return 1;
643}
644
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400645#if defined(CONFIG_NFS_V4_1)
646
Trond Myklebustd185a332010-06-16 09:52:25 -0400647static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400648{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500649 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400650 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500652 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400653
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500654 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500655 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500656
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500657 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500658 /* Be nice to the server: try to ensure that the last transmitted
659 * value for highest_user_slotid <= target_highest_slotid
660 */
661 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
662 send_new_highest_used_slotid = true;
663
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500664 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500665 send_new_highest_used_slotid = false;
666 goto out_unlock;
667 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500668 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500669
670 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
671 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500672out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500673 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400674 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500675 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400676 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400677}
678
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500679int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400680{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500681 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500682 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400683 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500684 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500685 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400686
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 if (slot == NULL)
688 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400689 /* don't increment the sequence number if the task wasn't sent */
690 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691 goto out;
692
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500693 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500694
Trond Myklebustac20d162012-12-15 15:36:07 -0500695 if (slot->interrupted) {
696 slot->interrupted = 0;
697 interrupted = true;
698 }
699
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400700 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500701 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400702 switch (res->sr_status) {
703 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400704 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500705 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500706 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500707 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500708 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400709 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500710 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400711 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500712 case 1:
713 /*
714 * sr_status remains 1 if an RPC level error occurred.
715 * The server may or may not have processed the sequence
716 * operation..
717 * Mark the slot as having hosted an interrupted RPC call.
718 */
719 slot->interrupted = 1;
720 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 case -NFS4ERR_DELAY:
722 /* The server detected a resend of the RPC call and
723 * returned NFS4ERR_DELAY as per Section 2.10.6.2
724 * of RFC5661.
725 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500726 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400727 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500728 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500729 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500731 case -NFS4ERR_BADSLOT:
732 /*
733 * The slot id we used was probably retired. Try again
734 * using a different slot id.
735 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500736 goto retry_nowait;
737 case -NFS4ERR_SEQ_MISORDERED:
738 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500739 * Was the last operation on this sequence interrupted?
740 * If so, retry after bumping the sequence number.
741 */
742 if (interrupted) {
743 ++slot->seq_nr;
744 goto retry_nowait;
745 }
746 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500747 * Could this slot have been previously retired?
748 * If so, then the server may be expecting seq_nr = 1!
749 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500750 if (slot->seq_nr != 1) {
751 slot->seq_nr = 1;
752 goto retry_nowait;
753 }
754 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500755 case -NFS4ERR_SEQ_FALSE_RETRY:
756 ++slot->seq_nr;
757 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 default:
759 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500760 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400761 }
762out:
763 /* The session may be reset by one of the error handlers. */
764 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400765 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500766out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500767 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500768retry_nowait:
769 if (rpc_restart_call_prepare(task)) {
770 task->tk_status = 0;
771 ret = 0;
772 }
773 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400774out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400775 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 goto out;
777 rpc_delay(task, NFS4_POLL_RETRY_MAX);
778 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400779}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500780EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400781
Peng Tao2c4b1312014-06-11 05:24:16 +0800782int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400783{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500784 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400785 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400786 if (!res->sr_slot->table->session)
787 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400788 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400789}
Peng Tao2c4b1312014-06-11 05:24:16 +0800790EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400791
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000792int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400793 struct nfs4_sequence_args *args,
794 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400795 struct rpc_task *task)
796{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400797 struct nfs4_slot *slot;
798 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400799
800 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400801 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400802 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400803 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400804
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400805 tbl = &session->fc_slot_table;
806
Trond Myklebust69d206b2012-11-22 13:21:02 -0500807 task->tk_timeout = 0;
808
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400809 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400810 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400811 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500812 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500813 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400814 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400815 }
816
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500817 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500818 if (IS_ERR(slot)) {
819 /* If out of memory, try again in 1/4 second */
820 if (slot == ERR_PTR(-ENOMEM))
821 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400822 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400823 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400824 }
825 spin_unlock(&tbl->slot_tbl_lock);
826
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500827 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400828
Chuck Levere8d92382013-08-09 12:47:51 -0400829 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500830 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400831
Benny Halevydfb4f3092010-09-24 09:17:01 -0400832 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500833 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400834 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400835 /*
836 * sr_status is only set in decode_sequence, and so will remain
837 * set to 1 if an rpc level failure occurs.
838 */
839 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400840 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400841out_success:
842 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400843 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400844out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400845 /* Privileged tasks are queued with top priority */
846 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400847 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
848 NULL, RPC_PRIORITY_PRIVILEGED);
849 else
850 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400851 spin_unlock(&tbl->slot_tbl_lock);
852 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000854EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855
Chuck Lever5a580e02013-08-09 12:48:09 -0400856static int nfs4_setup_sequence(const struct nfs_server *server,
857 struct nfs4_sequence_args *args,
858 struct nfs4_sequence_res *res,
859 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400860{
Trond Myklebust035168a2010-06-16 09:52:26 -0400861 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400862 int ret = 0;
863
Chuck Lever3bd23842013-08-09 12:49:19 -0400864 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800865 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
866 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400867
Chuck Levere8d92382013-08-09 12:47:51 -0400868 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400869 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400870 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400871
Trond Myklebust9d12b212012-01-17 22:04:25 -0500872 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400873
Andy Adamsonce5039c2009-04-01 09:22:13 -0400874 dprintk("<-- %s status=%d\n", __func__, ret);
875 return ret;
876}
877
Andy Adamsonce5039c2009-04-01 09:22:13 -0400878static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
879{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400880 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400881 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400882
Trond Myklebust035168a2010-06-16 09:52:26 -0400883 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
884
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400885 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400886}
887
Andy Adamson69ab40c2009-04-01 09:22:19 -0400888static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
889{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400890 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400891
Trond Myklebust14516c32010-07-31 14:29:06 -0400892 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400893}
894
Trond Myklebust17280172012-03-11 13:11:00 -0400895static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400896 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400897 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400898};
899
Chuck Lever3bd23842013-08-09 12:49:19 -0400900#else /* !CONFIG_NFS_V4_1 */
901
Chuck Lever5a580e02013-08-09 12:48:09 -0400902static int nfs4_setup_sequence(const struct nfs_server *server,
903 struct nfs4_sequence_args *args,
904 struct nfs4_sequence_res *res,
905 struct rpc_task *task)
906{
Peng Taocb04ad22014-06-11 05:24:15 +0800907 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
908 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400909}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400910
Peng Tao2c4b1312014-06-11 05:24:16 +0800911int nfs4_sequence_done(struct rpc_task *task,
912 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400913{
Chuck Lever3bd23842013-08-09 12:49:19 -0400914 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400915}
Peng Tao2c4b1312014-06-11 05:24:16 +0800916EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400917
918#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400919
Chuck Lever9915ea72013-08-09 12:48:27 -0400920static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
921{
922 struct nfs4_call_sync_data *data = calldata;
923 nfs4_setup_sequence(data->seq_server,
924 data->seq_args, data->seq_res, task);
925}
926
927static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
928{
929 struct nfs4_call_sync_data *data = calldata;
930 nfs4_sequence_done(task, data->seq_res);
931}
932
933static const struct rpc_call_ops nfs40_call_sync_ops = {
934 .rpc_call_prepare = nfs40_call_sync_prepare,
935 .rpc_call_done = nfs40_call_sync_done,
936};
937
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400938static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400939 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100941 struct nfs4_sequence_args *args,
942 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400943{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100944 int ret;
945 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400946 struct nfs_client *clp = server->nfs_client;
947 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100948 .seq_server = server,
949 .seq_args = args,
950 .seq_res = res,
951 };
952 struct rpc_task_setup task_setup = {
953 .rpc_client = clnt,
954 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400955 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100956 .callback_data = &data
957 };
958
959 task = rpc_run_task(&task_setup);
960 if (IS_ERR(task))
961 ret = PTR_ERR(task);
962 else {
963 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400964 rpc_put_task(task);
965 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100966 return ret;
967}
968
Bryan Schumaker7c513052011-03-24 17:12:24 +0000969int nfs4_call_sync(struct rpc_clnt *clnt,
970 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000971 struct rpc_message *msg,
972 struct nfs4_sequence_args *args,
973 struct nfs4_sequence_res *res,
974 int cache_reply)
975{
Chuck Levera9c92d62013-08-09 12:48:18 -0400976 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400977 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000978}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
981{
982 struct nfs_inode *nfsi = NFS_I(dir);
983
984 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400985 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700986 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700988 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500989 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000990 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_unlock(&dir->i_lock);
992}
993
994struct nfs4_opendata {
995 struct kref kref;
996 struct nfs_openargs o_arg;
997 struct nfs_openres o_res;
998 struct nfs_open_confirmargs c_arg;
999 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001000 struct nfs4_string owner_name;
1001 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001002 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001004 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001006 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 struct nfs4_state_owner *owner;
1008 struct nfs4_state *state;
1009 struct iattr attrs;
1010 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001011 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001012 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001013 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001014 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int cancelled;
1016};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001017
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001018static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1019 int err, struct nfs4_exception *exception)
1020{
1021 if (err != -EINVAL)
1022 return false;
1023 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1024 return false;
1025 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1026 exception->retry = 1;
1027 return true;
1028}
1029
Trond Myklebust6ae37332015-01-30 14:21:14 -05001030static u32
1031nfs4_map_atomic_open_share(struct nfs_server *server,
1032 fmode_t fmode, int openflags)
1033{
1034 u32 res = 0;
1035
1036 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1037 case FMODE_READ:
1038 res = NFS4_SHARE_ACCESS_READ;
1039 break;
1040 case FMODE_WRITE:
1041 res = NFS4_SHARE_ACCESS_WRITE;
1042 break;
1043 case FMODE_READ|FMODE_WRITE:
1044 res = NFS4_SHARE_ACCESS_BOTH;
1045 }
1046 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1047 goto out;
1048 /* Want no delegation if we're using O_DIRECT */
1049 if (openflags & O_DIRECT)
1050 res |= NFS4_SHARE_WANT_NO_DELEG;
1051out:
1052 return res;
1053}
1054
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001055static enum open_claim_type4
1056nfs4_map_atomic_open_claim(struct nfs_server *server,
1057 enum open_claim_type4 claim)
1058{
1059 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1060 return claim;
1061 switch (claim) {
1062 default:
1063 return claim;
1064 case NFS4_OPEN_CLAIM_FH:
1065 return NFS4_OPEN_CLAIM_NULL;
1066 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1067 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1068 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1069 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1070 }
1071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001074{
1075 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001076 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001077 p->o_res.seqid = p->o_arg.seqid;
1078 p->c_res.seqid = p->c_arg.seqid;
1079 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001080 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001081 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001082 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001083}
1084
Al Viro82a2c1b2011-06-22 18:30:55 -04001085static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001086 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001087 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001088 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001089 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001090 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001091{
Al Viro82a2c1b2011-06-22 18:30:55 -04001092 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001093 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001094 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001095 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001096 struct nfs4_opendata *p;
1097
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001098 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001099 if (p == NULL)
1100 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001101
1102 p->f_label = nfs4_label_alloc(server, gfp_mask);
1103 if (IS_ERR(p->f_label))
1104 goto err_free_p;
1105
Kinglong Meea49c2692015-07-27 15:31:38 +08001106 p->a_label = nfs4_label_alloc(server, gfp_mask);
1107 if (IS_ERR(p->a_label))
1108 goto err_free_f;
1109
Trond Myklebust63f5f792015-01-23 19:19:25 -05001110 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1111 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001112 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001113 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001114 nfs_sb_active(dentry->d_sb);
1115 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001116 p->dir = parent;
1117 p->owner = sp;
1118 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001119 p->o_arg.open_flags = flags;
1120 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001121 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1122 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001123 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1124 * will return permission denied for all bits until close */
1125 if (!(flags & O_EXCL)) {
1126 /* ask server to check for all possible rights as results
1127 * are cached */
1128 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1129 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001130 }
David Howells7539bba2006-08-22 20:06:09 -04001131 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001132 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1133 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001134 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001135 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001136 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001137 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001138 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1140 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001141 case NFS4_OPEN_CLAIM_NULL:
1142 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1143 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1144 p->o_arg.fh = NFS_FH(dir);
1145 break;
1146 case NFS4_OPEN_CLAIM_PREVIOUS:
1147 case NFS4_OPEN_CLAIM_FH:
1148 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1149 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001150 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001151 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001152 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001153 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001154
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001155 p->o_arg.u.attrs = &p->attrs;
1156 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001157
1158 verf[0] = jiffies;
1159 verf[1] = current->pid;
1160 memcpy(p->o_arg.u.verifier.data, verf,
1161 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001163 p->c_arg.fh = &p->o_res.fh;
1164 p->c_arg.stateid = &p->o_res.stateid;
1165 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001166 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001167 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001168 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001169
1170err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001171 nfs4_label_free(p->a_label);
1172err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001173 nfs4_label_free(p->f_label);
1174err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 kfree(p);
1176err:
1177 dput(parent);
1178 return NULL;
1179}
1180
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001181static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001182{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001183 struct nfs4_opendata *p = container_of(kref,
1184 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001185 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001186
1187 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001188 if (p->state != NULL)
1189 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001190 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001191
Kinglong Meea49c2692015-07-27 15:31:38 +08001192 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001193 nfs4_label_free(p->f_label);
1194
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001195 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001196 dput(p->dentry);
1197 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001198 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001199 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001200 kfree(p);
1201}
1202
1203static void nfs4_opendata_put(struct nfs4_opendata *p)
1204{
1205 if (p != NULL)
1206 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207}
1208
Trond Myklebust06f814a2006-01-03 09:55:07 +01001209static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1210{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001211 int ret;
1212
Trond Myklebust06f814a2006-01-03 09:55:07 +01001213 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001214 return ret;
1215}
1216
Trond Myklebust24311f82015-09-20 10:50:17 -04001217static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1218 fmode_t fmode)
1219{
1220 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1221 case FMODE_READ|FMODE_WRITE:
1222 return state->n_rdwr != 0;
1223 case FMODE_WRITE:
1224 return state->n_wronly != 0;
1225 case FMODE_READ:
1226 return state->n_rdonly != 0;
1227 }
1228 WARN_ON_ONCE(1);
1229 return false;
1230}
1231
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001232static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001233{
1234 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001235
Trond Myklebust536e43d2012-01-17 22:04:26 -05001236 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001237 goto out;
1238 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001239 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001240 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1241 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001242 break;
1243 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001244 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1245 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001246 break;
1247 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001248 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1249 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001250 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001251out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001252 return ret;
1253}
1254
Trond Myklebust2a606182015-08-19 22:30:00 -05001255static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1256 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001257{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001258 if (delegation == NULL)
1259 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001261 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001262 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1263 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001264 switch (claim) {
1265 case NFS4_OPEN_CLAIM_NULL:
1266 case NFS4_OPEN_CLAIM_FH:
1267 break;
1268 case NFS4_OPEN_CLAIM_PREVIOUS:
1269 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1270 break;
1271 default:
1272 return 0;
1273 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001274 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001275 return 1;
1276}
1277
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001278static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001279{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001280 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001281 case FMODE_WRITE:
1282 state->n_wronly++;
1283 break;
1284 case FMODE_READ:
1285 state->n_rdonly++;
1286 break;
1287 case FMODE_READ|FMODE_WRITE:
1288 state->n_rdwr++;
1289 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001290 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001291}
1292
Trond Myklebust4f14c192014-02-12 19:15:06 -05001293static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001294{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001295 struct nfs_client *clp = state->owner->so_server->nfs_client;
1296 bool need_recover = false;
1297
1298 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1299 need_recover = true;
1300 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1301 need_recover = true;
1302 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1303 need_recover = true;
1304 if (need_recover)
1305 nfs4_state_mark_reclaim_nograce(clp, state);
1306}
1307
Trond Myklebuste999e802014-02-10 18:20:47 -05001308static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1309 nfs4_stateid *stateid)
1310{
1311 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1312 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001313 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1314 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001315 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001316 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001317 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1318 return true;
1319 return false;
1320}
1321
Trond Myklebustf95549c2015-01-23 18:06:09 -05001322static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1323{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001324 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1325 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001326 if (state->n_wronly)
1327 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1328 if (state->n_rdonly)
1329 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1330 if (state->n_rdwr)
1331 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001332 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001333}
1334
Trond Myklebust226056c2014-02-11 10:41:07 -05001335static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001336 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001337 nfs4_stateid *stateid, fmode_t fmode)
1338{
1339 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1340 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_WRITE:
1342 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1343 break;
1344 case FMODE_READ:
1345 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1346 break;
1347 case 0:
1348 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1349 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1350 clear_bit(NFS_OPEN_STATE, &state->flags);
1351 }
1352 if (stateid == NULL)
1353 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001354 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001355 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1356 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1357 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001358 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001359 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001360 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001361 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001362 nfs4_stateid_copy(&state->stateid, stateid);
1363 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001364}
1365
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001366static void nfs_clear_open_stateid(struct nfs4_state *state,
1367 nfs4_stateid *arg_stateid,
1368 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001369{
1370 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001371 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001372 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001373 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1374 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001375}
1376
Trond Myklebust003707c2007-07-05 18:07:55 -04001377static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1378{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001379 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001380 case FMODE_READ:
1381 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1382 break;
1383 case FMODE_WRITE:
1384 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1385 break;
1386 case FMODE_READ|FMODE_WRITE:
1387 set_bit(NFS_O_RDWR_STATE, &state->flags);
1388 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001389 if (!nfs_need_update_open_stateid(state, stateid))
1390 return;
1391 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1392 nfs4_stateid_copy(&state->stateid, stateid);
1393 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001394}
1395
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001396static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001398 /*
1399 * Protect the call to nfs4_state_set_mode_locked and
1400 * serialise the stateid update
1401 */
Andrew Elble361cad32015-12-02 09:20:57 -05001402 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001403 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001404 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001406 set_bit(NFS_DELEGATED_STATE, &state->flags);
1407 }
1408 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001410 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001412 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001415static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001416{
1417 struct nfs_inode *nfsi = NFS_I(state->inode);
1418 struct nfs_delegation *deleg_cur;
1419 int ret = 0;
1420
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001421 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001422
1423 rcu_read_lock();
1424 deleg_cur = rcu_dereference(nfsi->delegation);
1425 if (deleg_cur == NULL)
1426 goto no_delegation;
1427
1428 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001429 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001430 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001431 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001432 goto no_delegation_unlock;
1433
1434 if (delegation == NULL)
1435 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001436 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001437 goto no_delegation_unlock;
1438
Trond Myklebustb7391f42008-12-23 15:21:52 -05001439 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001441 ret = 1;
1442no_delegation_unlock:
1443 spin_unlock(&deleg_cur->lock);
1444no_delegation:
1445 rcu_read_unlock();
1446
1447 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001448 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001449 ret = 1;
1450 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001451 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1452 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001453
1454 return ret;
1455}
1456
Trond Myklebust39071e62015-01-24 15:07:56 -05001457static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1458 const nfs4_stateid *stateid)
1459{
1460 struct nfs4_state *state = lsp->ls_state;
1461 bool ret = false;
1462
1463 spin_lock(&state->state_lock);
1464 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1465 goto out_noupdate;
1466 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1467 goto out_noupdate;
1468 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1469 ret = true;
1470out_noupdate:
1471 spin_unlock(&state->state_lock);
1472 return ret;
1473}
Trond Myklebust34310432008-12-23 15:21:38 -05001474
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001476{
1477 struct nfs_delegation *delegation;
1478
1479 rcu_read_lock();
1480 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001481 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001482 rcu_read_unlock();
1483 return;
1484 }
1485 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001486 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001487}
1488
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001490{
1491 struct nfs4_state *state = opendata->state;
1492 struct nfs_inode *nfsi = NFS_I(state->inode);
1493 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001494 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001496 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001497 nfs4_stateid stateid;
1498 int ret = -EAGAIN;
1499
Trond Myklebustaac00a82007-07-05 19:02:21 -04001500 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001501 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001502 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001503 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001504 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001505 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001506 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001507 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001508 rcu_read_lock();
1509 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001510 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001511 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001512 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001513 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001514 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001515 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001516 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001517 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001518 if (!opendata->is_recover) {
1519 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1520 if (ret != 0)
1521 goto out;
1522 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001523 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001524
1525 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001526 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001527 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001528 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001529out:
1530 return ERR_PTR(ret);
1531out_return_state:
1532 atomic_inc(&state->count);
1533 return state;
1534}
1535
Andy Adamsone23008e2012-10-02 21:07:32 -04001536static void
1537nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1538{
1539 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1540 struct nfs_delegation *delegation;
1541 int delegation_flags = 0;
1542
1543 rcu_read_lock();
1544 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1545 if (delegation)
1546 delegation_flags = delegation->flags;
1547 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001548 switch (data->o_arg.claim) {
1549 default:
1550 break;
1551 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1552 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001553 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1554 "returning a delegation for "
1555 "OPEN(CLAIM_DELEGATE_CUR)\n",
1556 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001557 return;
1558 }
1559 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001560 nfs_inode_set_delegation(state->inode,
1561 data->owner->so_cred,
1562 &data->o_res);
1563 else
1564 nfs_inode_reclaim_delegation(state->inode,
1565 data->owner->so_cred,
1566 &data->o_res);
1567}
1568
1569/*
1570 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1571 * and update the nfs4_state.
1572 */
1573static struct nfs4_state *
1574_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1575{
1576 struct inode *inode = data->state->inode;
1577 struct nfs4_state *state = data->state;
1578 int ret;
1579
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001580 if (!data->rpc_done) {
1581 if (data->rpc_status) {
1582 ret = data->rpc_status;
1583 goto err;
1584 }
1585 /* cached opens have already been processed */
1586 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001587 }
1588
Andy Adamsone23008e2012-10-02 21:07:32 -04001589 ret = nfs_refresh_inode(inode, &data->f_attr);
1590 if (ret)
1591 goto err;
1592
1593 if (data->o_res.delegation_type != 0)
1594 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001595update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001596 update_open_stateid(state, &data->o_res.stateid, NULL,
1597 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001598 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001599
1600 return state;
1601err:
1602 return ERR_PTR(ret);
1603
1604}
1605
1606static struct nfs4_state *
1607_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608{
1609 struct inode *inode;
1610 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001611 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612
Trond Myklebustaac00a82007-07-05 19:02:21 -04001613 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001614 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001615 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 goto out;
1617 }
1618
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001619 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001620 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001621 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001622 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001623 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001624 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001625 goto err;
1626 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 state = nfs4_get_open_state(inode, data->owner);
1628 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001629 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001630 if (data->o_res.delegation_type != 0)
1631 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001632 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001633 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001636 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001637 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001638err_put_inode:
1639 iput(inode);
1640err:
1641 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001642}
1643
Andy Adamsone23008e2012-10-02 21:07:32 -04001644static struct nfs4_state *
1645nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1646{
1647 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1648 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1649 return _nfs4_opendata_to_nfs4_state(data);
1650}
1651
Trond Myklebust864472e2006-01-03 09:55:15 +01001652static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1653{
1654 struct nfs_inode *nfsi = NFS_I(state->inode);
1655 struct nfs_open_context *ctx;
1656
1657 spin_lock(&state->inode->i_lock);
1658 list_for_each_entry(ctx, &nfsi->open_files, list) {
1659 if (ctx->state != state)
1660 continue;
1661 get_nfs_open_context(ctx);
1662 spin_unlock(&state->inode->i_lock);
1663 return ctx;
1664 }
1665 spin_unlock(&state->inode->i_lock);
1666 return ERR_PTR(-ENOENT);
1667}
1668
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001669static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1670 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001671{
1672 struct nfs4_opendata *opendata;
1673
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001674 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001675 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001676 if (opendata == NULL)
1677 return ERR_PTR(-ENOMEM);
1678 opendata->state = state;
1679 atomic_inc(&state->count);
1680 return opendata;
1681}
1682
Trond Myklebust24311f82015-09-20 10:50:17 -04001683static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1684 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001685{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001686 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 int ret;
1688
Trond Myklebust24311f82015-09-20 10:50:17 -04001689 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001690 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 opendata->o_arg.open_flags = 0;
1692 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001693 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1694 NFS_SB(opendata->dentry->d_sb),
1695 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001696 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1697 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1698 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001699 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001700 if (ret != 0)
1701 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001702 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001703 if (IS_ERR(newstate))
1704 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001705 if (newstate != opendata->state)
1706 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001707 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001708 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001709}
1710
1711static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1712{
Trond Myklebust864472e2006-01-03 09:55:15 +01001713 int ret;
1714
Trond Myklebust4f14c192014-02-12 19:15:06 -05001715 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1716 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1717 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1718 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001719 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001720 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001721 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001722 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001723 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1724 if (ret != 0)
1725 return ret;
1726 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1727 if (ret != 0)
1728 return ret;
1729 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1730 if (ret != 0)
1731 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001732 /*
1733 * We may have performed cached opens for all three recoveries.
1734 * Check if we need to update the current stateid.
1735 */
1736 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001737 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001738 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001739 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001740 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001741 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001742 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001743 return 0;
1744}
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746/*
1747 * OPEN_RECLAIM:
1748 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001750static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001752 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001753 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001754 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int status;
1756
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001757 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1758 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001759 if (IS_ERR(opendata))
1760 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001761 rcu_read_lock();
1762 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001763 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001764 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001765 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001766 opendata->o_arg.u.delegation_type = delegation_type;
1767 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001768 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return status;
1770}
1771
Trond Myklebust539cd032007-06-05 11:46:42 -04001772static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 struct nfs_server *server = NFS_SERVER(state->inode);
1775 struct nfs4_exception exception = { };
1776 int err;
1777 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001778 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001779 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001780 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1781 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001782 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001783 break;
1784 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 } while (exception.retry);
1786 return err;
1787}
1788
Trond Myklebust864472e2006-01-03 09:55:15 +01001789static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1790{
1791 struct nfs_open_context *ctx;
1792 int ret;
1793
1794 ctx = nfs4_state_find_open_context(state);
1795 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001796 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001797 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001798 put_nfs_open_context(ctx);
1799 return ret;
1800}
1801
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001802static 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 -07001803{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001804 switch (err) {
1805 default:
1806 printk(KERN_ERR "NFS: %s: unhandled error "
1807 "%d.\n", __func__, err);
1808 case 0:
1809 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001810 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001811 case -ESTALE:
1812 break;
1813 case -NFS4ERR_BADSESSION:
1814 case -NFS4ERR_BADSLOT:
1815 case -NFS4ERR_BAD_HIGH_SLOT:
1816 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1817 case -NFS4ERR_DEADSESSION:
1818 set_bit(NFS_DELEGATED_STATE, &state->flags);
1819 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1820 return -EAGAIN;
1821 case -NFS4ERR_STALE_CLIENTID:
1822 case -NFS4ERR_STALE_STATEID:
1823 set_bit(NFS_DELEGATED_STATE, &state->flags);
1824 case -NFS4ERR_EXPIRED:
1825 /* Don't recall a delegation if it was lost */
1826 nfs4_schedule_lease_recovery(server->nfs_client);
1827 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001828 case -NFS4ERR_MOVED:
1829 nfs4_schedule_migration_recovery(server);
1830 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001831 case -NFS4ERR_LEASE_MOVED:
1832 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1833 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001834 case -NFS4ERR_DELEG_REVOKED:
1835 case -NFS4ERR_ADMIN_REVOKED:
1836 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001837 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001838 nfs_inode_find_state_and_recover(state->inode,
1839 stateid);
1840 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001841 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001842 case -NFS4ERR_DELAY:
1843 case -NFS4ERR_GRACE:
1844 set_bit(NFS_DELEGATED_STATE, &state->flags);
1845 ssleep(1);
1846 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001847 case -ENOMEM:
1848 case -NFS4ERR_DENIED:
1849 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1850 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return err;
1853}
1854
Trond Myklebust24311f82015-09-20 10:50:17 -04001855int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1856 struct nfs4_state *state, const nfs4_stateid *stateid,
1857 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001858{
1859 struct nfs_server *server = NFS_SERVER(state->inode);
1860 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001861 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001862
1863 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1864 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1865 if (IS_ERR(opendata))
1866 return PTR_ERR(opendata);
1867 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001868 write_seqlock(&state->seqlock);
1869 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1870 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001871 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1872 switch (type & (FMODE_READ|FMODE_WRITE)) {
1873 case FMODE_READ|FMODE_WRITE:
1874 case FMODE_WRITE:
1875 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1876 if (err)
1877 break;
1878 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1879 if (err)
1880 break;
1881 case FMODE_READ:
1882 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1883 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001884 nfs4_opendata_put(opendata);
1885 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1886}
1887
Chuck Leverbe05c862013-08-09 12:49:47 -04001888static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1889{
1890 struct nfs4_opendata *data = calldata;
1891
Peng Taocb04ad22014-06-11 05:24:15 +08001892 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1893 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001894}
1895
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001896static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1897{
1898 struct nfs4_opendata *data = calldata;
1899
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001900 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001901
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001902 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001903 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001904 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001905 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001906 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001907 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001908 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001909}
1910
1911static void nfs4_open_confirm_release(void *calldata)
1912{
1913 struct nfs4_opendata *data = calldata;
1914 struct nfs4_state *state = NULL;
1915
1916 /* If this request hasn't been cancelled, do nothing */
1917 if (data->cancelled == 0)
1918 goto out_free;
1919 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001920 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001921 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001922 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001923 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001924 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001925out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001926 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001927}
1928
1929static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001930 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001931 .rpc_call_done = nfs4_open_confirm_done,
1932 .rpc_release = nfs4_open_confirm_release,
1933};
1934
1935/*
1936 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1937 */
1938static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1939{
David Howells2b0143b2015-03-17 22:25:59 +00001940 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001941 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001942 struct rpc_message msg = {
1943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1944 .rpc_argp = &data->c_arg,
1945 .rpc_resp = &data->c_res,
1946 .rpc_cred = data->owner->so_cred,
1947 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001948 struct rpc_task_setup task_setup_data = {
1949 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001950 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001951 .callback_ops = &nfs4_open_confirm_ops,
1952 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001953 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001954 .flags = RPC_TASK_ASYNC,
1955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int status;
1957
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001958 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001959 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001960 data->rpc_done = 0;
1961 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001962 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001963 if (data->is_recover)
1964 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001965 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001966 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001967 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001968 status = nfs4_wait_for_completion_rpc_task(task);
1969 if (status != 0) {
1970 data->cancelled = 1;
1971 smp_wmb();
1972 } else
1973 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001974 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return status;
1976}
1977
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001978static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001980 struct nfs4_opendata *data = calldata;
1981 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001982 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001983 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001984
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001985 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001986 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001987 /*
1988 * Check if we still need to send an OPEN call, or if we can use
1989 * a delegation instead.
1990 */
1991 if (data->state != NULL) {
1992 struct nfs_delegation *delegation;
1993
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001994 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001995 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001996 rcu_read_lock();
1997 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001998 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05001999 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002000 rcu_read_unlock();
2001 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002002 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002003 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002004 switch (claim) {
2005 default:
2006 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002007 case NFS4_OPEN_CLAIM_PREVIOUS:
2008 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2009 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002010 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002011 case NFS4_OPEN_CLAIM_FH:
2012 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002013 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2014 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002015 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002016 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002017 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002018 &data->o_res.seq_res,
2019 task) != 0)
2020 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002021
2022 /* Set the create mode (note dependency on the session type) */
2023 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2024 if (data->o_arg.open_flags & O_EXCL) {
2025 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2026 if (nfs4_has_persistent_session(clp))
2027 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2028 else if (clp->cl_mvops->minor_version > 0)
2029 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2030 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002031 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002032unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002033 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002034 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002035out_no_action:
2036 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002037out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002038 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002039}
2040
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002041static void nfs4_open_done(struct rpc_task *task, void *calldata)
2042{
2043 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002045 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002046
Trond Myklebust14516c32010-07-31 14:29:06 -04002047 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2048 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002049
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002050 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002051 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2052 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002053 case S_IFREG:
2054 break;
2055 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002056 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002057 break;
2058 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002059 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002060 break;
2061 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002062 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002063 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002064 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002065 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002066 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2067 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002068 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002069 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002070}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002071
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002072static void nfs4_open_release(void *calldata)
2073{
2074 struct nfs4_opendata *data = calldata;
2075 struct nfs4_state *state = NULL;
2076
2077 /* If this request hasn't been cancelled, do nothing */
2078 if (data->cancelled == 0)
2079 goto out_free;
2080 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002081 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002082 goto out_free;
2083 /* In case we need an open_confirm, no cleanup! */
2084 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2085 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002086 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002087 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002088 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002089out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002090 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002091}
2092
2093static const struct rpc_call_ops nfs4_open_ops = {
2094 .rpc_call_prepare = nfs4_open_prepare,
2095 .rpc_call_done = nfs4_open_done,
2096 .rpc_release = nfs4_open_release,
2097};
2098
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002099static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002100{
David Howells2b0143b2015-03-17 22:25:59 +00002101 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002102 struct nfs_server *server = NFS_SERVER(dir);
2103 struct nfs_openargs *o_arg = &data->o_arg;
2104 struct nfs_openres *o_res = &data->o_res;
2105 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2108 .rpc_argp = o_arg,
2109 .rpc_resp = o_res,
2110 .rpc_cred = data->owner->so_cred,
2111 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002112 struct rpc_task_setup task_setup_data = {
2113 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002114 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002115 .callback_ops = &nfs4_open_ops,
2116 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002117 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002118 .flags = RPC_TASK_ASYNC,
2119 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 int status;
2121
Chuck Levera9c92d62013-08-09 12:48:18 -04002122 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002123 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002124 data->rpc_done = 0;
2125 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002126 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002127 data->is_recover = 0;
2128 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002129 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002130 data->is_recover = 1;
2131 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002132 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002133 if (IS_ERR(task))
2134 return PTR_ERR(task);
2135 status = nfs4_wait_for_completion_rpc_task(task);
2136 if (status != 0) {
2137 data->cancelled = 1;
2138 smp_wmb();
2139 } else
2140 status = data->rpc_status;
2141 rpc_put_task(task);
2142
2143 return status;
2144}
2145
2146static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2147{
David Howells2b0143b2015-03-17 22:25:59 +00002148 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002149 struct nfs_openres *o_res = &data->o_res;
2150 int status;
2151
2152 status = nfs4_run_open_task(data, 1);
2153 if (status != 0 || !data->rpc_done)
2154 return status;
2155
Trond Myklebust6926afd2012-01-07 13:22:46 -05002156 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2157
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002158 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2159 status = _nfs4_proc_open_confirm(data);
2160 if (status != 0)
2161 return status;
2162 }
2163
2164 return status;
2165}
2166
Trond Myklebustf3792d62014-07-10 08:54:32 -04002167/*
2168 * Additional permission checks in order to distinguish between an
2169 * open for read, and an open for execute. This works around the
2170 * fact that NFSv4 OPEN treats read and execute permissions as being
2171 * the same.
2172 * Note that in the non-execute case, we want to turn off permission
2173 * checking if we just created a new file (POSIX open() semantics).
2174 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002175static int nfs4_opendata_access(struct rpc_cred *cred,
2176 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002177 struct nfs4_state *state, fmode_t fmode,
2178 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002179{
2180 struct nfs_access_entry cache;
2181 u32 mask;
2182
2183 /* access call failed or for some reason the server doesn't
2184 * support any access modes -- defer access call until later */
2185 if (opendata->o_res.access_supported == 0)
2186 return 0;
2187
2188 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002189 /*
2190 * Use openflags to check for exec, because fmode won't
2191 * always have FMODE_EXEC set when file open for exec.
2192 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002193 if (openflags & __FMODE_EXEC) {
2194 /* ONLY check for exec rights */
2195 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002196 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002197 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002198
2199 cache.cred = cred;
2200 cache.jiffies = jiffies;
2201 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2202 nfs_access_add_cache(state->inode, &cache);
2203
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002204 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002205 return 0;
2206
2207 /* even though OPEN succeeded, access is denied. Close the file */
2208 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002209 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002210}
2211
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002212/*
2213 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2214 */
2215static int _nfs4_proc_open(struct nfs4_opendata *data)
2216{
David Howells2b0143b2015-03-17 22:25:59 +00002217 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002218 struct nfs_server *server = NFS_SERVER(dir);
2219 struct nfs_openargs *o_arg = &data->o_arg;
2220 struct nfs_openres *o_res = &data->o_res;
2221 int status;
2222
2223 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002224 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002225 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002226 if (status != 0) {
2227 if (status == -NFS4ERR_BADNAME &&
2228 !(o_arg->open_flags & O_CREAT))
2229 return -ENOENT;
2230 return status;
2231 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002232
Trond Myklebust6926afd2012-01-07 13:22:46 -05002233 nfs_fattr_map_and_free_names(server, &data->f_attr);
2234
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002235 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002236 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002237 if (o_arg->open_flags & O_EXCL)
2238 data->file_created = 1;
2239 else if (o_res->cinfo.before != o_res->cinfo.after)
2240 data->file_created = 1;
2241 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002242 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2243 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002245 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002247 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
2249 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002250 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Andy Adamsond83217c2011-03-01 01:34:17 +00002254static int nfs4_recover_expired_lease(struct nfs_server *server)
2255{
2256 return nfs4_client_recover_expired_lease(server->nfs_client);
2257}
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
2260 * OPEN_EXPIRED:
2261 * reclaim state on the server after a network partition.
2262 * Assumes caller holds the appropriate lock
2263 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002264static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002266 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002269 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002270 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002271 if (IS_ERR(opendata))
2272 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002273 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002274 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002275 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002276 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
2279
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002280static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002281{
Trond Myklebust539cd032007-06-05 11:46:42 -04002282 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002283 struct nfs4_exception exception = { };
2284 int err;
2285
2286 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002287 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002288 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002289 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2290 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002291 switch (err) {
2292 default:
2293 goto out;
2294 case -NFS4ERR_GRACE:
2295 case -NFS4ERR_DELAY:
2296 nfs4_handle_exception(server, err, &exception);
2297 err = 0;
2298 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002299 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002300out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002301 return err;
2302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2305{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002307 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Trond Myklebust864472e2006-01-03 09:55:15 +01002309 ctx = nfs4_state_find_open_context(state);
2310 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002311 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002312 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002313 put_nfs_open_context(ctx);
2314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002317static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2318{
2319 nfs_remove_bad_delegation(state->inode);
2320 write_seqlock(&state->seqlock);
2321 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2322 write_sequnlock(&state->seqlock);
2323 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2324}
2325
2326static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2327{
2328 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2329 nfs_finish_clear_delegation_stateid(state);
2330}
2331
2332static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2333{
2334 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2335 nfs40_clear_delegation_stateid(state);
2336 return nfs4_open_expired(sp, state);
2337}
2338
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002339#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002340static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002341{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002342 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002343 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002344 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002345 struct rpc_cred *cred;
2346 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002347
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002348 /* Get the delegation credential for use by test/free_stateid */
2349 rcu_read_lock();
2350 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002351 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002352 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002353 return;
2354 }
2355
2356 nfs4_stateid_copy(&stateid, &delegation->stateid);
2357 cred = get_rpccred(delegation->cred);
2358 rcu_read_unlock();
2359 status = nfs41_test_stateid(server, &stateid, cred);
2360 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002361
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002362 if (status != NFS_OK) {
2363 /* Free the stateid unless the server explicitly
2364 * informs us the stateid is unrecognized. */
2365 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002366 nfs41_free_stateid(server, &stateid, cred);
2367 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002368 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002369
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002370 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002371}
2372
2373/**
2374 * nfs41_check_open_stateid - possibly free an open stateid
2375 *
2376 * @state: NFSv4 state for an inode
2377 *
2378 * Returns NFS_OK if recovery for this stateid is now finished.
2379 * Otherwise a negative NFS4ERR value is returned.
2380 */
2381static int nfs41_check_open_stateid(struct nfs4_state *state)
2382{
2383 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002384 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002385 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002386 int status;
2387
2388 /* If a state reset has been done, test_stateid is unneeded */
2389 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2390 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2391 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2392 return -NFS4ERR_BAD_STATEID;
2393
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002394 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002395 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002396 if (status != NFS_OK) {
2397 /* Free the stateid unless the server explicitly
2398 * informs us the stateid is unrecognized. */
2399 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002400 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002401
2402 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2403 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2404 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002405 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002406 }
2407 return status;
2408}
2409
2410static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2411{
Chuck Levereb64cf92012-07-11 16:30:05 -04002412 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002413
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002414 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002415 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002416 if (status != NFS_OK)
2417 status = nfs4_open_expired(sp, state);
2418 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002419}
2420#endif
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002423 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2424 * fields corresponding to attributes that were used to store the verifier.
2425 * Make sure we clobber those fields in the later setattr call
2426 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002427static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2428 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002429{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002430 const u32 *attrset = opendata->o_res.attrset;
2431
2432 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002433 !(sattr->ia_valid & ATTR_ATIME_SET))
2434 sattr->ia_valid |= ATTR_ATIME;
2435
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002436 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002437 !(sattr->ia_valid & ATTR_MTIME_SET))
2438 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002439
2440 /* Except MODE, it seems harmless of setting twice. */
2441 if ((attrset[1] & FATTR4_WORD1_MODE))
2442 sattr->ia_valid &= ~ATTR_MODE;
2443
2444 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2445 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002446}
2447
Trond Myklebustc21443c2013-02-07 14:26:21 -05002448static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2449 fmode_t fmode,
2450 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002451 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002452{
2453 struct nfs4_state_owner *sp = opendata->owner;
2454 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002455 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002456 struct nfs4_state *state;
2457 unsigned int seq;
2458 int ret;
2459
2460 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2461
2462 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002463 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002464 goto out;
2465
2466 state = nfs4_opendata_to_nfs4_state(opendata);
2467 ret = PTR_ERR(state);
2468 if (IS_ERR(state))
2469 goto out;
2470 if (server->caps & NFS_CAP_POSIX_LOCK)
2471 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2472
Trond Myklebust275bb302013-05-29 13:11:28 -04002473 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002474 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002475 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002476 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002477 alias = d_exact_alias(dentry, state->inode);
2478 if (!alias)
2479 alias = d_splice_alias(igrab(state->inode), dentry);
2480 /* d_splice_alias() can't fail here - it's a non-directory */
2481 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002482 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002483 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002484 }
2485 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002486 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002487 }
2488
Trond Myklebustc21443c2013-02-07 14:26:21 -05002489 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2490 if (ret != 0)
2491 goto out;
2492
Trond Myklebust3efb9722013-05-29 13:17:04 -04002493 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002494 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002495 nfs_inode_attach_open_context(ctx);
2496 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2497 nfs4_schedule_stateid_recovery(server, state);
2498 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002499out:
2500 return ret;
2501}
2502
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002503/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002504 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 */
Andy Adamson82be4172012-05-23 05:02:35 -04002506static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002507 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002508 int flags,
2509 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002510 struct nfs4_label *label,
2511 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 struct nfs4_state_owner *sp;
2514 struct nfs4_state *state = NULL;
2515 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002516 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002517 struct dentry *dentry = ctx->dentry;
2518 struct rpc_cred *cred = ctx->cred;
2519 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2520 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002521 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002522 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002523 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002527 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2528 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2530 goto out_err;
2531 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002532 status = nfs4_recover_expired_lease(server);
2533 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002534 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002535 if (d_really_is_positive(dentry))
2536 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002537 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002538 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002539 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002540 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002541 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002542 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002543 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
David Quigley14c43f72013-05-22 12:50:43 -04002545 if (label) {
2546 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2547 if (IS_ERR(olabel)) {
2548 status = PTR_ERR(olabel);
2549 goto err_opendata_put;
2550 }
2551 }
2552
Trond Myklebuste911b812014-03-26 13:24:37 -07002553 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2554 if (!opendata->f_attr.mdsthreshold) {
2555 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2556 if (!opendata->f_attr.mdsthreshold)
2557 goto err_free_label;
2558 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002559 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002560 }
David Howells2b0143b2015-03-17 22:25:59 +00002561 if (d_really_is_positive(dentry))
2562 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002563
Trond Myklebust3efb9722013-05-29 13:17:04 -04002564 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002565 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002566 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002567 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002568
NeilBrownefcbc042015-07-30 13:00:56 +10002569 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002570 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002571 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002572 /*
2573 * send create attributes which was not set by open
2574 * with an extra setattr.
2575 */
2576 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2577 nfs_fattr_init(opendata->o_res.f_attr);
2578 status = nfs4_do_setattr(state->inode, cred,
2579 opendata->o_res.f_attr, sattr,
2580 state, label, olabel);
2581 if (status == 0) {
2582 nfs_setattr_update_inode(state->inode, sattr,
2583 opendata->o_res.f_attr);
2584 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2585 }
David Quigley1775fd32013-05-22 12:50:42 -04002586 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002587 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002588 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002589 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002590
Trond Myklebuste911b812014-03-26 13:24:37 -07002591 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002592 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002593 opendata->f_attr.mdsthreshold = NULL;
2594 }
Andy Adamson82be4172012-05-23 05:02:35 -04002595
David Quigley14c43f72013-05-22 12:50:43 -04002596 nfs4_label_free(olabel);
2597
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002598 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002601err_free_label:
2602 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002603err_opendata_put:
2604 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002605err_put_state_owner:
2606 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return status;
2609}
2610
2611
Andy Adamson82be4172012-05-23 05:02:35 -04002612static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002613 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002614 int flags,
2615 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002616 struct nfs4_label *label,
2617 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002619 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct nfs4_exception exception = { };
2621 struct nfs4_state *res;
2622 int status;
2623
2624 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002625 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002626 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002627 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (status == 0)
2629 break;
2630 /* NOTE: BAD_SEQID means the server and client disagree about the
2631 * book-keeping w.r.t. state-changing operations
2632 * (OPEN/CLOSE/LOCK/LOCKU...)
2633 * It is actually a sign of a bug on the client or on the server.
2634 *
2635 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002636 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * have unhashed the old state_owner for us, and that we can
2638 * therefore safely retry using a new one. We should still warn
2639 * the user though...
2640 */
2641 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002642 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002643 " returned a bad sequence-id error!\n",
2644 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 exception.retry = 1;
2646 continue;
2647 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002648 /*
2649 * BAD_STATEID on OPEN means that the server cancelled our
2650 * state before it received the OPEN_CONFIRM.
2651 * Recover by retrying the request as per the discussion
2652 * on Page 181 of RFC3530.
2653 */
2654 if (status == -NFS4ERR_BAD_STATEID) {
2655 exception.retry = 1;
2656 continue;
2657 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002658 if (status == -EAGAIN) {
2659 /* We must have found a delegation */
2660 exception.retry = 1;
2661 continue;
2662 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002663 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2664 continue;
2665 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 status, &exception));
2667 } while (exception.retry);
2668 return res;
2669}
2670
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002671static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2672 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002673 struct nfs4_state *state, struct nfs4_label *ilabel,
2674 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002676 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002678 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 .iap = sattr,
2680 .server = server,
2681 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002682 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 };
2684 struct nfs_setattrres res = {
2685 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002686 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 .server = server,
2688 };
2689 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2691 .rpc_argp = &arg,
2692 .rpc_resp = &res,
2693 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002695 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002696 fmode_t fmode;
2697 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002698 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
David Quigleyaa9c2662013-05-22 12:50:44 -04002700 arg.bitmask = nfs4_bitmask(server, ilabel);
2701 if (ilabel)
2702 arg.bitmask = nfs4_bitmask(server, olabel);
2703
Trond Myklebust0e574af2005-10-27 22:12:38 -04002704 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Trond Myklebustee3ae842013-04-29 10:35:36 -04002706 /* Servers should only apply open mode checks for file size changes */
2707 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2708 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2709
2710 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2711 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002712 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002713 struct nfs_lockowner lockowner = {
2714 .l_owner = current->files,
2715 .l_pid = current->tgid,
2716 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002717 if (!nfs4_valid_open_stateid(state))
2718 return -EBADF;
2719 if (nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
2720 &lockowner) == -EIO)
2721 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002722 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002723 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Bryan Schumaker7c513052011-03-24 17:12:24 +00002725 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002726 if (status == 0 && state != NULL)
2727 renew_lease(server, timestamp);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05002728 trace_nfs4_setattr(inode, &arg.stateid, status);
Trond Myklebust65e43082005-08-16 11:49:44 -04002729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002732static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2733 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002734 struct nfs4_state *state, struct nfs4_label *ilabel,
2735 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002737 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002738 struct nfs4_exception exception = {
2739 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002740 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 int err;
2743 do {
David Quigley1775fd32013-05-22 12:50:42 -04002744 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002745 switch (err) {
2746 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002747 if (!(sattr->ia_valid & ATTR_SIZE)) {
2748 pr_warn_once("NFSv4: server %s is incorrectly "
2749 "applying open mode checks to "
2750 "a SETATTR that is not "
2751 "changing file size.\n",
2752 server->nfs_client->cl_hostname);
2753 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002754 if (state && !(state->state & FMODE_WRITE)) {
2755 err = -EBADF;
2756 if (sattr->ia_valid & ATTR_OPEN)
2757 err = -EACCES;
2758 goto out;
2759 }
2760 }
2761 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002763out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return err;
2765}
2766
Peng Tao500d7012015-09-22 11:35:22 +08002767static bool
2768nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2769{
2770 if (inode == NULL || !nfs_have_layout(inode))
2771 return false;
2772
2773 return pnfs_wait_on_layoutreturn(inode, task);
2774}
2775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776struct nfs4_closedata {
2777 struct inode *inode;
2778 struct nfs4_state *state;
2779 struct nfs_closeargs arg;
2780 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002781 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002782 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002783 bool roc;
2784 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785};
2786
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002787static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002788{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002789 struct nfs4_closedata *calldata = data;
2790 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002791 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002792
Fred Isamanf7e89172011-01-06 11:36:32 +00002793 if (calldata->roc)
2794 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002795 nfs4_put_open_state(calldata->state);
2796 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002797 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002798 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002799 kfree(calldata);
2800}
2801
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002802static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002804 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002807 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Chuck Levera3ca5652012-03-01 17:00:40 -05002809 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002810 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2811 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002812 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* hmm. we are done with the inode, and in the process of freeing
2814 * the state_owner. we keep this around to process errors
2815 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 switch (task->tk_status) {
2817 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002818 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002819 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002820 pnfs_roc_set_barrier(state->inode,
2821 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002822 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002823 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002824 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002826 case -NFS4ERR_OLD_STATEID:
2827 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002829 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002830 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002831 rpc_restart_call_prepare(task);
2832 goto out_release;
2833 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002834 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002835 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002837 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002838 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002839 goto out_release;
2840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002842 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2843 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002844out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002845 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002846 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002847 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002850static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002852 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002853 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002854 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002855 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002856 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002857
Chuck Levera3ca5652012-03-01 17:00:40 -05002858 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002859 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002860 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002861
Trond Myklebust88069f72009-12-08 08:33:16 -05002862 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002863 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002864 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2865 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2866 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002867 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002868 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002869 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002870 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002871 if (state->n_rdonly == 0)
2872 call_close |= is_rdonly;
2873 else if (is_rdonly)
2874 calldata->arg.fmode |= FMODE_READ;
2875 if (state->n_wronly == 0)
2876 call_close |= is_wronly;
2877 else if (is_wronly)
2878 calldata->arg.fmode |= FMODE_WRITE;
2879 } else if (is_rdwr)
2880 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2881
2882 if (calldata->arg.fmode == 0)
2883 call_close |= is_rdwr;
2884
Trond Myklebust5d422302013-03-14 16:57:48 -04002885 if (!nfs4_valid_open_stateid(state))
2886 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002887 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002888
2889 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002890 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002891 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002892 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002893
Peng Tao500d7012015-09-22 11:35:22 +08002894 if (nfs4_wait_on_layoutreturn(inode, task)) {
2895 nfs_release_seqid(calldata->arg.seqid);
2896 goto out_wait;
2897 }
2898
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002899 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002900 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002901 if (calldata->roc)
2902 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2903
Trond Myklebust6ae37332015-01-30 14:21:14 -05002904 calldata->arg.share_access =
2905 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2906 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002907
Trond Myklebust516a6af2005-10-27 22:12:41 -04002908 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002909 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002910 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002911 &calldata->arg.seq_args,
2912 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002913 task) != 0)
2914 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002915 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002916 return;
2917out_no_action:
2918 task->tk_action = NULL;
2919out_wait:
2920 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921}
2922
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002923static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002924 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002925 .rpc_call_done = nfs4_close_done,
2926 .rpc_release = nfs4_free_closedata,
2927};
2928
Peng Taofe08c542014-07-03 13:05:01 +08002929static bool nfs4_roc(struct inode *inode)
2930{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002931 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002932 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002933 return pnfs_roc(inode);
2934}
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936/*
2937 * It is possible for data to be read/written from a mem-mapped file
2938 * after the sys_close call (which hits the vfs layer as a flush).
2939 * This means that we can't safely call nfsv4 close on a file until
2940 * the inode is cleared. This in turn means that we are not good
2941 * NFSv4 citizens - we do not indicate to the server to update the file's
2942 * share state even when we are done with one of the three share
2943 * stateid's in the inode.
2944 *
2945 * NOTE: Caller must be holding the sp->so_owner semaphore!
2946 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002947int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002949 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002950 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002952 struct nfs4_state_owner *sp = state->owner;
2953 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002954 struct rpc_message msg = {
2955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2956 .rpc_cred = state->owner->so_cred,
2957 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002958 struct rpc_task_setup task_setup_data = {
2959 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002960 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002961 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002962 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002963 .flags = RPC_TASK_ASYNC,
2964 };
Trond Myklebust95121352005-10-18 14:20:12 -07002965 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002967 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2968 &task_setup_data.rpc_client, &msg);
2969
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002970 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002972 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002973 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002974 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002976 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002978 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2979 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002980 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002981 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002982 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002983 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002984 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002985 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002986 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002987 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002988 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002989
Trond Myklebust1174dd12010-12-21 10:52:24 -05002990 msg.rpc_argp = &calldata->arg;
2991 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002992 task_setup_data.callback_data = calldata;
2993 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002994 if (IS_ERR(task))
2995 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002996 status = 0;
2997 if (wait)
2998 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002999 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003000 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003001out_free_calldata:
3002 kfree(calldata);
3003out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003004 nfs4_put_open_state(state);
3005 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
Trond Myklebust2b484292010-09-17 10:56:51 -04003009static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003010nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3011 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003014 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3015
3016 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Trond Myklebust565277f2007-10-15 18:17:53 -04003018 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003019 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003020
3021 nfs4_label_release_security(label);
3022
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003023 if (IS_ERR(state))
3024 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003025 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Trond Myklebust1185a552009-12-03 15:54:02 -05003028static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003029{
3030 if (ctx->state == NULL)
3031 return;
3032 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003033 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003034 else
Al Viro643168c2011-06-22 18:20:23 -04003035 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Trond Myklebustb944dba2013-11-04 15:20:20 -05003038#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3039#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003040#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3043{
Kinglong Mee8c612822015-08-26 21:12:58 +08003044 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003045 struct nfs4_server_caps_arg args = {
3046 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003047 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003048 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 struct nfs4_server_caps_res res = {};
3050 struct rpc_message msg = {
3051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003052 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 .rpc_resp = &res,
3054 };
3055 int status;
3056
Kinglong Mee8c612822015-08-26 21:12:58 +08003057 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3058 FATTR4_WORD0_FH_EXPIRE_TYPE |
3059 FATTR4_WORD0_LINK_SUPPORT |
3060 FATTR4_WORD0_SYMLINK_SUPPORT |
3061 FATTR4_WORD0_ACLSUPPORT;
3062 if (minorversion)
3063 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3064
Bryan Schumaker7c513052011-03-24 17:12:24 +00003065 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003067 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003068 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003069 case 0:
3070 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3071 res.attr_bitmask[2] = 0;
3072 break;
3073 case 1:
3074 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3075 break;
3076 case 2:
3077 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003080 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3081 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3082 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3083 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003084 NFS_CAP_CTIME|NFS_CAP_MTIME|
3085 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003086 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3087 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 server->caps |= NFS_CAP_ACLS;
3089 if (res.has_links != 0)
3090 server->caps |= NFS_CAP_HARDLINKS;
3091 if (res.has_symlinks != 0)
3092 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003093 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3094 server->caps |= NFS_CAP_FILEID;
3095 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3096 server->caps |= NFS_CAP_MODE;
3097 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3098 server->caps |= NFS_CAP_NLINK;
3099 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3100 server->caps |= NFS_CAP_OWNER;
3101 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3102 server->caps |= NFS_CAP_OWNER_GROUP;
3103 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3104 server->caps |= NFS_CAP_ATIME;
3105 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3106 server->caps |= NFS_CAP_CTIME;
3107 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3108 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003109#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3110 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3111 server->caps |= NFS_CAP_SECURITY_LABEL;
3112#endif
3113 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3114 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003115 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003116
Trond Myklebusta65318b2009-03-11 14:10:28 -04003117 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3118 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3119 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003120 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003121 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3122 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003124 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return status;
3128}
3129
Trond Myklebust55a97592006-06-09 09:34:19 -04003130int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
3132 struct nfs4_exception exception = { };
3133 int err;
3134 do {
3135 err = nfs4_handle_exception(server,
3136 _nfs4_server_capabilities(server, fhandle),
3137 &exception);
3138 } while (exception.retry);
3139 return err;
3140}
3141
3142static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3143 struct nfs_fsinfo *info)
3144{
David Quigleyaa9c2662013-05-22 12:50:44 -04003145 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003147 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 };
3149 struct nfs4_lookup_res res = {
3150 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003151 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 .fh = fhandle,
3153 };
3154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3156 .rpc_argp = &args,
3157 .rpc_resp = &res,
3158 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003159
David Quigleyaa9c2662013-05-22 12:50:44 -04003160 bitmask[0] = nfs4_fattr_bitmap[0];
3161 bitmask[1] = nfs4_fattr_bitmap[1];
3162 /*
3163 * Process the label in the upcoming getfattr
3164 */
3165 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3166
Trond Myklebust0e574af2005-10-27 22:12:38 -04003167 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003168 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_fsinfo *info)
3173{
3174 struct nfs4_exception exception = { };
3175 int err;
3176 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003177 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003178 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003179 switch (err) {
3180 case 0:
3181 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003182 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003183 default:
3184 err = nfs4_handle_exception(server, err, &exception);
3185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003187out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return err;
3189}
3190
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003191static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3192 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3193{
Trond Myklebustc2190662013-08-26 19:23:04 -04003194 struct rpc_auth_create_args auth_args = {
3195 .pseudoflavor = flavor,
3196 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003197 struct rpc_auth *auth;
3198 int ret;
3199
Trond Myklebustc2190662013-08-26 19:23:04 -04003200 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003201 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003202 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003203 goto out;
3204 }
3205 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003206out:
3207 return ret;
3208}
3209
Chuck Lever9a744ba2013-03-16 15:56:02 -04003210/*
3211 * Retry pseudoroot lookup with various security flavors. We do this when:
3212 *
3213 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3214 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3215 *
3216 * Returns zero on success, or a negative NFS4ERR value, or a
3217 * negative errno value.
3218 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003219static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003220 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003222 /* Per 3530bis 15.33.5 */
3223 static const rpc_authflavor_t flav_array[] = {
3224 RPC_AUTH_GSS_KRB5P,
3225 RPC_AUTH_GSS_KRB5I,
3226 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003227 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003228 RPC_AUTH_NULL,
3229 };
3230 int status = -EPERM;
3231 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003233 if (server->auth_info.flavor_len > 0) {
3234 /* try each flavor specified by user */
3235 for (i = 0; i < server->auth_info.flavor_len; i++) {
3236 status = nfs4_lookup_root_sec(server, fhandle, info,
3237 server->auth_info.flavors[i]);
3238 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3239 continue;
3240 break;
3241 }
3242 } else {
3243 /* no flavors specified by user, try default list */
3244 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3245 status = nfs4_lookup_root_sec(server, fhandle, info,
3246 flav_array[i]);
3247 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3248 continue;
3249 break;
3250 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003251 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003252
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003253 /*
3254 * -EACCESS could mean that the user doesn't have correct permissions
3255 * to access the mount. It could also mean that we tried to mount
3256 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3257 * existing mount programs don't handle -EACCES very well so it should
3258 * be mapped to -EPERM instead.
3259 */
3260 if (status == -EACCES)
3261 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003262 return status;
3263}
3264
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003265static int nfs4_do_find_root_sec(struct nfs_server *server,
3266 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3267{
3268 int mv = server->nfs_client->cl_minorversion;
3269 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3270}
3271
3272/**
3273 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3274 * @server: initialized nfs_server handle
3275 * @fhandle: we fill in the pseudo-fs root file handle
3276 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003277 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003278 *
3279 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003280 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003281int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003282 struct nfs_fsinfo *info,
3283 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003284{
Andre Przywarac7757072015-04-23 17:17:40 +01003285 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003286
Andre Przywarac7757072015-04-23 17:17:40 +01003287 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003288 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003289
3290 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003291 status = nfs4_do_find_root_sec(server, fhandle, info);
3292
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (status == 0)
3294 status = nfs4_server_capabilities(server, fhandle);
3295 if (status == 0)
3296 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003297
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003298 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
Bryan Schumakerbae36242012-05-10 15:07:31 -04003301static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3302 struct nfs_fsinfo *info)
3303{
3304 int error;
3305 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003306 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003307
3308 error = nfs4_server_capabilities(server, mntfh);
3309 if (error < 0) {
3310 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3311 return error;
3312 }
3313
David Quigley14c43f72013-05-22 12:50:43 -04003314 label = nfs4_label_alloc(server, GFP_KERNEL);
3315 if (IS_ERR(label))
3316 return PTR_ERR(label);
3317
David Quigley1775fd32013-05-22 12:50:42 -04003318 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003319 if (error < 0) {
3320 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003321 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003322 }
3323
3324 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3325 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3326 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3327
David Quigley14c43f72013-05-22 12:50:43 -04003328err_free_label:
3329 nfs4_label_free(label);
3330
Bryan Schumakerbae36242012-05-10 15:07:31 -04003331 return error;
3332}
3333
Manoj Naik6b97fd32006-06-09 09:34:29 -04003334/*
3335 * Get locations and (maybe) other attributes of a referral.
3336 * Note that we'll actually follow the referral later when
3337 * we detect fsid mismatch in inode revalidation
3338 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003339static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3340 const struct qstr *name, struct nfs_fattr *fattr,
3341 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003342{
3343 int status = -ENOMEM;
3344 struct page *page = NULL;
3345 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003346
3347 page = alloc_page(GFP_KERNEL);
3348 if (page == NULL)
3349 goto out;
3350 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3351 if (locations == NULL)
3352 goto out;
3353
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003354 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003355 if (status != 0)
3356 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003357
3358 /*
3359 * If the fsid didn't change, this is a migration event, not a
3360 * referral. Cause us to drop into the exception handler, which
3361 * will kick off migration recovery.
3362 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003363 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003364 dprintk("%s: server did not return a different fsid for"
3365 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003366 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003367 goto out;
3368 }
Andy Adamson533eb462011-06-13 18:25:56 -04003369 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3370 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003371
Andy Adamson533eb462011-06-13 18:25:56 -04003372 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003373 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003374 memset(fhandle, 0, sizeof(struct nfs_fh));
3375out:
3376 if (page)
3377 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003378 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003379 return status;
3380}
3381
David Quigley1775fd32013-05-22 12:50:42 -04003382static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3383 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 struct nfs4_getattr_arg args = {
3386 .fh = fhandle,
3387 .bitmask = server->attr_bitmask,
3388 };
3389 struct nfs4_getattr_res res = {
3390 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003391 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 .server = server,
3393 };
3394 struct rpc_message msg = {
3395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3396 .rpc_argp = &args,
3397 .rpc_resp = &res,
3398 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003399
3400 args.bitmask = nfs4_bitmask(server, label);
3401
Trond Myklebust0e574af2005-10-27 22:12:38 -04003402 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003403 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404}
3405
David Quigley1775fd32013-05-22 12:50:42 -04003406static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
3409 struct nfs4_exception exception = { };
3410 int err;
3411 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003412 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3413 trace_nfs4_getattr(server, fhandle, fattr, err);
3414 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 &exception);
3416 } while (exception.retry);
3417 return err;
3418}
3419
3420/*
3421 * The file is not closed if it is opened due to the a request to change
3422 * the size of the file. The open call will not be needed once the
3423 * VFS layer lookup-intents are implemented.
3424 *
3425 * Close is called when the inode is destroyed.
3426 * If we haven't opened the file for O_WRONLY, we
3427 * need to in the size_change case to obtain a stateid.
3428 *
3429 * Got race?
3430 * Because OPEN is always done by name in nfsv4, it is
3431 * possible that we opened a different file by the same
3432 * name. We can recognize this race condition, but we
3433 * can't do anything about it besides returning an error.
3434 *
3435 * This will be fixed with VFS changes (lookup-intent).
3436 */
3437static int
3438nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3439 struct iattr *sattr)
3440{
David Howells2b0143b2015-03-17 22:25:59 +00003441 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003442 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003443 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003444 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 int status;
3446
Peng Tao88ac8152014-09-12 11:04:10 +08003447 if (pnfs_ld_layoutret_on_setattr(inode) &&
3448 sattr->ia_valid & ATTR_SIZE &&
3449 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003450 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003451
Trond Myklebust0e574af2005-10-27 22:12:38 -04003452 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Andy Adamson26699402012-05-30 16:12:24 -04003454 /* Deal with open(O_TRUNC) */
3455 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003456 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003457
3458 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003459 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003460 return 0;
3461
Trond Myklebustd5308382005-11-04 15:33:38 -05003462 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003463 if (sattr->ia_valid & ATTR_FILE) {
3464 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003465
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003466 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003467 if (ctx) {
3468 cred = ctx->cred;
3469 state = ctx->state;
3470 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003471 }
3472
David Quigley14c43f72013-05-22 12:50:43 -04003473 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3474 if (IS_ERR(label))
3475 return PTR_ERR(label);
3476
3477 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003478 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003479 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003480 nfs_setsecurity(inode, fattr, label);
3481 }
David Quigley14c43f72013-05-22 12:50:43 -04003482 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 return status;
3484}
3485
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003486static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3487 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003488 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003489{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003490 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003491 int status;
3492 struct nfs4_lookup_arg args = {
3493 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003494 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003495 .name = name,
3496 };
3497 struct nfs4_lookup_res res = {
3498 .server = server,
3499 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003500 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003501 .fh = fhandle,
3502 };
3503 struct rpc_message msg = {
3504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3505 .rpc_argp = &args,
3506 .rpc_resp = &res,
3507 };
3508
David Quigleyaa9c2662013-05-22 12:50:44 -04003509 args.bitmask = nfs4_bitmask(server, label);
3510
David Howells2b3de442006-08-22 20:06:09 -04003511 nfs_fattr_init(fattr);
3512
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003514 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 dprintk("NFS reply lookup: %d\n", status);
3516 return status;
3517}
3518
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003519static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003520{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003521 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003522 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003523 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3524 fattr->nlink = 2;
3525}
3526
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003527static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3528 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003529 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003530{
3531 struct nfs4_exception exception = { };
3532 struct rpc_clnt *client = *clnt;
3533 int err;
3534 do {
David Quigley1775fd32013-05-22 12:50:42 -04003535 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003536 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003537 switch (err) {
3538 case -NFS4ERR_BADNAME:
3539 err = -ENOENT;
3540 goto out;
3541 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003542 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003543 if (err == -NFS4ERR_MOVED)
3544 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003545 goto out;
3546 case -NFS4ERR_WRONGSEC:
3547 err = -EPERM;
3548 if (client != *clnt)
3549 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003550 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003551 if (IS_ERR(client))
3552 return PTR_ERR(client);
3553
3554 exception.retry = 1;
3555 break;
3556 default:
3557 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3558 }
3559 } while (exception.retry);
3560
3561out:
3562 if (err == 0)
3563 *clnt = client;
3564 else if (client != *clnt)
3565 rpc_shutdown_client(client);
3566
3567 return err;
3568}
3569
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003570static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003571 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3572 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003574 int status;
3575 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003576
David Quigley1775fd32013-05-22 12:50:42 -04003577 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003578 if (client != NFS_CLIENT(dir)) {
3579 rpc_shutdown_client(client);
3580 nfs_fixup_secinfo_attributes(fattr);
3581 }
3582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003585struct rpc_clnt *
3586nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3587 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3588{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003589 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003590 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003591
David Quigley1775fd32013-05-22 12:50:42 -04003592 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003593 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003594 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003595 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003596}
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3599{
Trond Myklebust76b32992007-08-10 17:45:11 -04003600 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 struct nfs4_accessargs args = {
3602 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003603 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003605 struct nfs4_accessres res = {
3606 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003607 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3610 .rpc_argp = &args,
3611 .rpc_resp = &res,
3612 .rpc_cred = entry->cred,
3613 };
3614 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003615 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617 /*
3618 * Determine which access bits we want to ask for...
3619 */
3620 if (mode & MAY_READ)
3621 args.access |= NFS4_ACCESS_READ;
3622 if (S_ISDIR(inode->i_mode)) {
3623 if (mode & MAY_WRITE)
3624 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3625 if (mode & MAY_EXEC)
3626 args.access |= NFS4_ACCESS_LOOKUP;
3627 } else {
3628 if (mode & MAY_WRITE)
3629 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3630 if (mode & MAY_EXEC)
3631 args.access |= NFS4_ACCESS_EXECUTE;
3632 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003633
3634 res.fattr = nfs_alloc_fattr();
3635 if (res.fattr == NULL)
3636 return -ENOMEM;
3637
Bryan Schumaker7c513052011-03-24 17:12:24 +00003638 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003640 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003641 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003643 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return status;
3645}
3646
3647static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3648{
3649 struct nfs4_exception exception = { };
3650 int err;
3651 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003652 err = _nfs4_proc_access(inode, entry);
3653 trace_nfs4_access(inode, err);
3654 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 &exception);
3656 } while (exception.retry);
3657 return err;
3658}
3659
3660/*
3661 * TODO: For the time being, we don't try to get any attributes
3662 * along with any of the zero-copy operations READ, READDIR,
3663 * READLINK, WRITE.
3664 *
3665 * In the case of the first three, we want to put the GETATTR
3666 * after the read-type operation -- this is because it is hard
3667 * to predict the length of a GETATTR response in v4, and thus
3668 * align the READ data correctly. This means that the GETATTR
3669 * may end up partially falling into the page cache, and we should
3670 * shift it into the 'tail' of the xdr_buf before processing.
3671 * To do this efficiently, we need to know the total length
3672 * of data received, which doesn't seem to be available outside
3673 * of the RPC layer.
3674 *
3675 * In the case of WRITE, we also want to put the GETATTR after
3676 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003677 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 *
3679 * Both of these changes to the XDR layer would in fact be quite
3680 * minor, but I decided to leave them for a subsequent patch.
3681 */
3682static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3683 unsigned int pgbase, unsigned int pglen)
3684{
3685 struct nfs4_readlink args = {
3686 .fh = NFS_FH(inode),
3687 .pgbase = pgbase,
3688 .pglen = pglen,
3689 .pages = &page,
3690 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003691 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 struct rpc_message msg = {
3693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3694 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003695 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 };
3697
Bryan Schumaker7c513052011-03-24 17:12:24 +00003698 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 -07003699}
3700
3701static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3702 unsigned int pgbase, unsigned int pglen)
3703{
3704 struct nfs4_exception exception = { };
3705 int err;
3706 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003707 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3708 trace_nfs4_readlink(inode, err);
3709 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 &exception);
3711 } while (exception.retry);
3712 return err;
3713}
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003716 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718static int
3719nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003720 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
David Quigleyaa9c2662013-05-22 12:50:44 -04003722 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003723 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 int status = 0;
3726
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003727 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3728 if (IS_ERR(ctx))
3729 return PTR_ERR(ctx);
3730
David Quigleyaa9c2662013-05-22 12:50:44 -04003731 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3732
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003733 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003734 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (IS_ERR(state)) {
3736 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003740 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003741 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return status;
3743}
3744
3745static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3746{
Trond Myklebust16e42952005-10-27 22:12:44 -04003747 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003748 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003750 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003752 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003753 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3757 .rpc_argp = &args,
3758 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003760 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003761
Bryan Schumaker7c513052011-03-24 17:12:24 +00003762 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003763 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003764 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 return status;
3766}
3767
3768static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3769{
3770 struct nfs4_exception exception = { };
3771 int err;
3772 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003773 err = _nfs4_proc_remove(dir, name);
3774 trace_nfs4_remove(dir, name, err);
3775 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 &exception);
3777 } while (exception.retry);
3778 return err;
3779}
3780
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003781static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003783 struct nfs_server *server = NFS_SERVER(dir);
3784 struct nfs_removeargs *args = msg->rpc_argp;
3785 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003787 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003789 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003790
3791 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003794static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3795{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003796 nfs4_setup_sequence(NFS_SERVER(data->dir),
3797 &data->args.seq_args,
3798 &data->res.seq_res,
3799 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800}
3801
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003802static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003804 struct nfs_unlinkdata *data = task->tk_calldata;
3805 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003806
Trond Myklebust14516c32010-07-31 14:29:06 -04003807 if (!nfs4_sequence_done(task, &res->seq_res))
3808 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003809 if (nfs4_async_handle_error(task, res->server, NULL,
3810 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003811 return 0;
3812 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003813 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814}
3815
Jeff Laytond3d41522010-09-17 17:31:57 -04003816static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3817{
3818 struct nfs_server *server = NFS_SERVER(dir);
3819 struct nfs_renameargs *arg = msg->rpc_argp;
3820 struct nfs_renameres *res = msg->rpc_resp;
3821
3822 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003823 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003824 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003825}
3826
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003827static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3828{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003829 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3830 &data->args.seq_args,
3831 &data->res.seq_res,
3832 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003833}
3834
3835static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3836 struct inode *new_dir)
3837{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003838 struct nfs_renamedata *data = task->tk_calldata;
3839 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003840
3841 if (!nfs4_sequence_done(task, &res->seq_res))
3842 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003843 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003844 return 0;
3845
3846 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003847 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003848 return 1;
3849}
3850
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3852{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003853 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 struct nfs4_link_arg arg = {
3855 .fh = NFS_FH(inode),
3856 .dir_fh = NFS_FH(dir),
3857 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003858 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003860 struct nfs4_link_res res = {
3861 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003862 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003863 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 struct rpc_message msg = {
3865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3866 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003867 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003869 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Trond Myklebust136f2622010-04-16 16:22:49 -04003871 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003872 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003873 goto out;
3874
David Quigley14c43f72013-05-22 12:50:43 -04003875 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3876 if (IS_ERR(res.label)) {
3877 status = PTR_ERR(res.label);
3878 goto out;
3879 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003880 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003881
Bryan Schumaker7c513052011-03-24 17:12:24 +00003882 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003883 if (!status) {
3884 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003885 status = nfs_post_op_update_inode(inode, res.fattr);
3886 if (!status)
3887 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003888 }
David Quigley14c43f72013-05-22 12:50:43 -04003889
3890
3891 nfs4_label_free(res.label);
3892
Trond Myklebust136f2622010-04-16 16:22:49 -04003893out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003894 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 return status;
3896}
3897
3898static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3899{
3900 struct nfs4_exception exception = { };
3901 int err;
3902 do {
3903 err = nfs4_handle_exception(NFS_SERVER(inode),
3904 _nfs4_proc_link(inode, dir, name),
3905 &exception);
3906 } while (exception.retry);
3907 return err;
3908}
3909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003910struct nfs4_createdata {
3911 struct rpc_message msg;
3912 struct nfs4_create_arg arg;
3913 struct nfs4_create_res res;
3914 struct nfs_fh fh;
3915 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003916 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003917};
3918
3919static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3920 struct qstr *name, struct iattr *sattr, u32 ftype)
3921{
3922 struct nfs4_createdata *data;
3923
3924 data = kzalloc(sizeof(*data), GFP_KERNEL);
3925 if (data != NULL) {
3926 struct nfs_server *server = NFS_SERVER(dir);
3927
David Quigley14c43f72013-05-22 12:50:43 -04003928 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3929 if (IS_ERR(data->label))
3930 goto out_free;
3931
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003932 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3933 data->msg.rpc_argp = &data->arg;
3934 data->msg.rpc_resp = &data->res;
3935 data->arg.dir_fh = NFS_FH(dir);
3936 data->arg.server = server;
3937 data->arg.name = name;
3938 data->arg.attrs = sattr;
3939 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003940 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003941 data->res.server = server;
3942 data->res.fh = &data->fh;
3943 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003944 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003945 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003946 }
3947 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003948out_free:
3949 kfree(data);
3950 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003951}
3952
3953static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3954{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003955 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003956 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003957 if (status == 0) {
3958 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003959 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003960 }
3961 return status;
3962}
3963
3964static void nfs4_free_createdata(struct nfs4_createdata *data)
3965{
David Quigley14c43f72013-05-22 12:50:43 -04003966 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003967 kfree(data);
3968}
3969
Chuck Lever4f390c12006-08-22 20:06:22 -04003970static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003971 struct page *page, unsigned int len, struct iattr *sattr,
3972 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003974 struct nfs4_createdata *data;
3975 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
Chuck Lever94a6d752006-08-22 20:06:23 -04003977 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003978 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003979
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003980 status = -ENOMEM;
3981 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3982 if (data == NULL)
3983 goto out;
3984
3985 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3986 data->arg.u.symlink.pages = &page;
3987 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003988 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003990 status = nfs4_do_create(dir, dentry, data);
3991
3992 nfs4_free_createdata(data);
3993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return status;
3995}
3996
Chuck Lever4f390c12006-08-22 20:06:22 -04003997static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003998 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999{
4000 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004001 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004003
4004 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004007 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4008 trace_nfs4_symlink(dir, &dentry->d_name, err);
4009 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 &exception);
4011 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004012
4013 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 return err;
4015}
4016
4017static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004018 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004020 struct nfs4_createdata *data;
4021 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004023 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4024 if (data == NULL)
4025 goto out;
4026
David Quigley1775fd32013-05-22 12:50:42 -04004027 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004028 status = nfs4_do_create(dir, dentry, data);
4029
4030 nfs4_free_createdata(data);
4031out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 return status;
4033}
4034
4035static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4036 struct iattr *sattr)
4037{
4038 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004039 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004041
David Quigleyaa9c2662013-05-22 12:50:44 -04004042 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4043
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004044 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004046 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4047 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4048 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 &exception);
4050 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004051 nfs4_label_release_security(label);
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return err;
4054}
4055
4056static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004057 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058{
David Howells2b0143b2015-03-17 22:25:59 +00004059 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 struct nfs4_readdir_arg args = {
4061 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004062 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 .pgbase = 0,
4064 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004065 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004066 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 };
4068 struct nfs4_readdir_res res;
4069 struct rpc_message msg = {
4070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4071 .rpc_argp = &args,
4072 .rpc_resp = &res,
4073 .rpc_cred = cred,
4074 };
4075 int status;
4076
Al Viro6de14722013-09-16 10:53:17 -04004077 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4078 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004079 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004080 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004082 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 -05004083 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004084 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004085 status += args.pgbase;
4086 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004087
4088 nfs_invalidate_atime(dir);
4089
Harvey Harrison3110ff82008-05-02 13:42:44 -07004090 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 return status;
4092}
4093
4094static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004095 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096{
4097 struct nfs4_exception exception = { };
4098 int err;
4099 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004100 err = _nfs4_proc_readdir(dentry, cred, cookie,
4101 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004102 trace_nfs4_readdir(d_inode(dentry), err);
4103 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 &exception);
4105 } while (exception.retry);
4106 return err;
4107}
4108
4109static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004110 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004112 struct nfs4_createdata *data;
4113 int mode = sattr->ia_mode;
4114 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004116 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4117 if (data == NULL)
4118 goto out;
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004121 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004123 data->arg.ftype = NF4BLK;
4124 data->arg.u.device.specdata1 = MAJOR(rdev);
4125 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
4127 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004128 data->arg.ftype = NF4CHR;
4129 data->arg.u.device.specdata1 = MAJOR(rdev);
4130 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004131 } else if (!S_ISSOCK(mode)) {
4132 status = -EINVAL;
4133 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
David Quigley1775fd32013-05-22 12:50:42 -04004135
David Quigleyaa9c2662013-05-22 12:50:44 -04004136 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004137 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004138out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004139 nfs4_free_createdata(data);
4140out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return status;
4142}
4143
4144static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4145 struct iattr *sattr, dev_t rdev)
4146{
4147 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004148 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004150
David Quigleyaa9c2662013-05-22 12:50:44 -04004151 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4152
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004153 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004155 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4156 trace_nfs4_mknod(dir, &dentry->d_name, err);
4157 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 &exception);
4159 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004160
4161 nfs4_label_release_security(label);
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return err;
4164}
4165
4166static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4167 struct nfs_fsstat *fsstat)
4168{
4169 struct nfs4_statfs_arg args = {
4170 .fh = fhandle,
4171 .bitmask = server->attr_bitmask,
4172 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004173 struct nfs4_statfs_res res = {
4174 .fsstat = fsstat,
4175 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 struct rpc_message msg = {
4177 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4178 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004179 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 };
4181
Trond Myklebust0e574af2005-10-27 22:12:38 -04004182 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004183 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184}
4185
4186static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4187{
4188 struct nfs4_exception exception = { };
4189 int err;
4190 do {
4191 err = nfs4_handle_exception(server,
4192 _nfs4_proc_statfs(server, fhandle, fsstat),
4193 &exception);
4194 } while (exception.retry);
4195 return err;
4196}
4197
4198static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4199 struct nfs_fsinfo *fsinfo)
4200{
4201 struct nfs4_fsinfo_arg args = {
4202 .fh = fhandle,
4203 .bitmask = server->attr_bitmask,
4204 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004205 struct nfs4_fsinfo_res res = {
4206 .fsinfo = fsinfo,
4207 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 struct rpc_message msg = {
4209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4210 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004211 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 };
4213
Bryan Schumaker7c513052011-03-24 17:12:24 +00004214 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
4217static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4218{
4219 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004220 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 int err;
4222
4223 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004224 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004225 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004226 if (err == 0) {
4227 struct nfs_client *clp = server->nfs_client;
4228
4229 spin_lock(&clp->cl_lock);
4230 clp->cl_lease_time = fsinfo->lease_time * HZ;
4231 clp->cl_last_renewal = now;
4232 spin_unlock(&clp->cl_lock);
4233 break;
4234 }
4235 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 } while (exception.retry);
4237 return err;
4238}
4239
4240static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4241{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004242 int error;
4243
Trond Myklebust0e574af2005-10-27 22:12:38 -04004244 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004245 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004246 if (error == 0) {
4247 /* block layout checks this! */
4248 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004249 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004250 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004251
4252 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253}
4254
4255static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4256 struct nfs_pathconf *pathconf)
4257{
4258 struct nfs4_pathconf_arg args = {
4259 .fh = fhandle,
4260 .bitmask = server->attr_bitmask,
4261 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004262 struct nfs4_pathconf_res res = {
4263 .pathconf = pathconf,
4264 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 struct rpc_message msg = {
4266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4267 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004268 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 };
4270
4271 /* None of the pathconf attributes are mandatory to implement */
4272 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4273 memset(pathconf, 0, sizeof(*pathconf));
4274 return 0;
4275 }
4276
Trond Myklebust0e574af2005-10-27 22:12:38 -04004277 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004278 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
4281static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4282 struct nfs_pathconf *pathconf)
4283{
4284 struct nfs4_exception exception = { };
4285 int err;
4286
4287 do {
4288 err = nfs4_handle_exception(server,
4289 _nfs4_proc_pathconf(server, fhandle, pathconf),
4290 &exception);
4291 } while (exception.retry);
4292 return err;
4293}
4294
Trond Myklebust5521abf2013-03-16 20:54:34 -04004295int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004296 const struct nfs_open_context *ctx,
4297 const struct nfs_lock_context *l_ctx,
4298 fmode_t fmode)
4299{
4300 const struct nfs_lockowner *lockowner = NULL;
4301
4302 if (l_ctx != NULL)
4303 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004304 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04004305}
4306EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4307
Trond Myklebust5521abf2013-03-16 20:54:34 -04004308static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4309 const struct nfs_open_context *ctx,
4310 const struct nfs_lock_context *l_ctx,
4311 fmode_t fmode)
4312{
4313 nfs4_stateid current_stateid;
4314
Trond Myklebuste1253be2014-03-05 08:44:23 -05004315 /* If the current stateid represents a lost lock, then exit */
4316 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4317 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004318 return nfs4_stateid_match(stateid, &current_stateid);
4319}
4320
4321static bool nfs4_error_stateid_expired(int err)
4322{
4323 switch (err) {
4324 case -NFS4ERR_DELEG_REVOKED:
4325 case -NFS4ERR_ADMIN_REVOKED:
4326 case -NFS4ERR_BAD_STATEID:
4327 case -NFS4ERR_STALE_STATEID:
4328 case -NFS4ERR_OLD_STATEID:
4329 case -NFS4ERR_OPENMODE:
4330 case -NFS4ERR_EXPIRED:
4331 return true;
4332 }
4333 return false;
4334}
4335
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004336void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004337{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004338 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004339}
4340
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004341static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004343 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004345 trace_nfs4_read(hdr, task->tk_status);
4346 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004347 hdr->args.context->state,
4348 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004349 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004350 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004352
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004353 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004355 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Trond Myklebust5521abf2013-03-16 20:54:34 -04004359static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004360 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004361{
4362
4363 if (!nfs4_error_stateid_expired(task->tk_status) ||
4364 nfs4_stateid_is_current(&args->stateid,
4365 args->context,
4366 args->lock_context,
4367 FMODE_READ))
4368 return false;
4369 rpc_restart_call_prepare(task);
4370 return true;
4371}
4372
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004373static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004374{
4375
4376 dprintk("--> %s\n", __func__);
4377
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004378 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004379 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004380 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004381 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004382 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4383 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004384}
4385
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004386static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4387 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004389 hdr->timestamp = jiffies;
4390 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004391 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004392 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393}
4394
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004395static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4396 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004397{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004398 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4399 &hdr->args.seq_args,
4400 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004401 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004402 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004403 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4404 hdr->args.lock_context,
4405 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004406 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004407 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004408 return -EIO;
4409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004412static int nfs4_write_done_cb(struct rpc_task *task,
4413 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004415 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004416
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004417 trace_nfs4_write(hdr, task->tk_status);
4418 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004419 hdr->args.context->state,
4420 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004421 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004422 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004424 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004425 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004426 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004427 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
Trond Myklebust5521abf2013-03-16 20:54:34 -04004431static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004432 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004433{
4434
4435 if (!nfs4_error_stateid_expired(task->tk_status) ||
4436 nfs4_stateid_is_current(&args->stateid,
4437 args->context,
4438 args->lock_context,
4439 FMODE_WRITE))
4440 return false;
4441 rpc_restart_call_prepare(task);
4442 return true;
4443}
4444
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004445static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004446{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004447 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004448 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004449 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004450 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004451 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4452 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004453}
4454
Trond Myklebust5a37f852012-04-28 14:55:16 -04004455static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004456bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004457{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004458 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004459 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004460 return false;
4461 /* Otherwise, request attributes if and only if we don't hold
4462 * a delegation
4463 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004464 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004465}
Fred Isamana69aef12011-03-03 15:13:47 +00004466
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004467static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4468 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004470 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004471
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004472 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4473 hdr->args.bitmask = NULL;
4474 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004475 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004476 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004477
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004478 if (!hdr->pgio_done_cb)
4479 hdr->pgio_done_cb = nfs4_write_done_cb;
4480 hdr->res.server = server;
4481 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004483 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
Fred Isaman0b7c0152012-04-20 14:47:39 -04004487static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4488{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004489 nfs4_setup_sequence(NFS_SERVER(data->inode),
4490 &data->args.seq_args,
4491 &data->res.seq_res,
4492 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493}
4494
Fred Isaman0b7c0152012-04-20 14:47:39 -04004495static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004498
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004499 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004500 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4501 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004502 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004503 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
Fred Isaman0b7c0152012-04-20 14:47:39 -04004508static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004509{
4510 if (!nfs4_sequence_done(task, &data->res.seq_res))
4511 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004512 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004513}
4514
Fred Isaman0b7c0152012-04-20 14:47:39 -04004515static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004517 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004518
Fred Isaman0b7c0152012-04-20 14:47:39 -04004519 if (data->commit_done_cb == NULL)
4520 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004521 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004522 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004523 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004526struct nfs4_renewdata {
4527 struct nfs_client *client;
4528 unsigned long timestamp;
4529};
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531/*
4532 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4533 * standalone procedure for queueing an asynchronous RENEW.
4534 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004535static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004536{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004537 struct nfs4_renewdata *data = calldata;
4538 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004539
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004540 if (atomic_read(&clp->cl_count) > 1)
4541 nfs4_schedule_state_renewal(clp);
4542 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004543 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004544}
4545
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004546static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004548 struct nfs4_renewdata *data = calldata;
4549 struct nfs_client *clp = data->client;
4550 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004552 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004553 switch (task->tk_status) {
4554 case 0:
4555 break;
4556 case -NFS4ERR_LEASE_MOVED:
4557 nfs4_schedule_lease_moved_recovery(clp);
4558 break;
4559 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004560 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004561 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4562 return;
4563 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004564 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004565 return;
4566 }
4567 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004569 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
4571
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004572static const struct rpc_call_ops nfs4_renew_ops = {
4573 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004574 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004575};
4576
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004577static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578{
4579 struct rpc_message msg = {
4580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4581 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004582 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004584 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004586 if (renew_flags == 0)
4587 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004588 if (!atomic_inc_not_zero(&clp->cl_count))
4589 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004590 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004591 if (data == NULL)
4592 return -ENOMEM;
4593 data->client = clp;
4594 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004595 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004596 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597}
4598
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004599static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
4601 struct rpc_message msg = {
4602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4603 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004604 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 };
4606 unsigned long now = jiffies;
4607 int status;
4608
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004609 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (status < 0)
4611 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004612 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return 0;
4614}
4615
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004616static inline int nfs4_server_supports_acls(struct nfs_server *server)
4617{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004618 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004619}
4620
Trond Myklebust21f498c2012-08-24 10:59:25 -04004621/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4622 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004623 * the stack.
4624 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004625#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004626
Neil Hormane9e3d722011-03-04 19:26:03 -05004627static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004628 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004629{
4630 struct page *newpage, **spages;
4631 int rc = 0;
4632 size_t len;
4633 spages = pages;
4634
4635 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004636 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004637 newpage = alloc_page(GFP_KERNEL);
4638
4639 if (newpage == NULL)
4640 goto unwind;
4641 memcpy(page_address(newpage), buf, len);
4642 buf += len;
4643 buflen -= len;
4644 *pages++ = newpage;
4645 rc++;
4646 } while (buflen != 0);
4647
4648 return rc;
4649
4650unwind:
4651 for(; rc > 0; rc--)
4652 __free_page(spages[rc-1]);
4653 return -ENOMEM;
4654}
4655
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004656struct nfs4_cached_acl {
4657 int cached;
4658 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004659 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004660};
4661
4662static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004663{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004664 struct nfs_inode *nfsi = NFS_I(inode);
4665
4666 spin_lock(&inode->i_lock);
4667 kfree(nfsi->nfs4_acl);
4668 nfsi->nfs4_acl = acl;
4669 spin_unlock(&inode->i_lock);
4670}
4671
4672static void nfs4_zap_acl_attr(struct inode *inode)
4673{
4674 nfs4_set_cached_acl(inode, NULL);
4675}
4676
4677static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4678{
4679 struct nfs_inode *nfsi = NFS_I(inode);
4680 struct nfs4_cached_acl *acl;
4681 int ret = -ENOENT;
4682
4683 spin_lock(&inode->i_lock);
4684 acl = nfsi->nfs4_acl;
4685 if (acl == NULL)
4686 goto out;
4687 if (buf == NULL) /* user is just asking for length */
4688 goto out_len;
4689 if (acl->cached == 0)
4690 goto out;
4691 ret = -ERANGE; /* see getxattr(2) man page */
4692 if (acl->len > buflen)
4693 goto out;
4694 memcpy(buf, acl->data, acl->len);
4695out_len:
4696 ret = acl->len;
4697out:
4698 spin_unlock(&inode->i_lock);
4699 return ret;
4700}
4701
Sachin Prabhu5794d212012-04-17 14:36:40 +01004702static 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 +00004703{
4704 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004705 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004706
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004707 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004708 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004709 if (acl == NULL)
4710 goto out;
4711 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004712 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004713 } else {
4714 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4715 if (acl == NULL)
4716 goto out;
4717 acl->cached = 0;
4718 }
4719 acl->len = acl_len;
4720out:
4721 nfs4_set_cached_acl(inode, acl);
4722}
4723
Andy Adamsonbf118a32011-12-07 11:55:27 -05004724/*
4725 * The getxattr API returns the required buffer length when called with a
4726 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4727 * the required buf. On a NULL buf, we send a page of data to the server
4728 * guessing that the ACL request can be serviced by a page. If so, we cache
4729 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4730 * the cache. If not so, we throw away the page, and cache the required
4731 * length. The next getxattr call will then produce another round trip to
4732 * the server, this time with the input buf of the required size.
4733 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004734static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004735{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004736 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004737 struct nfs_getaclargs args = {
4738 .fh = NFS_FH(inode),
4739 .acl_pages = pages,
4740 .acl_len = buflen,
4741 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004742 struct nfs_getaclres res = {
4743 .acl_len = buflen,
4744 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004745 struct rpc_message msg = {
4746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4747 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004748 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004749 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004750 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4751 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004752
Andy Adamsonbf118a32011-12-07 11:55:27 -05004753 /* As long as we're doing a round trip to the server anyway,
4754 * let's be prepared for a page of acl data. */
4755 if (npages == 0)
4756 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004757 if (npages > ARRAY_SIZE(pages))
4758 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004759
Andy Adamsonbf118a32011-12-07 11:55:27 -05004760 for (i = 0; i < npages; i++) {
4761 pages[i] = alloc_page(GFP_KERNEL);
4762 if (!pages[i])
4763 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004764 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004765
4766 /* for decoding across pages */
4767 res.acl_scratch = alloc_page(GFP_KERNEL);
4768 if (!res.acl_scratch)
4769 goto out_free;
4770
Andy Adamsonbf118a32011-12-07 11:55:27 -05004771 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004772
Peng Taode040be2012-01-10 22:42:47 +08004773 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004774 __func__, buf, buflen, npages, args.acl_len);
4775 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4776 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004777 if (ret)
4778 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004779
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004780 /* Handle the case where the passed-in buffer is too short */
4781 if (res.acl_flags & NFS4_ACL_TRUNC) {
4782 /* Did the user only issue a request for the acl length? */
4783 if (buf == NULL)
4784 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004785 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004786 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004787 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004788 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004789 if (buf) {
4790 if (res.acl_len > buflen) {
4791 ret = -ERANGE;
4792 goto out_free;
4793 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004794 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004795 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004796out_ok:
4797 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004798out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004799 for (i = 0; i < npages; i++)
4800 if (pages[i])
4801 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004802 if (res.acl_scratch)
4803 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004804 return ret;
4805}
4806
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004807static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4808{
4809 struct nfs4_exception exception = { };
4810 ssize_t ret;
4811 do {
4812 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004813 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004814 if (ret >= 0)
4815 break;
4816 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4817 } while (exception.retry);
4818 return ret;
4819}
4820
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004821static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4822{
4823 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004824 int ret;
4825
4826 if (!nfs4_server_supports_acls(server))
4827 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004828 ret = nfs_revalidate_inode(server, inode);
4829 if (ret < 0)
4830 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004831 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4832 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004833 ret = nfs4_read_cached_acl(inode, buf, buflen);
4834 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004835 /* -ENOENT is returned if there is no ACL or if there is an ACL
4836 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004837 return ret;
4838 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004839}
4840
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004841static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004842{
4843 struct nfs_server *server = NFS_SERVER(inode);
4844 struct page *pages[NFS4ACL_MAXPAGES];
4845 struct nfs_setaclargs arg = {
4846 .fh = NFS_FH(inode),
4847 .acl_pages = pages,
4848 .acl_len = buflen,
4849 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004850 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4853 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004854 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004855 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004856 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004857 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004858
4859 if (!nfs4_server_supports_acls(server))
4860 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004861 if (npages > ARRAY_SIZE(pages))
4862 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004863 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004864 if (i < 0)
4865 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004866 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004867 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004868
4869 /*
4870 * Free each page after tx, so the only ref left is
4871 * held by the network stack
4872 */
4873 for (; i > 0; i--)
4874 put_page(pages[i-1]);
4875
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004876 /*
4877 * Acl update can result in inode attribute update.
4878 * so mark the attribute cache invalid.
4879 */
4880 spin_lock(&inode->i_lock);
4881 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4882 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004883 nfs_access_zap_cache(inode);
4884 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004885 return ret;
4886}
4887
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004888static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4889{
4890 struct nfs4_exception exception = { };
4891 int err;
4892 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004893 err = __nfs4_proc_set_acl(inode, buf, buflen);
4894 trace_nfs4_set_acl(inode, err);
4895 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004896 &exception);
4897 } while (exception.retry);
4898 return err;
4899}
4900
David Quigleyaa9c2662013-05-22 12:50:44 -04004901#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4902static int _nfs4_get_security_label(struct inode *inode, void *buf,
4903 size_t buflen)
4904{
4905 struct nfs_server *server = NFS_SERVER(inode);
4906 struct nfs_fattr fattr;
4907 struct nfs4_label label = {0, 0, buflen, buf};
4908
4909 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004910 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004911 .fh = NFS_FH(inode),
4912 .bitmask = bitmask,
4913 };
4914 struct nfs4_getattr_res res = {
4915 .fattr = &fattr,
4916 .label = &label,
4917 .server = server,
4918 };
4919 struct rpc_message msg = {
4920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004921 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004922 .rpc_resp = &res,
4923 };
4924 int ret;
4925
4926 nfs_fattr_init(&fattr);
4927
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004928 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004929 if (ret)
4930 return ret;
4931 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4932 return -ENOENT;
4933 if (buflen < label.len)
4934 return -ERANGE;
4935 return 0;
4936}
4937
4938static int nfs4_get_security_label(struct inode *inode, void *buf,
4939 size_t buflen)
4940{
4941 struct nfs4_exception exception = { };
4942 int err;
4943
4944 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4945 return -EOPNOTSUPP;
4946
4947 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004948 err = _nfs4_get_security_label(inode, buf, buflen);
4949 trace_nfs4_get_security_label(inode, err);
4950 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004951 &exception);
4952 } while (exception.retry);
4953 return err;
4954}
4955
4956static int _nfs4_do_set_security_label(struct inode *inode,
4957 struct nfs4_label *ilabel,
4958 struct nfs_fattr *fattr,
4959 struct nfs4_label *olabel)
4960{
4961
4962 struct iattr sattr = {0};
4963 struct nfs_server *server = NFS_SERVER(inode);
4964 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004965 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004966 .fh = NFS_FH(inode),
4967 .iap = &sattr,
4968 .server = server,
4969 .bitmask = bitmask,
4970 .label = ilabel,
4971 };
4972 struct nfs_setattrres res = {
4973 .fattr = fattr,
4974 .label = olabel,
4975 .server = server,
4976 };
4977 struct rpc_message msg = {
4978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004979 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004980 .rpc_resp = &res,
4981 };
4982 int status;
4983
Jeff Layton12207f62013-11-01 10:49:32 -04004984 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004985
Jeff Layton12207f62013-11-01 10:49:32 -04004986 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004987 if (status)
4988 dprintk("%s failed: %d\n", __func__, status);
4989
4990 return status;
4991}
4992
4993static int nfs4_do_set_security_label(struct inode *inode,
4994 struct nfs4_label *ilabel,
4995 struct nfs_fattr *fattr,
4996 struct nfs4_label *olabel)
4997{
4998 struct nfs4_exception exception = { };
4999 int err;
5000
5001 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005002 err = _nfs4_do_set_security_label(inode, ilabel,
5003 fattr, olabel);
5004 trace_nfs4_set_security_label(inode, err);
5005 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005006 &exception);
5007 } while (exception.retry);
5008 return err;
5009}
5010
5011static int
5012nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
5013{
5014 struct nfs4_label ilabel, *olabel = NULL;
5015 struct nfs_fattr fattr;
5016 struct rpc_cred *cred;
David Howells2b0143b2015-03-17 22:25:59 +00005017 struct inode *inode = d_inode(dentry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005018 int status;
5019
5020 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5021 return -EOPNOTSUPP;
5022
5023 nfs_fattr_init(&fattr);
5024
5025 ilabel.pi = 0;
5026 ilabel.lfs = 0;
5027 ilabel.label = (char *)buf;
5028 ilabel.len = buflen;
5029
5030 cred = rpc_lookup_cred();
5031 if (IS_ERR(cred))
5032 return PTR_ERR(cred);
5033
5034 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5035 if (IS_ERR(olabel)) {
5036 status = -PTR_ERR(olabel);
5037 goto out;
5038 }
5039
5040 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5041 if (status == 0)
5042 nfs_setsecurity(inode, &fattr, olabel);
5043
5044 nfs4_label_free(olabel);
5045out:
5046 put_rpccred(cred);
5047 return status;
5048}
5049#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5050
5051
Chuck Leverf0920752012-05-21 22:45:41 -04005052static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5053 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005054{
5055 __be32 verf[2];
5056
Chuck Lever2c820d92012-05-21 22:45:33 -04005057 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5058 /* An impossible timestamp guarantees this value
5059 * will never match a generated boot time. */
5060 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005061 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005062 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005063 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005064 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5065 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005066 }
Chuck Levercd937102012-03-02 17:14:31 -05005067 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5068}
5069
Jeff Laytona3192682015-06-09 19:43:59 -04005070static int
5071nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005072{
Jeff Laytona3192682015-06-09 19:43:59 -04005073 size_t len;
5074 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005075
Trond Myklebustceb3a162015-01-03 15:16:04 -05005076 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005077 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005078
Jeff Laytona3192682015-06-09 19:43:59 -04005079 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005080 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005081 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5082 1 +
5083 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5084 1;
5085 rcu_read_unlock();
5086
5087 if (len > NFS4_OPAQUE_LIMIT + 1)
5088 return -EINVAL;
5089
5090 /*
5091 * Since this string is allocated at mount time, and held until the
5092 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5093 * about a memory-reclaim deadlock.
5094 */
5095 str = kmalloc(len, GFP_KERNEL);
5096 if (!str)
5097 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005098
Chuck Levere984a552012-09-14 17:24:21 -04005099 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005100 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005101 clp->cl_ipaddr,
5102 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5103 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005104 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005105
Jeff Laytona3192682015-06-09 19:43:59 -04005106 clp->cl_owner_id = str;
5107 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005108}
5109
Jeff Layton873e3852015-06-09 19:44:00 -04005110static int
5111nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005112{
Jeff Layton873e3852015-06-09 19:44:00 -04005113 size_t len;
5114 char *str;
5115
5116 len = 10 + 10 + 1 + 10 + 1 +
5117 strlen(nfs4_client_id_uniquifier) + 1 +
5118 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5119
5120 if (len > NFS4_OPAQUE_LIMIT + 1)
5121 return -EINVAL;
5122
5123 /*
5124 * Since this string is allocated at mount time, and held until the
5125 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5126 * about a memory-reclaim deadlock.
5127 */
5128 str = kmalloc(len, GFP_KERNEL);
5129 if (!str)
5130 return -ENOMEM;
5131
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005132 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005133 clp->rpc_ops->version, clp->cl_minorversion,
5134 nfs4_client_id_uniquifier,
5135 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005136 clp->cl_owner_id = str;
5137 return 0;
5138}
5139
5140static int
5141nfs4_init_uniform_client_string(struct nfs_client *clp)
5142{
Jeff Layton873e3852015-06-09 19:44:00 -04005143 size_t len;
5144 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005145
5146 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005147 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005148
5149 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005150 return nfs4_init_uniquifier_client_string(clp);
5151
5152 len = 10 + 10 + 1 + 10 + 1 +
5153 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5154
5155 if (len > NFS4_OPAQUE_LIMIT + 1)
5156 return -EINVAL;
5157
5158 /*
5159 * Since this string is allocated at mount time, and held until the
5160 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5161 * about a memory-reclaim deadlock.
5162 */
5163 str = kmalloc(len, GFP_KERNEL);
5164 if (!str)
5165 return -ENOMEM;
5166
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005167 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005168 clp->rpc_ops->version, clp->cl_minorversion,
5169 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005170 clp->cl_owner_id = str;
5171 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005172}
5173
Chuck Lever706cb8d2014-03-12 12:51:47 -04005174/*
5175 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5176 * services. Advertise one based on the address family of the
5177 * clientaddr.
5178 */
5179static unsigned int
5180nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5181{
5182 if (strchr(clp->cl_ipaddr, ':') != NULL)
5183 return scnprintf(buf, len, "tcp6");
5184 else
5185 return scnprintf(buf, len, "tcp");
5186}
5187
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005188static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5189{
5190 struct nfs4_setclientid *sc = calldata;
5191
5192 if (task->tk_status == 0)
5193 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5194}
5195
5196static const struct rpc_call_ops nfs4_setclientid_ops = {
5197 .rpc_call_done = nfs4_setclientid_done,
5198};
5199
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005200/**
5201 * nfs4_proc_setclientid - Negotiate client ID
5202 * @clp: state data structure
5203 * @program: RPC program for NFSv4 callback service
5204 * @port: IP port number for NFS4 callback service
5205 * @cred: RPC credential to use for this call
5206 * @res: where to place the result
5207 *
5208 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5209 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005210int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5211 unsigned short port, struct rpc_cred *cred,
5212 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213{
5214 nfs4_verifier sc_verifier;
5215 struct nfs4_setclientid setclientid = {
5216 .sc_verifier = &sc_verifier,
5217 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005218 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 };
5220 struct rpc_message msg = {
5221 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5222 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005223 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005224 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005226 struct rpc_task *task;
5227 struct rpc_task_setup task_setup_data = {
5228 .rpc_client = clp->cl_rpcclient,
5229 .rpc_message = &msg,
5230 .callback_ops = &nfs4_setclientid_ops,
5231 .callback_data = &setclientid,
5232 .flags = RPC_TASK_TIMEOUT,
5233 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005234 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Chuck Leverde734832012-07-11 16:30:50 -04005236 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005237 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005238
5239 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5240 status = nfs4_init_uniform_client_string(clp);
5241 else
Jeff Laytona3192682015-06-09 19:43:59 -04005242 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005243
5244 if (status)
5245 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005246
Chuck Leverde734832012-07-11 16:30:50 -04005247 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005248 setclientid.sc_netid_len =
5249 nfs4_init_callback_netid(clp,
5250 setclientid.sc_netid,
5251 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005252 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005253 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 clp->cl_ipaddr, port >> 8, port & 255);
5255
Jeff Layton3a6bb732015-06-09 19:43:57 -04005256 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005257 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005258 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005259 task = rpc_run_task(&task_setup_data);
5260 if (IS_ERR(task)) {
5261 status = PTR_ERR(task);
5262 goto out;
5263 }
5264 status = task->tk_status;
5265 if (setclientid.sc_cred) {
5266 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5267 put_rpccred(setclientid.sc_cred);
5268 }
5269 rpc_put_task(task);
5270out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005271 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005272 dprintk("NFS reply setclientid: %d\n", status);
5273 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005276/**
5277 * nfs4_proc_setclientid_confirm - Confirm client ID
5278 * @clp: state data structure
5279 * @res: result of a previous SETCLIENTID
5280 * @cred: RPC credential to use for this call
5281 *
5282 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5283 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005284int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005285 struct nfs4_setclientid_res *arg,
5286 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 struct rpc_message msg = {
5289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005290 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005291 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 int status;
5294
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005295 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5296 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5297 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005298 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005299 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005300 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 return status;
5302}
5303
Trond Myklebustfe650402006-01-03 09:55:18 +01005304struct nfs4_delegreturndata {
5305 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005306 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005307 struct nfs_fh fh;
5308 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005309 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005310 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005311 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005312 struct inode *inode;
5313 bool roc;
5314 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005315};
5316
Trond Myklebustfe650402006-01-03 09:55:18 +01005317static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5318{
5319 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005320
Trond Myklebust14516c32010-07-31 14:29:06 -04005321 if (!nfs4_sequence_done(task, &data->res.seq_res))
5322 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005323
Trond Myklebustca8acf82013-08-13 10:36:56 -04005324 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005325 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005326 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005327 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005328 case -NFS4ERR_ADMIN_REVOKED:
5329 case -NFS4ERR_DELEG_REVOKED:
5330 case -NFS4ERR_BAD_STATEID:
5331 case -NFS4ERR_OLD_STATEID:
5332 case -NFS4ERR_STALE_STATEID:
5333 case -NFS4ERR_EXPIRED:
5334 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005335 if (data->roc)
5336 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005337 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005338 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005339 if (nfs4_async_handle_error(task, data->res.server,
5340 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005341 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005342 return;
5343 }
5344 }
5345 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005346}
5347
5348static void nfs4_delegreturn_release(void *calldata)
5349{
Peng Tao039b7562014-07-03 13:05:02 +08005350 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005351 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005352
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005353 if (inode) {
5354 if (data->roc)
5355 pnfs_roc_release(inode);
5356 nfs_iput_and_deactive(inode);
5357 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005358 kfree(calldata);
5359}
5360
Andy Adamson938e1012009-04-01 09:22:28 -04005361static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5362{
5363 struct nfs4_delegreturndata *d_data;
5364
5365 d_data = (struct nfs4_delegreturndata *)data;
5366
Peng Tao500d7012015-09-22 11:35:22 +08005367 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5368 return;
5369
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005370 if (d_data->roc)
5371 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005372
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005373 nfs4_setup_sequence(d_data->res.server,
5374 &d_data->args.seq_args,
5375 &d_data->res.seq_res,
5376 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005377}
Andy Adamson938e1012009-04-01 09:22:28 -04005378
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005379static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005380 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005381 .rpc_call_done = nfs4_delegreturn_done,
5382 .rpc_release = nfs4_delegreturn_release,
5383};
5384
Trond Myklebuste6f81072008-01-24 18:14:34 -05005385static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005386{
5387 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005388 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005389 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005390 struct rpc_message msg = {
5391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5392 .rpc_cred = cred,
5393 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005394 struct rpc_task_setup task_setup_data = {
5395 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005396 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005397 .callback_ops = &nfs4_delegreturn_ops,
5398 .flags = RPC_TASK_ASYNC,
5399 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005400 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005401
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005402 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005403 if (data == NULL)
5404 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005405 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005406
5407 nfs4_state_protect(server->nfs_client,
5408 NFS_SP4_MACH_CRED_CLEANUP,
5409 &task_setup_data.rpc_client, &msg);
5410
Trond Myklebustfe650402006-01-03 09:55:18 +01005411 data->args.fhandle = &data->fh;
5412 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005413 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005414 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005415 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005416 data->res.fattr = &data->fattr;
5417 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005418 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005419 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005420 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005421 data->inode = nfs_igrab_and_active(inode);
5422 if (data->inode)
5423 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005424
Trond Myklebustc970aa82007-07-14 15:39:59 -04005425 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005426 msg.rpc_argp = &data->args;
5427 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005428 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005429 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005430 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005431 if (!issync)
5432 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005433 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005434 if (status != 0)
5435 goto out;
5436 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005437 if (status == 0)
5438 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5439 else
5440 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005441out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005442 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005443 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444}
5445
Trond Myklebuste6f81072008-01-24 18:14:34 -05005446int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447{
5448 struct nfs_server *server = NFS_SERVER(inode);
5449 struct nfs4_exception exception = { };
5450 int err;
5451 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005452 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005453 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 switch (err) {
5455 case -NFS4ERR_STALE_STATEID:
5456 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 case 0:
5458 return 0;
5459 }
5460 err = nfs4_handle_exception(server, err, &exception);
5461 } while (exception.retry);
5462 return err;
5463}
5464
5465#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5466#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5467
5468/*
5469 * sleep, with exponential backoff, and retry the LOCK operation.
5470 */
5471static unsigned long
5472nfs4_set_lock_task_retry(unsigned long timeout)
5473{
Colin Cross416ad3c2013-05-06 23:50:06 +00005474 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 timeout <<= 1;
5476 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5477 return NFS4_LOCK_MAXTIMEOUT;
5478 return timeout;
5479}
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5482{
5483 struct inode *inode = state->inode;
5484 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005485 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005486 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005488 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005490 struct nfs_lockt_res res = {
5491 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 };
5493 struct rpc_message msg = {
5494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5495 .rpc_argp = &arg,
5496 .rpc_resp = &res,
5497 .rpc_cred = state->owner->so_cred,
5498 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 struct nfs4_lock_state *lsp;
5500 int status;
5501
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005502 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005503 status = nfs4_set_lock_state(state, request);
5504 if (status != 0)
5505 goto out;
5506 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005507 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005508 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005509 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005510 switch (status) {
5511 case 0:
5512 request->fl_type = F_UNLCK;
5513 break;
5514 case -NFS4ERR_DENIED:
5515 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005517 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005518 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 return status;
5521}
5522
5523static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5524{
5525 struct nfs4_exception exception = { };
5526 int err;
5527
5528 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005529 err = _nfs4_proc_getlk(state, cmd, request);
5530 trace_nfs4_get_lock(request, state, cmd, err);
5531 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 &exception);
5533 } while (exception.retry);
5534 return err;
5535}
5536
Jeff Layton83bfff22015-07-11 06:43:03 -04005537static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005539 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540}
5541
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005542struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005543 struct nfs_locku_args arg;
5544 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005545 struct nfs4_lock_state *lsp;
5546 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005547 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005548 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005549 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005550};
5551
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005552static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5553 struct nfs_open_context *ctx,
5554 struct nfs4_lock_state *lsp,
5555 struct nfs_seqid *seqid)
5556{
5557 struct nfs4_unlockdata *p;
5558 struct inode *inode = lsp->ls_state->inode;
5559
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005560 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005561 if (p == NULL)
5562 return NULL;
5563 p->arg.fh = NFS_FH(inode);
5564 p->arg.fl = &p->fl;
5565 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005566 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005567 p->lsp = lsp;
5568 atomic_inc(&lsp->ls_count);
5569 /* Ensure we don't close file until we're done freeing locks! */
5570 p->ctx = get_nfs_open_context(ctx);
5571 memcpy(&p->fl, fl, sizeof(p->fl));
5572 p->server = NFS_SERVER(inode);
5573 return p;
5574}
5575
Trond Myklebust06f814a2006-01-03 09:55:07 +01005576static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005577{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005578 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005579 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005580 nfs4_put_lock_state(calldata->lsp);
5581 put_nfs_open_context(calldata->ctx);
5582 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005583}
5584
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005585static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005586{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005587 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005588
Trond Myklebust14516c32010-07-31 14:29:06 -04005589 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5590 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005591 switch (task->tk_status) {
5592 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005593 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005594 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005595 if (nfs4_update_lock_stateid(calldata->lsp,
5596 &calldata->res.stateid))
5597 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005598 case -NFS4ERR_BAD_STATEID:
5599 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005600 case -NFS4ERR_STALE_STATEID:
5601 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005602 if (!nfs4_stateid_match(&calldata->arg.stateid,
5603 &calldata->lsp->ls_stateid))
5604 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605 break;
5606 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005607 if (nfs4_async_handle_error(task, calldata->server,
5608 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005609 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005610 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005611 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005612}
5613
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005614static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005615{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005616 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005618 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005619 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005620 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005621 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005622 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005623 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005624 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005625 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005626 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005627 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005628 &calldata->res.seq_res,
5629 task) != 0)
5630 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005631 return;
5632out_no_action:
5633 task->tk_action = NULL;
5634out_wait:
5635 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636}
5637
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005638static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005639 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005640 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005641 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005642};
5643
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005644static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5645 struct nfs_open_context *ctx,
5646 struct nfs4_lock_state *lsp,
5647 struct nfs_seqid *seqid)
5648{
5649 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005650 struct rpc_message msg = {
5651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5652 .rpc_cred = ctx->cred,
5653 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005654 struct rpc_task_setup task_setup_data = {
5655 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005656 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005657 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005658 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005659 .flags = RPC_TASK_ASYNC,
5660 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005661
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005662 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5663 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5664
Frank Filz137d6ac2007-07-09 15:32:29 -07005665 /* Ensure this is an unlock - when canceling a lock, the
5666 * canceled lock is passed in, and it won't be an unlock.
5667 */
5668 fl->fl_type = F_UNLCK;
5669
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005670 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5671 if (data == NULL) {
5672 nfs_free_seqid(seqid);
5673 return ERR_PTR(-ENOMEM);
5674 }
5675
Chuck Levera9c92d62013-08-09 12:48:18 -04005676 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005677 msg.rpc_argp = &data->arg;
5678 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005679 task_setup_data.callback_data = data;
5680 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005681}
5682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5684{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005685 struct inode *inode = state->inode;
5686 struct nfs4_state_owner *sp = state->owner;
5687 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005688 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005689 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005690 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005691 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005692 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005693 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Trond Myklebust9b073572006-06-29 16:38:34 -04005695 status = nfs4_set_lock_state(state, request);
5696 /* Unlock _before_ we do the RPC call */
5697 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005698 /* Exclude nfs_delegation_claim_locks() */
5699 mutex_lock(&sp->so_delegreturn_mutex);
5700 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005701 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005702 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005703 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005704 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005705 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005706 }
5707 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005708 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005709 if (status != 0)
5710 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005711 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005712 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005713 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5714 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005715 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5716 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005717 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005718 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005719 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005720 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005721 status = PTR_ERR(task);
5722 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005723 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005724 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005725 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005726out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005727 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005728 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005729 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730}
5731
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005732struct nfs4_lockdata {
5733 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005734 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005735 struct nfs4_lock_state *lsp;
5736 struct nfs_open_context *ctx;
5737 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005738 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005739 int rpc_status;
5740 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005741 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005742};
5743
5744static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005745 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5746 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005747{
5748 struct nfs4_lockdata *p;
5749 struct inode *inode = lsp->ls_state->inode;
5750 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005751 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005754 if (p == NULL)
5755 return NULL;
5756
5757 p->arg.fh = NFS_FH(inode);
5758 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005759 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005760 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005761 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005762 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5763 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005764 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005765 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005766 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005767 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005768 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005769 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005770 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005771 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005772 atomic_inc(&lsp->ls_count);
5773 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005774 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005775 memcpy(&p->fl, fl, sizeof(p->fl));
5776 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005777out_free_seqid:
5778 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005779out_free:
5780 kfree(p);
5781 return NULL;
5782}
5783
5784static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5785{
5786 struct nfs4_lockdata *data = calldata;
5787 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
Harvey Harrison3110ff82008-05-02 13:42:44 -07005789 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005790 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005791 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005792 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005793 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005794 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005795 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005796 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005797 nfs4_stateid_copy(&data->arg.open_stateid,
5798 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005799 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005800 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005801 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005802 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005803 nfs4_stateid_copy(&data->arg.lock_stateid,
5804 &data->lsp->ls_stateid);
5805 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005806 if (!nfs4_valid_open_stateid(state)) {
5807 data->rpc_status = -EBADF;
5808 task->tk_action = NULL;
5809 goto out_release_open_seqid;
5810 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005811 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005812 if (nfs4_setup_sequence(data->server,
5813 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005814 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005815 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005816 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005817out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005818 nfs_release_seqid(data->arg.open_seqid);
5819out_release_lock_seqid:
5820 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005821out_wait:
5822 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005823 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005824}
5825
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005826static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5827{
5828 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005829 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005830
Harvey Harrison3110ff82008-05-02 13:42:44 -07005831 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005832
Trond Myklebust14516c32010-07-31 14:29:06 -04005833 if (!nfs4_sequence_done(task, &data->res.seq_res))
5834 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005835
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005836 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005837 switch (task->tk_status) {
5838 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005839 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005840 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005841 if (data->arg.new_lock) {
5842 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005843 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005844 rpc_restart_call_prepare(task);
5845 break;
5846 }
5847 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005848 if (data->arg.new_lock_owner != 0) {
5849 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5850 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5851 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5852 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5853 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005854 break;
5855 case -NFS4ERR_BAD_STATEID:
5856 case -NFS4ERR_OLD_STATEID:
5857 case -NFS4ERR_STALE_STATEID:
5858 case -NFS4ERR_EXPIRED:
5859 if (data->arg.new_lock_owner != 0) {
5860 if (!nfs4_stateid_match(&data->arg.open_stateid,
5861 &lsp->ls_state->open_stateid))
5862 rpc_restart_call_prepare(task);
5863 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5864 &lsp->ls_stateid))
5865 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005866 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005867 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005868}
5869
5870static void nfs4_lock_release(void *calldata)
5871{
5872 struct nfs4_lockdata *data = calldata;
5873
Harvey Harrison3110ff82008-05-02 13:42:44 -07005874 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005875 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005876 if (data->cancelled != 0) {
5877 struct rpc_task *task;
5878 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5879 data->arg.lock_seqid);
5880 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005881 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005882 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005883 } else
5884 nfs_free_seqid(data->arg.lock_seqid);
5885 nfs4_put_lock_state(data->lsp);
5886 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005887 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005889 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005890}
5891
5892static const struct rpc_call_ops nfs4_lock_ops = {
5893 .rpc_call_prepare = nfs4_lock_prepare,
5894 .rpc_call_done = nfs4_lock_done,
5895 .rpc_release = nfs4_lock_release,
5896};
5897
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005898static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5899{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005900 switch (error) {
5901 case -NFS4ERR_ADMIN_REVOKED:
5902 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005903 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005904 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005905 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005906 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005907 break;
5908 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005909 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005910 case -NFS4ERR_EXPIRED:
5911 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005912 };
5913}
5914
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005915static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005916{
5917 struct nfs4_lockdata *data;
5918 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005919 struct rpc_message msg = {
5920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5921 .rpc_cred = state->owner->so_cred,
5922 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005923 struct rpc_task_setup task_setup_data = {
5924 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005925 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005926 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005927 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005928 .flags = RPC_TASK_ASYNC,
5929 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005930 int ret;
5931
Harvey Harrison3110ff82008-05-02 13:42:44 -07005932 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005933 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005934 fl->fl_u.nfs4_fl.owner,
5935 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005936 if (data == NULL)
5937 return -ENOMEM;
5938 if (IS_SETLKW(cmd))
5939 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005940 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005941 msg.rpc_argp = &data->arg;
5942 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005943 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005944 if (recovery_type > NFS_LOCK_NEW) {
5945 if (recovery_type == NFS_LOCK_RECLAIM)
5946 data->arg.reclaim = NFS_LOCK_RECLAIM;
5947 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005948 } else
5949 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005950 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005951 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005952 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005953 ret = nfs4_wait_for_completion_rpc_task(task);
5954 if (ret == 0) {
5955 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005956 if (ret)
5957 nfs4_handle_setlk_error(data->server, data->lsp,
5958 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005959 } else
5960 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005961 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005962 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005963 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965}
5966
5967static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5968{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005969 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005970 struct nfs4_exception exception = {
5971 .inode = state->inode,
5972 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005973 int err;
5974
5975 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005976 /* Cache the lock if possible... */
5977 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5978 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005979 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005980 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005981 break;
5982 nfs4_handle_exception(server, err, &exception);
5983 } while (exception.retry);
5984 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985}
5986
5987static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5988{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005989 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005990 struct nfs4_exception exception = {
5991 .inode = state->inode,
5992 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005993 int err;
5994
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005995 err = nfs4_set_lock_state(state, request);
5996 if (err != 0)
5997 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04005998 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10005999 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6000 return 0;
6001 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006002 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006003 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6004 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006005 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006006 switch (err) {
6007 default:
6008 goto out;
6009 case -NFS4ERR_GRACE:
6010 case -NFS4ERR_DELAY:
6011 nfs4_handle_exception(server, err, &exception);
6012 err = 0;
6013 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006014 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006015out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006016 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006019#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006020/**
6021 * nfs41_check_expired_locks - possibly free a lock stateid
6022 *
6023 * @state: NFSv4 state for an inode
6024 *
6025 * Returns NFS_OK if recovery for this stateid is now finished.
6026 * Otherwise a negative NFS4ERR value is returned.
6027 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006028static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006029{
Chuck Levereb64cf92012-07-11 16:30:05 -04006030 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006031 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006032 struct nfs_server *server = NFS_SERVER(state->inode);
6033
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006034 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006035 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006036 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6037
6038 status = nfs41_test_stateid(server,
6039 &lsp->ls_stateid,
6040 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006041 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006042 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006043 /* Free the stateid unless the server
6044 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006045 if (status != -NFS4ERR_BAD_STATEID)
6046 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006047 &lsp->ls_stateid,
6048 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006049 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006050 ret = status;
6051 }
6052 }
6053 };
6054
6055 return ret;
6056}
6057
6058static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6059{
6060 int status = NFS_OK;
6061
6062 if (test_bit(LK_STATE_IN_USE, &state->flags))
6063 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006064 if (status != NFS_OK)
6065 status = nfs4_lock_expired(state, request);
6066 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006067}
6068#endif
6069
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6071{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006072 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006073 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006074 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006075 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006077 if ((fl_flags & FL_POSIX) &&
6078 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6079 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006080 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006081 status = nfs4_set_lock_state(state, request);
6082 if (status != 0)
6083 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006084 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006085 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006086 if (status < 0)
6087 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006088 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006089 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006090 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006091 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006092 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006093 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006094 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006095 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006096 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006097 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006098 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006099 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006100 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006101 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006102out:
6103 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return status;
6105}
6106
6107static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6108{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006109 struct nfs4_exception exception = {
6110 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006111 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006112 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 int err;
6114
6115 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006116 err = _nfs4_proc_setlk(state, cmd, request);
6117 if (err == -NFS4ERR_DENIED)
6118 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006120 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 } while (exception.retry);
6122 return err;
6123}
6124
6125static int
6126nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6127{
6128 struct nfs_open_context *ctx;
6129 struct nfs4_state *state;
6130 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6131 int status;
6132
6133 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006134 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 state = ctx->state;
6136
6137 if (request->fl_start < 0 || request->fl_end < 0)
6138 return -EINVAL;
6139
Trond Myklebustd9531262009-07-21 19:22:38 -04006140 if (IS_GETLK(cmd)) {
6141 if (state != NULL)
6142 return nfs4_proc_getlk(state, F_GETLK, request);
6143 return 0;
6144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
6146 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6147 return -EINVAL;
6148
Trond Myklebustd9531262009-07-21 19:22:38 -04006149 if (request->fl_type == F_UNLCK) {
6150 if (state != NULL)
6151 return nfs4_proc_unlck(state, cmd, request);
6152 return 0;
6153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Trond Myklebustd9531262009-07-21 19:22:38 -04006155 if (state == NULL)
6156 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006157 /*
6158 * Don't rely on the VFS having checked the file open mode,
6159 * since it won't do this for flock() locks.
6160 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006161 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006162 case F_RDLCK:
6163 if (!(filp->f_mode & FMODE_READ))
6164 return -EBADF;
6165 break;
6166 case F_WRLCK:
6167 if (!(filp->f_mode & FMODE_WRITE))
6168 return -EBADF;
6169 }
6170
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 do {
6172 status = nfs4_proc_setlk(state, cmd, request);
6173 if ((status != -EAGAIN) || IS_SETLK(cmd))
6174 break;
6175 timeout = nfs4_set_lock_task_retry(timeout);
6176 status = -ERESTARTSYS;
6177 if (signalled())
6178 break;
6179 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 return status;
6181}
6182
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006183int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006184{
6185 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006186 int err;
6187
6188 err = nfs4_set_lock_state(state, fl);
6189 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006190 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006191 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006192 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006193}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006194
Trond Myklebustcf470c32012-03-07 13:49:12 -05006195struct nfs_release_lockowner_data {
6196 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006197 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006198 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006199 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006200 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006201};
6202
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006203static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6204{
6205 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006206 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006207 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6208 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006209 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006210 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006211}
6212
6213static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6214{
6215 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006216 struct nfs_server *server = data->server;
6217
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006218 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006219
6220 switch (task->tk_status) {
6221 case 0:
6222 renew_lease(server, data->timestamp);
6223 break;
6224 case -NFS4ERR_STALE_CLIENTID:
6225 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006226 nfs4_schedule_lease_recovery(server->nfs_client);
6227 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006228 case -NFS4ERR_LEASE_MOVED:
6229 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006230 if (nfs4_async_handle_error(task, server,
6231 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006232 rpc_restart_call_prepare(task);
6233 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006234}
6235
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006236static void nfs4_release_lockowner_release(void *calldata)
6237{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006238 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006239 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006240 kfree(calldata);
6241}
6242
Trond Myklebust17280172012-03-11 13:11:00 -04006243static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006244 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6245 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006246 .rpc_release = nfs4_release_lockowner_release,
6247};
6248
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006249static void
6250nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006251{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006252 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006253 struct rpc_message msg = {
6254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6255 };
6256
6257 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006258 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006259
Trond Myklebustcf470c32012-03-07 13:49:12 -05006260 data = kmalloc(sizeof(*data), GFP_NOFS);
6261 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006262 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006263 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006264 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006265 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6266 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6267 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006268
Trond Myklebustcf470c32012-03-07 13:49:12 -05006269 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006270 msg.rpc_resp = &data->res;
6271 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006272 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006273}
6274
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006275#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6276
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006277static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
6278 struct dentry *dentry, const char *key,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006279 const void *buf, size_t buflen,
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006280 int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006281{
David Howells2b0143b2015-03-17 22:25:59 +00006282 return nfs4_proc_set_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006283}
6284
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006285static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
6286 struct dentry *dentry, const char *key,
6287 void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006288{
David Howells2b0143b2015-03-17 22:25:59 +00006289 return nfs4_proc_get_acl(d_inode(dentry), buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006290}
6291
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006292static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006293{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006294 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006295}
6296
David Quigleyc9bccef2013-05-22 12:50:45 -04006297#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006298
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006299static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
6300 struct dentry *dentry, const char *key,
6301 const void *buf, size_t buflen,
6302 int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006303{
6304 if (security_ismaclabel(key))
6305 return nfs4_set_security_label(dentry, buf, buflen);
6306
6307 return -EOPNOTSUPP;
6308}
6309
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006310static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
6311 struct dentry *dentry, const char *key,
6312 void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006313{
6314 if (security_ismaclabel(key))
David Howells2b0143b2015-03-17 22:25:59 +00006315 return nfs4_get_security_label(d_inode(dentry), buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006316 return -EOPNOTSUPP;
6317}
6318
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006319static ssize_t
6320nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006321{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006322 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006323
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006324 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6325 len = security_inode_listsecurity(inode, list, list_len);
6326 if (list_len && len > list_len)
6327 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006328 }
6329 return len;
6330}
6331
6332static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6333 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006334 .get = nfs4_xattr_get_nfs4_label,
6335 .set = nfs4_xattr_set_nfs4_label,
6336};
David Quigleyc9bccef2013-05-22 12:50:45 -04006337
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006338#else
6339
6340static ssize_t
6341nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6342{
6343 return 0;
6344}
6345
6346#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006347
Andy Adamson533eb462011-06-13 18:25:56 -04006348/*
6349 * nfs_fhget will use either the mounted_on_fileid or the fileid
6350 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006351static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6352{
Andy Adamson533eb462011-06-13 18:25:56 -04006353 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6354 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6355 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006356 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006357 return;
6358
6359 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006360 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006361 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6362 fattr->nlink = 2;
6363}
6364
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006365static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6366 const struct qstr *name,
6367 struct nfs4_fs_locations *fs_locations,
6368 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006369{
6370 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006371 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006372 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006373 };
6374 struct nfs4_fs_locations_arg args = {
6375 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006376 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006377 .page = page,
6378 .bitmask = bitmask,
6379 };
Benny Halevy22958462009-04-01 09:22:02 -04006380 struct nfs4_fs_locations_res res = {
6381 .fs_locations = fs_locations,
6382 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6385 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006386 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006387 };
6388 int status;
6389
Harvey Harrison3110ff82008-05-02 13:42:44 -07006390 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006391
6392 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6393 * is not supported */
6394 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6395 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6396 else
6397 bitmask[0] |= FATTR4_WORD0_FILEID;
6398
Trond Myklebustc228fd32007-01-13 02:28:11 -05006399 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006400 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006401 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006402 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006403 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006404 return status;
6405}
6406
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006407int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6408 const struct qstr *name,
6409 struct nfs4_fs_locations *fs_locations,
6410 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006411{
6412 struct nfs4_exception exception = { };
6413 int err;
6414 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006415 err = _nfs4_proc_fs_locations(client, dir, name,
6416 fs_locations, page);
6417 trace_nfs4_get_fs_locations(dir, name, err);
6418 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006419 &exception);
6420 } while (exception.retry);
6421 return err;
6422}
6423
Chuck Leverb03d7352013-10-17 14:12:50 -04006424/*
6425 * This operation also signals the server that this client is
6426 * performing migration recovery. The server can stop returning
6427 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6428 * appended to this compound to identify the client ID which is
6429 * performing recovery.
6430 */
6431static int _nfs40_proc_get_locations(struct inode *inode,
6432 struct nfs4_fs_locations *locations,
6433 struct page *page, struct rpc_cred *cred)
6434{
6435 struct nfs_server *server = NFS_SERVER(inode);
6436 struct rpc_clnt *clnt = server->client;
6437 u32 bitmask[2] = {
6438 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6439 };
6440 struct nfs4_fs_locations_arg args = {
6441 .clientid = server->nfs_client->cl_clientid,
6442 .fh = NFS_FH(inode),
6443 .page = page,
6444 .bitmask = bitmask,
6445 .migration = 1, /* skip LOOKUP */
6446 .renew = 1, /* append RENEW */
6447 };
6448 struct nfs4_fs_locations_res res = {
6449 .fs_locations = locations,
6450 .migration = 1,
6451 .renew = 1,
6452 };
6453 struct rpc_message msg = {
6454 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6455 .rpc_argp = &args,
6456 .rpc_resp = &res,
6457 .rpc_cred = cred,
6458 };
6459 unsigned long now = jiffies;
6460 int status;
6461
6462 nfs_fattr_init(&locations->fattr);
6463 locations->server = server;
6464 locations->nlocations = 0;
6465
6466 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6467 nfs4_set_sequence_privileged(&args.seq_args);
6468 status = nfs4_call_sync_sequence(clnt, server, &msg,
6469 &args.seq_args, &res.seq_res);
6470 if (status)
6471 return status;
6472
6473 renew_lease(server, now);
6474 return 0;
6475}
6476
6477#ifdef CONFIG_NFS_V4_1
6478
6479/*
6480 * This operation also signals the server that this client is
6481 * performing migration recovery. The server can stop asserting
6482 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6483 * performing this operation is identified in the SEQUENCE
6484 * operation in this compound.
6485 *
6486 * When the client supports GETATTR(fs_locations_info), it can
6487 * be plumbed in here.
6488 */
6489static int _nfs41_proc_get_locations(struct inode *inode,
6490 struct nfs4_fs_locations *locations,
6491 struct page *page, struct rpc_cred *cred)
6492{
6493 struct nfs_server *server = NFS_SERVER(inode);
6494 struct rpc_clnt *clnt = server->client;
6495 u32 bitmask[2] = {
6496 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6497 };
6498 struct nfs4_fs_locations_arg args = {
6499 .fh = NFS_FH(inode),
6500 .page = page,
6501 .bitmask = bitmask,
6502 .migration = 1, /* skip LOOKUP */
6503 };
6504 struct nfs4_fs_locations_res res = {
6505 .fs_locations = locations,
6506 .migration = 1,
6507 };
6508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6510 .rpc_argp = &args,
6511 .rpc_resp = &res,
6512 .rpc_cred = cred,
6513 };
6514 int status;
6515
6516 nfs_fattr_init(&locations->fattr);
6517 locations->server = server;
6518 locations->nlocations = 0;
6519
6520 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6521 nfs4_set_sequence_privileged(&args.seq_args);
6522 status = nfs4_call_sync_sequence(clnt, server, &msg,
6523 &args.seq_args, &res.seq_res);
6524 if (status == NFS4_OK &&
6525 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6526 status = -NFS4ERR_LEASE_MOVED;
6527 return status;
6528}
6529
6530#endif /* CONFIG_NFS_V4_1 */
6531
6532/**
6533 * nfs4_proc_get_locations - discover locations for a migrated FSID
6534 * @inode: inode on FSID that is migrating
6535 * @locations: result of query
6536 * @page: buffer
6537 * @cred: credential to use for this operation
6538 *
6539 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6540 * operation failed, or a negative errno if a local error occurred.
6541 *
6542 * On success, "locations" is filled in, but if the server has
6543 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6544 * asserted.
6545 *
6546 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6547 * from this client that require migration recovery.
6548 */
6549int nfs4_proc_get_locations(struct inode *inode,
6550 struct nfs4_fs_locations *locations,
6551 struct page *page, struct rpc_cred *cred)
6552{
6553 struct nfs_server *server = NFS_SERVER(inode);
6554 struct nfs_client *clp = server->nfs_client;
6555 const struct nfs4_mig_recovery_ops *ops =
6556 clp->cl_mvops->mig_recovery_ops;
6557 struct nfs4_exception exception = { };
6558 int status;
6559
6560 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6561 (unsigned long long)server->fsid.major,
6562 (unsigned long long)server->fsid.minor,
6563 clp->cl_hostname);
6564 nfs_display_fhandle(NFS_FH(inode), __func__);
6565
6566 do {
6567 status = ops->get_locations(inode, locations, page, cred);
6568 if (status != -NFS4ERR_DELAY)
6569 break;
6570 nfs4_handle_exception(server, status, &exception);
6571 } while (exception.retry);
6572 return status;
6573}
6574
Chuck Lever44c99932013-10-17 14:13:30 -04006575/*
6576 * This operation also signals the server that this client is
6577 * performing "lease moved" recovery. The server can stop
6578 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6579 * is appended to this compound to identify the client ID which is
6580 * performing recovery.
6581 */
6582static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6583{
6584 struct nfs_server *server = NFS_SERVER(inode);
6585 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6586 struct rpc_clnt *clnt = server->client;
6587 struct nfs4_fsid_present_arg args = {
6588 .fh = NFS_FH(inode),
6589 .clientid = clp->cl_clientid,
6590 .renew = 1, /* append RENEW */
6591 };
6592 struct nfs4_fsid_present_res res = {
6593 .renew = 1,
6594 };
6595 struct rpc_message msg = {
6596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6597 .rpc_argp = &args,
6598 .rpc_resp = &res,
6599 .rpc_cred = cred,
6600 };
6601 unsigned long now = jiffies;
6602 int status;
6603
6604 res.fh = nfs_alloc_fhandle();
6605 if (res.fh == NULL)
6606 return -ENOMEM;
6607
6608 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6609 nfs4_set_sequence_privileged(&args.seq_args);
6610 status = nfs4_call_sync_sequence(clnt, server, &msg,
6611 &args.seq_args, &res.seq_res);
6612 nfs_free_fhandle(res.fh);
6613 if (status)
6614 return status;
6615
6616 do_renew_lease(clp, now);
6617 return 0;
6618}
6619
6620#ifdef CONFIG_NFS_V4_1
6621
6622/*
6623 * This operation also signals the server that this client is
6624 * performing "lease moved" recovery. The server can stop asserting
6625 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6626 * this operation is identified in the SEQUENCE operation in this
6627 * compound.
6628 */
6629static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6630{
6631 struct nfs_server *server = NFS_SERVER(inode);
6632 struct rpc_clnt *clnt = server->client;
6633 struct nfs4_fsid_present_arg args = {
6634 .fh = NFS_FH(inode),
6635 };
6636 struct nfs4_fsid_present_res res = {
6637 };
6638 struct rpc_message msg = {
6639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6640 .rpc_argp = &args,
6641 .rpc_resp = &res,
6642 .rpc_cred = cred,
6643 };
6644 int status;
6645
6646 res.fh = nfs_alloc_fhandle();
6647 if (res.fh == NULL)
6648 return -ENOMEM;
6649
6650 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6651 nfs4_set_sequence_privileged(&args.seq_args);
6652 status = nfs4_call_sync_sequence(clnt, server, &msg,
6653 &args.seq_args, &res.seq_res);
6654 nfs_free_fhandle(res.fh);
6655 if (status == NFS4_OK &&
6656 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6657 status = -NFS4ERR_LEASE_MOVED;
6658 return status;
6659}
6660
6661#endif /* CONFIG_NFS_V4_1 */
6662
6663/**
6664 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6665 * @inode: inode on FSID to check
6666 * @cred: credential to use for this operation
6667 *
6668 * Server indicates whether the FSID is present, moved, or not
6669 * recognized. This operation is necessary to clear a LEASE_MOVED
6670 * condition for this client ID.
6671 *
6672 * Returns NFS4_OK if the FSID is present on this server,
6673 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6674 * NFS4ERR code if some error occurred on the server, or a
6675 * negative errno if a local failure occurred.
6676 */
6677int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6678{
6679 struct nfs_server *server = NFS_SERVER(inode);
6680 struct nfs_client *clp = server->nfs_client;
6681 const struct nfs4_mig_recovery_ops *ops =
6682 clp->cl_mvops->mig_recovery_ops;
6683 struct nfs4_exception exception = { };
6684 int status;
6685
6686 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6687 (unsigned long long)server->fsid.major,
6688 (unsigned long long)server->fsid.minor,
6689 clp->cl_hostname);
6690 nfs_display_fhandle(NFS_FH(inode), __func__);
6691
6692 do {
6693 status = ops->fsid_present(inode, cred);
6694 if (status != -NFS4ERR_DELAY)
6695 break;
6696 nfs4_handle_exception(server, status, &exception);
6697 } while (exception.retry);
6698 return status;
6699}
6700
Andy Adamson5ec16a82013-08-08 10:57:55 -04006701/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006702 * If 'use_integrity' is true and the state managment nfs_client
6703 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6704 * and the machine credential as per RFC3530bis and RFC5661 Security
6705 * Considerations sections. Otherwise, just use the user cred with the
6706 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006707 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006708static 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 +00006709{
6710 int status;
6711 struct nfs4_secinfo_arg args = {
6712 .dir_fh = NFS_FH(dir),
6713 .name = name,
6714 };
6715 struct nfs4_secinfo_res res = {
6716 .flavors = flavors,
6717 };
6718 struct rpc_message msg = {
6719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6720 .rpc_argp = &args,
6721 .rpc_resp = &res,
6722 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006723 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006724 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006725
6726 if (use_integrity) {
6727 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006728 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6729 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006730 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006731
6732 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006733
6734 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6735 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6736
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006737 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6738 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006739 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006740
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006741 if (cred)
6742 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006743
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006744 return status;
6745}
6746
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006747int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6748 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006749{
6750 struct nfs4_exception exception = { };
6751 int err;
6752 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006753 err = -NFS4ERR_WRONGSEC;
6754
6755 /* try to use integrity protection with machine cred */
6756 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6757 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6758
6759 /*
6760 * if unable to use integrity protection, or SECINFO with
6761 * integrity protection returns NFS4ERR_WRONGSEC (which is
6762 * disallowed by spec, but exists in deployed servers) use
6763 * the current filesystem's rpc_client and the user cred.
6764 */
6765 if (err == -NFS4ERR_WRONGSEC)
6766 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6767
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006768 trace_nfs4_secinfo(dir, name, err);
6769 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006770 &exception);
6771 } while (exception.retry);
6772 return err;
6773}
6774
Andy Adamson557134a2009-04-01 09:21:53 -04006775#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006776/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006777 * Check the exchange flags returned by the server for invalid flags, having
6778 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6779 * DS flags set.
6780 */
6781static int nfs4_check_cl_exchange_flags(u32 flags)
6782{
6783 if (flags & ~EXCHGID4_FLAG_MASK_R)
6784 goto out_inval;
6785 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6786 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6787 goto out_inval;
6788 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6789 goto out_inval;
6790 return NFS_OK;
6791out_inval:
6792 return -NFS4ERR_INVAL;
6793}
6794
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006795static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006796nfs41_same_server_scope(struct nfs41_server_scope *a,
6797 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006798{
6799 if (a->server_scope_sz == b->server_scope_sz &&
6800 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6801 return true;
6802
6803 return false;
6804}
6805
Andy Adamson02a95de2016-02-05 16:08:37 -05006806static void
6807nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6808{
6809}
6810
6811static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6812 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6813};
6814
Andy Adamson357f54d2010-12-14 10:11:57 -05006815/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006816 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006817 *
6818 * The 4.1 client currently uses the same TCP connection for the
6819 * fore and backchannel.
6820 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006821static
6822int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6823 struct rpc_xprt *xprt,
6824 struct nfs_client *clp,
6825 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006826{
6827 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006828 struct nfs41_bind_conn_to_session_args args = {
6829 .client = clp,
6830 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6831 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006832 struct nfs41_bind_conn_to_session_res res;
6833 struct rpc_message msg = {
6834 .rpc_proc =
6835 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006836 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006837 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006838 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006839 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006840 struct rpc_task_setup task_setup_data = {
6841 .rpc_client = clnt,
6842 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006843 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006844 .rpc_message = &msg,
6845 .flags = RPC_TASK_TIMEOUT,
6846 };
6847 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006848
6849 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006850
Trond Myklebust71a097c2015-02-18 09:27:18 -08006851 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6852 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6853 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006854
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006855 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6856 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6857 args.dir = NFS4_CDFC4_FORE;
6858
6859 task = rpc_run_task(&task_setup_data);
6860 if (!IS_ERR(task)) {
6861 status = task->tk_status;
6862 rpc_put_task(task);
6863 } else
6864 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006865 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006866 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006867 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006868 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6869 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6870 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006871 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006872 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006873 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006874 dprintk("NFS: %s: Unexpected direction from server\n",
6875 __func__);
6876 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006877 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006878 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006879 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006880 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6881 __func__);
6882 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006883 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006884 }
6885 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006886out:
6887 dprintk("<-- %s status= %d\n", __func__, status);
6888 return status;
6889}
6890
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006891struct rpc_bind_conn_calldata {
6892 struct nfs_client *clp;
6893 struct rpc_cred *cred;
6894};
6895
6896static int
6897nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6898 struct rpc_xprt *xprt,
6899 void *calldata)
6900{
6901 struct rpc_bind_conn_calldata *p = calldata;
6902
6903 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6904}
6905
6906int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6907{
6908 struct rpc_bind_conn_calldata data = {
6909 .clp = clp,
6910 .cred = cred,
6911 };
6912 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6913 nfs4_proc_bind_conn_to_session_callback, &data);
6914}
6915
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006916/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006917 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6918 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006919 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006920static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6921 .how = SP4_MACH_CRED,
6922 .enforce.u.words = {
6923 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6924 1 << (OP_EXCHANGE_ID - 32) |
6925 1 << (OP_CREATE_SESSION - 32) |
6926 1 << (OP_DESTROY_SESSION - 32) |
6927 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006928 },
6929 .allow.u.words = {
6930 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006931 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006932 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006933 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006934 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006935 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006936 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006937 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006938 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006939 1 << (OP_FREE_STATEID - 32) |
6940 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006941 }
6942};
6943
6944/*
6945 * Select the state protection mode for client `clp' given the server results
6946 * from exchange_id in `sp'.
6947 *
6948 * Returns 0 on success, negative errno otherwise.
6949 */
6950static int nfs4_sp4_select_mode(struct nfs_client *clp,
6951 struct nfs41_state_protection *sp)
6952{
6953 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6954 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6955 1 << (OP_EXCHANGE_ID - 32) |
6956 1 << (OP_CREATE_SESSION - 32) |
6957 1 << (OP_DESTROY_SESSION - 32) |
6958 1 << (OP_DESTROY_CLIENTID - 32)
6959 };
6960 unsigned int i;
6961
6962 if (sp->how == SP4_MACH_CRED) {
6963 /* Print state protect result */
6964 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6965 for (i = 0; i <= LAST_NFS4_OP; i++) {
6966 if (test_bit(i, sp->enforce.u.longs))
6967 dfprintk(MOUNT, " enforce op %d\n", i);
6968 if (test_bit(i, sp->allow.u.longs))
6969 dfprintk(MOUNT, " allow op %d\n", i);
6970 }
6971
6972 /* make sure nothing is on enforce list that isn't supported */
6973 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6974 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6975 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6976 return -EINVAL;
6977 }
6978 }
6979
6980 /*
6981 * Minimal mode - state operations are allowed to use machine
6982 * credential. Note this already happens by default, so the
6983 * client doesn't have to do anything more than the negotiation.
6984 *
6985 * NOTE: we don't care if EXCHANGE_ID is in the list -
6986 * we're already using the machine cred for exchange_id
6987 * and will never use a different cred.
6988 */
6989 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6990 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6991 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6992 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6993 dfprintk(MOUNT, "sp4_mach_cred:\n");
6994 dfprintk(MOUNT, " minimal mode enabled\n");
6995 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
6996 } else {
6997 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6998 return -EINVAL;
6999 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007000
7001 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007002 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7003 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007004 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7005 dfprintk(MOUNT, " cleanup mode enabled\n");
7006 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7007 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007008
Andrew Elble99ade3c2015-12-02 09:39:51 -05007009 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7010 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7011 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7012 &clp->cl_sp4_flags);
7013 }
7014
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007015 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7016 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7017 dfprintk(MOUNT, " secinfo mode enabled\n");
7018 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7019 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007020
7021 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7022 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7023 dfprintk(MOUNT, " stateid mode enabled\n");
7024 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7025 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007026
7027 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7028 dfprintk(MOUNT, " write mode enabled\n");
7029 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7030 }
7031
7032 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7033 dfprintk(MOUNT, " commit mode enabled\n");
7034 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7035 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007036 }
7037
7038 return 0;
7039}
7040
7041/*
7042 * _nfs4_proc_exchange_id()
7043 *
7044 * Wrapper for EXCHANGE_ID operation.
7045 */
7046static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7047 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007048{
7049 nfs4_verifier verifier;
7050 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007051 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007052 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007053#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007054 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007055 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7056 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7057#else
7058 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7059 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7060#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007061 };
7062 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007063 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007064 };
7065 int status;
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7068 .rpc_argp = &args,
7069 .rpc_resp = &res,
7070 .rpc_cred = cred,
7071 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007072
Chuck Leverf0920752012-05-21 22:45:41 -04007073 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007074
7075 status = nfs4_init_uniform_client_string(clp);
7076 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007077 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007078
7079 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007080 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007081 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007082
Chuck Leveracdeb692012-05-21 22:46:16 -04007083 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007084 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007085 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007086 status = -ENOMEM;
7087 goto out;
7088 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007089
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007090 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007091 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007092 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007093 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007094 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007095 }
7096
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007097 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007098 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007099 status = -ENOMEM;
7100 goto out_server_scope;
7101 }
7102
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007103 switch (sp4_how) {
7104 case SP4_NONE:
7105 args.state_protect.how = SP4_NONE;
7106 break;
7107
7108 case SP4_MACH_CRED:
7109 args.state_protect = nfs4_sp4_mach_cred_request;
7110 break;
7111
7112 default:
7113 /* unsupported! */
7114 WARN_ON_ONCE(1);
7115 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007116 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007117 }
7118
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007119 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007120 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007121 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007122 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007123
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007124 if (status == 0)
7125 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7126
Chuck Lever177313f2012-05-21 22:44:58 -04007127 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007128 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007129 clp->cl_exchange_flags = res.flags;
7130 /* Client ID is not confirmed */
7131 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7132 clear_bit(NFS4_SESSION_ESTABLISHED,
7133 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007134 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007135 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007136
Chuck Leveracdeb692012-05-21 22:46:16 -04007137 kfree(clp->cl_serverowner);
7138 clp->cl_serverowner = res.server_owner;
7139 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007140
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007141 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007142 kfree(clp->cl_implid);
7143 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007144 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007145
Chuck Lever177313f2012-05-21 22:44:58 -04007146 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007147 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007148 res.server_scope)) {
7149 dprintk("%s: server_scope mismatch detected\n",
7150 __func__);
7151 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007152 kfree(clp->cl_serverscope);
7153 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007154 }
7155
Chuck Lever177313f2012-05-21 22:44:58 -04007156 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007157 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007158 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007159 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007160 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007161
Kinglong Mee6b559702015-07-01 11:54:53 +08007162out_impl_id:
7163 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007164out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007165 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007166out_server_owner:
7167 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007168out:
Chuck Lever177313f2012-05-21 22:44:58 -04007169 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007170 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007171 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007172 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007173 clp->cl_implid->date.seconds,
7174 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007175 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007176 return status;
7177}
7178
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007179/*
7180 * nfs4_proc_exchange_id()
7181 *
7182 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7183 *
7184 * Since the clientid has expired, all compounds using sessions
7185 * associated with the stale clientid will be returning
7186 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7187 * be in some phase of session reset.
7188 *
7189 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7190 */
7191int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7192{
7193 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7194 int status;
7195
7196 /* try SP4_MACH_CRED if krb5i/p */
7197 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7198 authflavor == RPC_AUTH_GSS_KRB5P) {
7199 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7200 if (!status)
7201 return 0;
7202 }
7203
7204 /* try SP4_NONE */
7205 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7206}
7207
Trond Myklebust66245532012-05-25 17:18:09 -04007208static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7209 struct rpc_cred *cred)
7210{
7211 struct rpc_message msg = {
7212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7213 .rpc_argp = clp,
7214 .rpc_cred = cred,
7215 };
7216 int status;
7217
7218 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007219 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007220 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007221 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007222 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7223 return status;
7224}
7225
7226static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7227 struct rpc_cred *cred)
7228{
7229 unsigned int loop;
7230 int ret;
7231
7232 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7233 ret = _nfs4_proc_destroy_clientid(clp, cred);
7234 switch (ret) {
7235 case -NFS4ERR_DELAY:
7236 case -NFS4ERR_CLIENTID_BUSY:
7237 ssleep(1);
7238 break;
7239 default:
7240 return ret;
7241 }
7242 }
7243 return 0;
7244}
7245
7246int nfs4_destroy_clientid(struct nfs_client *clp)
7247{
7248 struct rpc_cred *cred;
7249 int ret = 0;
7250
7251 if (clp->cl_mvops->minor_version < 1)
7252 goto out;
7253 if (clp->cl_exchange_flags == 0)
7254 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007255 if (clp->cl_preserve_clid)
7256 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007257 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007258 ret = nfs4_proc_destroy_clientid(clp, cred);
7259 if (cred)
7260 put_rpccred(cred);
7261 switch (ret) {
7262 case 0:
7263 case -NFS4ERR_STALE_CLIENTID:
7264 clp->cl_exchange_flags = 0;
7265 }
7266out:
7267 return ret;
7268}
7269
Andy Adamson2050f0c2009-04-01 09:22:30 -04007270struct nfs4_get_lease_time_data {
7271 struct nfs4_get_lease_time_args *args;
7272 struct nfs4_get_lease_time_res *res;
7273 struct nfs_client *clp;
7274};
7275
7276static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7277 void *calldata)
7278{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007279 struct nfs4_get_lease_time_data *data =
7280 (struct nfs4_get_lease_time_data *)calldata;
7281
7282 dprintk("--> %s\n", __func__);
7283 /* just setup sequence, do not trigger session recovery
7284 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007285 nfs41_setup_sequence(data->clp->cl_session,
7286 &data->args->la_seq_args,
7287 &data->res->lr_seq_res,
7288 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007289 dprintk("<-- %s\n", __func__);
7290}
7291
7292/*
7293 * Called from nfs4_state_manager thread for session setup, so don't recover
7294 * from sequence operation or clientid errors.
7295 */
7296static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7297{
7298 struct nfs4_get_lease_time_data *data =
7299 (struct nfs4_get_lease_time_data *)calldata;
7300
7301 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007302 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7303 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007304 switch (task->tk_status) {
7305 case -NFS4ERR_DELAY:
7306 case -NFS4ERR_GRACE:
7307 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7308 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7309 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007310 /* fall through */
7311 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007312 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007313 return;
7314 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007315 dprintk("<-- %s\n", __func__);
7316}
7317
Trond Myklebust17280172012-03-11 13:11:00 -04007318static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007319 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7320 .rpc_call_done = nfs4_get_lease_time_done,
7321};
7322
7323int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7324{
7325 struct rpc_task *task;
7326 struct nfs4_get_lease_time_args args;
7327 struct nfs4_get_lease_time_res res = {
7328 .lr_fsinfo = fsinfo,
7329 };
7330 struct nfs4_get_lease_time_data data = {
7331 .args = &args,
7332 .res = &res,
7333 .clp = clp,
7334 };
7335 struct rpc_message msg = {
7336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7337 .rpc_argp = &args,
7338 .rpc_resp = &res,
7339 };
7340 struct rpc_task_setup task_setup = {
7341 .rpc_client = clp->cl_rpcclient,
7342 .rpc_message = &msg,
7343 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007344 .callback_data = &data,
7345 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007346 };
7347 int status;
7348
Chuck Levera9c92d62013-08-09 12:48:18 -04007349 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007350 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007351 dprintk("--> %s\n", __func__);
7352 task = rpc_run_task(&task_setup);
7353
7354 if (IS_ERR(task))
7355 status = PTR_ERR(task);
7356 else {
7357 status = task->tk_status;
7358 rpc_put_task(task);
7359 }
7360 dprintk("<-- %s return %d\n", __func__, status);
7361
7362 return status;
7363}
7364
Andy Adamsonfc931582009-04-01 09:22:31 -04007365/*
7366 * Initialize the values to be used by the client in CREATE_SESSION
7367 * If nfs4_init_session set the fore channel request and response sizes,
7368 * use them.
7369 *
7370 * Set the back channel max_resp_sz_cached to zero to force the client to
7371 * always set csa_cachethis to FALSE because the current implementation
7372 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7373 */
7374static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
7375{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007376 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007377
Andy Adamson18aad3d2013-06-26 12:21:49 -04007378 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7379 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7380
Andy Adamsonfc931582009-04-01 09:22:31 -04007381 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007382 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7383 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007384 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007385 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007386
7387 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007388 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007389 __func__,
7390 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007391 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007392
7393 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04007394 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
7395 args->bc_attrs.max_resp_sz = PAGE_SIZE;
7396 args->bc_attrs.max_resp_sz_cached = 0;
7397 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebustf4f58ed2016-01-23 14:12:52 -05007398 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
Andy Adamsonfc931582009-04-01 09:22:31 -04007399
7400 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7401 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7402 __func__,
7403 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7404 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7405 args->bc_attrs.max_reqs);
7406}
7407
Trond Myklebust79969dd2015-02-18 11:30:18 -08007408static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7409 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007410{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007411 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007412 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007413
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007414 if (rcvd->max_resp_sz > sent->max_resp_sz)
7415 return -EINVAL;
7416 /*
7417 * Our requested max_ops is the minimum we need; we're not
7418 * prepared to break up compounds into smaller pieces than that.
7419 * So, no point even trying to continue if the server won't
7420 * cooperate:
7421 */
7422 if (rcvd->max_ops < sent->max_ops)
7423 return -EINVAL;
7424 if (rcvd->max_reqs == 0)
7425 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007426 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7427 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007428 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007429}
7430
Trond Myklebust79969dd2015-02-18 11:30:18 -08007431static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7432 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007433{
7434 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007435 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007436
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007437 if (!(res->flags & SESSION4_BACK_CHAN))
7438 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007439 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7440 return -EINVAL;
7441 if (rcvd->max_resp_sz < sent->max_resp_sz)
7442 return -EINVAL;
7443 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7444 return -EINVAL;
7445 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007446 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007447 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007448 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007449 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007450out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007451 return 0;
7452}
Andy Adamson8d353012009-04-01 09:22:32 -04007453
Andy Adamson8d353012009-04-01 09:22:32 -04007454static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007455 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007456{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007457 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007458
Trond Myklebust79969dd2015-02-18 11:30:18 -08007459 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007460 if (ret)
7461 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007462 return nfs4_verify_back_channel_attrs(args, res);
7463}
7464
7465static void nfs4_update_session(struct nfs4_session *session,
7466 struct nfs41_create_session_res *res)
7467{
7468 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007469 /* Mark client id and session as being confirmed */
7470 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7471 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007472 session->flags = res->flags;
7473 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007474 if (res->flags & SESSION4_BACK_CHAN)
7475 memcpy(&session->bc_attrs, &res->bc_attrs,
7476 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007477}
7478
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007479static int _nfs4_proc_create_session(struct nfs_client *clp,
7480 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007481{
7482 struct nfs4_session *session = clp->cl_session;
7483 struct nfs41_create_session_args args = {
7484 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007485 .clientid = clp->cl_clientid,
7486 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007487 .cb_program = NFS4_CALLBACK,
7488 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007489 struct nfs41_create_session_res res;
7490
Andy Adamsonfc931582009-04-01 09:22:31 -04007491 struct rpc_message msg = {
7492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7493 .rpc_argp = &args,
7494 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007495 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007496 };
7497 int status;
7498
7499 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04007500 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007501
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007502 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007503 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007504
Trond Myklebust43095d32012-11-20 11:13:12 -05007505 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007506 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007507 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007508 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007509 if (clp->cl_seqid == res.seqid)
7510 clp->cl_seqid++;
7511 if (status)
7512 goto out;
7513 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007514 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007515out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007516 return status;
7517}
7518
7519/*
7520 * Issues a CREATE_SESSION operation to the server.
7521 * It is the responsibility of the caller to verify the session is
7522 * expired before calling this routine.
7523 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007524int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007525{
7526 int status;
7527 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007528 struct nfs4_session *session = clp->cl_session;
7529
7530 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7531
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007532 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007533 if (status)
7534 goto out;
7535
Andy Adamsonaacd5532011-11-09 13:58:21 -05007536 /* Init or reset the session slot tables */
7537 status = nfs4_setup_session_slot_tables(session);
7538 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007539 if (status)
7540 goto out;
7541
7542 ptr = (unsigned *)&session->sess_id.data[0];
7543 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7544 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007545out:
7546 dprintk("<-- %s\n", __func__);
7547 return status;
7548}
7549
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007550/*
7551 * Issue the over-the-wire RPC DESTROY_SESSION.
7552 * The caller must serialize access to this routine.
7553 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007554int nfs4_proc_destroy_session(struct nfs4_session *session,
7555 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007556{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007557 struct rpc_message msg = {
7558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7559 .rpc_argp = session,
7560 .rpc_cred = cred,
7561 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007562 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007563
7564 dprintk("--> nfs4_proc_destroy_session\n");
7565
7566 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007567 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7568 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007569
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007570 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007571 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007572
7573 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007574 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007575 "Session has been destroyed regardless...\n", status);
7576
7577 dprintk("<-- nfs4_proc_destroy_session\n");
7578 return status;
7579}
7580
Trond Myklebust7b38c362012-05-23 13:23:31 -04007581/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007582 * Renew the cl_session lease.
7583 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007584struct nfs4_sequence_data {
7585 struct nfs_client *clp;
7586 struct nfs4_sequence_args args;
7587 struct nfs4_sequence_res res;
7588};
7589
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007590static void nfs41_sequence_release(void *data)
7591{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007592 struct nfs4_sequence_data *calldata = data;
7593 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007594
Alexandros Batsakis71358402010-02-05 03:45:05 -08007595 if (atomic_read(&clp->cl_count) > 1)
7596 nfs4_schedule_state_renewal(clp);
7597 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007598 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007599}
7600
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007601static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7602{
7603 switch(task->tk_status) {
7604 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007605 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7606 return -EAGAIN;
7607 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007608 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007609 }
7610 return 0;
7611}
7612
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007613static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007614{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007615 struct nfs4_sequence_data *calldata = data;
7616 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007617
Trond Myklebust14516c32010-07-31 14:29:06 -04007618 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7619 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007620
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007621 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007622 if (task->tk_status < 0) {
7623 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007624 if (atomic_read(&clp->cl_count) == 1)
7625 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007626
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007627 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7628 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007629 return;
7630 }
7631 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007632 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007633out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007634 dprintk("<-- %s\n", __func__);
7635}
7636
7637static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7638{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007639 struct nfs4_sequence_data *calldata = data;
7640 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007641 struct nfs4_sequence_args *args;
7642 struct nfs4_sequence_res *res;
7643
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007644 args = task->tk_msg.rpc_argp;
7645 res = task->tk_msg.rpc_resp;
7646
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007647 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007648}
7649
7650static const struct rpc_call_ops nfs41_sequence_ops = {
7651 .rpc_call_done = nfs41_sequence_call_done,
7652 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007653 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007654};
7655
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007656static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7657 struct rpc_cred *cred,
7658 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007659{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007660 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007661 struct rpc_message msg = {
7662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7663 .rpc_cred = cred,
7664 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007665 struct rpc_task_setup task_setup_data = {
7666 .rpc_client = clp->cl_rpcclient,
7667 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007668 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007669 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007670 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007671
Alexandros Batsakis71358402010-02-05 03:45:05 -08007672 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007673 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007674 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007675 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007676 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007677 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007678 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007679 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007680 if (is_privileged)
7681 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007682 msg.rpc_argp = &calldata->args;
7683 msg.rpc_resp = &calldata->res;
7684 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007685 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007686
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007687 return rpc_run_task(&task_setup_data);
7688}
7689
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007690static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007691{
7692 struct rpc_task *task;
7693 int ret = 0;
7694
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007695 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007696 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007697 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007698 if (IS_ERR(task))
7699 ret = PTR_ERR(task);
7700 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007701 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007702 dprintk("<-- %s status=%d\n", __func__, ret);
7703 return ret;
7704}
7705
7706static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7707{
7708 struct rpc_task *task;
7709 int ret;
7710
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007711 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007712 if (IS_ERR(task)) {
7713 ret = PTR_ERR(task);
7714 goto out;
7715 }
7716 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007717 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007718 ret = task->tk_status;
7719 rpc_put_task(task);
7720out:
7721 dprintk("<-- %s status=%d\n", __func__, ret);
7722 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007723}
7724
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007725struct nfs4_reclaim_complete_data {
7726 struct nfs_client *clp;
7727 struct nfs41_reclaim_complete_args arg;
7728 struct nfs41_reclaim_complete_res res;
7729};
7730
7731static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7732{
7733 struct nfs4_reclaim_complete_data *calldata = data;
7734
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007735 nfs41_setup_sequence(calldata->clp->cl_session,
7736 &calldata->arg.seq_args,
7737 &calldata->res.seq_res,
7738 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007739}
7740
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007741static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7742{
7743 switch(task->tk_status) {
7744 case 0:
7745 case -NFS4ERR_COMPLETE_ALREADY:
7746 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7747 break;
7748 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007749 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007750 /* fall through */
7751 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007752 return -EAGAIN;
7753 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007754 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007755 }
7756 return 0;
7757}
7758
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007759static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7760{
7761 struct nfs4_reclaim_complete_data *calldata = data;
7762 struct nfs_client *clp = calldata->clp;
7763 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7764
7765 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007766 if (!nfs41_sequence_done(task, res))
7767 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007768
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007769 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007770 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7771 rpc_restart_call_prepare(task);
7772 return;
7773 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007774 dprintk("<-- %s\n", __func__);
7775}
7776
7777static void nfs4_free_reclaim_complete_data(void *data)
7778{
7779 struct nfs4_reclaim_complete_data *calldata = data;
7780
7781 kfree(calldata);
7782}
7783
7784static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7785 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7786 .rpc_call_done = nfs4_reclaim_complete_done,
7787 .rpc_release = nfs4_free_reclaim_complete_data,
7788};
7789
7790/*
7791 * Issue a global reclaim complete.
7792 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007793static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7794 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007795{
7796 struct nfs4_reclaim_complete_data *calldata;
7797 struct rpc_task *task;
7798 struct rpc_message msg = {
7799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007800 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007801 };
7802 struct rpc_task_setup task_setup_data = {
7803 .rpc_client = clp->cl_rpcclient,
7804 .rpc_message = &msg,
7805 .callback_ops = &nfs4_reclaim_complete_call_ops,
7806 .flags = RPC_TASK_ASYNC,
7807 };
7808 int status = -ENOMEM;
7809
7810 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007811 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007812 if (calldata == NULL)
7813 goto out;
7814 calldata->clp = clp;
7815 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007816
Chuck Levera9c92d62013-08-09 12:48:18 -04007817 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007818 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007819 msg.rpc_argp = &calldata->arg;
7820 msg.rpc_resp = &calldata->res;
7821 task_setup_data.callback_data = calldata;
7822 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007823 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007824 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007825 goto out;
7826 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007827 status = nfs4_wait_for_completion_rpc_task(task);
7828 if (status == 0)
7829 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007830 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007831 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007832out:
7833 dprintk("<-- %s status=%d\n", __func__, status);
7834 return status;
7835}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007836
7837static void
7838nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7839{
7840 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007841 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007842 struct nfs4_session *session = nfs4_get_session(server);
Trond Myklebustab7d7632015-12-28 10:32:11 -05007843 int ret;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007844
7845 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00007846 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
7847 * right now covering the LAYOUTGET we are about to send.
7848 * However, that is not so catastrophic, and there seems
7849 * to be no way to prevent it completely.
7850 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007851 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05007852 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007853 return;
Trond Myklebustab7d7632015-12-28 10:32:11 -05007854 ret = pnfs_choose_layoutget_stateid(&lgp->args.stateid,
Fred Isamancf7d63f2011-01-06 11:36:25 +00007855 NFS_I(lgp->args.inode)->layout,
Peng Taoce6ab4f2014-09-06 00:53:24 +08007856 &lgp->args.range,
Trond Myklebustab7d7632015-12-28 10:32:11 -05007857 lgp->args.ctx->state);
7858 if (ret < 0)
7859 rpc_exit(task, ret);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007860}
7861
7862static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7863{
7864 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07007865 struct inode *inode = lgp->args.inode;
7866 struct nfs_server *server = NFS_SERVER(inode);
7867 struct pnfs_layout_hdr *lo;
7868 struct nfs4_state *state = NULL;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007869 unsigned long timeo, now, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007870
Boaz Harroshed7e5422014-01-22 20:34:54 +02007871 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007872
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007873 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07007874 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007875
7876 switch (task->tk_status) {
7877 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007878 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007879
7880 /*
7881 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7882 * on the file. set tk_status to -ENODATA to tell upper layer to
7883 * retry go inband.
7884 */
7885 case -NFS4ERR_LAYOUTUNAVAILABLE:
7886 task->tk_status = -ENODATA;
7887 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007888 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007889 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7890 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7891 */
7892 case -NFS4ERR_BADLAYOUT:
7893 goto out_overflow;
7894 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007895 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007896 * (or clients) writing to the same RAID stripe except when
7897 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Boaz Harroshed7e5422014-01-22 20:34:54 +02007898 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007899 case -NFS4ERR_LAYOUTTRYLATER:
Trond Myklebust21b874c2015-08-31 01:19:22 -07007900 if (lgp->args.minlength == 0)
7901 goto out_overflow;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007902 /*
7903 * NFS4ERR_RECALLCONFLICT is when conflict with self (must recall
7904 * existing layout before getting a new one).
7905 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007906 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05007907 timeo = rpc_get_timeout(task->tk_client);
7908 giveup = lgp->args.timestamp + timeo;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007909 now = jiffies;
7910 if (time_after(giveup, now)) {
7911 unsigned long delay;
7912
7913 /* Delay for:
7914 * - Not less then NFS4_POLL_RETRY_MIN.
7915 * - One last time a jiffie before we give up
7916 * - exponential backoff (time_now minus start_attempt)
7917 */
7918 delay = max_t(unsigned long, NFS4_POLL_RETRY_MIN,
7919 min((giveup - now - 1),
7920 now - lgp->args.timestamp));
7921
7922 dprintk("%s: NFS4ERR_RECALLCONFLICT waiting %lu\n",
7923 __func__, delay);
7924 rpc_delay(task, delay);
Trond Myklebust2259f962015-09-20 13:30:30 -04007925 /* Do not call nfs4_async_handle_error() */
7926 goto out_restart;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007927 }
Trond Myklebustee314c22012-10-01 17:25:48 -07007928 break;
7929 case -NFS4ERR_EXPIRED:
7930 case -NFS4ERR_BAD_STATEID:
7931 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007932 if (nfs4_stateid_match(&lgp->args.stateid,
7933 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007934 spin_unlock(&inode->i_lock);
7935 /* If the open stateid was bad, then recover it. */
7936 state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007937 break;
7938 }
7939 lo = NFS_I(inode)->layout;
7940 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7941 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007942 LIST_HEAD(head);
7943
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007944 /*
7945 * Mark the bad layout state as invalid, then retry
7946 * with the current stateid.
7947 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007948 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Trond Myklebustee314c22012-10-01 17:25:48 -07007949 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
7950 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007951 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007952 } else
7953 spin_unlock(&inode->i_lock);
7954 goto out_restart;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007955 }
Jeff Layton91ab4b42015-11-19 14:30:26 -05007956 if (nfs4_async_handle_error(task, server, state, &lgp->timeout) == -EAGAIN)
Trond Myklebust2259f962015-09-20 13:30:30 -04007957 goto out_restart;
Trond Myklebustee314c22012-10-01 17:25:48 -07007958out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007959 dprintk("<-- %s\n", __func__);
Trond Myklebust21b874c2015-08-31 01:19:22 -07007960 return;
Trond Myklebust2259f962015-09-20 13:30:30 -04007961out_restart:
7962 task->tk_status = 0;
7963 rpc_restart_call_prepare(task);
7964 return;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007965out_overflow:
7966 task->tk_status = -EOVERFLOW;
7967 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007968}
7969
Idan Kedar85541162012-08-02 11:47:10 +03007970static size_t max_response_pages(struct nfs_server *server)
7971{
7972 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7973 return nfs_page_array_len(0, max_resp_sz);
7974}
7975
7976static void nfs4_free_pages(struct page **pages, size_t size)
7977{
7978 int i;
7979
7980 if (!pages)
7981 return;
7982
7983 for (i = 0; i < size; i++) {
7984 if (!pages[i])
7985 break;
7986 __free_page(pages[i]);
7987 }
7988 kfree(pages);
7989}
7990
7991static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7992{
7993 struct page **pages;
7994 int i;
7995
7996 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7997 if (!pages) {
7998 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7999 return NULL;
8000 }
8001
8002 for (i = 0; i < size; i++) {
8003 pages[i] = alloc_page(gfp_flags);
8004 if (!pages[i]) {
8005 dprintk("%s: failed to allocate page\n", __func__);
8006 nfs4_free_pages(pages, size);
8007 return NULL;
8008 }
8009 }
8010
8011 return pages;
8012}
8013
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008014static void nfs4_layoutget_release(void *calldata)
8015{
8016 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008017 struct inode *inode = lgp->args.inode;
8018 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008019 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008020
8021 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008022 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008023 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008024 put_nfs_open_context(lgp->args.ctx);
8025 kfree(calldata);
8026 dprintk("<-- %s\n", __func__);
8027}
8028
8029static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8030 .rpc_call_prepare = nfs4_layoutget_prepare,
8031 .rpc_call_done = nfs4_layoutget_done,
8032 .rpc_release = nfs4_layoutget_release,
8033};
8034
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008035struct pnfs_layout_segment *
8036nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008037{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008038 struct inode *inode = lgp->args.inode;
8039 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008040 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008041 struct rpc_task *task;
8042 struct rpc_message msg = {
8043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8044 .rpc_argp = &lgp->args,
8045 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008046 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008047 };
8048 struct rpc_task_setup task_setup_data = {
8049 .rpc_client = server->client,
8050 .rpc_message = &msg,
8051 .callback_ops = &nfs4_layoutget_call_ops,
8052 .callback_data = lgp,
8053 .flags = RPC_TASK_ASYNC,
8054 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008055 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008056 int status = 0;
8057
8058 dprintk("--> %s\n", __func__);
8059
Peng Tao4bd5a982014-11-17 11:05:17 +08008060 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8061 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8062
Idan Kedar85541162012-08-02 11:47:10 +03008063 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8064 if (!lgp->args.layout.pages) {
8065 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008066 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008067 }
8068 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05008069 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03008070
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008071 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008072 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008073 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008074
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008075 task = rpc_run_task(&task_setup_data);
8076 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008077 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008078 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00008079 if (status == 0)
8080 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008081 trace_nfs4_layoutget(lgp->args.ctx,
8082 &lgp->args.range,
8083 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008084 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008085 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008086 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8087 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008088 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008089 rpc_put_task(task);
8090 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008091 if (status)
8092 return ERR_PTR(status);
8093 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008094}
8095
Benny Halevycbe82602011-05-22 19:52:37 +03008096static void
8097nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8098{
8099 struct nfs4_layoutreturn *lrp = calldata;
8100
8101 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008102 nfs41_setup_sequence(lrp->clp->cl_session,
8103 &lrp->args.seq_args,
8104 &lrp->res.seq_res,
8105 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008106}
8107
8108static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8109{
8110 struct nfs4_layoutreturn *lrp = calldata;
8111 struct nfs_server *server;
8112
8113 dprintk("--> %s\n", __func__);
8114
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008115 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008116 return;
8117
8118 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008119 switch (task->tk_status) {
8120 default:
8121 task->tk_status = 0;
8122 case 0:
8123 break;
8124 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008125 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008126 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008127 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008128 return;
8129 }
Benny Halevycbe82602011-05-22 19:52:37 +03008130 dprintk("<-- %s\n", __func__);
8131}
8132
8133static void nfs4_layoutreturn_release(void *calldata)
8134{
8135 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008136 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008137 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008138
8139 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008140 spin_lock(&lo->plh_inode->i_lock);
Trond Myklebust0654cc72015-12-28 11:48:14 -05008141 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range);
8142 pnfs_mark_layout_returned_if_empty(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008143 if (lrp->res.lrs_present)
8144 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05008145 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008146 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008147 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008148 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008149 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008150 kfree(calldata);
8151 dprintk("<-- %s\n", __func__);
8152}
8153
8154static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8155 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8156 .rpc_call_done = nfs4_layoutreturn_done,
8157 .rpc_release = nfs4_layoutreturn_release,
8158};
8159
Peng Tao6c166052014-11-17 09:30:40 +08008160int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008161{
8162 struct rpc_task *task;
8163 struct rpc_message msg = {
8164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8165 .rpc_argp = &lrp->args,
8166 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008167 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008168 };
8169 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008170 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008171 .rpc_message = &msg,
8172 .callback_ops = &nfs4_layoutreturn_call_ops,
8173 .callback_data = lrp,
8174 };
Peng Tao6c166052014-11-17 09:30:40 +08008175 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008176
Andrew Elble99ade3c2015-12-02 09:39:51 -05008177 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8178 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8179 &task_setup_data.rpc_client, &msg);
8180
Benny Halevycbe82602011-05-22 19:52:37 +03008181 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008182 if (!sync) {
8183 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8184 if (!lrp->inode) {
8185 nfs4_layoutreturn_release(lrp);
8186 return -EAGAIN;
8187 }
8188 task_setup_data.flags |= RPC_TASK_ASYNC;
8189 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008190 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008191 task = rpc_run_task(&task_setup_data);
8192 if (IS_ERR(task))
8193 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008194 if (sync)
8195 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008196 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008197 dprintk("<-- %s status=%d\n", __func__, status);
8198 rpc_put_task(task);
8199 return status;
8200}
8201
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008202static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008203_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8204 struct pnfs_device *pdev,
8205 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008206{
8207 struct nfs4_getdeviceinfo_args args = {
8208 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008209 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8210 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008211 };
8212 struct nfs4_getdeviceinfo_res res = {
8213 .pdev = pdev,
8214 };
8215 struct rpc_message msg = {
8216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8217 .rpc_argp = &args,
8218 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008219 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008220 };
8221 int status;
8222
8223 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008224 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008225 if (res.notification & ~args.notify_types)
8226 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008227 if (res.notification != args.notify_types)
8228 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008229
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008230 dprintk("<-- %s status=%d\n", __func__, status);
8231
8232 return status;
8233}
8234
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008235int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8236 struct pnfs_device *pdev,
8237 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008238{
8239 struct nfs4_exception exception = { };
8240 int err;
8241
8242 do {
8243 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008244 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008245 &exception);
8246 } while (exception.retry);
8247 return err;
8248}
8249EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8250
Andy Adamson863a3c62011-03-23 13:27:54 +00008251static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8252{
8253 struct nfs4_layoutcommit_data *data = calldata;
8254 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008255 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008256
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008257 nfs41_setup_sequence(session,
8258 &data->args.seq_args,
8259 &data->res.seq_res,
8260 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008261}
8262
8263static void
8264nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8265{
8266 struct nfs4_layoutcommit_data *data = calldata;
8267 struct nfs_server *server = NFS_SERVER(data->args.inode);
8268
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008269 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008270 return;
8271
8272 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008273 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8274 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8275 case -NFS4ERR_BADLAYOUT: /* no layout */
8276 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008277 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008278 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008279 break;
8280 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008281 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008282 rpc_restart_call_prepare(task);
8283 return;
8284 }
8285 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008286}
8287
8288static void nfs4_layoutcommit_release(void *calldata)
8289{
8290 struct nfs4_layoutcommit_data *data = calldata;
8291
Andy Adamsondb29c082011-07-30 20:52:38 -04008292 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008293 nfs_post_op_update_inode_force_wcc(data->args.inode,
8294 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008295 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008296 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008297 kfree(data);
8298}
8299
8300static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8301 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8302 .rpc_call_done = nfs4_layoutcommit_done,
8303 .rpc_release = nfs4_layoutcommit_release,
8304};
8305
8306int
Andy Adamsonef311532011-03-12 02:58:10 -05008307nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008308{
8309 struct rpc_message msg = {
8310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8311 .rpc_argp = &data->args,
8312 .rpc_resp = &data->res,
8313 .rpc_cred = data->cred,
8314 };
8315 struct rpc_task_setup task_setup_data = {
8316 .task = &data->task,
8317 .rpc_client = NFS_CLIENT(data->args.inode),
8318 .rpc_message = &msg,
8319 .callback_ops = &nfs4_layoutcommit_ops,
8320 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008321 };
8322 struct rpc_task *task;
8323 int status = 0;
8324
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008325 dprintk("NFS: initiating layoutcommit call. sync %d "
8326 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008327 data->args.lastbytewritten,
8328 data->args.inode->i_ino);
8329
Trond Myklebust472e2592015-02-05 16:50:30 -05008330 if (!sync) {
8331 data->inode = nfs_igrab_and_active(data->args.inode);
8332 if (data->inode == NULL) {
8333 nfs4_layoutcommit_release(data);
8334 return -EAGAIN;
8335 }
8336 task_setup_data.flags = RPC_TASK_ASYNC;
8337 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008338 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008339 task = rpc_run_task(&task_setup_data);
8340 if (IS_ERR(task))
8341 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008342 if (sync)
8343 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008344 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008345 dprintk("%s: status %d\n", __func__, status);
8346 rpc_put_task(task);
8347 return status;
8348}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008349
Andy Adamson97431202013-08-08 10:57:56 -04008350/**
8351 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8352 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8353 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008354static int
8355_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008356 struct nfs_fsinfo *info,
8357 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008358{
8359 struct nfs41_secinfo_no_name_args args = {
8360 .style = SECINFO_STYLE_CURRENT_FH,
8361 };
8362 struct nfs4_secinfo_res res = {
8363 .flavors = flavors,
8364 };
8365 struct rpc_message msg = {
8366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8367 .rpc_argp = &args,
8368 .rpc_resp = &res,
8369 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008370 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008371 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008372 int status;
8373
8374 if (use_integrity) {
8375 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008376 cred = nfs4_get_clid_cred(server->nfs_client);
8377 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008378 }
8379
8380 dprintk("--> %s\n", __func__);
8381 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8382 &res.seq_res, 0);
8383 dprintk("<-- %s status=%d\n", __func__, status);
8384
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008385 if (cred)
8386 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008387
8388 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008389}
8390
8391static int
8392nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8393 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8394{
8395 struct nfs4_exception exception = { };
8396 int err;
8397 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008398 /* first try using integrity protection */
8399 err = -NFS4ERR_WRONGSEC;
8400
8401 /* try to use integrity protection with machine cred */
8402 if (_nfs4_is_integrity_protected(server->nfs_client))
8403 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8404 flavors, true);
8405
8406 /*
8407 * if unable to use integrity protection, or SECINFO with
8408 * integrity protection returns NFS4ERR_WRONGSEC (which is
8409 * disallowed by spec, but exists in deployed servers) use
8410 * the current filesystem's rpc_client and the user cred.
8411 */
8412 if (err == -NFS4ERR_WRONGSEC)
8413 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8414 flavors, false);
8415
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008416 switch (err) {
8417 case 0:
8418 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008419 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008420 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008421 default:
8422 err = nfs4_handle_exception(server, err, &exception);
8423 }
8424 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008425out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008426 return err;
8427}
8428
8429static int
8430nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8431 struct nfs_fsinfo *info)
8432{
8433 int err;
8434 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008435 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008436 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008437 struct nfs4_secinfo4 *secinfo;
8438 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008439
8440 page = alloc_page(GFP_KERNEL);
8441 if (!page) {
8442 err = -ENOMEM;
8443 goto out;
8444 }
8445
8446 flavors = page_address(page);
8447 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8448
8449 /*
8450 * Fall back on "guess and check" method if
8451 * the server doesn't support SECINFO_NO_NAME
8452 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008453 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008454 err = nfs4_find_root_sec(server, fhandle, info);
8455 goto out_freepage;
8456 }
8457 if (err)
8458 goto out_freepage;
8459
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008460 for (i = 0; i < flavors->num_flavors; i++) {
8461 secinfo = &flavors->flavors[i];
8462
8463 switch (secinfo->flavor) {
8464 case RPC_AUTH_NULL:
8465 case RPC_AUTH_UNIX:
8466 case RPC_AUTH_GSS:
8467 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8468 &secinfo->flavor_info);
8469 break;
8470 default:
8471 flavor = RPC_AUTH_MAXFLAVOR;
8472 break;
8473 }
8474
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008475 if (!nfs_auth_info_match(&server->auth_info, flavor))
8476 flavor = RPC_AUTH_MAXFLAVOR;
8477
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008478 if (flavor != RPC_AUTH_MAXFLAVOR) {
8479 err = nfs4_lookup_root_sec(server, fhandle,
8480 info, flavor);
8481 if (!err)
8482 break;
8483 }
8484 }
8485
8486 if (flavor == RPC_AUTH_MAXFLAVOR)
8487 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008488
8489out_freepage:
8490 put_page(page);
8491 if (err == -EACCES)
8492 return -EPERM;
8493out:
8494 return err;
8495}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008496
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008497static int _nfs41_test_stateid(struct nfs_server *server,
8498 nfs4_stateid *stateid,
8499 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008500{
8501 int status;
8502 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008503 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008504 };
8505 struct nfs41_test_stateid_res res;
8506 struct rpc_message msg = {
8507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8508 .rpc_argp = &args,
8509 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008510 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008511 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008512 struct rpc_clnt *rpc_client = server->client;
8513
8514 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8515 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008516
Chuck Lever38527b12012-07-11 16:30:23 -04008517 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008518 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008519 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008520 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008521 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008522 if (status != NFS_OK) {
8523 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008524 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008525 }
8526 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008527 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008528}
8529
Chuck Lever38527b12012-07-11 16:30:23 -04008530/**
8531 * nfs41_test_stateid - perform a TEST_STATEID operation
8532 *
8533 * @server: server / transport on which to perform the operation
8534 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008535 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008536 *
8537 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8538 * Otherwise a negative NFS4ERR value is returned if the operation
8539 * failed or the state ID is not currently valid.
8540 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008541static int nfs41_test_stateid(struct nfs_server *server,
8542 nfs4_stateid *stateid,
8543 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008544{
8545 struct nfs4_exception exception = { };
8546 int err;
8547 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008548 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008549 if (err != -NFS4ERR_DELAY)
8550 break;
8551 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008552 } while (exception.retry);
8553 return err;
8554}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008555
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008556struct nfs_free_stateid_data {
8557 struct nfs_server *server;
8558 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008559 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008560};
8561
8562static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8563{
8564 struct nfs_free_stateid_data *data = calldata;
8565 nfs41_setup_sequence(nfs4_get_session(data->server),
8566 &data->args.seq_args,
8567 &data->res.seq_res,
8568 task);
8569}
8570
8571static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8572{
8573 struct nfs_free_stateid_data *data = calldata;
8574
8575 nfs41_sequence_done(task, &data->res.seq_res);
8576
8577 switch (task->tk_status) {
8578 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008579 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008580 rpc_restart_call_prepare(task);
8581 }
8582}
8583
8584static void nfs41_free_stateid_release(void *calldata)
8585{
8586 kfree(calldata);
8587}
8588
Trond Myklebust17f26b12013-08-21 15:48:42 -04008589static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008590 .rpc_call_prepare = nfs41_free_stateid_prepare,
8591 .rpc_call_done = nfs41_free_stateid_done,
8592 .rpc_release = nfs41_free_stateid_release,
8593};
8594
8595static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8596 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008597 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008598 bool privileged)
8599{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008600 struct rpc_message msg = {
8601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008602 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008603 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008604 struct rpc_task_setup task_setup = {
8605 .rpc_client = server->client,
8606 .rpc_message = &msg,
8607 .callback_ops = &nfs41_free_stateid_ops,
8608 .flags = RPC_TASK_ASYNC,
8609 };
8610 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008611
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008612 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8613 &task_setup.rpc_client, &msg);
8614
Chuck Lever38527b12012-07-11 16:30:23 -04008615 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008616 data = kmalloc(sizeof(*data), GFP_NOFS);
8617 if (!data)
8618 return ERR_PTR(-ENOMEM);
8619 data->server = server;
8620 nfs4_stateid_copy(&data->args.stateid, stateid);
8621
8622 task_setup.callback_data = data;
8623
8624 msg.rpc_argp = &data->args;
8625 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008626 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008627 if (privileged)
8628 nfs4_set_sequence_privileged(&data->args.seq_args);
8629
8630 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008631}
8632
Chuck Lever38527b12012-07-11 16:30:23 -04008633/**
8634 * nfs41_free_stateid - perform a FREE_STATEID operation
8635 *
8636 * @server: server / transport on which to perform the operation
8637 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008638 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008639 *
8640 * Returns NFS_OK if the server freed "stateid". Otherwise a
8641 * negative NFS4ERR value is returned.
8642 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008643static int nfs41_free_stateid(struct nfs_server *server,
8644 nfs4_stateid *stateid,
8645 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008646{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008647 struct rpc_task *task;
8648 int ret;
8649
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008650 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008651 if (IS_ERR(task))
8652 return PTR_ERR(task);
8653 ret = rpc_wait_for_completion_task(task);
8654 if (!ret)
8655 ret = task->tk_status;
8656 rpc_put_task(task);
8657 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008658}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008659
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008660static void
8661nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008662{
8663 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008664 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008665
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008666 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008667 nfs4_free_lock_state(server, lsp);
8668 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008669 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008670 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008671}
8672
Trond Myklebust36281ca2012-03-04 18:13:56 -05008673static bool nfs41_match_stateid(const nfs4_stateid *s1,
8674 const nfs4_stateid *s2)
8675{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008676 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008677 return false;
8678
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008679 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008680 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008681 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008682 return true;
8683
8684 return false;
8685}
8686
Andy Adamson557134a2009-04-01 09:21:53 -04008687#endif /* CONFIG_NFS_V4_1 */
8688
Trond Myklebust36281ca2012-03-04 18:13:56 -05008689static bool nfs4_match_stateid(const nfs4_stateid *s1,
8690 const nfs4_stateid *s2)
8691{
Trond Myklebustf597c532012-03-04 18:13:56 -05008692 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008693}
8694
8695
Trond Myklebust17280172012-03-11 13:11:00 -04008696static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008697 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008698 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008699 .recover_open = nfs4_open_reclaim,
8700 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008701 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008702 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008703};
8704
Andy Adamson591d71c2009-04-01 09:22:47 -04008705#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008706static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008707 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8708 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8709 .recover_open = nfs4_open_reclaim,
8710 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008711 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008712 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008713 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008714};
8715#endif /* CONFIG_NFS_V4_1 */
8716
Trond Myklebust17280172012-03-11 13:11:00 -04008717static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008718 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008719 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008720 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008721 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008722 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008723};
8724
Andy Adamson591d71c2009-04-01 09:22:47 -04008725#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008726static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008727 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8728 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008729 .recover_open = nfs41_open_expired,
8730 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008731 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008732};
8733#endif /* CONFIG_NFS_V4_1 */
8734
Trond Myklebust17280172012-03-11 13:11:00 -04008735static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008736 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008737 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008738 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008739};
8740
8741#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008742static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008743 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008744 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008745 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008746};
8747#endif
8748
Chuck Leverec011fe2013-10-17 14:12:39 -04008749static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008750 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008751 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008752};
8753
8754#if defined(CONFIG_NFS_V4_1)
8755static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008756 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008757 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008758};
8759#endif /* CONFIG_NFS_V4_1 */
8760
Trond Myklebust97dc1352010-06-16 09:52:26 -04008761static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8762 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008763 .init_caps = NFS_CAP_READDIRPLUS
8764 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008765 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008766 .init_client = nfs40_init_client,
8767 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008768 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008769 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008770 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008771 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008772 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008773 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8774 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8775 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008776 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008777};
8778
8779#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008780static struct nfs_seqid *
8781nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8782{
8783 return NULL;
8784}
8785
Trond Myklebust97dc1352010-06-16 09:52:26 -04008786static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8787 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008788 .init_caps = NFS_CAP_READDIRPLUS
8789 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008790 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008791 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008792 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008793 .init_client = nfs41_init_client,
8794 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008795 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008796 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008797 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008798 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008799 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008800 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8801 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8802 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008803 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008804};
8805#endif
8806
Steve Dickson42c2c422013-05-22 12:50:38 -04008807#if defined(CONFIG_NFS_V4_2)
8808static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8809 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008810 .init_caps = NFS_CAP_READDIRPLUS
8811 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008812 | NFS_CAP_POSIX_LOCK
8813 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008814 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008815 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008816 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008817 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008818 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008819 | NFS_CAP_LAYOUTSTATS
8820 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008821 .init_client = nfs41_init_client,
8822 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008823 .match_stateid = nfs41_match_stateid,
8824 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008825 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008826 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008827 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008828 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8829 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8830 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008831 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008832};
8833#endif
8834
Trond Myklebust97dc1352010-06-16 09:52:26 -04008835const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8836 [0] = &nfs_v4_0_minor_ops,
8837#if defined(CONFIG_NFS_V4_1)
8838 [1] = &nfs_v4_1_minor_ops,
8839#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008840#if defined(CONFIG_NFS_V4_2)
8841 [2] = &nfs_v4_2_minor_ops,
8842#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008843};
8844
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008845ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
8846{
8847 ssize_t error, error2;
8848
8849 error = generic_listxattr(dentry, list, size);
8850 if (error < 0)
8851 return error;
8852 if (list) {
8853 list += error;
8854 size -= error;
8855 }
8856
8857 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8858 if (error2 < 0)
8859 return error2;
8860 return error + error2;
8861}
8862
Trond Myklebust17f26b12013-08-21 15:48:42 -04008863static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008864 .create = nfs_create,
8865 .lookup = nfs_lookup,
8866 .atomic_open = nfs_atomic_open,
8867 .link = nfs_link,
8868 .unlink = nfs_unlink,
8869 .symlink = nfs_symlink,
8870 .mkdir = nfs_mkdir,
8871 .rmdir = nfs_rmdir,
8872 .mknod = nfs_mknod,
8873 .rename = nfs_rename,
8874 .permission = nfs_permission,
8875 .getattr = nfs_getattr,
8876 .setattr = nfs_setattr,
8877 .getxattr = generic_getxattr,
8878 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008879 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008880 .removexattr = generic_removexattr,
8881};
8882
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008883static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008884 .permission = nfs_permission,
8885 .getattr = nfs_getattr,
8886 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008887 .getxattr = generic_getxattr,
8888 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008889 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008890 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008891};
8892
David Howells509de812006-08-22 20:06:11 -04008893const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008894 .version = 4, /* protocol version */
8895 .dentry_ops = &nfs4_dentry_operations,
8896 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008897 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008898 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008899 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008900 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008901 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008902 .getattr = nfs4_proc_getattr,
8903 .setattr = nfs4_proc_setattr,
8904 .lookup = nfs4_proc_lookup,
8905 .access = nfs4_proc_access,
8906 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008907 .create = nfs4_proc_create,
8908 .remove = nfs4_proc_remove,
8909 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008910 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008911 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008912 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008913 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008914 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008915 .link = nfs4_proc_link,
8916 .symlink = nfs4_proc_symlink,
8917 .mkdir = nfs4_proc_mkdir,
8918 .rmdir = nfs4_proc_remove,
8919 .readdir = nfs4_proc_readdir,
8920 .mknod = nfs4_proc_mknod,
8921 .statfs = nfs4_proc_statfs,
8922 .fsinfo = nfs4_proc_fsinfo,
8923 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008924 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008926 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008927 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008928 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008929 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008930 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008931 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008932 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008933 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008934 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008935 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008936 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008937 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008938 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008939 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008940 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008941 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008942 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008943 .create_server = nfs4_create_server,
8944 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008945};
8946
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008947static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01008948 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008949 .list = nfs4_xattr_list_nfs4_acl,
8950 .get = nfs4_xattr_get_nfs4_acl,
8951 .set = nfs4_xattr_set_nfs4_acl,
8952};
8953
8954const struct xattr_handler *nfs4_xattr_handlers[] = {
8955 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008956#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8957 &nfs4_xattr_nfs4_label_handler,
8958#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008959 NULL
8960};
8961
Linus Torvalds1da177e2005-04-16 15:20:36 -07008962/*
8963 * Local variables:
8964 * c-basic-offset: 8
8965 * End:
8966 */