blob: 27fe63b502d5348986b2b7f1a973076005deb8a1 [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:
Jeff Layton183d9e72016-05-17 12:28:47 -0400430 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400431 exception->delay = 1;
432 return 0;
433
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400434 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 case -NFS4ERR_OLD_STATEID:
436 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800437 break;
438 case -NFS4ERR_BADOWNER:
439 /* The following works around a Linux server bug! */
440 case -NFS4ERR_BADNAME:
441 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
442 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
443 exception->retry = 1;
444 printk(KERN_WARNING "NFS: v4 server %s "
445 "does not accept raw "
446 "uid/gids. "
447 "Reenabling the idmapper.\n",
448 server->nfs_client->cl_hostname);
449 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500450 }
451 /* We failed to handle the error */
452 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500453wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400454 exception->recovering = 1;
455 return 0;
456}
457
458/* This is the error handling routine for processes that are allowed
459 * to sleep.
460 */
461int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
462{
463 struct nfs_client *clp = server->nfs_client;
464 int ret;
465
466 ret = nfs4_do_handle_exception(server, errorcode, exception);
467 if (exception->delay) {
468 ret = nfs4_delay(server->client, &exception->timeout);
469 goto out_retry;
470 }
471 if (exception->recovering) {
472 ret = nfs4_wait_clnt_recover(clp);
473 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
474 return -EIO;
475 goto out_retry;
476 }
477 return ret;
478out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500479 if (ret == 0)
480 exception->retry = 1;
481 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500482}
483
Trond Myklebust037fc982015-09-20 15:51:00 -0400484static int
485nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
486 int errorcode, struct nfs4_exception *exception)
487{
488 struct nfs_client *clp = server->nfs_client;
489 int ret;
490
491 ret = nfs4_do_handle_exception(server, errorcode, exception);
492 if (exception->delay) {
493 rpc_delay(task, nfs4_update_delay(&exception->timeout));
494 goto out_retry;
495 }
496 if (exception->recovering) {
497 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
498 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
499 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
500 goto out_retry;
501 }
502 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
503 ret = -EIO;
504 return ret;
505out_retry:
506 if (ret == 0)
507 exception->retry = 1;
508 return ret;
509}
510
511static int
512nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
513 struct nfs4_state *state, long *timeout)
514{
515 struct nfs4_exception exception = {
516 .state = state,
517 };
518
519 if (task->tk_status >= 0)
520 return 0;
521 if (timeout)
522 exception.timeout = *timeout;
523 task->tk_status = nfs4_async_handle_exception(task, server,
524 task->tk_status,
525 &exception);
526 if (exception.delay && timeout)
527 *timeout = exception.timeout;
528 if (exception.retry)
529 return -EAGAIN;
530 return 0;
531}
532
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400533/*
534 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
535 * or 'false' otherwise.
536 */
537static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
538{
539 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
540
541 if (flavor == RPC_AUTH_GSS_KRB5I ||
542 flavor == RPC_AUTH_GSS_KRB5P)
543 return true;
544
545 return false;
546}
Trond Myklebust65de8722008-12-23 15:21:44 -0500547
Trond Myklebust452e9352010-07-31 14:29:06 -0400548static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 spin_lock(&clp->cl_lock);
551 if (time_before(clp->cl_last_renewal,timestamp))
552 clp->cl_last_renewal = timestamp;
553 spin_unlock(&clp->cl_lock);
554}
555
Trond Myklebust452e9352010-07-31 14:29:06 -0400556static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
557{
Trond Myklebustbe824162015-07-05 14:50:46 -0400558 struct nfs_client *clp = server->nfs_client;
559
560 if (!nfs4_has_session(clp))
561 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400562}
563
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400564struct nfs4_call_sync_data {
565 const struct nfs_server *seq_server;
566 struct nfs4_sequence_args *seq_args;
567 struct nfs4_sequence_res *seq_res;
568};
569
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800570void nfs4_init_sequence(struct nfs4_sequence_args *args,
571 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400572{
573 args->sa_slot = NULL;
574 args->sa_cache_this = cache_reply;
575 args->sa_privileged = 0;
576
577 res->sr_slot = NULL;
578}
579
580static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
581{
582 args->sa_privileged = 1;
583}
584
Peng Taocb04ad22014-06-11 05:24:15 +0800585int nfs40_setup_sequence(struct nfs4_slot_table *tbl,
586 struct nfs4_sequence_args *args,
587 struct nfs4_sequence_res *res,
588 struct rpc_task *task)
Chuck Lever3bd23842013-08-09 12:49:19 -0400589{
Chuck Lever3bd23842013-08-09 12:49:19 -0400590 struct nfs4_slot *slot;
591
592 /* slot already allocated? */
593 if (res->sr_slot != NULL)
594 goto out_start;
595
596 spin_lock(&tbl->slot_tbl_lock);
597 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
598 goto out_sleep;
599
600 slot = nfs4_alloc_slot(tbl);
601 if (IS_ERR(slot)) {
602 if (slot == ERR_PTR(-ENOMEM))
603 task->tk_timeout = HZ >> 2;
604 goto out_sleep;
605 }
606 spin_unlock(&tbl->slot_tbl_lock);
607
608 args->sa_slot = slot;
609 res->sr_slot = slot;
610
611out_start:
612 rpc_call_start(task);
613 return 0;
614
615out_sleep:
616 if (args->sa_privileged)
617 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
618 NULL, RPC_PRIORITY_PRIVILEGED);
619 else
620 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
621 spin_unlock(&tbl->slot_tbl_lock);
622 return -EAGAIN;
623}
Peng Taocb04ad22014-06-11 05:24:15 +0800624EXPORT_SYMBOL_GPL(nfs40_setup_sequence);
Chuck Lever3bd23842013-08-09 12:49:19 -0400625
626static int nfs40_sequence_done(struct rpc_task *task,
627 struct nfs4_sequence_res *res)
628{
629 struct nfs4_slot *slot = res->sr_slot;
630 struct nfs4_slot_table *tbl;
631
Trond Myklebustcab92c12014-01-29 12:12:15 -0500632 if (slot == NULL)
Chuck Lever3bd23842013-08-09 12:49:19 -0400633 goto out;
634
635 tbl = slot->table;
636 spin_lock(&tbl->slot_tbl_lock);
637 if (!nfs41_wake_and_assign_slot(tbl, slot))
638 nfs4_free_slot(tbl, slot);
639 spin_unlock(&tbl->slot_tbl_lock);
640
641 res->sr_slot = NULL;
642out:
643 return 1;
644}
645
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400646#if defined(CONFIG_NFS_V4_1)
647
Trond Myklebustd185a332010-06-16 09:52:25 -0400648static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400649{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500650 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400651 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500652 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500653 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400654
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500655 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500656 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500657
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500658 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500659 /* Be nice to the server: try to ensure that the last transmitted
660 * value for highest_user_slotid <= target_highest_slotid
661 */
662 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
663 send_new_highest_used_slotid = true;
664
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500665 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500666 send_new_highest_used_slotid = false;
667 goto out_unlock;
668 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500669 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500670
671 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
672 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500673out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500674 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400675 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500676 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400677 nfs41_notify_server(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400678}
679
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500680int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400681{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500682 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500683 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400684 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500685 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500686 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400687
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500688 if (slot == NULL)
689 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400690 /* don't increment the sequence number if the task wasn't sent */
691 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692 goto out;
693
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500694 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500695
Trond Myklebustac20d162012-12-15 15:36:07 -0500696 if (slot->interrupted) {
697 slot->interrupted = 0;
698 interrupted = true;
699 }
700
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400701 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500702 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400703 switch (res->sr_status) {
704 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400705 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500706 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500707 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500708 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500709 /* Check sequence flags */
Trond Myklebustb15c7cd2015-07-05 15:01:36 -0400710 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500711 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500713 case 1:
714 /*
715 * sr_status remains 1 if an RPC level error occurred.
716 * The server may or may not have processed the sequence
717 * operation..
718 * Mark the slot as having hosted an interrupted RPC call.
719 */
720 slot->interrupted = 1;
721 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400722 case -NFS4ERR_DELAY:
723 /* The server detected a resend of the RPC call and
724 * returned NFS4ERR_DELAY as per Section 2.10.6.2
725 * of RFC5661.
726 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500727 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400728 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500729 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500730 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400731 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500732 case -NFS4ERR_BADSLOT:
733 /*
734 * The slot id we used was probably retired. Try again
735 * using a different slot id.
736 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500737 goto retry_nowait;
738 case -NFS4ERR_SEQ_MISORDERED:
739 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500740 * Was the last operation on this sequence interrupted?
741 * If so, retry after bumping the sequence number.
742 */
743 if (interrupted) {
744 ++slot->seq_nr;
745 goto retry_nowait;
746 }
747 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500748 * Could this slot have been previously retired?
749 * If so, then the server may be expecting seq_nr = 1!
750 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500751 if (slot->seq_nr != 1) {
752 slot->seq_nr = 1;
753 goto retry_nowait;
754 }
755 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500756 case -NFS4ERR_SEQ_FALSE_RETRY:
757 ++slot->seq_nr;
758 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400759 default:
760 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500761 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400762 }
763out:
764 /* The session may be reset by one of the error handlers. */
765 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400766 nfs41_sequence_free_slot(res);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500767out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500768 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500769retry_nowait:
770 if (rpc_restart_call_prepare(task)) {
771 task->tk_status = 0;
772 ret = 0;
773 }
774 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400775out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400776 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 goto out;
778 rpc_delay(task, NFS4_POLL_RETRY_MAX);
779 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400780}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500781EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400782
Peng Tao2c4b1312014-06-11 05:24:16 +0800783int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400784{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500785 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400786 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400787 if (!res->sr_slot->table->session)
788 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400789 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400790}
Peng Tao2c4b1312014-06-11 05:24:16 +0800791EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400792
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000793int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400796 struct rpc_task *task)
797{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400798 struct nfs4_slot *slot;
799 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400800
801 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400802 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400803 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400804 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400805
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400806 tbl = &session->fc_slot_table;
807
Trond Myklebust69d206b2012-11-22 13:21:02 -0500808 task->tk_timeout = 0;
809
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400810 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400811 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400812 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500813 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500814 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400815 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400816 }
817
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500818 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500819 if (IS_ERR(slot)) {
820 /* If out of memory, try again in 1/4 second */
821 if (slot == ERR_PTR(-ENOMEM))
822 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400823 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400824 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400825 }
826 spin_unlock(&tbl->slot_tbl_lock);
827
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500828 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400829
Chuck Levere8d92382013-08-09 12:47:51 -0400830 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500831 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400832
Benny Halevydfb4f3092010-09-24 09:17:01 -0400833 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500834 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400835 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400836 /*
837 * sr_status is only set in decode_sequence, and so will remain
838 * set to 1 if an rpc level failure occurs.
839 */
840 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400841 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400842out_success:
843 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400845out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400846 /* Privileged tasks are queued with top priority */
847 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400848 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
849 NULL, RPC_PRIORITY_PRIVILEGED);
850 else
851 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400852 spin_unlock(&tbl->slot_tbl_lock);
853 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000855EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400856
Chuck Lever5a580e02013-08-09 12:48:09 -0400857static int nfs4_setup_sequence(const struct nfs_server *server,
858 struct nfs4_sequence_args *args,
859 struct nfs4_sequence_res *res,
860 struct rpc_task *task)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400861{
Trond Myklebust035168a2010-06-16 09:52:26 -0400862 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400863 int ret = 0;
864
Chuck Lever3bd23842013-08-09 12:49:19 -0400865 if (!session)
Peng Taocb04ad22014-06-11 05:24:15 +0800866 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
867 args, res, task);
Trond Myklebust035168a2010-06-16 09:52:26 -0400868
Chuck Levere8d92382013-08-09 12:47:51 -0400869 dprintk("--> %s clp %p session %p sr_slot %u\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400870 __func__, session->clp, session, res->sr_slot ?
Chuck Levere8d92382013-08-09 12:47:51 -0400871 res->sr_slot->slot_nr : NFS4_NO_SLOT);
Trond Myklebust035168a2010-06-16 09:52:26 -0400872
Trond Myklebust9d12b212012-01-17 22:04:25 -0500873 ret = nfs41_setup_sequence(session, args, res, task);
Chuck Lever3bd23842013-08-09 12:49:19 -0400874
Andy Adamsonce5039c2009-04-01 09:22:13 -0400875 dprintk("<-- %s status=%d\n", __func__, ret);
876 return ret;
877}
878
Andy Adamsonce5039c2009-04-01 09:22:13 -0400879static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
880{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400881 struct nfs4_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400882 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400883
Trond Myklebust035168a2010-06-16 09:52:26 -0400884 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
885
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400886 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887}
888
Andy Adamson69ab40c2009-04-01 09:22:19 -0400889static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
890{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400891 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400892
Trond Myklebust14516c32010-07-31 14:29:06 -0400893 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400894}
895
Trond Myklebust17280172012-03-11 13:11:00 -0400896static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400897 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400898 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400899};
900
Chuck Lever3bd23842013-08-09 12:49:19 -0400901#else /* !CONFIG_NFS_V4_1 */
902
Chuck Lever5a580e02013-08-09 12:48:09 -0400903static int nfs4_setup_sequence(const struct nfs_server *server,
904 struct nfs4_sequence_args *args,
905 struct nfs4_sequence_res *res,
906 struct rpc_task *task)
907{
Peng Taocb04ad22014-06-11 05:24:15 +0800908 return nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
909 args, res, task);
Chuck Lever5a580e02013-08-09 12:48:09 -0400910}
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400911
Peng Tao2c4b1312014-06-11 05:24:16 +0800912int nfs4_sequence_done(struct rpc_task *task,
913 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400914{
Chuck Lever3bd23842013-08-09 12:49:19 -0400915 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400916}
Peng Tao2c4b1312014-06-11 05:24:16 +0800917EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400918
919#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400920
Chuck Lever9915ea72013-08-09 12:48:27 -0400921static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
922{
923 struct nfs4_call_sync_data *data = calldata;
924 nfs4_setup_sequence(data->seq_server,
925 data->seq_args, data->seq_res, task);
926}
927
928static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
929{
930 struct nfs4_call_sync_data *data = calldata;
931 nfs4_sequence_done(task, data->seq_res);
932}
933
934static const struct rpc_call_ops nfs40_call_sync_ops = {
935 .rpc_call_prepare = nfs40_call_sync_prepare,
936 .rpc_call_done = nfs40_call_sync_done,
937};
938
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400939static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400940 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 struct nfs4_sequence_args *args,
943 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400944{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100945 int ret;
946 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400947 struct nfs_client *clp = server->nfs_client;
948 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949 .seq_server = server,
950 .seq_args = args,
951 .seq_res = res,
952 };
953 struct rpc_task_setup task_setup = {
954 .rpc_client = clnt,
955 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400956 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100957 .callback_data = &data
958 };
959
960 task = rpc_run_task(&task_setup);
961 if (IS_ERR(task))
962 ret = PTR_ERR(task);
963 else {
964 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400965 rpc_put_task(task);
966 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967 return ret;
968}
969
Bryan Schumaker7c513052011-03-24 17:12:24 +0000970int nfs4_call_sync(struct rpc_clnt *clnt,
971 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000972 struct rpc_message *msg,
973 struct nfs4_sequence_args *args,
974 struct nfs4_sequence_res *res,
975 int cache_reply)
976{
Chuck Levera9c92d62013-08-09 12:48:18 -0400977 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400978 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000979}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
982{
983 struct nfs_inode *nfsi = NFS_I(dir);
984
985 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400986 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700987 if (!cinfo->atomic || cinfo->before != dir->i_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700989 dir->i_version = cinfo->after;
Trond Myklebust3235b402015-02-26 19:52:06 -0500990 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +0000991 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 spin_unlock(&dir->i_lock);
993}
994
995struct nfs4_opendata {
996 struct kref kref;
997 struct nfs_openargs o_arg;
998 struct nfs_openres o_res;
999 struct nfs_open_confirmargs c_arg;
1000 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001001 struct nfs4_string owner_name;
1002 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001003 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001005 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001007 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 struct nfs4_state_owner *owner;
1009 struct nfs4_state *state;
1010 struct iattr attrs;
1011 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001012 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001013 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001014 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001015 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int cancelled;
1017};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001018
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001019static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1020 int err, struct nfs4_exception *exception)
1021{
1022 if (err != -EINVAL)
1023 return false;
1024 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1025 return false;
1026 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1027 exception->retry = 1;
1028 return true;
1029}
1030
Trond Myklebust6ae37332015-01-30 14:21:14 -05001031static u32
1032nfs4_map_atomic_open_share(struct nfs_server *server,
1033 fmode_t fmode, int openflags)
1034{
1035 u32 res = 0;
1036
1037 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1038 case FMODE_READ:
1039 res = NFS4_SHARE_ACCESS_READ;
1040 break;
1041 case FMODE_WRITE:
1042 res = NFS4_SHARE_ACCESS_WRITE;
1043 break;
1044 case FMODE_READ|FMODE_WRITE:
1045 res = NFS4_SHARE_ACCESS_BOTH;
1046 }
1047 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1048 goto out;
1049 /* Want no delegation if we're using O_DIRECT */
1050 if (openflags & O_DIRECT)
1051 res |= NFS4_SHARE_WANT_NO_DELEG;
1052out:
1053 return res;
1054}
1055
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001056static enum open_claim_type4
1057nfs4_map_atomic_open_claim(struct nfs_server *server,
1058 enum open_claim_type4 claim)
1059{
1060 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1061 return claim;
1062 switch (claim) {
1063 default:
1064 return claim;
1065 case NFS4_OPEN_CLAIM_FH:
1066 return NFS4_OPEN_CLAIM_NULL;
1067 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1068 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1069 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1070 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1071 }
1072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001075{
1076 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001077 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001078 p->o_res.seqid = p->o_arg.seqid;
1079 p->c_res.seqid = p->c_arg.seqid;
1080 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001081 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001082 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001083 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001084}
1085
Al Viro82a2c1b2011-06-22 18:30:55 -04001086static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001087 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001088 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001089 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001090 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001091 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001092{
Al Viro82a2c1b2011-06-22 18:30:55 -04001093 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001094 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001095 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001096 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001097 struct nfs4_opendata *p;
1098
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001099 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001100 if (p == NULL)
1101 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001102
1103 p->f_label = nfs4_label_alloc(server, gfp_mask);
1104 if (IS_ERR(p->f_label))
1105 goto err_free_p;
1106
Kinglong Meea49c2692015-07-27 15:31:38 +08001107 p->a_label = nfs4_label_alloc(server, gfp_mask);
1108 if (IS_ERR(p->a_label))
1109 goto err_free_f;
1110
Trond Myklebust63f5f792015-01-23 19:19:25 -05001111 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1112 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001113 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001114 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001115 nfs_sb_active(dentry->d_sb);
1116 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001117 p->dir = parent;
1118 p->owner = sp;
1119 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 p->o_arg.open_flags = flags;
1121 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001122 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1123 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001124 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1125 * will return permission denied for all bits until close */
1126 if (!(flags & O_EXCL)) {
1127 /* ask server to check for all possible rights as results
1128 * are cached */
1129 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
1130 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001131 }
David Howells7539bba2006-08-22 20:06:09 -04001132 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001133 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1134 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001135 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001136 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001137 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001138 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001139 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001140 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
1141 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001142 case NFS4_OPEN_CLAIM_NULL:
1143 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1144 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1145 p->o_arg.fh = NFS_FH(dir);
1146 break;
1147 case NFS4_OPEN_CLAIM_PREVIOUS:
1148 case NFS4_OPEN_CLAIM_FH:
1149 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1150 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001151 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001152 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001153 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001154 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001155
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 p->o_arg.u.attrs = &p->attrs;
1157 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001158
1159 verf[0] = jiffies;
1160 verf[1] = current->pid;
1161 memcpy(p->o_arg.u.verifier.data, verf,
1162 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001164 p->c_arg.fh = &p->o_res.fh;
1165 p->c_arg.stateid = &p->o_res.stateid;
1166 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001168 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001170
1171err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001172 nfs4_label_free(p->a_label);
1173err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001174 nfs4_label_free(p->f_label);
1175err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 kfree(p);
1177err:
1178 dput(parent);
1179 return NULL;
1180}
1181
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001182static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001183{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001184 struct nfs4_opendata *p = container_of(kref,
1185 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001186 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001187
1188 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001189 if (p->state != NULL)
1190 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001191 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001192
Kinglong Meea49c2692015-07-27 15:31:38 +08001193 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001194 nfs4_label_free(p->f_label);
1195
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001196 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001197 dput(p->dentry);
1198 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001199 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001200 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001201 kfree(p);
1202}
1203
1204static void nfs4_opendata_put(struct nfs4_opendata *p)
1205{
1206 if (p != NULL)
1207 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001208}
1209
Trond Myklebust06f814a2006-01-03 09:55:07 +01001210static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1211{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001212 int ret;
1213
Trond Myklebust06f814a2006-01-03 09:55:07 +01001214 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001215 return ret;
1216}
1217
Trond Myklebust24311f82015-09-20 10:50:17 -04001218static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1219 fmode_t fmode)
1220{
1221 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1222 case FMODE_READ|FMODE_WRITE:
1223 return state->n_rdwr != 0;
1224 case FMODE_WRITE:
1225 return state->n_wronly != 0;
1226 case FMODE_READ:
1227 return state->n_rdonly != 0;
1228 }
1229 WARN_ON_ONCE(1);
1230 return false;
1231}
1232
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001233static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001234{
1235 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001236
Trond Myklebust536e43d2012-01-17 22:04:26 -05001237 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001238 goto out;
1239 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001240 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001241 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1242 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001243 break;
1244 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001245 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1246 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001247 break;
1248 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001249 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1250 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001251 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001252out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001253 return ret;
1254}
1255
Trond Myklebust2a606182015-08-19 22:30:00 -05001256static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1257 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001258{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001259 if (delegation == NULL)
1260 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001261 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001262 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001263 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1264 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001265 switch (claim) {
1266 case NFS4_OPEN_CLAIM_NULL:
1267 case NFS4_OPEN_CLAIM_FH:
1268 break;
1269 case NFS4_OPEN_CLAIM_PREVIOUS:
1270 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1271 break;
1272 default:
1273 return 0;
1274 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001275 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001276 return 1;
1277}
1278
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001280{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001281 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001282 case FMODE_WRITE:
1283 state->n_wronly++;
1284 break;
1285 case FMODE_READ:
1286 state->n_rdonly++;
1287 break;
1288 case FMODE_READ|FMODE_WRITE:
1289 state->n_rdwr++;
1290 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001292}
1293
Trond Myklebust4f14c192014-02-12 19:15:06 -05001294static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001295{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001296 struct nfs_client *clp = state->owner->so_server->nfs_client;
1297 bool need_recover = false;
1298
1299 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1300 need_recover = true;
1301 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1302 need_recover = true;
1303 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1304 need_recover = true;
1305 if (need_recover)
1306 nfs4_state_mark_reclaim_nograce(clp, state);
1307}
1308
Trond Myklebuste999e802014-02-10 18:20:47 -05001309static bool nfs_need_update_open_stateid(struct nfs4_state *state,
1310 nfs4_stateid *stateid)
1311{
1312 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1313 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001314 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1315 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001316 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001317 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001318 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1319 return true;
1320 return false;
1321}
1322
Trond Myklebustf95549c2015-01-23 18:06:09 -05001323static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1324{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001325 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1326 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001327 if (state->n_wronly)
1328 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1329 if (state->n_rdonly)
1330 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1331 if (state->n_rdwr)
1332 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001333 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001334}
1335
Trond Myklebust226056c2014-02-11 10:41:07 -05001336static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001337 nfs4_stateid *arg_stateid,
Trond Myklebust226056c2014-02-11 10:41:07 -05001338 nfs4_stateid *stateid, fmode_t fmode)
1339{
1340 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1341 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1342 case FMODE_WRITE:
1343 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1344 break;
1345 case FMODE_READ:
1346 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1347 break;
1348 case 0:
1349 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1350 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1351 clear_bit(NFS_OPEN_STATE, &state->flags);
1352 }
1353 if (stateid == NULL)
1354 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001355 /* Handle races with OPEN */
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001356 if (!nfs4_stateid_match_other(arg_stateid, &state->open_stateid) ||
1357 (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1358 !nfs4_stateid_is_newer(stateid, &state->open_stateid))) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001359 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001360 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001361 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001362 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001363 nfs4_stateid_copy(&state->stateid, stateid);
1364 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001365}
1366
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001367static void nfs_clear_open_stateid(struct nfs4_state *state,
1368 nfs4_stateid *arg_stateid,
1369 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001370{
1371 write_seqlock(&state->seqlock);
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001372 nfs_clear_open_stateid_locked(state, arg_stateid, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001373 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001374 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1375 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001376}
1377
Trond Myklebust003707c2007-07-05 18:07:55 -04001378static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
1379{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001380 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001381 case FMODE_READ:
1382 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1383 break;
1384 case FMODE_WRITE:
1385 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1386 break;
1387 case FMODE_READ|FMODE_WRITE:
1388 set_bit(NFS_O_RDWR_STATE, &state->flags);
1389 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001390 if (!nfs_need_update_open_stateid(state, stateid))
1391 return;
1392 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1393 nfs4_stateid_copy(&state->stateid, stateid);
1394 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001395}
1396
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001397static 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 -07001398{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001399 /*
1400 * Protect the call to nfs4_state_set_mode_locked and
1401 * serialise the stateid update
1402 */
Andrew Elble361cad32015-12-02 09:20:57 -05001403 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001404 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001405 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001406 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001407 set_bit(NFS_DELEGATED_STATE, &state->flags);
1408 }
1409 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001410 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001411 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001412 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001413 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001416static 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 -05001417{
1418 struct nfs_inode *nfsi = NFS_I(state->inode);
1419 struct nfs_delegation *deleg_cur;
1420 int ret = 0;
1421
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001423
1424 rcu_read_lock();
1425 deleg_cur = rcu_dereference(nfsi->delegation);
1426 if (deleg_cur == NULL)
1427 goto no_delegation;
1428
1429 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001430 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001431 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001433 goto no_delegation_unlock;
1434
1435 if (delegation == NULL)
1436 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001437 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001438 goto no_delegation_unlock;
1439
Trond Myklebustb7391f42008-12-23 15:21:52 -05001440 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001442 ret = 1;
1443no_delegation_unlock:
1444 spin_unlock(&deleg_cur->lock);
1445no_delegation:
1446 rcu_read_unlock();
1447
1448 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001449 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001450 ret = 1;
1451 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001452 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1453 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust34310432008-12-23 15:21:38 -05001454
1455 return ret;
1456}
1457
Trond Myklebust39071e62015-01-24 15:07:56 -05001458static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1459 const nfs4_stateid *stateid)
1460{
1461 struct nfs4_state *state = lsp->ls_state;
1462 bool ret = false;
1463
1464 spin_lock(&state->state_lock);
1465 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1466 goto out_noupdate;
1467 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1468 goto out_noupdate;
1469 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1470 ret = true;
1471out_noupdate:
1472 spin_unlock(&state->state_lock);
1473 return ret;
1474}
Trond Myklebust34310432008-12-23 15:21:38 -05001475
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001476static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001477{
1478 struct nfs_delegation *delegation;
1479
1480 rcu_read_lock();
1481 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001482 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001483 rcu_read_unlock();
1484 return;
1485 }
1486 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001487 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001488}
1489
Trond Myklebust6ee41262007-07-08 14:11:36 -04001490static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001491{
1492 struct nfs4_state *state = opendata->state;
1493 struct nfs_inode *nfsi = NFS_I(state->inode);
1494 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001495 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001497 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001498 nfs4_stateid stateid;
1499 int ret = -EAGAIN;
1500
Trond Myklebustaac00a82007-07-05 19:02:21 -04001501 for (;;) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001502 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef752014-09-03 14:15:40 -04001504 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001505 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef752014-09-03 14:15:40 -04001506 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001507 }
Anna Schumaker61beef752014-09-03 14:15:40 -04001508 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001509 rcu_read_lock();
1510 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001511 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001512 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001513 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001514 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001515 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001516 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001517 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001518 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001519 if (!opendata->is_recover) {
1520 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1521 if (ret != 0)
1522 goto out;
1523 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001524 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001525
1526 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001527 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001528 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001529 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001530out:
1531 return ERR_PTR(ret);
1532out_return_state:
1533 atomic_inc(&state->count);
1534 return state;
1535}
1536
Andy Adamsone23008e2012-10-02 21:07:32 -04001537static void
1538nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1539{
1540 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1541 struct nfs_delegation *delegation;
1542 int delegation_flags = 0;
1543
1544 rcu_read_lock();
1545 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1546 if (delegation)
1547 delegation_flags = delegation->flags;
1548 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001549 switch (data->o_arg.claim) {
1550 default:
1551 break;
1552 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1553 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001554 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1555 "returning a delegation for "
1556 "OPEN(CLAIM_DELEGATE_CUR)\n",
1557 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001558 return;
1559 }
1560 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001561 nfs_inode_set_delegation(state->inode,
1562 data->owner->so_cred,
1563 &data->o_res);
1564 else
1565 nfs_inode_reclaim_delegation(state->inode,
1566 data->owner->so_cred,
1567 &data->o_res);
1568}
1569
1570/*
1571 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1572 * and update the nfs4_state.
1573 */
1574static struct nfs4_state *
1575_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1576{
1577 struct inode *inode = data->state->inode;
1578 struct nfs4_state *state = data->state;
1579 int ret;
1580
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001581 if (!data->rpc_done) {
1582 if (data->rpc_status) {
1583 ret = data->rpc_status;
1584 goto err;
1585 }
1586 /* cached opens have already been processed */
1587 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001588 }
1589
Andy Adamsone23008e2012-10-02 21:07:32 -04001590 ret = nfs_refresh_inode(inode, &data->f_attr);
1591 if (ret)
1592 goto err;
1593
1594 if (data->o_res.delegation_type != 0)
1595 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001596update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001597 update_open_stateid(state, &data->o_res.stateid, NULL,
1598 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001599 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001600
1601 return state;
1602err:
1603 return ERR_PTR(ret);
1604
1605}
1606
1607static struct nfs4_state *
1608_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609{
1610 struct inode *inode;
1611 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001612 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613
Trond Myklebustaac00a82007-07-05 19:02:21 -04001614 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001615 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001616 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617 goto out;
1618 }
1619
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001620 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001621 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001622 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001623 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001624 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001625 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001626 goto err;
1627 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001628 state = nfs4_get_open_state(inode, data->owner);
1629 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001630 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001631 if (data->o_res.delegation_type != 0)
1632 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001633 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001634 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001635 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001637 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001638 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001639err_put_inode:
1640 iput(inode);
1641err:
1642 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001643}
1644
Andy Adamsone23008e2012-10-02 21:07:32 -04001645static struct nfs4_state *
1646nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1647{
1648 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1649 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1650 return _nfs4_opendata_to_nfs4_state(data);
1651}
1652
Trond Myklebust864472e2006-01-03 09:55:15 +01001653static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1654{
1655 struct nfs_inode *nfsi = NFS_I(state->inode);
1656 struct nfs_open_context *ctx;
1657
1658 spin_lock(&state->inode->i_lock);
1659 list_for_each_entry(ctx, &nfsi->open_files, list) {
1660 if (ctx->state != state)
1661 continue;
1662 get_nfs_open_context(ctx);
1663 spin_unlock(&state->inode->i_lock);
1664 return ctx;
1665 }
1666 spin_unlock(&state->inode->i_lock);
1667 return ERR_PTR(-ENOENT);
1668}
1669
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001670static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1671 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001672{
1673 struct nfs4_opendata *opendata;
1674
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001675 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001676 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001677 if (opendata == NULL)
1678 return ERR_PTR(-ENOMEM);
1679 opendata->state = state;
1680 atomic_inc(&state->count);
1681 return opendata;
1682}
1683
Trond Myklebust24311f82015-09-20 10:50:17 -04001684static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1685 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001686{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001687 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 int ret;
1689
Trond Myklebust24311f82015-09-20 10:50:17 -04001690 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001691 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001692 opendata->o_arg.open_flags = 0;
1693 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001694 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1695 NFS_SB(opendata->dentry->d_sb),
1696 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001697 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1698 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1699 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001700 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001701 if (ret != 0)
1702 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001703 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001704 if (IS_ERR(newstate))
1705 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001706 if (newstate != opendata->state)
1707 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001708 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001709 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001710}
1711
1712static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1713{
Trond Myklebust864472e2006-01-03 09:55:15 +01001714 int ret;
1715
Trond Myklebust4f14c192014-02-12 19:15:06 -05001716 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1717 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1718 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1719 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001720 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001721 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001722 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001723 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001724 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1725 if (ret != 0)
1726 return ret;
1727 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1728 if (ret != 0)
1729 return ret;
1730 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1731 if (ret != 0)
1732 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001733 /*
1734 * We may have performed cached opens for all three recoveries.
1735 * Check if we need to update the current stateid.
1736 */
1737 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001738 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001739 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001740 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001741 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001742 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001743 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001744 return 0;
1745}
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747/*
1748 * OPEN_RECLAIM:
1749 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001751static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001753 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001754 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001755 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int status;
1757
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001758 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1759 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001760 if (IS_ERR(opendata))
1761 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001762 rcu_read_lock();
1763 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001764 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001765 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001766 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001767 opendata->o_arg.u.delegation_type = delegation_type;
1768 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001769 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return status;
1771}
1772
Trond Myklebust539cd032007-06-05 11:46:42 -04001773static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct nfs_server *server = NFS_SERVER(state->inode);
1776 struct nfs4_exception exception = { };
1777 int err;
1778 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001779 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001780 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001781 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1782 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001783 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001784 break;
1785 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 } while (exception.retry);
1787 return err;
1788}
1789
Trond Myklebust864472e2006-01-03 09:55:15 +01001790static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1791{
1792 struct nfs_open_context *ctx;
1793 int ret;
1794
1795 ctx = nfs4_state_find_open_context(state);
1796 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001797 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001798 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001799 put_nfs_open_context(ctx);
1800 return ret;
1801}
1802
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001803static 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 -07001804{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001805 switch (err) {
1806 default:
1807 printk(KERN_ERR "NFS: %s: unhandled error "
1808 "%d.\n", __func__, err);
1809 case 0:
1810 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001811 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001812 case -ESTALE:
1813 break;
1814 case -NFS4ERR_BADSESSION:
1815 case -NFS4ERR_BADSLOT:
1816 case -NFS4ERR_BAD_HIGH_SLOT:
1817 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1818 case -NFS4ERR_DEADSESSION:
1819 set_bit(NFS_DELEGATED_STATE, &state->flags);
1820 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1821 return -EAGAIN;
1822 case -NFS4ERR_STALE_CLIENTID:
1823 case -NFS4ERR_STALE_STATEID:
1824 set_bit(NFS_DELEGATED_STATE, &state->flags);
1825 case -NFS4ERR_EXPIRED:
1826 /* Don't recall a delegation if it was lost */
1827 nfs4_schedule_lease_recovery(server->nfs_client);
1828 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001829 case -NFS4ERR_MOVED:
1830 nfs4_schedule_migration_recovery(server);
1831 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001832 case -NFS4ERR_LEASE_MOVED:
1833 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1834 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001835 case -NFS4ERR_DELEG_REVOKED:
1836 case -NFS4ERR_ADMIN_REVOKED:
1837 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001838 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001839 nfs_inode_find_state_and_recover(state->inode,
1840 stateid);
1841 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001842 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001843 case -NFS4ERR_DELAY:
1844 case -NFS4ERR_GRACE:
1845 set_bit(NFS_DELEGATED_STATE, &state->flags);
1846 ssleep(1);
1847 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001848 case -ENOMEM:
1849 case -NFS4ERR_DENIED:
1850 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1851 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return err;
1854}
1855
Trond Myklebust24311f82015-09-20 10:50:17 -04001856int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1857 struct nfs4_state *state, const nfs4_stateid *stateid,
1858 fmode_t type)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001859{
1860 struct nfs_server *server = NFS_SERVER(state->inode);
1861 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001862 int err = 0;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001863
1864 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1865 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1866 if (IS_ERR(opendata))
1867 return PTR_ERR(opendata);
1868 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001869 write_seqlock(&state->seqlock);
1870 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1871 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001872 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1873 switch (type & (FMODE_READ|FMODE_WRITE)) {
1874 case FMODE_READ|FMODE_WRITE:
1875 case FMODE_WRITE:
1876 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1877 if (err)
1878 break;
1879 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1880 if (err)
1881 break;
1882 case FMODE_READ:
1883 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1884 }
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001885 nfs4_opendata_put(opendata);
1886 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1887}
1888
Chuck Leverbe05c862013-08-09 12:49:47 -04001889static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1890{
1891 struct nfs4_opendata *data = calldata;
1892
Peng Taocb04ad22014-06-11 05:24:15 +08001893 nfs40_setup_sequence(data->o_arg.server->nfs_client->cl_slot_tbl,
1894 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001895}
1896
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001897static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1898{
1899 struct nfs4_opendata *data = calldata;
1900
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001901 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001902
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001903 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001904 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001905 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001906 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001907 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001908 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001909 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001910}
1911
1912static void nfs4_open_confirm_release(void *calldata)
1913{
1914 struct nfs4_opendata *data = calldata;
1915 struct nfs4_state *state = NULL;
1916
1917 /* If this request hasn't been cancelled, do nothing */
1918 if (data->cancelled == 0)
1919 goto out_free;
1920 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001921 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001922 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001923 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001924 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001925 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001926out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001927 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001928}
1929
1930static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001931 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001932 .rpc_call_done = nfs4_open_confirm_done,
1933 .rpc_release = nfs4_open_confirm_release,
1934};
1935
1936/*
1937 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1938 */
1939static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1940{
David Howells2b0143b2015-03-17 22:25:59 +00001941 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001942 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001943 struct rpc_message msg = {
1944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1945 .rpc_argp = &data->c_arg,
1946 .rpc_resp = &data->c_res,
1947 .rpc_cred = data->owner->so_cred,
1948 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001949 struct rpc_task_setup task_setup_data = {
1950 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001951 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001952 .callback_ops = &nfs4_open_confirm_ops,
1953 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001954 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001955 .flags = RPC_TASK_ASYNC,
1956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int status;
1958
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001959 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001960 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001961 data->rpc_done = 0;
1962 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001963 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04001964 if (data->is_recover)
1965 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04001966 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001967 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001968 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001969 status = nfs4_wait_for_completion_rpc_task(task);
1970 if (status != 0) {
1971 data->cancelled = 1;
1972 smp_wmb();
1973 } else
1974 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001975 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 return status;
1977}
1978
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001979static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001981 struct nfs4_opendata *data = calldata;
1982 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001983 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05001984 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001985
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001986 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001987 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001988 /*
1989 * Check if we still need to send an OPEN call, or if we can use
1990 * a delegation instead.
1991 */
1992 if (data->state != NULL) {
1993 struct nfs_delegation *delegation;
1994
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001995 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001996 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001997 rcu_read_lock();
1998 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001999 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002000 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002001 rcu_read_unlock();
2002 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002003 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002004 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002005 switch (claim) {
2006 default:
2007 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002008 case NFS4_OPEN_CLAIM_PREVIOUS:
2009 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2010 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002011 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002012 case NFS4_OPEN_CLAIM_FH:
2013 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002014 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2015 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002016 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002017 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04002018 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002019 &data->o_res.seq_res,
2020 task) != 0)
2021 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002022
2023 /* Set the create mode (note dependency on the session type) */
2024 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2025 if (data->o_arg.open_flags & O_EXCL) {
2026 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2027 if (nfs4_has_persistent_session(clp))
2028 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2029 else if (clp->cl_mvops->minor_version > 0)
2030 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2031 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002032 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002033unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002034 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002035 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002036out_no_action:
2037 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002038out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002039 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002040}
2041
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002042static void nfs4_open_done(struct rpc_task *task, void *calldata)
2043{
2044 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002046 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002047
Trond Myklebust14516c32010-07-31 14:29:06 -04002048 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
2049 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002050
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002051 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002052 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2053 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002054 case S_IFREG:
2055 break;
2056 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002057 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002058 break;
2059 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002060 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002061 break;
2062 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002063 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002064 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002065 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002066 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002067 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2068 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002069 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002070 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002071}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002072
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002073static void nfs4_open_release(void *calldata)
2074{
2075 struct nfs4_opendata *data = calldata;
2076 struct nfs4_state *state = NULL;
2077
2078 /* If this request hasn't been cancelled, do nothing */
2079 if (data->cancelled == 0)
2080 goto out_free;
2081 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002082 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002083 goto out_free;
2084 /* In case we need an open_confirm, no cleanup! */
2085 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2086 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002087 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002088 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002089 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002090out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002091 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002092}
2093
2094static const struct rpc_call_ops nfs4_open_ops = {
2095 .rpc_call_prepare = nfs4_open_prepare,
2096 .rpc_call_done = nfs4_open_done,
2097 .rpc_release = nfs4_open_release,
2098};
2099
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002100static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002101{
David Howells2b0143b2015-03-17 22:25:59 +00002102 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002103 struct nfs_server *server = NFS_SERVER(dir);
2104 struct nfs_openargs *o_arg = &data->o_arg;
2105 struct nfs_openres *o_res = &data->o_res;
2106 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002107 struct rpc_message msg = {
2108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2109 .rpc_argp = o_arg,
2110 .rpc_resp = o_res,
2111 .rpc_cred = data->owner->so_cred,
2112 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002113 struct rpc_task_setup task_setup_data = {
2114 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002115 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002116 .callback_ops = &nfs4_open_ops,
2117 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002118 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002119 .flags = RPC_TASK_ASYNC,
2120 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002121 int status;
2122
Chuck Levera9c92d62013-08-09 12:48:18 -04002123 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002124 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002125 data->rpc_done = 0;
2126 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002127 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002128 data->is_recover = 0;
2129 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002130 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002131 data->is_recover = 1;
2132 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002133 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002134 if (IS_ERR(task))
2135 return PTR_ERR(task);
2136 status = nfs4_wait_for_completion_rpc_task(task);
2137 if (status != 0) {
2138 data->cancelled = 1;
2139 smp_wmb();
2140 } else
2141 status = data->rpc_status;
2142 rpc_put_task(task);
2143
2144 return status;
2145}
2146
2147static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2148{
David Howells2b0143b2015-03-17 22:25:59 +00002149 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002150 struct nfs_openres *o_res = &data->o_res;
2151 int status;
2152
2153 status = nfs4_run_open_task(data, 1);
2154 if (status != 0 || !data->rpc_done)
2155 return status;
2156
Trond Myklebust6926afd2012-01-07 13:22:46 -05002157 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2158
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002159 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2160 status = _nfs4_proc_open_confirm(data);
2161 if (status != 0)
2162 return status;
2163 }
2164
2165 return status;
2166}
2167
Trond Myklebustf3792d62014-07-10 08:54:32 -04002168/*
2169 * Additional permission checks in order to distinguish between an
2170 * open for read, and an open for execute. This works around the
2171 * fact that NFSv4 OPEN treats read and execute permissions as being
2172 * the same.
2173 * Note that in the non-execute case, we want to turn off permission
2174 * checking if we just created a new file (POSIX open() semantics).
2175 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002176static int nfs4_opendata_access(struct rpc_cred *cred,
2177 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002178 struct nfs4_state *state, fmode_t fmode,
2179 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002180{
2181 struct nfs_access_entry cache;
2182 u32 mask;
2183
2184 /* access call failed or for some reason the server doesn't
2185 * support any access modes -- defer access call until later */
2186 if (opendata->o_res.access_supported == 0)
2187 return 0;
2188
2189 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002190 /*
2191 * Use openflags to check for exec, because fmode won't
2192 * always have FMODE_EXEC set when file open for exec.
2193 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002194 if (openflags & __FMODE_EXEC) {
2195 /* ONLY check for exec rights */
2196 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002197 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002198 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002199
2200 cache.cred = cred;
2201 cache.jiffies = jiffies;
2202 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2203 nfs_access_add_cache(state->inode, &cache);
2204
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002205 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002206 return 0;
2207
2208 /* even though OPEN succeeded, access is denied. Close the file */
2209 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002210 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002211}
2212
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002213/*
2214 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2215 */
2216static int _nfs4_proc_open(struct nfs4_opendata *data)
2217{
David Howells2b0143b2015-03-17 22:25:59 +00002218 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002219 struct nfs_server *server = NFS_SERVER(dir);
2220 struct nfs_openargs *o_arg = &data->o_arg;
2221 struct nfs_openres *o_res = &data->o_res;
2222 int status;
2223
2224 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002225 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002226 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002227 if (status != 0) {
2228 if (status == -NFS4ERR_BADNAME &&
2229 !(o_arg->open_flags & O_CREAT))
2230 return -ENOENT;
2231 return status;
2232 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002233
Trond Myklebust6926afd2012-01-07 13:22:46 -05002234 nfs_fattr_map_and_free_names(server, &data->f_attr);
2235
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002236 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust56ae19f2005-10-27 22:12:40 -04002237 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002238 if (o_arg->open_flags & O_EXCL)
2239 data->file_created = 1;
2240 else if (o_res->cinfo.before != o_res->cinfo.after)
2241 data->file_created = 1;
2242 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002243 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2244 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002246 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002248 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002251 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
Andy Adamsond83217c2011-03-01 01:34:17 +00002255static int nfs4_recover_expired_lease(struct nfs_server *server)
2256{
2257 return nfs4_client_recover_expired_lease(server->nfs_client);
2258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/*
2261 * OPEN_EXPIRED:
2262 * reclaim state on the server after a network partition.
2263 * Assumes caller holds the appropriate lock
2264 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002265static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002267 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002268 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002270 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002271 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002272 if (IS_ERR(opendata))
2273 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002274 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002275 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002276 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002277 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002281static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002282{
Trond Myklebust539cd032007-06-05 11:46:42 -04002283 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002284 struct nfs4_exception exception = { };
2285 int err;
2286
2287 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002288 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002289 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002290 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2291 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002292 switch (err) {
2293 default:
2294 goto out;
2295 case -NFS4ERR_GRACE:
2296 case -NFS4ERR_DELAY:
2297 nfs4_handle_exception(server, err, &exception);
2298 err = 0;
2299 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002300 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002301out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002302 return err;
2303}
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002308 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Trond Myklebust864472e2006-01-03 09:55:15 +01002310 ctx = nfs4_state_find_open_context(state);
2311 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002312 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002313 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002314 put_nfs_open_context(ctx);
2315 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002318static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state)
2319{
2320 nfs_remove_bad_delegation(state->inode);
2321 write_seqlock(&state->seqlock);
2322 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2323 write_sequnlock(&state->seqlock);
2324 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2325}
2326
2327static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2328{
2329 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
2330 nfs_finish_clear_delegation_stateid(state);
2331}
2332
2333static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2334{
2335 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2336 nfs40_clear_delegation_stateid(state);
2337 return nfs4_open_expired(sp, state);
2338}
2339
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002340#if defined(CONFIG_NFS_V4_1)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002341static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002342{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002343 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002344 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002345 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002346 struct rpc_cred *cred;
2347 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002348
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002349 /* Get the delegation credential for use by test/free_stateid */
2350 rcu_read_lock();
2351 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002352 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002353 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002354 return;
2355 }
2356
2357 nfs4_stateid_copy(&stateid, &delegation->stateid);
2358 cred = get_rpccred(delegation->cred);
2359 rcu_read_unlock();
2360 status = nfs41_test_stateid(server, &stateid, cred);
2361 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002362
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002363 if (status != NFS_OK) {
2364 /* Free the stateid unless the server explicitly
2365 * informs us the stateid is unrecognized. */
2366 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002367 nfs41_free_stateid(server, &stateid, cred);
2368 nfs_finish_clear_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002369 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002370
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002371 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002372}
2373
2374/**
2375 * nfs41_check_open_stateid - possibly free an open stateid
2376 *
2377 * @state: NFSv4 state for an inode
2378 *
2379 * Returns NFS_OK if recovery for this stateid is now finished.
2380 * Otherwise a negative NFS4ERR value is returned.
2381 */
2382static int nfs41_check_open_stateid(struct nfs4_state *state)
2383{
2384 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002385 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002386 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002387 int status;
2388
2389 /* If a state reset has been done, test_stateid is unneeded */
2390 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
2391 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
2392 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
2393 return -NFS4ERR_BAD_STATEID;
2394
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002395 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002396 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002397 if (status != NFS_OK) {
2398 /* Free the stateid unless the server explicitly
2399 * informs us the stateid is unrecognized. */
2400 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002401 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002402
2403 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2404 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2405 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002406 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002407 }
2408 return status;
2409}
2410
2411static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2412{
Chuck Levereb64cf92012-07-11 16:30:05 -04002413 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002414
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002415 nfs41_check_delegation_stateid(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002416 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002417 if (status != NFS_OK)
2418 status = nfs4_open_expired(sp, state);
2419 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002420}
2421#endif
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002424 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2425 * fields corresponding to attributes that were used to store the verifier.
2426 * Make sure we clobber those fields in the later setattr call
2427 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002428static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2429 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002430{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002431 const u32 *attrset = opendata->o_res.attrset;
2432
2433 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002434 !(sattr->ia_valid & ATTR_ATIME_SET))
2435 sattr->ia_valid |= ATTR_ATIME;
2436
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002437 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002438 !(sattr->ia_valid & ATTR_MTIME_SET))
2439 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002440
2441 /* Except MODE, it seems harmless of setting twice. */
2442 if ((attrset[1] & FATTR4_WORD1_MODE))
2443 sattr->ia_valid &= ~ATTR_MODE;
2444
2445 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2446 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002447}
2448
Trond Myklebustc21443c2013-02-07 14:26:21 -05002449static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2450 fmode_t fmode,
2451 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002452 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002453{
2454 struct nfs4_state_owner *sp = opendata->owner;
2455 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002456 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002457 struct nfs4_state *state;
2458 unsigned int seq;
2459 int ret;
2460
2461 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2462
2463 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002464 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002465 goto out;
2466
2467 state = nfs4_opendata_to_nfs4_state(opendata);
2468 ret = PTR_ERR(state);
2469 if (IS_ERR(state))
2470 goto out;
2471 if (server->caps & NFS_CAP_POSIX_LOCK)
2472 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2473
Trond Myklebust275bb302013-05-29 13:11:28 -04002474 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002475 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002476 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002477 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002478 alias = d_exact_alias(dentry, state->inode);
2479 if (!alias)
2480 alias = d_splice_alias(igrab(state->inode), dentry);
2481 /* d_splice_alias() can't fail here - it's a non-directory */
2482 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002483 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002484 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002485 }
2486 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002487 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002488 }
2489
Trond Myklebustc21443c2013-02-07 14:26:21 -05002490 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2491 if (ret != 0)
2492 goto out;
2493
Trond Myklebust3efb9722013-05-29 13:17:04 -04002494 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002495 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002496 nfs_inode_attach_open_context(ctx);
2497 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2498 nfs4_schedule_stateid_recovery(server, state);
2499 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002500out:
2501 return ret;
2502}
2503
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002504/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002505 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Andy Adamson82be4172012-05-23 05:02:35 -04002507static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002508 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002509 int flags,
2510 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002511 struct nfs4_label *label,
2512 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 struct nfs4_state_owner *sp;
2515 struct nfs4_state *state = NULL;
2516 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002517 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002518 struct dentry *dentry = ctx->dentry;
2519 struct rpc_cred *cred = ctx->cred;
2520 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2521 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002522 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002523 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002524 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
2526 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002528 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2529 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2531 goto out_err;
2532 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002533 status = nfs4_recover_expired_lease(server);
2534 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002535 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002536 if (d_really_is_positive(dentry))
2537 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002538 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002539 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002540 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002541 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002542 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002543 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002544 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
David Quigley14c43f72013-05-22 12:50:43 -04002546 if (label) {
2547 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2548 if (IS_ERR(olabel)) {
2549 status = PTR_ERR(olabel);
2550 goto err_opendata_put;
2551 }
2552 }
2553
Trond Myklebuste911b812014-03-26 13:24:37 -07002554 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2555 if (!opendata->f_attr.mdsthreshold) {
2556 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2557 if (!opendata->f_attr.mdsthreshold)
2558 goto err_free_label;
2559 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002560 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002561 }
David Howells2b0143b2015-03-17 22:25:59 +00002562 if (d_really_is_positive(dentry))
2563 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002564
Trond Myklebust3efb9722013-05-29 13:17:04 -04002565 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002566 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002567 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002568 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002569
NeilBrownefcbc042015-07-30 13:00:56 +10002570 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002571 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002572 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002573 /*
2574 * send create attributes which was not set by open
2575 * with an extra setattr.
2576 */
2577 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2578 nfs_fattr_init(opendata->o_res.f_attr);
2579 status = nfs4_do_setattr(state->inode, cred,
2580 opendata->o_res.f_attr, sattr,
2581 state, label, olabel);
2582 if (status == 0) {
2583 nfs_setattr_update_inode(state->inode, sattr,
2584 opendata->o_res.f_attr);
2585 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2586 }
David Quigley1775fd32013-05-22 12:50:42 -04002587 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002588 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002589 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002590 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002591
Trond Myklebuste911b812014-03-26 13:24:37 -07002592 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002593 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002594 opendata->f_attr.mdsthreshold = NULL;
2595 }
Andy Adamson82be4172012-05-23 05:02:35 -04002596
David Quigley14c43f72013-05-22 12:50:43 -04002597 nfs4_label_free(olabel);
2598
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002599 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002602err_free_label:
2603 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002604err_opendata_put:
2605 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002606err_put_state_owner:
2607 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return status;
2610}
2611
2612
Andy Adamson82be4172012-05-23 05:02:35 -04002613static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002614 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002615 int flags,
2616 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002617 struct nfs4_label *label,
2618 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002620 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 struct nfs4_exception exception = { };
2622 struct nfs4_state *res;
2623 int status;
2624
2625 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002626 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002627 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002628 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (status == 0)
2630 break;
2631 /* NOTE: BAD_SEQID means the server and client disagree about the
2632 * book-keeping w.r.t. state-changing operations
2633 * (OPEN/CLOSE/LOCK/LOCKU...)
2634 * It is actually a sign of a bug on the client or on the server.
2635 *
2636 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002637 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * have unhashed the old state_owner for us, and that we can
2639 * therefore safely retry using a new one. We should still warn
2640 * the user though...
2641 */
2642 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002643 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002644 " returned a bad sequence-id error!\n",
2645 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 exception.retry = 1;
2647 continue;
2648 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002649 /*
2650 * BAD_STATEID on OPEN means that the server cancelled our
2651 * state before it received the OPEN_CONFIRM.
2652 * Recover by retrying the request as per the discussion
2653 * on Page 181 of RFC3530.
2654 */
2655 if (status == -NFS4ERR_BAD_STATEID) {
2656 exception.retry = 1;
2657 continue;
2658 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002659 if (status == -EAGAIN) {
2660 /* We must have found a delegation */
2661 exception.retry = 1;
2662 continue;
2663 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002664 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2665 continue;
2666 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 status, &exception));
2668 } while (exception.retry);
2669 return res;
2670}
2671
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002672static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2673 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002674 struct nfs4_state *state, struct nfs4_label *ilabel,
2675 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002677 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002679 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .iap = sattr,
2681 .server = server,
2682 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002683 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 };
2685 struct nfs_setattrres res = {
2686 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002687 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 .server = server,
2689 };
2690 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002691 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2692 .rpc_argp = &arg,
2693 .rpc_resp = &res,
2694 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 };
Trond Myklebustabf4e132016-05-16 17:42:44 -04002696 struct rpc_cred *delegation_cred = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002697 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002698 fmode_t fmode;
2699 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002700 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
David Quigleyaa9c2662013-05-22 12:50:44 -04002702 arg.bitmask = nfs4_bitmask(server, ilabel);
2703 if (ilabel)
2704 arg.bitmask = nfs4_bitmask(server, olabel);
2705
Trond Myklebust0e574af2005-10-27 22:12:38 -04002706 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Trond Myklebustee3ae842013-04-29 10:35:36 -04002708 /* Servers should only apply open mode checks for file size changes */
2709 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2710 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2711
Trond Myklebustabf4e132016-05-16 17:42:44 -04002712 if (nfs4_copy_delegation_stateid(inode, fmode, &arg.stateid, &delegation_cred)) {
Trond Myklebustee3ae842013-04-29 10:35:36 -04002713 /* Use that stateid */
Trond Myklebust0418dae2014-03-04 13:48:16 -05002714 } else if (truncate && state != NULL) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002715 struct nfs_lockowner lockowner = {
2716 .l_owner = current->files,
2717 .l_pid = current->tgid,
2718 };
Trond Myklebust0418dae2014-03-04 13:48:16 -05002719 if (!nfs4_valid_open_stateid(state))
2720 return -EBADF;
Trond Myklebustabf4e132016-05-16 17:42:44 -04002721 if (nfs4_select_rw_stateid(state, FMODE_WRITE, &lockowner,
2722 &arg.stateid, &delegation_cred) == -EIO)
Trond Myklebust0418dae2014-03-04 13:48:16 -05002723 return -EBADF;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002724 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002725 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Trond Myklebustabf4e132016-05-16 17:42:44 -04002726 if (delegation_cred)
2727 msg.rpc_cred = delegation_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Bryan Schumaker7c513052011-03-24 17:12:24 +00002729 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebustabf4e132016-05-16 17:42:44 -04002730
2731 put_rpccred(delegation_cred);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002732 if (status == 0 && state != NULL)
2733 renew_lease(server, timestamp);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05002734 trace_nfs4_setattr(inode, &arg.stateid, status);
Trond Myklebust65e43082005-08-16 11:49:44 -04002735 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002738static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2739 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002740 struct nfs4_state *state, struct nfs4_label *ilabel,
2741 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002743 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002744 struct nfs4_exception exception = {
2745 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002746 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002747 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 int err;
2749 do {
David Quigley1775fd32013-05-22 12:50:42 -04002750 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebust451146b2012-04-18 16:29:11 -04002751 switch (err) {
2752 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002753 if (!(sattr->ia_valid & ATTR_SIZE)) {
2754 pr_warn_once("NFSv4: server %s is incorrectly "
2755 "applying open mode checks to "
2756 "a SETATTR that is not "
2757 "changing file size.\n",
2758 server->nfs_client->cl_hostname);
2759 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002760 if (state && !(state->state & FMODE_WRITE)) {
2761 err = -EBADF;
2762 if (sattr->ia_valid & ATTR_OPEN)
2763 err = -EACCES;
2764 goto out;
2765 }
2766 }
2767 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002769out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return err;
2771}
2772
Peng Tao500d7012015-09-22 11:35:22 +08002773static bool
2774nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2775{
2776 if (inode == NULL || !nfs_have_layout(inode))
2777 return false;
2778
2779 return pnfs_wait_on_layoutreturn(inode, task);
2780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782struct nfs4_closedata {
2783 struct inode *inode;
2784 struct nfs4_state *state;
2785 struct nfs_closeargs arg;
2786 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002787 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002788 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002789 bool roc;
2790 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791};
2792
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002793static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002794{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002795 struct nfs4_closedata *calldata = data;
2796 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002797 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002798
Fred Isamanf7e89172011-01-06 11:36:32 +00002799 if (calldata->roc)
2800 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002801 nfs4_put_open_state(calldata->state);
2802 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002803 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002804 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002805 kfree(calldata);
2806}
2807
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002808static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002810 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002813 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Chuck Levera3ca5652012-03-01 17:00:40 -05002815 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002816 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2817 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002818 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 /* hmm. we are done with the inode, and in the process of freeing
2820 * the state_owner. we keep this around to process errors
2821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 switch (task->tk_status) {
2823 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04002824 res_stateid = &calldata->res.stateid;
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002825 if (calldata->roc)
Fred Isamanf7e89172011-01-06 11:36:32 +00002826 pnfs_roc_set_barrier(state->inode,
2827 calldata->roc_barrier);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002828 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002829 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05002830 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002832 case -NFS4ERR_OLD_STATEID:
2833 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 case -NFS4ERR_EXPIRED:
Trond Myklebust566fcec2015-01-23 15:32:46 -05002835 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05002836 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05002837 rpc_restart_call_prepare(task);
2838 goto out_release;
2839 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002840 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10002843 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05002844 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002845 goto out_release;
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07002848 nfs_clear_open_stateid(state, &calldata->arg.stateid,
2849 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05002850out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05002851 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002852 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002853 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002856static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002858 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002859 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002860 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04002861 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05002862 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002863
Chuck Levera3ca5652012-03-01 17:00:40 -05002864 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002865 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002866 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002867
Trond Myklebust88069f72009-12-08 08:33:16 -05002868 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05002869 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04002870 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
2871 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
2872 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05002873 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04002874 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002875 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01002876 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04002877 if (state->n_rdonly == 0)
2878 call_close |= is_rdonly;
2879 else if (is_rdonly)
2880 calldata->arg.fmode |= FMODE_READ;
2881 if (state->n_wronly == 0)
2882 call_close |= is_wronly;
2883 else if (is_wronly)
2884 calldata->arg.fmode |= FMODE_WRITE;
2885 } else if (is_rdwr)
2886 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
2887
2888 if (calldata->arg.fmode == 0)
2889 call_close |= is_rdwr;
2890
Trond Myklebust5d422302013-03-14 16:57:48 -04002891 if (!nfs4_valid_open_stateid(state))
2892 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002893 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002894
2895 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002896 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002897 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002898 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002899
Peng Tao500d7012015-09-22 11:35:22 +08002900 if (nfs4_wait_on_layoutreturn(inode, task)) {
2901 nfs_release_seqid(calldata->arg.seqid);
2902 goto out_wait;
2903 }
2904
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002905 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05002906 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3c13cb52015-08-18 23:45:13 -05002907 if (calldata->roc)
2908 pnfs_roc_get_barrier(inode, &calldata->roc_barrier);
2909
Trond Myklebust6ae37332015-01-30 14:21:14 -05002910 calldata->arg.share_access =
2911 nfs4_map_atomic_open_share(NFS_SERVER(inode),
2912 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05002913
Trond Myklebust516a6af2005-10-27 22:12:41 -04002914 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002915 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002916 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002917 &calldata->arg.seq_args,
2918 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002919 task) != 0)
2920 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002921 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002922 return;
2923out_no_action:
2924 task->tk_action = NULL;
2925out_wait:
2926 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002929static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002930 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002931 .rpc_call_done = nfs4_close_done,
2932 .rpc_release = nfs4_free_closedata,
2933};
2934
Peng Taofe08c542014-07-03 13:05:01 +08002935static bool nfs4_roc(struct inode *inode)
2936{
Trond Myklebust40dd4b72015-01-24 13:54:37 -05002937 if (!nfs_have_layout(inode))
Peng Taofe08c542014-07-03 13:05:01 +08002938 return false;
Peng Taofe08c542014-07-03 13:05:01 +08002939 return pnfs_roc(inode);
2940}
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942/*
2943 * It is possible for data to be read/written from a mem-mapped file
2944 * after the sys_close call (which hits the vfs layer as a flush).
2945 * This means that we can't safely call nfsv4 close on a file until
2946 * the inode is cleared. This in turn means that we are not good
2947 * NFSv4 citizens - we do not indicate to the server to update the file's
2948 * share state even when we are done with one of the three share
2949 * stateid's in the inode.
2950 *
2951 * NOTE: Caller must be holding the sp->so_owner semaphore!
2952 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002953int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002955 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05002956 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002958 struct nfs4_state_owner *sp = state->owner;
2959 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002960 struct rpc_message msg = {
2961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2962 .rpc_cred = state->owner->so_cred,
2963 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002964 struct rpc_task_setup task_setup_data = {
2965 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002966 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002967 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002968 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002969 .flags = RPC_TASK_ASYNC,
2970 };
Trond Myklebust95121352005-10-18 14:20:12 -07002971 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04002973 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
2974 &task_setup_data.rpc_client, &msg);
2975
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002976 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002978 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04002979 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002980 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002982 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05002984 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
2985 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05002986 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07002987 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002988 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002989 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002990 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002991 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002992 calldata->res.server = server;
Peng Taofe08c542014-07-03 13:05:01 +08002993 calldata->roc = nfs4_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002994 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002995
Trond Myklebust1174dd12010-12-21 10:52:24 -05002996 msg.rpc_argp = &calldata->arg;
2997 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002998 task_setup_data.callback_data = calldata;
2999 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003000 if (IS_ERR(task))
3001 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003002 status = 0;
3003 if (wait)
3004 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003005 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003006 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003007out_free_calldata:
3008 kfree(calldata);
3009out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003010 nfs4_put_open_state(state);
3011 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003012 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
Trond Myklebust2b484292010-09-17 10:56:51 -04003015static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003016nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3017 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003020 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3021
3022 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Trond Myklebust565277f2007-10-15 18:17:53 -04003024 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003025 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003026
3027 nfs4_label_release_security(label);
3028
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003029 if (IS_ERR(state))
3030 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003031 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Trond Myklebust1185a552009-12-03 15:54:02 -05003034static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003035{
3036 if (ctx->state == NULL)
3037 return;
3038 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003039 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003040 else
Al Viro643168c2011-06-22 18:20:23 -04003041 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003042}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Trond Myklebustb944dba2013-11-04 15:20:20 -05003044#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3045#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
J. Bruce Fields999e5682014-06-03 17:33:35 -04003046#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_SECURITY_LABEL - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3049{
Kinglong Mee8c612822015-08-26 21:12:58 +08003050 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003051 struct nfs4_server_caps_arg args = {
3052 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003053 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003054 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 struct nfs4_server_caps_res res = {};
3056 struct rpc_message msg = {
3057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003058 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 .rpc_resp = &res,
3060 };
3061 int status;
3062
Kinglong Mee8c612822015-08-26 21:12:58 +08003063 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3064 FATTR4_WORD0_FH_EXPIRE_TYPE |
3065 FATTR4_WORD0_LINK_SUPPORT |
3066 FATTR4_WORD0_SYMLINK_SUPPORT |
3067 FATTR4_WORD0_ACLSUPPORT;
3068 if (minorversion)
3069 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3070
Bryan Schumaker7c513052011-03-24 17:12:24 +00003071 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003073 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003074 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003075 case 0:
3076 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3077 res.attr_bitmask[2] = 0;
3078 break;
3079 case 1:
3080 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3081 break;
3082 case 2:
3083 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003086 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3087 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3088 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3089 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003090 NFS_CAP_CTIME|NFS_CAP_MTIME|
3091 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003092 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3093 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 server->caps |= NFS_CAP_ACLS;
3095 if (res.has_links != 0)
3096 server->caps |= NFS_CAP_HARDLINKS;
3097 if (res.has_symlinks != 0)
3098 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003099 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3100 server->caps |= NFS_CAP_FILEID;
3101 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3102 server->caps |= NFS_CAP_MODE;
3103 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3104 server->caps |= NFS_CAP_NLINK;
3105 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3106 server->caps |= NFS_CAP_OWNER;
3107 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3108 server->caps |= NFS_CAP_OWNER_GROUP;
3109 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3110 server->caps |= NFS_CAP_ATIME;
3111 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3112 server->caps |= NFS_CAP_CTIME;
3113 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3114 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003115#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3116 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3117 server->caps |= NFS_CAP_SECURITY_LABEL;
3118#endif
3119 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3120 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003121 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003122
Trond Myklebusta65318b2009-03-11 14:10:28 -04003123 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3124 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3125 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003126 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003127 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3128 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003130 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return status;
3134}
3135
Trond Myklebust55a97592006-06-09 09:34:19 -04003136int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137{
3138 struct nfs4_exception exception = { };
3139 int err;
3140 do {
3141 err = nfs4_handle_exception(server,
3142 _nfs4_server_capabilities(server, fhandle),
3143 &exception);
3144 } while (exception.retry);
3145 return err;
3146}
3147
3148static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3149 struct nfs_fsinfo *info)
3150{
David Quigleyaa9c2662013-05-22 12:50:44 -04003151 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003153 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 };
3155 struct nfs4_lookup_res res = {
3156 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003157 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 .fh = fhandle,
3159 };
3160 struct rpc_message msg = {
3161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3162 .rpc_argp = &args,
3163 .rpc_resp = &res,
3164 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003165
David Quigleyaa9c2662013-05-22 12:50:44 -04003166 bitmask[0] = nfs4_fattr_bitmap[0];
3167 bitmask[1] = nfs4_fattr_bitmap[1];
3168 /*
3169 * Process the label in the upcoming getfattr
3170 */
3171 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3172
Trond Myklebust0e574af2005-10-27 22:12:38 -04003173 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003174 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
3177static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3178 struct nfs_fsinfo *info)
3179{
3180 struct nfs4_exception exception = { };
3181 int err;
3182 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003183 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003184 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003185 switch (err) {
3186 case 0:
3187 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003188 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003189 default:
3190 err = nfs4_handle_exception(server, err, &exception);
3191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003193out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return err;
3195}
3196
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003197static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3198 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3199{
Trond Myklebustc2190662013-08-26 19:23:04 -04003200 struct rpc_auth_create_args auth_args = {
3201 .pseudoflavor = flavor,
3202 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003203 struct rpc_auth *auth;
3204 int ret;
3205
Trond Myklebustc2190662013-08-26 19:23:04 -04003206 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003207 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003208 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003209 goto out;
3210 }
3211 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003212out:
3213 return ret;
3214}
3215
Chuck Lever9a744ba2013-03-16 15:56:02 -04003216/*
3217 * Retry pseudoroot lookup with various security flavors. We do this when:
3218 *
3219 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3220 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3221 *
3222 * Returns zero on success, or a negative NFS4ERR value, or a
3223 * negative errno value.
3224 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003225static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003226 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003228 /* Per 3530bis 15.33.5 */
3229 static const rpc_authflavor_t flav_array[] = {
3230 RPC_AUTH_GSS_KRB5P,
3231 RPC_AUTH_GSS_KRB5I,
3232 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003233 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003234 RPC_AUTH_NULL,
3235 };
3236 int status = -EPERM;
3237 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003239 if (server->auth_info.flavor_len > 0) {
3240 /* try each flavor specified by user */
3241 for (i = 0; i < server->auth_info.flavor_len; i++) {
3242 status = nfs4_lookup_root_sec(server, fhandle, info,
3243 server->auth_info.flavors[i]);
3244 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3245 continue;
3246 break;
3247 }
3248 } else {
3249 /* no flavors specified by user, try default list */
3250 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3251 status = nfs4_lookup_root_sec(server, fhandle, info,
3252 flav_array[i]);
3253 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3254 continue;
3255 break;
3256 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003257 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003258
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003259 /*
3260 * -EACCESS could mean that the user doesn't have correct permissions
3261 * to access the mount. It could also mean that we tried to mount
3262 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3263 * existing mount programs don't handle -EACCES very well so it should
3264 * be mapped to -EPERM instead.
3265 */
3266 if (status == -EACCES)
3267 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003268 return status;
3269}
3270
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003271static int nfs4_do_find_root_sec(struct nfs_server *server,
3272 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
3273{
3274 int mv = server->nfs_client->cl_minorversion;
3275 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
3276}
3277
3278/**
3279 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3280 * @server: initialized nfs_server handle
3281 * @fhandle: we fill in the pseudo-fs root file handle
3282 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003283 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003284 *
3285 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003286 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003287int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003288 struct nfs_fsinfo *info,
3289 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003290{
Andre Przywarac7757072015-04-23 17:17:40 +01003291 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003292
Andre Przywarac7757072015-04-23 17:17:40 +01003293 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003294 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003295
3296 if (auth_probe || status == NFS4ERR_WRONGSEC)
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003297 status = nfs4_do_find_root_sec(server, fhandle, info);
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (status == 0)
3300 status = nfs4_server_capabilities(server, fhandle);
3301 if (status == 0)
3302 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003303
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003304 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305}
3306
Bryan Schumakerbae36242012-05-10 15:07:31 -04003307static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3308 struct nfs_fsinfo *info)
3309{
3310 int error;
3311 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003312 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003313
3314 error = nfs4_server_capabilities(server, mntfh);
3315 if (error < 0) {
3316 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3317 return error;
3318 }
3319
David Quigley14c43f72013-05-22 12:50:43 -04003320 label = nfs4_label_alloc(server, GFP_KERNEL);
3321 if (IS_ERR(label))
3322 return PTR_ERR(label);
3323
David Quigley1775fd32013-05-22 12:50:42 -04003324 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003325 if (error < 0) {
3326 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003327 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003328 }
3329
3330 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3331 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3332 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3333
David Quigley14c43f72013-05-22 12:50:43 -04003334err_free_label:
3335 nfs4_label_free(label);
3336
Bryan Schumakerbae36242012-05-10 15:07:31 -04003337 return error;
3338}
3339
Manoj Naik6b97fd32006-06-09 09:34:29 -04003340/*
3341 * Get locations and (maybe) other attributes of a referral.
3342 * Note that we'll actually follow the referral later when
3343 * we detect fsid mismatch in inode revalidation
3344 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003345static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3346 const struct qstr *name, struct nfs_fattr *fattr,
3347 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003348{
3349 int status = -ENOMEM;
3350 struct page *page = NULL;
3351 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003352
3353 page = alloc_page(GFP_KERNEL);
3354 if (page == NULL)
3355 goto out;
3356 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3357 if (locations == NULL)
3358 goto out;
3359
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003360 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003361 if (status != 0)
3362 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003363
3364 /*
3365 * If the fsid didn't change, this is a migration event, not a
3366 * referral. Cause us to drop into the exception handler, which
3367 * will kick off migration recovery.
3368 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003369 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003370 dprintk("%s: server did not return a different fsid for"
3371 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003372 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003373 goto out;
3374 }
Andy Adamson533eb462011-06-13 18:25:56 -04003375 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3376 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003377
Andy Adamson533eb462011-06-13 18:25:56 -04003378 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003379 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003380 memset(fhandle, 0, sizeof(struct nfs_fh));
3381out:
3382 if (page)
3383 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003384 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003385 return status;
3386}
3387
David Quigley1775fd32013-05-22 12:50:42 -04003388static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3389 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 struct nfs4_getattr_arg args = {
3392 .fh = fhandle,
3393 .bitmask = server->attr_bitmask,
3394 };
3395 struct nfs4_getattr_res res = {
3396 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003397 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 .server = server,
3399 };
3400 struct rpc_message msg = {
3401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3402 .rpc_argp = &args,
3403 .rpc_resp = &res,
3404 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003405
3406 args.bitmask = nfs4_bitmask(server, label);
3407
Trond Myklebust0e574af2005-10-27 22:12:38 -04003408 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003409 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410}
3411
David Quigley1775fd32013-05-22 12:50:42 -04003412static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3413 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414{
3415 struct nfs4_exception exception = { };
3416 int err;
3417 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003418 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3419 trace_nfs4_getattr(server, fhandle, fattr, err);
3420 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 &exception);
3422 } while (exception.retry);
3423 return err;
3424}
3425
3426/*
3427 * The file is not closed if it is opened due to the a request to change
3428 * the size of the file. The open call will not be needed once the
3429 * VFS layer lookup-intents are implemented.
3430 *
3431 * Close is called when the inode is destroyed.
3432 * If we haven't opened the file for O_WRONLY, we
3433 * need to in the size_change case to obtain a stateid.
3434 *
3435 * Got race?
3436 * Because OPEN is always done by name in nfsv4, it is
3437 * possible that we opened a different file by the same
3438 * name. We can recognize this race condition, but we
3439 * can't do anything about it besides returning an error.
3440 *
3441 * This will be fixed with VFS changes (lookup-intent).
3442 */
3443static int
3444nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3445 struct iattr *sattr)
3446{
David Howells2b0143b2015-03-17 22:25:59 +00003447 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003448 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05003449 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003450 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 int status;
3452
Peng Tao88ac8152014-09-12 11:04:10 +08003453 if (pnfs_ld_layoutret_on_setattr(inode) &&
3454 sattr->ia_valid & ATTR_SIZE &&
3455 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003456 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003457
Trond Myklebust0e574af2005-10-27 22:12:38 -04003458 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459
Andy Adamson26699402012-05-30 16:12:24 -04003460 /* Deal with open(O_TRUNC) */
3461 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003462 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003463
3464 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003465 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003466 return 0;
3467
Trond Myklebustd5308382005-11-04 15:33:38 -05003468 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003469 if (sattr->ia_valid & ATTR_FILE) {
3470 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003471
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003472 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11003473 if (ctx) {
3474 cred = ctx->cred;
3475 state = ctx->state;
3476 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003477 }
3478
David Quigley14c43f72013-05-22 12:50:43 -04003479 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3480 if (IS_ERR(label))
3481 return PTR_ERR(label);
3482
3483 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003484 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003485 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003486 nfs_setsecurity(inode, fattr, label);
3487 }
David Quigley14c43f72013-05-22 12:50:43 -04003488 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return status;
3490}
3491
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003492static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3493 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003494 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003495{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003496 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003497 int status;
3498 struct nfs4_lookup_arg args = {
3499 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003500 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003501 .name = name,
3502 };
3503 struct nfs4_lookup_res res = {
3504 .server = server,
3505 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003506 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003507 .fh = fhandle,
3508 };
3509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3511 .rpc_argp = &args,
3512 .rpc_resp = &res,
3513 };
3514
David Quigleyaa9c2662013-05-22 12:50:44 -04003515 args.bitmask = nfs4_bitmask(server, label);
3516
David Howells2b3de442006-08-22 20:06:09 -04003517 nfs_fattr_init(fattr);
3518
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003520 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 dprintk("NFS reply lookup: %d\n", status);
3522 return status;
3523}
3524
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003525static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003526{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003527 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003528 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003529 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3530 fattr->nlink = 2;
3531}
3532
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003533static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3534 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003535 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003536{
3537 struct nfs4_exception exception = { };
3538 struct rpc_clnt *client = *clnt;
3539 int err;
3540 do {
David Quigley1775fd32013-05-22 12:50:42 -04003541 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003542 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003543 switch (err) {
3544 case -NFS4ERR_BADNAME:
3545 err = -ENOENT;
3546 goto out;
3547 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003548 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003549 if (err == -NFS4ERR_MOVED)
3550 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003551 goto out;
3552 case -NFS4ERR_WRONGSEC:
3553 err = -EPERM;
3554 if (client != *clnt)
3555 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003556 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003557 if (IS_ERR(client))
3558 return PTR_ERR(client);
3559
3560 exception.retry = 1;
3561 break;
3562 default:
3563 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3564 }
3565 } while (exception.retry);
3566
3567out:
3568 if (err == 0)
3569 *clnt = client;
3570 else if (client != *clnt)
3571 rpc_shutdown_client(client);
3572
3573 return err;
3574}
3575
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003576static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003577 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3578 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003580 int status;
3581 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003582
David Quigley1775fd32013-05-22 12:50:42 -04003583 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003584 if (client != NFS_CLIENT(dir)) {
3585 rpc_shutdown_client(client);
3586 nfs_fixup_secinfo_attributes(fattr);
3587 }
3588 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589}
3590
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003591struct rpc_clnt *
3592nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3593 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3594{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003595 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003596 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003597
David Quigley1775fd32013-05-22 12:50:42 -04003598 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003599 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003600 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003601 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003602}
3603
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3605{
Trond Myklebust76b32992007-08-10 17:45:11 -04003606 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 struct nfs4_accessargs args = {
3608 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003609 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003611 struct nfs4_accessres res = {
3612 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003613 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 struct rpc_message msg = {
3615 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3616 .rpc_argp = &args,
3617 .rpc_resp = &res,
3618 .rpc_cred = entry->cred,
3619 };
3620 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003621 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
3623 /*
3624 * Determine which access bits we want to ask for...
3625 */
3626 if (mode & MAY_READ)
3627 args.access |= NFS4_ACCESS_READ;
3628 if (S_ISDIR(inode->i_mode)) {
3629 if (mode & MAY_WRITE)
3630 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3631 if (mode & MAY_EXEC)
3632 args.access |= NFS4_ACCESS_LOOKUP;
3633 } else {
3634 if (mode & MAY_WRITE)
3635 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3636 if (mode & MAY_EXEC)
3637 args.access |= NFS4_ACCESS_EXECUTE;
3638 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003639
3640 res.fattr = nfs_alloc_fattr();
3641 if (res.fattr == NULL)
3642 return -ENOMEM;
3643
Bryan Schumaker7c513052011-03-24 17:12:24 +00003644 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003646 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003647 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003649 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 return status;
3651}
3652
3653static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3654{
3655 struct nfs4_exception exception = { };
3656 int err;
3657 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003658 err = _nfs4_proc_access(inode, entry);
3659 trace_nfs4_access(inode, err);
3660 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 &exception);
3662 } while (exception.retry);
3663 return err;
3664}
3665
3666/*
3667 * TODO: For the time being, we don't try to get any attributes
3668 * along with any of the zero-copy operations READ, READDIR,
3669 * READLINK, WRITE.
3670 *
3671 * In the case of the first three, we want to put the GETATTR
3672 * after the read-type operation -- this is because it is hard
3673 * to predict the length of a GETATTR response in v4, and thus
3674 * align the READ data correctly. This means that the GETATTR
3675 * may end up partially falling into the page cache, and we should
3676 * shift it into the 'tail' of the xdr_buf before processing.
3677 * To do this efficiently, we need to know the total length
3678 * of data received, which doesn't seem to be available outside
3679 * of the RPC layer.
3680 *
3681 * In the case of WRITE, we also want to put the GETATTR after
3682 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003683 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 *
3685 * Both of these changes to the XDR layer would in fact be quite
3686 * minor, but I decided to leave them for a subsequent patch.
3687 */
3688static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3689 unsigned int pgbase, unsigned int pglen)
3690{
3691 struct nfs4_readlink args = {
3692 .fh = NFS_FH(inode),
3693 .pgbase = pgbase,
3694 .pglen = pglen,
3695 .pages = &page,
3696 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003697 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 struct rpc_message msg = {
3699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3700 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003701 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 };
3703
Bryan Schumaker7c513052011-03-24 17:12:24 +00003704 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 -07003705}
3706
3707static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3708 unsigned int pgbase, unsigned int pglen)
3709{
3710 struct nfs4_exception exception = { };
3711 int err;
3712 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003713 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3714 trace_nfs4_readlink(inode, err);
3715 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 &exception);
3717 } while (exception.retry);
3718 return err;
3719}
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003722 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724static int
3725nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003726 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
David Quigleyaa9c2662013-05-22 12:50:44 -04003728 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003729 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 int status = 0;
3732
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003733 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3734 if (IS_ERR(ctx))
3735 return PTR_ERR(ctx);
3736
David Quigleyaa9c2662013-05-22 12:50:44 -04003737 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3738
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003739 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003740 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (IS_ERR(state)) {
3742 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003743 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003746 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003747 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 return status;
3749}
3750
3751static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3752{
Trond Myklebust16e42952005-10-27 22:12:44 -04003753 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003754 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003756 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003758 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003759 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3763 .rpc_argp = &args,
3764 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003766 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003767
Bryan Schumaker7c513052011-03-24 17:12:24 +00003768 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003769 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003770 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return status;
3772}
3773
3774static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3775{
3776 struct nfs4_exception exception = { };
3777 int err;
3778 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003779 err = _nfs4_proc_remove(dir, name);
3780 trace_nfs4_remove(dir, name, err);
3781 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 &exception);
3783 } while (exception.retry);
3784 return err;
3785}
3786
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003787static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003789 struct nfs_server *server = NFS_SERVER(dir);
3790 struct nfs_removeargs *args = msg->rpc_argp;
3791 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003793 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003795 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003796
3797 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003800static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3801{
Al Viro884be172016-04-28 23:56:31 -04003802 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003803 &data->args.seq_args,
3804 &data->res.seq_res,
3805 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806}
3807
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003808static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003810 struct nfs_unlinkdata *data = task->tk_calldata;
3811 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003812
Trond Myklebust14516c32010-07-31 14:29:06 -04003813 if (!nfs4_sequence_done(task, &res->seq_res))
3814 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003815 if (nfs4_async_handle_error(task, res->server, NULL,
3816 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003817 return 0;
3818 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003819 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Jeff Laytond3d41522010-09-17 17:31:57 -04003822static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3823{
3824 struct nfs_server *server = NFS_SERVER(dir);
3825 struct nfs_renameargs *arg = msg->rpc_argp;
3826 struct nfs_renameres *res = msg->rpc_resp;
3827
3828 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003829 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04003830 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003831}
3832
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003833static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3834{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003835 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3836 &data->args.seq_args,
3837 &data->res.seq_res,
3838 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003839}
3840
3841static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3842 struct inode *new_dir)
3843{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003844 struct nfs_renamedata *data = task->tk_calldata;
3845 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003846
3847 if (!nfs4_sequence_done(task, &res->seq_res))
3848 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10003849 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04003850 return 0;
3851
3852 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003853 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003854 return 1;
3855}
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3858{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003859 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 struct nfs4_link_arg arg = {
3861 .fh = NFS_FH(inode),
3862 .dir_fh = NFS_FH(dir),
3863 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003864 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003866 struct nfs4_link_res res = {
3867 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003868 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 struct rpc_message msg = {
3871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3872 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003873 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003875 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Trond Myklebust136f2622010-04-16 16:22:49 -04003877 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003878 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003879 goto out;
3880
David Quigley14c43f72013-05-22 12:50:43 -04003881 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3882 if (IS_ERR(res.label)) {
3883 status = PTR_ERR(res.label);
3884 goto out;
3885 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003886 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003887
Bryan Schumaker7c513052011-03-24 17:12:24 +00003888 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003889 if (!status) {
3890 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003891 status = nfs_post_op_update_inode(inode, res.fattr);
3892 if (!status)
3893 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003894 }
David Quigley14c43f72013-05-22 12:50:43 -04003895
3896
3897 nfs4_label_free(res.label);
3898
Trond Myklebust136f2622010-04-16 16:22:49 -04003899out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003900 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return status;
3902}
3903
3904static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3905{
3906 struct nfs4_exception exception = { };
3907 int err;
3908 do {
3909 err = nfs4_handle_exception(NFS_SERVER(inode),
3910 _nfs4_proc_link(inode, dir, name),
3911 &exception);
3912 } while (exception.retry);
3913 return err;
3914}
3915
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003916struct nfs4_createdata {
3917 struct rpc_message msg;
3918 struct nfs4_create_arg arg;
3919 struct nfs4_create_res res;
3920 struct nfs_fh fh;
3921 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003922 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003923};
3924
3925static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3926 struct qstr *name, struct iattr *sattr, u32 ftype)
3927{
3928 struct nfs4_createdata *data;
3929
3930 data = kzalloc(sizeof(*data), GFP_KERNEL);
3931 if (data != NULL) {
3932 struct nfs_server *server = NFS_SERVER(dir);
3933
David Quigley14c43f72013-05-22 12:50:43 -04003934 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3935 if (IS_ERR(data->label))
3936 goto out_free;
3937
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003938 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3939 data->msg.rpc_argp = &data->arg;
3940 data->msg.rpc_resp = &data->res;
3941 data->arg.dir_fh = NFS_FH(dir);
3942 data->arg.server = server;
3943 data->arg.name = name;
3944 data->arg.attrs = sattr;
3945 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003946 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003947 data->res.server = server;
3948 data->res.fh = &data->fh;
3949 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003950 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003951 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003952 }
3953 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003954out_free:
3955 kfree(data);
3956 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003957}
3958
3959static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3960{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003961 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003962 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003963 if (status == 0) {
3964 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003965 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003966 }
3967 return status;
3968}
3969
3970static void nfs4_free_createdata(struct nfs4_createdata *data)
3971{
David Quigley14c43f72013-05-22 12:50:43 -04003972 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003973 kfree(data);
3974}
3975
Chuck Lever4f390c12006-08-22 20:06:22 -04003976static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003977 struct page *page, unsigned int len, struct iattr *sattr,
3978 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003980 struct nfs4_createdata *data;
3981 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Chuck Lever94a6d752006-08-22 20:06:23 -04003983 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003984 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003985
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003986 status = -ENOMEM;
3987 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3988 if (data == NULL)
3989 goto out;
3990
3991 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3992 data->arg.u.symlink.pages = &page;
3993 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003994 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003996 status = nfs4_do_create(dir, dentry, data);
3997
3998 nfs4_free_createdata(data);
3999out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return status;
4001}
4002
Chuck Lever4f390c12006-08-22 20:06:22 -04004003static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004004 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
4006 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004007 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004009
4010 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004013 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4014 trace_nfs4_symlink(dir, &dentry->d_name, err);
4015 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 &exception);
4017 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004018
4019 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return err;
4021}
4022
4023static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004024 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004026 struct nfs4_createdata *data;
4027 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004029 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4030 if (data == NULL)
4031 goto out;
4032
David Quigley1775fd32013-05-22 12:50:42 -04004033 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004034 status = nfs4_do_create(dir, dentry, data);
4035
4036 nfs4_free_createdata(data);
4037out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return status;
4039}
4040
4041static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4042 struct iattr *sattr)
4043{
4044 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004045 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004047
David Quigleyaa9c2662013-05-22 12:50:44 -04004048 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4049
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004050 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004052 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4053 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4054 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 &exception);
4056 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004057 nfs4_label_release_security(label);
4058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return err;
4060}
4061
4062static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004063 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064{
David Howells2b0143b2015-03-17 22:25:59 +00004065 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 struct nfs4_readdir_arg args = {
4067 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004068 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 .pgbase = 0,
4070 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004071 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004072 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 };
4074 struct nfs4_readdir_res res;
4075 struct rpc_message msg = {
4076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4077 .rpc_argp = &args,
4078 .rpc_resp = &res,
4079 .rpc_cred = cred,
4080 };
4081 int status;
4082
Al Viro6de14722013-09-16 10:53:17 -04004083 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4084 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004085 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004086 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004088 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 -05004089 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004090 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004091 status += args.pgbase;
4092 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004093
4094 nfs_invalidate_atime(dir);
4095
Harvey Harrison3110ff82008-05-02 13:42:44 -07004096 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return status;
4098}
4099
4100static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004101 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 struct nfs4_exception exception = { };
4104 int err;
4105 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004106 err = _nfs4_proc_readdir(dentry, cred, cookie,
4107 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004108 trace_nfs4_readdir(d_inode(dentry), err);
4109 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 &exception);
4111 } while (exception.retry);
4112 return err;
4113}
4114
4115static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004116 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004118 struct nfs4_createdata *data;
4119 int mode = sattr->ia_mode;
4120 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004122 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4123 if (data == NULL)
4124 goto out;
4125
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004127 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004129 data->arg.ftype = NF4BLK;
4130 data->arg.u.device.specdata1 = MAJOR(rdev);
4131 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
4133 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004134 data->arg.ftype = NF4CHR;
4135 data->arg.u.device.specdata1 = MAJOR(rdev);
4136 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004137 } else if (!S_ISSOCK(mode)) {
4138 status = -EINVAL;
4139 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 }
David Quigley1775fd32013-05-22 12:50:42 -04004141
David Quigleyaa9c2662013-05-22 12:50:44 -04004142 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004143 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004144out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004145 nfs4_free_createdata(data);
4146out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return status;
4148}
4149
4150static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4151 struct iattr *sattr, dev_t rdev)
4152{
4153 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004154 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004156
David Quigleyaa9c2662013-05-22 12:50:44 -04004157 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4158
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004159 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004161 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4162 trace_nfs4_mknod(dir, &dentry->d_name, err);
4163 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 &exception);
4165 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004166
4167 nfs4_label_release_security(label);
4168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 return err;
4170}
4171
4172static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4173 struct nfs_fsstat *fsstat)
4174{
4175 struct nfs4_statfs_arg args = {
4176 .fh = fhandle,
4177 .bitmask = server->attr_bitmask,
4178 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004179 struct nfs4_statfs_res res = {
4180 .fsstat = fsstat,
4181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 struct rpc_message msg = {
4183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4184 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004185 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 };
4187
Trond Myklebust0e574af2005-10-27 22:12:38 -04004188 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004189 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190}
4191
4192static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4193{
4194 struct nfs4_exception exception = { };
4195 int err;
4196 do {
4197 err = nfs4_handle_exception(server,
4198 _nfs4_proc_statfs(server, fhandle, fsstat),
4199 &exception);
4200 } while (exception.retry);
4201 return err;
4202}
4203
4204static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4205 struct nfs_fsinfo *fsinfo)
4206{
4207 struct nfs4_fsinfo_arg args = {
4208 .fh = fhandle,
4209 .bitmask = server->attr_bitmask,
4210 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004211 struct nfs4_fsinfo_res res = {
4212 .fsinfo = fsinfo,
4213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 struct rpc_message msg = {
4215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4216 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004217 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 };
4219
Bryan Schumaker7c513052011-03-24 17:12:24 +00004220 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
4223static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4224{
4225 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004226 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 int err;
4228
4229 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004230 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004231 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004232 if (err == 0) {
4233 struct nfs_client *clp = server->nfs_client;
4234
4235 spin_lock(&clp->cl_lock);
4236 clp->cl_lease_time = fsinfo->lease_time * HZ;
4237 clp->cl_last_renewal = now;
4238 spin_unlock(&clp->cl_lock);
4239 break;
4240 }
4241 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 } while (exception.retry);
4243 return err;
4244}
4245
4246static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4247{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004248 int error;
4249
Trond Myklebust0e574af2005-10-27 22:12:38 -04004250 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004251 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004252 if (error == 0) {
4253 /* block layout checks this! */
4254 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04004255 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08004256 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004257
4258 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
4261static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4262 struct nfs_pathconf *pathconf)
4263{
4264 struct nfs4_pathconf_arg args = {
4265 .fh = fhandle,
4266 .bitmask = server->attr_bitmask,
4267 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004268 struct nfs4_pathconf_res res = {
4269 .pathconf = pathconf,
4270 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4273 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004274 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 };
4276
4277 /* None of the pathconf attributes are mandatory to implement */
4278 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4279 memset(pathconf, 0, sizeof(*pathconf));
4280 return 0;
4281 }
4282
Trond Myklebust0e574af2005-10-27 22:12:38 -04004283 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004284 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
4287static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4288 struct nfs_pathconf *pathconf)
4289{
4290 struct nfs4_exception exception = { };
4291 int err;
4292
4293 do {
4294 err = nfs4_handle_exception(server,
4295 _nfs4_proc_pathconf(server, fhandle, pathconf),
4296 &exception);
4297 } while (exception.retry);
4298 return err;
4299}
4300
Trond Myklebust5521abf2013-03-16 20:54:34 -04004301int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004302 const struct nfs_open_context *ctx,
4303 const struct nfs_lock_context *l_ctx,
4304 fmode_t fmode)
4305{
4306 const struct nfs_lockowner *lockowner = NULL;
4307
4308 if (l_ctx != NULL)
4309 lockowner = &l_ctx->lockowner;
Trond Myklebustabf4e132016-05-16 17:42:44 -04004310 return nfs4_select_rw_stateid(ctx->state, fmode, lockowner, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004311}
4312EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4313
Trond Myklebust5521abf2013-03-16 20:54:34 -04004314static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4315 const struct nfs_open_context *ctx,
4316 const struct nfs_lock_context *l_ctx,
4317 fmode_t fmode)
4318{
4319 nfs4_stateid current_stateid;
4320
Trond Myklebuste1253be2014-03-05 08:44:23 -05004321 /* If the current stateid represents a lost lock, then exit */
4322 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4323 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004324 return nfs4_stateid_match(stateid, &current_stateid);
4325}
4326
4327static bool nfs4_error_stateid_expired(int err)
4328{
4329 switch (err) {
4330 case -NFS4ERR_DELEG_REVOKED:
4331 case -NFS4ERR_ADMIN_REVOKED:
4332 case -NFS4ERR_BAD_STATEID:
4333 case -NFS4ERR_STALE_STATEID:
4334 case -NFS4ERR_OLD_STATEID:
4335 case -NFS4ERR_OPENMODE:
4336 case -NFS4ERR_EXPIRED:
4337 return true;
4338 }
4339 return false;
4340}
4341
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004342void __nfs4_read_done_cb(struct nfs_pgio_header *hdr)
Benny Halevyd20581a2011-05-22 19:52:03 +03004343{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004344 nfs_invalidate_atime(hdr->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03004345}
4346
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004347static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004349 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004351 trace_nfs4_read(hdr, task->tk_status);
4352 if (nfs4_async_handle_error(task, server,
NeilBrown8478eaa2014-09-18 16:09:27 +10004353 hdr->args.context->state,
4354 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004355 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05004356 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004358
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004359 __nfs4_read_done_cb(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004361 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004362 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363}
4364
Trond Myklebust5521abf2013-03-16 20:54:34 -04004365static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004366 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004367{
4368
4369 if (!nfs4_error_stateid_expired(task->tk_status) ||
4370 nfs4_stateid_is_current(&args->stateid,
4371 args->context,
4372 args->lock_context,
4373 FMODE_READ))
4374 return false;
4375 rpc_restart_call_prepare(task);
4376 return true;
4377}
4378
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004379static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004380{
4381
4382 dprintk("--> %s\n", __func__);
4383
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004384 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004385 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004386 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004387 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004388 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4389 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00004390}
4391
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004392static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4393 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004395 hdr->timestamp = jiffies;
4396 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004397 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004398 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004401static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4402 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004403{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004404 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode),
4405 &hdr->args.seq_args,
4406 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004407 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004408 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004409 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4410 hdr->args.lock_context,
4411 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004412 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004413 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004414 return -EIO;
4415 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004418static int nfs4_write_done_cb(struct rpc_task *task,
4419 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004421 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004422
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004423 trace_nfs4_write(hdr, task->tk_status);
4424 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
NeilBrown8478eaa2014-09-18 16:09:27 +10004425 hdr->args.context->state,
4426 NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004427 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004428 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004430 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004431 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004432 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004433 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
Trond Myklebust5521abf2013-03-16 20:54:34 -04004437static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004438 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004439{
4440
4441 if (!nfs4_error_stateid_expired(task->tk_status) ||
4442 nfs4_stateid_is_current(&args->stateid,
4443 args->context,
4444 args->lock_context,
4445 FMODE_WRITE))
4446 return false;
4447 rpc_restart_call_prepare(task);
4448 return true;
4449}
4450
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004451static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004452{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004453 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004454 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004455 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004456 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004457 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4458 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004459}
4460
Trond Myklebust5a37f852012-04-28 14:55:16 -04004461static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004462bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004463{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004464 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004465 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004466 return false;
4467 /* Otherwise, request attributes if and only if we don't hold
4468 * a delegation
4469 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004470 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004471}
Fred Isamana69aef12011-03-03 15:13:47 +00004472
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004473static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4474 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004476 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004477
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004478 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4479 hdr->args.bitmask = NULL;
4480 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004481 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004482 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004483
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004484 if (!hdr->pgio_done_cb)
4485 hdr->pgio_done_cb = nfs4_write_done_cb;
4486 hdr->res.server = server;
4487 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004489 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004490 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
Fred Isaman0b7c0152012-04-20 14:47:39 -04004493static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4494{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004495 nfs4_setup_sequence(NFS_SERVER(data->inode),
4496 &data->args.seq_args,
4497 &data->res.seq_res,
4498 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
Fred Isaman0b7c0152012-04-20 14:47:39 -04004501static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004504
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004505 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004506 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4507 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004508 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004509 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512}
4513
Fred Isaman0b7c0152012-04-20 14:47:39 -04004514static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004515{
4516 if (!nfs4_sequence_done(task, &data->res.seq_res))
4517 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004518 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004519}
4520
Fred Isaman0b7c0152012-04-20 14:47:39 -04004521static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004523 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004524
Fred Isaman0b7c0152012-04-20 14:47:39 -04004525 if (data->commit_done_cb == NULL)
4526 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004527 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004528 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004529 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530}
4531
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004532struct nfs4_renewdata {
4533 struct nfs_client *client;
4534 unsigned long timestamp;
4535};
4536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537/*
4538 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4539 * standalone procedure for queueing an asynchronous RENEW.
4540 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004541static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004542{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004543 struct nfs4_renewdata *data = calldata;
4544 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004545
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004546 if (atomic_read(&clp->cl_count) > 1)
4547 nfs4_schedule_state_renewal(clp);
4548 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004549 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004550}
4551
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004552static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004554 struct nfs4_renewdata *data = calldata;
4555 struct nfs_client *clp = data->client;
4556 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004558 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004559 switch (task->tk_status) {
4560 case 0:
4561 break;
4562 case -NFS4ERR_LEASE_MOVED:
4563 nfs4_schedule_lease_moved_recovery(clp);
4564 break;
4565 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004566 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004567 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4568 return;
4569 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004570 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004571 return;
4572 }
4573 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004575 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576}
4577
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004578static const struct rpc_call_ops nfs4_renew_ops = {
4579 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004580 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004581};
4582
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004583static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
4585 struct rpc_message msg = {
4586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4587 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004588 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004590 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004592 if (renew_flags == 0)
4593 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004594 if (!atomic_inc_not_zero(&clp->cl_count))
4595 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004596 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004597 if (data == NULL)
4598 return -ENOMEM;
4599 data->client = clp;
4600 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004601 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004602 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603}
4604
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004605static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
4607 struct rpc_message msg = {
4608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4609 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004610 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 };
4612 unsigned long now = jiffies;
4613 int status;
4614
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004615 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (status < 0)
4617 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004618 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 return 0;
4620}
4621
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004622static inline int nfs4_server_supports_acls(struct nfs_server *server)
4623{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004624 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004625}
4626
Trond Myklebust21f498c2012-08-24 10:59:25 -04004627/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4628 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004629 * the stack.
4630 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004631#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004632
Neil Hormane9e3d722011-03-04 19:26:03 -05004633static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004634 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004635{
4636 struct page *newpage, **spages;
4637 int rc = 0;
4638 size_t len;
4639 spages = pages;
4640
4641 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004642 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004643 newpage = alloc_page(GFP_KERNEL);
4644
4645 if (newpage == NULL)
4646 goto unwind;
4647 memcpy(page_address(newpage), buf, len);
4648 buf += len;
4649 buflen -= len;
4650 *pages++ = newpage;
4651 rc++;
4652 } while (buflen != 0);
4653
4654 return rc;
4655
4656unwind:
4657 for(; rc > 0; rc--)
4658 __free_page(spages[rc-1]);
4659 return -ENOMEM;
4660}
4661
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004662struct nfs4_cached_acl {
4663 int cached;
4664 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004665 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004666};
4667
4668static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004669{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004670 struct nfs_inode *nfsi = NFS_I(inode);
4671
4672 spin_lock(&inode->i_lock);
4673 kfree(nfsi->nfs4_acl);
4674 nfsi->nfs4_acl = acl;
4675 spin_unlock(&inode->i_lock);
4676}
4677
4678static void nfs4_zap_acl_attr(struct inode *inode)
4679{
4680 nfs4_set_cached_acl(inode, NULL);
4681}
4682
4683static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4684{
4685 struct nfs_inode *nfsi = NFS_I(inode);
4686 struct nfs4_cached_acl *acl;
4687 int ret = -ENOENT;
4688
4689 spin_lock(&inode->i_lock);
4690 acl = nfsi->nfs4_acl;
4691 if (acl == NULL)
4692 goto out;
4693 if (buf == NULL) /* user is just asking for length */
4694 goto out_len;
4695 if (acl->cached == 0)
4696 goto out;
4697 ret = -ERANGE; /* see getxattr(2) man page */
4698 if (acl->len > buflen)
4699 goto out;
4700 memcpy(buf, acl->data, acl->len);
4701out_len:
4702 ret = acl->len;
4703out:
4704 spin_unlock(&inode->i_lock);
4705 return ret;
4706}
4707
Sachin Prabhu5794d212012-04-17 14:36:40 +01004708static 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 +00004709{
4710 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004711 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004712
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004713 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004714 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004715 if (acl == NULL)
4716 goto out;
4717 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004718 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004719 } else {
4720 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4721 if (acl == NULL)
4722 goto out;
4723 acl->cached = 0;
4724 }
4725 acl->len = acl_len;
4726out:
4727 nfs4_set_cached_acl(inode, acl);
4728}
4729
Andy Adamsonbf118a32011-12-07 11:55:27 -05004730/*
4731 * The getxattr API returns the required buffer length when called with a
4732 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4733 * the required buf. On a NULL buf, we send a page of data to the server
4734 * guessing that the ACL request can be serviced by a page. If so, we cache
4735 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4736 * the cache. If not so, we throw away the page, and cache the required
4737 * length. The next getxattr call will then produce another round trip to
4738 * the server, this time with the input buf of the required size.
4739 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004740static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004741{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004742 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004743 struct nfs_getaclargs args = {
4744 .fh = NFS_FH(inode),
4745 .acl_pages = pages,
4746 .acl_len = buflen,
4747 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004748 struct nfs_getaclres res = {
4749 .acl_len = buflen,
4750 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004751 struct rpc_message msg = {
4752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4753 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004754 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004755 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004756 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4757 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004758
Andy Adamsonbf118a32011-12-07 11:55:27 -05004759 /* As long as we're doing a round trip to the server anyway,
4760 * let's be prepared for a page of acl data. */
4761 if (npages == 0)
4762 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004763 if (npages > ARRAY_SIZE(pages))
4764 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004765
Andy Adamsonbf118a32011-12-07 11:55:27 -05004766 for (i = 0; i < npages; i++) {
4767 pages[i] = alloc_page(GFP_KERNEL);
4768 if (!pages[i])
4769 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004770 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004771
4772 /* for decoding across pages */
4773 res.acl_scratch = alloc_page(GFP_KERNEL);
4774 if (!res.acl_scratch)
4775 goto out_free;
4776
Andy Adamsonbf118a32011-12-07 11:55:27 -05004777 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004778
Peng Taode040be2012-01-10 22:42:47 +08004779 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004780 __func__, buf, buflen, npages, args.acl_len);
4781 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4782 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004783 if (ret)
4784 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004785
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004786 /* Handle the case where the passed-in buffer is too short */
4787 if (res.acl_flags & NFS4_ACL_TRUNC) {
4788 /* Did the user only issue a request for the acl length? */
4789 if (buf == NULL)
4790 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004791 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004792 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004793 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004794 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004795 if (buf) {
4796 if (res.acl_len > buflen) {
4797 ret = -ERANGE;
4798 goto out_free;
4799 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004800 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004801 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004802out_ok:
4803 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004804out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004805 for (i = 0; i < npages; i++)
4806 if (pages[i])
4807 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004808 if (res.acl_scratch)
4809 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004810 return ret;
4811}
4812
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004813static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4814{
4815 struct nfs4_exception exception = { };
4816 ssize_t ret;
4817 do {
4818 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004819 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004820 if (ret >= 0)
4821 break;
4822 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4823 } while (exception.retry);
4824 return ret;
4825}
4826
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004827static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4828{
4829 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004830 int ret;
4831
4832 if (!nfs4_server_supports_acls(server))
4833 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004834 ret = nfs_revalidate_inode(server, inode);
4835 if (ret < 0)
4836 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004837 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4838 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004839 ret = nfs4_read_cached_acl(inode, buf, buflen);
4840 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004841 /* -ENOENT is returned if there is no ACL or if there is an ACL
4842 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004843 return ret;
4844 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004845}
4846
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004847static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004848{
4849 struct nfs_server *server = NFS_SERVER(inode);
4850 struct page *pages[NFS4ACL_MAXPAGES];
4851 struct nfs_setaclargs arg = {
4852 .fh = NFS_FH(inode),
4853 .acl_pages = pages,
4854 .acl_len = buflen,
4855 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004856 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004857 struct rpc_message msg = {
4858 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4859 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004860 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004861 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004862 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004863 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004864
4865 if (!nfs4_server_supports_acls(server))
4866 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004867 if (npages > ARRAY_SIZE(pages))
4868 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004869 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05004870 if (i < 0)
4871 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004872 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004873 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004874
4875 /*
4876 * Free each page after tx, so the only ref left is
4877 * held by the network stack
4878 */
4879 for (; i > 0; i--)
4880 put_page(pages[i-1]);
4881
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004882 /*
4883 * Acl update can result in inode attribute update.
4884 * so mark the attribute cache invalid.
4885 */
4886 spin_lock(&inode->i_lock);
4887 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4888 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004889 nfs_access_zap_cache(inode);
4890 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004891 return ret;
4892}
4893
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004894static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4895{
4896 struct nfs4_exception exception = { };
4897 int err;
4898 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004899 err = __nfs4_proc_set_acl(inode, buf, buflen);
4900 trace_nfs4_set_acl(inode, err);
4901 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004902 &exception);
4903 } while (exception.retry);
4904 return err;
4905}
4906
David Quigleyaa9c2662013-05-22 12:50:44 -04004907#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4908static int _nfs4_get_security_label(struct inode *inode, void *buf,
4909 size_t buflen)
4910{
4911 struct nfs_server *server = NFS_SERVER(inode);
4912 struct nfs_fattr fattr;
4913 struct nfs4_label label = {0, 0, buflen, buf};
4914
4915 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004916 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004917 .fh = NFS_FH(inode),
4918 .bitmask = bitmask,
4919 };
4920 struct nfs4_getattr_res res = {
4921 .fattr = &fattr,
4922 .label = &label,
4923 .server = server,
4924 };
4925 struct rpc_message msg = {
4926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004927 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004928 .rpc_resp = &res,
4929 };
4930 int ret;
4931
4932 nfs_fattr_init(&fattr);
4933
Trond Myklebustfcb63a92013-11-01 12:42:25 -04004934 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004935 if (ret)
4936 return ret;
4937 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4938 return -ENOENT;
4939 if (buflen < label.len)
4940 return -ERANGE;
4941 return 0;
4942}
4943
4944static int nfs4_get_security_label(struct inode *inode, void *buf,
4945 size_t buflen)
4946{
4947 struct nfs4_exception exception = { };
4948 int err;
4949
4950 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4951 return -EOPNOTSUPP;
4952
4953 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004954 err = _nfs4_get_security_label(inode, buf, buflen);
4955 trace_nfs4_get_security_label(inode, err);
4956 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004957 &exception);
4958 } while (exception.retry);
4959 return err;
4960}
4961
4962static int _nfs4_do_set_security_label(struct inode *inode,
4963 struct nfs4_label *ilabel,
4964 struct nfs_fattr *fattr,
4965 struct nfs4_label *olabel)
4966{
4967
4968 struct iattr sattr = {0};
4969 struct nfs_server *server = NFS_SERVER(inode);
4970 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04004971 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04004972 .fh = NFS_FH(inode),
4973 .iap = &sattr,
4974 .server = server,
4975 .bitmask = bitmask,
4976 .label = ilabel,
4977 };
4978 struct nfs_setattrres res = {
4979 .fattr = fattr,
4980 .label = olabel,
4981 .server = server,
4982 };
4983 struct rpc_message msg = {
4984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04004985 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04004986 .rpc_resp = &res,
4987 };
4988 int status;
4989
Jeff Layton12207f62013-11-01 10:49:32 -04004990 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04004991
Jeff Layton12207f62013-11-01 10:49:32 -04004992 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004993 if (status)
4994 dprintk("%s failed: %d\n", __func__, status);
4995
4996 return status;
4997}
4998
4999static int nfs4_do_set_security_label(struct inode *inode,
5000 struct nfs4_label *ilabel,
5001 struct nfs_fattr *fattr,
5002 struct nfs4_label *olabel)
5003{
5004 struct nfs4_exception exception = { };
5005 int err;
5006
5007 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005008 err = _nfs4_do_set_security_label(inode, ilabel,
5009 fattr, olabel);
5010 trace_nfs4_set_security_label(inode, err);
5011 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005012 &exception);
5013 } while (exception.retry);
5014 return err;
5015}
5016
5017static int
Al Viro59301222016-05-27 10:19:30 -04005018nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005019{
5020 struct nfs4_label ilabel, *olabel = NULL;
5021 struct nfs_fattr fattr;
5022 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005023 int status;
5024
5025 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5026 return -EOPNOTSUPP;
5027
5028 nfs_fattr_init(&fattr);
5029
5030 ilabel.pi = 0;
5031 ilabel.lfs = 0;
5032 ilabel.label = (char *)buf;
5033 ilabel.len = buflen;
5034
5035 cred = rpc_lookup_cred();
5036 if (IS_ERR(cred))
5037 return PTR_ERR(cred);
5038
5039 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5040 if (IS_ERR(olabel)) {
5041 status = -PTR_ERR(olabel);
5042 goto out;
5043 }
5044
5045 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5046 if (status == 0)
5047 nfs_setsecurity(inode, &fattr, olabel);
5048
5049 nfs4_label_free(olabel);
5050out:
5051 put_rpccred(cred);
5052 return status;
5053}
5054#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5055
5056
Chuck Leverf0920752012-05-21 22:45:41 -04005057static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5058 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005059{
5060 __be32 verf[2];
5061
Chuck Lever2c820d92012-05-21 22:45:33 -04005062 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5063 /* An impossible timestamp guarantees this value
5064 * will never match a generated boot time. */
5065 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04005066 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04005067 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005068 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04005069 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
5070 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04005071 }
Chuck Levercd937102012-03-02 17:14:31 -05005072 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5073}
5074
Jeff Laytona3192682015-06-09 19:43:59 -04005075static int
5076nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005077{
Jeff Laytona3192682015-06-09 19:43:59 -04005078 size_t len;
5079 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005080
Trond Myklebustceb3a162015-01-03 15:16:04 -05005081 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005082 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005083
Jeff Laytona3192682015-06-09 19:43:59 -04005084 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005085 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005086 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5087 1 +
5088 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5089 1;
5090 rcu_read_unlock();
5091
5092 if (len > NFS4_OPAQUE_LIMIT + 1)
5093 return -EINVAL;
5094
5095 /*
5096 * Since this string is allocated at mount time, and held until the
5097 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5098 * about a memory-reclaim deadlock.
5099 */
5100 str = kmalloc(len, GFP_KERNEL);
5101 if (!str)
5102 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005103
Chuck Levere984a552012-09-14 17:24:21 -04005104 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005105 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005106 clp->cl_ipaddr,
5107 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5108 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005109 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005110
Jeff Laytona3192682015-06-09 19:43:59 -04005111 clp->cl_owner_id = str;
5112 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005113}
5114
Jeff Layton873e3852015-06-09 19:44:00 -04005115static int
5116nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005117{
Jeff Layton873e3852015-06-09 19:44:00 -04005118 size_t len;
5119 char *str;
5120
5121 len = 10 + 10 + 1 + 10 + 1 +
5122 strlen(nfs4_client_id_uniquifier) + 1 +
5123 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5124
5125 if (len > NFS4_OPAQUE_LIMIT + 1)
5126 return -EINVAL;
5127
5128 /*
5129 * Since this string is allocated at mount time, and held until the
5130 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5131 * about a memory-reclaim deadlock.
5132 */
5133 str = kmalloc(len, GFP_KERNEL);
5134 if (!str)
5135 return -ENOMEM;
5136
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005137 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005138 clp->rpc_ops->version, clp->cl_minorversion,
5139 nfs4_client_id_uniquifier,
5140 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005141 clp->cl_owner_id = str;
5142 return 0;
5143}
5144
5145static int
5146nfs4_init_uniform_client_string(struct nfs_client *clp)
5147{
Jeff Layton873e3852015-06-09 19:44:00 -04005148 size_t len;
5149 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005150
5151 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005152 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005153
5154 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005155 return nfs4_init_uniquifier_client_string(clp);
5156
5157 len = 10 + 10 + 1 + 10 + 1 +
5158 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5159
5160 if (len > NFS4_OPAQUE_LIMIT + 1)
5161 return -EINVAL;
5162
5163 /*
5164 * Since this string is allocated at mount time, and held until the
5165 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5166 * about a memory-reclaim deadlock.
5167 */
5168 str = kmalloc(len, GFP_KERNEL);
5169 if (!str)
5170 return -ENOMEM;
5171
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005172 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005173 clp->rpc_ops->version, clp->cl_minorversion,
5174 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005175 clp->cl_owner_id = str;
5176 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005177}
5178
Chuck Lever706cb8d2014-03-12 12:51:47 -04005179/*
5180 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5181 * services. Advertise one based on the address family of the
5182 * clientaddr.
5183 */
5184static unsigned int
5185nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5186{
5187 if (strchr(clp->cl_ipaddr, ':') != NULL)
5188 return scnprintf(buf, len, "tcp6");
5189 else
5190 return scnprintf(buf, len, "tcp");
5191}
5192
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005193static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5194{
5195 struct nfs4_setclientid *sc = calldata;
5196
5197 if (task->tk_status == 0)
5198 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5199}
5200
5201static const struct rpc_call_ops nfs4_setclientid_ops = {
5202 .rpc_call_done = nfs4_setclientid_done,
5203};
5204
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005205/**
5206 * nfs4_proc_setclientid - Negotiate client ID
5207 * @clp: state data structure
5208 * @program: RPC program for NFSv4 callback service
5209 * @port: IP port number for NFS4 callback service
5210 * @cred: RPC credential to use for this call
5211 * @res: where to place the result
5212 *
5213 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5214 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005215int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5216 unsigned short port, struct rpc_cred *cred,
5217 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218{
5219 nfs4_verifier sc_verifier;
5220 struct nfs4_setclientid setclientid = {
5221 .sc_verifier = &sc_verifier,
5222 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005223 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 };
5225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5227 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005228 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005229 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005231 struct rpc_task *task;
5232 struct rpc_task_setup task_setup_data = {
5233 .rpc_client = clp->cl_rpcclient,
5234 .rpc_message = &msg,
5235 .callback_ops = &nfs4_setclientid_ops,
5236 .callback_data = &setclientid,
5237 .flags = RPC_TASK_TIMEOUT,
5238 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005239 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
Chuck Leverde734832012-07-11 16:30:50 -04005241 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005242 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005243
5244 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5245 status = nfs4_init_uniform_client_string(clp);
5246 else
Jeff Laytona3192682015-06-09 19:43:59 -04005247 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005248
5249 if (status)
5250 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005251
Chuck Leverde734832012-07-11 16:30:50 -04005252 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005253 setclientid.sc_netid_len =
5254 nfs4_init_callback_netid(clp,
5255 setclientid.sc_netid,
5256 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005257 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005258 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 clp->cl_ipaddr, port >> 8, port & 255);
5260
Jeff Layton3a6bb732015-06-09 19:43:57 -04005261 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005262 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005263 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005264 task = rpc_run_task(&task_setup_data);
5265 if (IS_ERR(task)) {
5266 status = PTR_ERR(task);
5267 goto out;
5268 }
5269 status = task->tk_status;
5270 if (setclientid.sc_cred) {
5271 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5272 put_rpccred(setclientid.sc_cred);
5273 }
5274 rpc_put_task(task);
5275out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005276 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005277 dprintk("NFS reply setclientid: %d\n", status);
5278 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005281/**
5282 * nfs4_proc_setclientid_confirm - Confirm client ID
5283 * @clp: state data structure
5284 * @res: result of a previous SETCLIENTID
5285 * @cred: RPC credential to use for this call
5286 *
5287 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5288 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005289int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005290 struct nfs4_setclientid_res *arg,
5291 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 struct rpc_message msg = {
5294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005295 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005296 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 int status;
5299
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005300 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5301 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5302 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005303 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005304 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005305 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return status;
5307}
5308
Trond Myklebustfe650402006-01-03 09:55:18 +01005309struct nfs4_delegreturndata {
5310 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005311 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005312 struct nfs_fh fh;
5313 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005314 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005315 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005316 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005317 struct inode *inode;
5318 bool roc;
5319 u32 roc_barrier;
Trond Myklebustfe650402006-01-03 09:55:18 +01005320};
5321
Trond Myklebustfe650402006-01-03 09:55:18 +01005322static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5323{
5324 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005325
Trond Myklebust14516c32010-07-31 14:29:06 -04005326 if (!nfs4_sequence_done(task, &data->res.seq_res))
5327 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005328
Trond Myklebustca8acf82013-08-13 10:36:56 -04005329 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005330 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005331 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005332 renew_lease(data->res.server, data->timestamp);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005333 case -NFS4ERR_ADMIN_REVOKED:
5334 case -NFS4ERR_DELEG_REVOKED:
5335 case -NFS4ERR_BAD_STATEID:
5336 case -NFS4ERR_OLD_STATEID:
5337 case -NFS4ERR_STALE_STATEID:
5338 case -NFS4ERR_EXPIRED:
5339 task->tk_status = 0;
Peng Tao039b7562014-07-03 13:05:02 +08005340 if (data->roc)
5341 pnfs_roc_set_barrier(data->inode, data->roc_barrier);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005342 break;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005343 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005344 if (nfs4_async_handle_error(task, data->res.server,
5345 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005346 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005347 return;
5348 }
5349 }
5350 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005351}
5352
5353static void nfs4_delegreturn_release(void *calldata)
5354{
Peng Tao039b7562014-07-03 13:05:02 +08005355 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005356 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005357
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005358 if (inode) {
5359 if (data->roc)
5360 pnfs_roc_release(inode);
5361 nfs_iput_and_deactive(inode);
5362 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005363 kfree(calldata);
5364}
5365
Andy Adamson938e1012009-04-01 09:22:28 -04005366static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5367{
5368 struct nfs4_delegreturndata *d_data;
5369
5370 d_data = (struct nfs4_delegreturndata *)data;
5371
Peng Tao500d7012015-09-22 11:35:22 +08005372 if (nfs4_wait_on_layoutreturn(d_data->inode, task))
5373 return;
5374
Trond Myklebust4ff376f2015-08-18 23:23:21 -05005375 if (d_data->roc)
5376 pnfs_roc_get_barrier(d_data->inode, &d_data->roc_barrier);
Peng Tao039b7562014-07-03 13:05:02 +08005377
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005378 nfs4_setup_sequence(d_data->res.server,
5379 &d_data->args.seq_args,
5380 &d_data->res.seq_res,
5381 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005382}
Andy Adamson938e1012009-04-01 09:22:28 -04005383
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005384static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005385 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005386 .rpc_call_done = nfs4_delegreturn_done,
5387 .rpc_release = nfs4_delegreturn_release,
5388};
5389
Trond Myklebuste6f81072008-01-24 18:14:34 -05005390static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005391{
5392 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005393 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005394 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005395 struct rpc_message msg = {
5396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5397 .rpc_cred = cred,
5398 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005399 struct rpc_task_setup task_setup_data = {
5400 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005401 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005402 .callback_ops = &nfs4_delegreturn_ops,
5403 .flags = RPC_TASK_ASYNC,
5404 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005405 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005406
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005407 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005408 if (data == NULL)
5409 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005410 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005411
5412 nfs4_state_protect(server->nfs_client,
5413 NFS_SP4_MACH_CRED_CLEANUP,
5414 &task_setup_data.rpc_client, &msg);
5415
Trond Myklebustfe650402006-01-03 09:55:18 +01005416 data->args.fhandle = &data->fh;
5417 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005418 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005419 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005420 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005421 data->res.fattr = &data->fattr;
5422 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005423 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005424 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005425 data->rpc_status = 0;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005426 data->inode = nfs_igrab_and_active(inode);
5427 if (data->inode)
5428 data->roc = nfs4_roc(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005429
Trond Myklebustc970aa82007-07-14 15:39:59 -04005430 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005431 msg.rpc_argp = &data->args;
5432 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005433 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005434 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005435 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005436 if (!issync)
5437 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005438 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005439 if (status != 0)
5440 goto out;
5441 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04005442 if (status == 0)
5443 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
5444 else
5445 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005446out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005447 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449}
5450
Trond Myklebuste6f81072008-01-24 18:14:34 -05005451int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452{
5453 struct nfs_server *server = NFS_SERVER(inode);
5454 struct nfs4_exception exception = { };
5455 int err;
5456 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005457 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005458 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 switch (err) {
5460 case -NFS4ERR_STALE_STATEID:
5461 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 case 0:
5463 return 0;
5464 }
5465 err = nfs4_handle_exception(server, err, &exception);
5466 } while (exception.retry);
5467 return err;
5468}
5469
5470#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
5471#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
5472
5473/*
5474 * sleep, with exponential backoff, and retry the LOCK operation.
5475 */
5476static unsigned long
5477nfs4_set_lock_task_retry(unsigned long timeout)
5478{
Colin Cross416ad3c2013-05-06 23:50:06 +00005479 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 timeout <<= 1;
5481 if (timeout > NFS4_LOCK_MAXTIMEOUT)
5482 return NFS4_LOCK_MAXTIMEOUT;
5483 return timeout;
5484}
5485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5487{
5488 struct inode *inode = state->inode;
5489 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005490 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005491 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005493 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005495 struct nfs_lockt_res res = {
5496 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 };
5498 struct rpc_message msg = {
5499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5500 .rpc_argp = &arg,
5501 .rpc_resp = &res,
5502 .rpc_cred = state->owner->so_cred,
5503 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 struct nfs4_lock_state *lsp;
5505 int status;
5506
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005507 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005508 status = nfs4_set_lock_state(state, request);
5509 if (status != 0)
5510 goto out;
5511 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005512 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005513 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005514 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005515 switch (status) {
5516 case 0:
5517 request->fl_type = F_UNLCK;
5518 break;
5519 case -NFS4ERR_DENIED:
5520 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005522 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005523 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005524out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 return status;
5526}
5527
5528static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5529{
5530 struct nfs4_exception exception = { };
5531 int err;
5532
5533 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005534 err = _nfs4_proc_getlk(state, cmd, request);
5535 trace_nfs4_get_lock(request, state, cmd, err);
5536 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 &exception);
5538 } while (exception.retry);
5539 return err;
5540}
5541
Jeff Layton83bfff22015-07-11 06:43:03 -04005542static int do_vfs_lock(struct inode *inode, struct file_lock *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543{
Benjamin Coddington4f656362015-10-22 13:38:14 -04005544 return locks_lock_inode_wait(inode, fl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545}
5546
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005547struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005548 struct nfs_locku_args arg;
5549 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005550 struct nfs4_lock_state *lsp;
5551 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005552 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005553 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005554 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005555};
5556
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005557static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5558 struct nfs_open_context *ctx,
5559 struct nfs4_lock_state *lsp,
5560 struct nfs_seqid *seqid)
5561{
5562 struct nfs4_unlockdata *p;
5563 struct inode *inode = lsp->ls_state->inode;
5564
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005565 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005566 if (p == NULL)
5567 return NULL;
5568 p->arg.fh = NFS_FH(inode);
5569 p->arg.fl = &p->fl;
5570 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005571 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005572 p->lsp = lsp;
5573 atomic_inc(&lsp->ls_count);
5574 /* Ensure we don't close file until we're done freeing locks! */
5575 p->ctx = get_nfs_open_context(ctx);
5576 memcpy(&p->fl, fl, sizeof(p->fl));
5577 p->server = NFS_SERVER(inode);
5578 return p;
5579}
5580
Trond Myklebust06f814a2006-01-03 09:55:07 +01005581static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005582{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005583 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005584 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005585 nfs4_put_lock_state(calldata->lsp);
5586 put_nfs_open_context(calldata->ctx);
5587 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005588}
5589
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005590static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005591{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005592 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005593
Trond Myklebust14516c32010-07-31 14:29:06 -04005594 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5595 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005596 switch (task->tk_status) {
5597 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005598 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton83bfff22015-07-11 06:43:03 -04005599 do_vfs_lock(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005600 if (nfs4_update_lock_stateid(calldata->lsp,
5601 &calldata->res.stateid))
5602 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005603 case -NFS4ERR_BAD_STATEID:
5604 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005605 case -NFS4ERR_STALE_STATEID:
5606 case -NFS4ERR_EXPIRED:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005607 if (!nfs4_stateid_match(&calldata->arg.stateid,
5608 &calldata->lsp->ls_stateid))
5609 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005610 break;
5611 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005612 if (nfs4_async_handle_error(task, calldata->server,
5613 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005614 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005615 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005616 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005617}
5618
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005619static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005620{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005621 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005623 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005624 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005625 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005626 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005627 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005628 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005629 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005630 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005631 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005632 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005633 &calldata->res.seq_res,
5634 task) != 0)
5635 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005636 return;
5637out_no_action:
5638 task->tk_action = NULL;
5639out_wait:
5640 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641}
5642
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005643static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005644 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005645 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005646 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005647};
5648
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005649static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5650 struct nfs_open_context *ctx,
5651 struct nfs4_lock_state *lsp,
5652 struct nfs_seqid *seqid)
5653{
5654 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005655 struct rpc_message msg = {
5656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5657 .rpc_cred = ctx->cred,
5658 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005659 struct rpc_task_setup task_setup_data = {
5660 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005661 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005662 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005663 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005664 .flags = RPC_TASK_ASYNC,
5665 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005666
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005667 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5668 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5669
Frank Filz137d6ac2007-07-09 15:32:29 -07005670 /* Ensure this is an unlock - when canceling a lock, the
5671 * canceled lock is passed in, and it won't be an unlock.
5672 */
5673 fl->fl_type = F_UNLCK;
5674
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005675 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5676 if (data == NULL) {
5677 nfs_free_seqid(seqid);
5678 return ERR_PTR(-ENOMEM);
5679 }
5680
Chuck Levera9c92d62013-08-09 12:48:18 -04005681 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005682 msg.rpc_argp = &data->arg;
5683 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005684 task_setup_data.callback_data = data;
5685 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005686}
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5689{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005690 struct inode *inode = state->inode;
5691 struct nfs4_state_owner *sp = state->owner;
5692 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005693 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005694 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005695 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005696 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005697 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005698 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699
Trond Myklebust9b073572006-06-29 16:38:34 -04005700 status = nfs4_set_lock_state(state, request);
5701 /* Unlock _before_ we do the RPC call */
5702 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005703 /* Exclude nfs_delegation_claim_locks() */
5704 mutex_lock(&sp->so_delegreturn_mutex);
5705 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005706 down_read(&nfsi->rwsem);
Jeff Layton83bfff22015-07-11 06:43:03 -04005707 if (do_vfs_lock(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005708 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005709 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005710 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005711 }
5712 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005713 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005714 if (status != 0)
5715 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005716 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005717 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005718 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5719 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005720 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5721 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005722 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005723 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005724 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005725 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005726 status = PTR_ERR(task);
5727 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005728 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005729 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005730 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005731out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005732 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005733 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005734 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735}
5736
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005737struct nfs4_lockdata {
5738 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005739 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005740 struct nfs4_lock_state *lsp;
5741 struct nfs_open_context *ctx;
5742 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005743 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005744 int rpc_status;
5745 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005746 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005747};
5748
5749static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005750 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5751 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005752{
5753 struct nfs4_lockdata *p;
5754 struct inode *inode = lsp->ls_state->inode;
5755 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05005756 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005757
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005758 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005759 if (p == NULL)
5760 return NULL;
5761
5762 p->arg.fh = NFS_FH(inode);
5763 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005764 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005765 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005766 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005767 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
5768 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005769 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005770 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04005771 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005772 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005773 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005774 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005775 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005776 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005777 atomic_inc(&lsp->ls_count);
5778 p->ctx = get_nfs_open_context(ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005779 get_file(fl->fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005780 memcpy(&p->fl, fl, sizeof(p->fl));
5781 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005782out_free_seqid:
5783 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005784out_free:
5785 kfree(p);
5786 return NULL;
5787}
5788
5789static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5790{
5791 struct nfs4_lockdata *data = calldata;
5792 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793
Harvey Harrison3110ff82008-05-02 13:42:44 -07005794 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005795 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005796 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005797 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05005798 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005799 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005800 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005801 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05005802 nfs4_stateid_copy(&data->arg.open_stateid,
5803 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005804 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005805 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005806 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005807 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005808 nfs4_stateid_copy(&data->arg.lock_stateid,
5809 &data->lsp->ls_stateid);
5810 }
Trond Myklebust5d422302013-03-14 16:57:48 -04005811 if (!nfs4_valid_open_stateid(state)) {
5812 data->rpc_status = -EBADF;
5813 task->tk_action = NULL;
5814 goto out_release_open_seqid;
5815 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005816 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005817 if (nfs4_setup_sequence(data->server,
5818 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005819 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005820 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005821 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005822out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005823 nfs_release_seqid(data->arg.open_seqid);
5824out_release_lock_seqid:
5825 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005826out_wait:
5827 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005828 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005829}
5830
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005831static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5832{
5833 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05005834 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005835
Harvey Harrison3110ff82008-05-02 13:42:44 -07005836 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005837
Trond Myklebust14516c32010-07-31 14:29:06 -04005838 if (!nfs4_sequence_done(task, &data->res.seq_res))
5839 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005840
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005841 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005842 switch (task->tk_status) {
5843 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00005844 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05005845 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005846 if (data->arg.new_lock) {
5847 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton83bfff22015-07-11 06:43:03 -04005848 if (do_vfs_lock(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05005849 rpc_restart_call_prepare(task);
5850 break;
5851 }
5852 }
Trond Myklebust39071e62015-01-24 15:07:56 -05005853 if (data->arg.new_lock_owner != 0) {
5854 nfs_confirm_seqid(&lsp->ls_seqid, 0);
5855 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
5856 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
5857 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
5858 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05005859 break;
5860 case -NFS4ERR_BAD_STATEID:
5861 case -NFS4ERR_OLD_STATEID:
5862 case -NFS4ERR_STALE_STATEID:
5863 case -NFS4ERR_EXPIRED:
5864 if (data->arg.new_lock_owner != 0) {
5865 if (!nfs4_stateid_match(&data->arg.open_stateid,
5866 &lsp->ls_state->open_stateid))
5867 rpc_restart_call_prepare(task);
5868 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
5869 &lsp->ls_stateid))
5870 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005871 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07005872 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005873}
5874
5875static void nfs4_lock_release(void *calldata)
5876{
5877 struct nfs4_lockdata *data = calldata;
5878
Harvey Harrison3110ff82008-05-02 13:42:44 -07005879 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005880 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005881 if (data->cancelled != 0) {
5882 struct rpc_task *task;
5883 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5884 data->arg.lock_seqid);
5885 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005886 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005887 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005888 } else
5889 nfs_free_seqid(data->arg.lock_seqid);
5890 nfs4_put_lock_state(data->lsp);
5891 put_nfs_open_context(data->ctx);
Jeff Laytonfeaff8e2015-05-12 15:48:10 -04005892 fput(data->fl.fl_file);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005893 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005894 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005895}
5896
5897static const struct rpc_call_ops nfs4_lock_ops = {
5898 .rpc_call_prepare = nfs4_lock_prepare,
5899 .rpc_call_done = nfs4_lock_done,
5900 .rpc_release = nfs4_lock_release,
5901};
5902
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005903static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5904{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005905 switch (error) {
5906 case -NFS4ERR_ADMIN_REVOKED:
5907 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005908 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005909 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005910 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005911 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005912 break;
5913 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005914 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005915 case -NFS4ERR_EXPIRED:
5916 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005917 };
5918}
5919
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005920static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005921{
5922 struct nfs4_lockdata *data;
5923 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005924 struct rpc_message msg = {
5925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5926 .rpc_cred = state->owner->so_cred,
5927 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005928 struct rpc_task_setup task_setup_data = {
5929 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005930 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005931 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005932 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005933 .flags = RPC_TASK_ASYNC,
5934 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005935 int ret;
5936
Harvey Harrison3110ff82008-05-02 13:42:44 -07005937 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005938 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005939 fl->fl_u.nfs4_fl.owner,
5940 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005941 if (data == NULL)
5942 return -ENOMEM;
5943 if (IS_SETLKW(cmd))
5944 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04005945 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005946 msg.rpc_argp = &data->arg;
5947 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005948 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005949 if (recovery_type > NFS_LOCK_NEW) {
5950 if (recovery_type == NFS_LOCK_RECLAIM)
5951 data->arg.reclaim = NFS_LOCK_RECLAIM;
5952 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005953 } else
5954 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005955 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005956 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005957 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005958 ret = nfs4_wait_for_completion_rpc_task(task);
5959 if (ret == 0) {
5960 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005961 if (ret)
5962 nfs4_handle_setlk_error(data->server, data->lsp,
5963 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005964 } else
5965 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005966 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005967 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005968 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
5971
5972static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5973{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005974 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005975 struct nfs4_exception exception = {
5976 .inode = state->inode,
5977 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005978 int err;
5979
5980 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005981 /* Cache the lock if possible... */
5982 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5983 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005984 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04005985 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005986 break;
5987 nfs4_handle_exception(server, err, &exception);
5988 } while (exception.retry);
5989 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990}
5991
5992static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5993{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005994 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005995 struct nfs4_exception exception = {
5996 .inode = state->inode,
5997 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005998 int err;
5999
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006000 err = nfs4_set_lock_state(state, request);
6001 if (err != 0)
6002 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006003 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006004 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6005 return 0;
6006 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006007 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006008 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6009 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006010 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006011 switch (err) {
6012 default:
6013 goto out;
6014 case -NFS4ERR_GRACE:
6015 case -NFS4ERR_DELAY:
6016 nfs4_handle_exception(server, err, &exception);
6017 err = 0;
6018 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006019 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006020out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022}
6023
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006024#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006025/**
6026 * nfs41_check_expired_locks - possibly free a lock stateid
6027 *
6028 * @state: NFSv4 state for an inode
6029 *
6030 * Returns NFS_OK if recovery for this stateid is now finished.
6031 * Otherwise a negative NFS4ERR value is returned.
6032 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006033static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006034{
Chuck Levereb64cf92012-07-11 16:30:05 -04006035 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006036 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006037 struct nfs_server *server = NFS_SERVER(state->inode);
6038
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006039 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04006040 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006041 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
6042
6043 status = nfs41_test_stateid(server,
6044 &lsp->ls_stateid,
6045 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04006046 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006047 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04006048 /* Free the stateid unless the server
6049 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04006050 if (status != -NFS4ERR_BAD_STATEID)
6051 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04006052 &lsp->ls_stateid,
6053 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006054 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006055 ret = status;
6056 }
6057 }
6058 };
6059
6060 return ret;
6061}
6062
6063static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6064{
6065 int status = NFS_OK;
6066
6067 if (test_bit(LK_STATE_IN_USE, &state->flags))
6068 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04006069 if (status != NFS_OK)
6070 status = nfs4_lock_expired(state, request);
6071 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006072}
6073#endif
6074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6076{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006077 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006078 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006079 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006080 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
Trond Myklebust8e469eb2010-01-26 15:42:30 -05006082 if ((fl_flags & FL_POSIX) &&
6083 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6084 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006085 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006086 status = nfs4_set_lock_state(state, request);
6087 if (status != 0)
6088 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006089 request->fl_flags |= FL_ACCESS;
Jeff Layton83bfff22015-07-11 06:43:03 -04006090 status = do_vfs_lock(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006091 if (status < 0)
6092 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006093 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006094 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006095 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006096 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006097 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006098 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton83bfff22015-07-11 06:43:03 -04006099 status = do_vfs_lock(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006100 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006101 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006102 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006103 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05006104 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006105 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006106 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006107out:
6108 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 return status;
6110}
6111
6112static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6113{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006114 struct nfs4_exception exception = {
6115 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006116 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006117 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 int err;
6119
6120 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006121 err = _nfs4_proc_setlk(state, cmd, request);
6122 if (err == -NFS4ERR_DENIED)
6123 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006125 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 } while (exception.retry);
6127 return err;
6128}
6129
6130static int
6131nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6132{
6133 struct nfs_open_context *ctx;
6134 struct nfs4_state *state;
6135 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6136 int status;
6137
6138 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006139 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 state = ctx->state;
6141
6142 if (request->fl_start < 0 || request->fl_end < 0)
6143 return -EINVAL;
6144
Trond Myklebustd9531262009-07-21 19:22:38 -04006145 if (IS_GETLK(cmd)) {
6146 if (state != NULL)
6147 return nfs4_proc_getlk(state, F_GETLK, request);
6148 return 0;
6149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6152 return -EINVAL;
6153
Trond Myklebustd9531262009-07-21 19:22:38 -04006154 if (request->fl_type == F_UNLCK) {
6155 if (state != NULL)
6156 return nfs4_proc_unlck(state, cmd, request);
6157 return 0;
6158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
Trond Myklebustd9531262009-07-21 19:22:38 -04006160 if (state == NULL)
6161 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04006162 /*
6163 * Don't rely on the VFS having checked the file open mode,
6164 * since it won't do this for flock() locks.
6165 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006166 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006167 case F_RDLCK:
6168 if (!(filp->f_mode & FMODE_READ))
6169 return -EBADF;
6170 break;
6171 case F_WRLCK:
6172 if (!(filp->f_mode & FMODE_WRITE))
6173 return -EBADF;
6174 }
6175
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 do {
6177 status = nfs4_proc_setlk(state, cmd, request);
6178 if ((status != -EAGAIN) || IS_SETLK(cmd))
6179 break;
6180 timeout = nfs4_set_lock_task_retry(timeout);
6181 status = -ERESTARTSYS;
6182 if (signalled())
6183 break;
6184 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 return status;
6186}
6187
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006188int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006189{
6190 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006191 int err;
6192
6193 err = nfs4_set_lock_state(state, fl);
6194 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006195 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006196 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04006197 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006198}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006199
Trond Myklebustcf470c32012-03-07 13:49:12 -05006200struct nfs_release_lockowner_data {
6201 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006202 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006203 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006204 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006205 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006206};
6207
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006208static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6209{
6210 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006211 struct nfs_server *server = data->server;
Peng Taocb04ad22014-06-11 05:24:15 +08006212 nfs40_setup_sequence(server->nfs_client->cl_slot_tbl,
6213 &data->args.seq_args, &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006214 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006215 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006216}
6217
6218static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6219{
6220 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006221 struct nfs_server *server = data->server;
6222
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006223 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006224
6225 switch (task->tk_status) {
6226 case 0:
6227 renew_lease(server, data->timestamp);
6228 break;
6229 case -NFS4ERR_STALE_CLIENTID:
6230 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006231 nfs4_schedule_lease_recovery(server->nfs_client);
6232 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006233 case -NFS4ERR_LEASE_MOVED:
6234 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006235 if (nfs4_async_handle_error(task, server,
6236 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006237 rpc_restart_call_prepare(task);
6238 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006239}
6240
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006241static void nfs4_release_lockowner_release(void *calldata)
6242{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006243 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006244 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006245 kfree(calldata);
6246}
6247
Trond Myklebust17280172012-03-11 13:11:00 -04006248static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006249 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6250 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006251 .rpc_release = nfs4_release_lockowner_release,
6252};
6253
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006254static void
6255nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006256{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006257 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006258 struct rpc_message msg = {
6259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6260 };
6261
6262 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006263 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006264
Trond Myklebustcf470c32012-03-07 13:49:12 -05006265 data = kmalloc(sizeof(*data), GFP_NOFS);
6266 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006267 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006268 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006269 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006270 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6271 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6272 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006273
Trond Myklebustcf470c32012-03-07 13:49:12 -05006274 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006275 msg.rpc_resp = &data->res;
6276 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006277 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006278}
6279
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006280#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6281
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006282static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006283 struct dentry *unused, struct inode *inode,
6284 const char *key, const void *buf,
6285 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006286{
Al Viro59301222016-05-27 10:19:30 -04006287 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006288}
6289
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006290static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006291 struct dentry *unused, struct inode *inode,
6292 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006293{
Al Virob2968212016-04-10 20:48:24 -04006294 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006295}
6296
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006297static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006298{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006299 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006300}
6301
David Quigleyc9bccef2013-05-22 12:50:45 -04006302#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006303
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006304static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006305 struct dentry *unused, struct inode *inode,
6306 const char *key, const void *buf,
6307 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006308{
6309 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006310 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006311
6312 return -EOPNOTSUPP;
6313}
6314
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006315static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006316 struct dentry *unused, struct inode *inode,
6317 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006318{
6319 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006320 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006321 return -EOPNOTSUPP;
6322}
6323
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006324static ssize_t
6325nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006326{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006327 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006328
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006329 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6330 len = security_inode_listsecurity(inode, list, list_len);
6331 if (list_len && len > list_len)
6332 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006333 }
6334 return len;
6335}
6336
6337static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6338 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006339 .get = nfs4_xattr_get_nfs4_label,
6340 .set = nfs4_xattr_set_nfs4_label,
6341};
David Quigleyc9bccef2013-05-22 12:50:45 -04006342
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006343#else
6344
6345static ssize_t
6346nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6347{
6348 return 0;
6349}
6350
6351#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006352
Andy Adamson533eb462011-06-13 18:25:56 -04006353/*
6354 * nfs_fhget will use either the mounted_on_fileid or the fileid
6355 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006356static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6357{
Andy Adamson533eb462011-06-13 18:25:56 -04006358 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6359 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6360 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006361 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006362 return;
6363
6364 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006365 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006366 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6367 fattr->nlink = 2;
6368}
6369
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006370static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6371 const struct qstr *name,
6372 struct nfs4_fs_locations *fs_locations,
6373 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006374{
6375 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006376 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006377 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006378 };
6379 struct nfs4_fs_locations_arg args = {
6380 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006381 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006382 .page = page,
6383 .bitmask = bitmask,
6384 };
Benny Halevy22958462009-04-01 09:22:02 -04006385 struct nfs4_fs_locations_res res = {
6386 .fs_locations = fs_locations,
6387 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006388 struct rpc_message msg = {
6389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6390 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006391 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006392 };
6393 int status;
6394
Harvey Harrison3110ff82008-05-02 13:42:44 -07006395 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006396
6397 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6398 * is not supported */
6399 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6400 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6401 else
6402 bitmask[0] |= FATTR4_WORD0_FILEID;
6403
Trond Myklebustc228fd32007-01-13 02:28:11 -05006404 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006405 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006406 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006407 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006408 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006409 return status;
6410}
6411
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006412int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6413 const struct qstr *name,
6414 struct nfs4_fs_locations *fs_locations,
6415 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006416{
6417 struct nfs4_exception exception = { };
6418 int err;
6419 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006420 err = _nfs4_proc_fs_locations(client, dir, name,
6421 fs_locations, page);
6422 trace_nfs4_get_fs_locations(dir, name, err);
6423 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006424 &exception);
6425 } while (exception.retry);
6426 return err;
6427}
6428
Chuck Leverb03d7352013-10-17 14:12:50 -04006429/*
6430 * This operation also signals the server that this client is
6431 * performing migration recovery. The server can stop returning
6432 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6433 * appended to this compound to identify the client ID which is
6434 * performing recovery.
6435 */
6436static int _nfs40_proc_get_locations(struct inode *inode,
6437 struct nfs4_fs_locations *locations,
6438 struct page *page, struct rpc_cred *cred)
6439{
6440 struct nfs_server *server = NFS_SERVER(inode);
6441 struct rpc_clnt *clnt = server->client;
6442 u32 bitmask[2] = {
6443 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6444 };
6445 struct nfs4_fs_locations_arg args = {
6446 .clientid = server->nfs_client->cl_clientid,
6447 .fh = NFS_FH(inode),
6448 .page = page,
6449 .bitmask = bitmask,
6450 .migration = 1, /* skip LOOKUP */
6451 .renew = 1, /* append RENEW */
6452 };
6453 struct nfs4_fs_locations_res res = {
6454 .fs_locations = locations,
6455 .migration = 1,
6456 .renew = 1,
6457 };
6458 struct rpc_message msg = {
6459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6460 .rpc_argp = &args,
6461 .rpc_resp = &res,
6462 .rpc_cred = cred,
6463 };
6464 unsigned long now = jiffies;
6465 int status;
6466
6467 nfs_fattr_init(&locations->fattr);
6468 locations->server = server;
6469 locations->nlocations = 0;
6470
6471 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6472 nfs4_set_sequence_privileged(&args.seq_args);
6473 status = nfs4_call_sync_sequence(clnt, server, &msg,
6474 &args.seq_args, &res.seq_res);
6475 if (status)
6476 return status;
6477
6478 renew_lease(server, now);
6479 return 0;
6480}
6481
6482#ifdef CONFIG_NFS_V4_1
6483
6484/*
6485 * This operation also signals the server that this client is
6486 * performing migration recovery. The server can stop asserting
6487 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6488 * performing this operation is identified in the SEQUENCE
6489 * operation in this compound.
6490 *
6491 * When the client supports GETATTR(fs_locations_info), it can
6492 * be plumbed in here.
6493 */
6494static int _nfs41_proc_get_locations(struct inode *inode,
6495 struct nfs4_fs_locations *locations,
6496 struct page *page, struct rpc_cred *cred)
6497{
6498 struct nfs_server *server = NFS_SERVER(inode);
6499 struct rpc_clnt *clnt = server->client;
6500 u32 bitmask[2] = {
6501 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6502 };
6503 struct nfs4_fs_locations_arg args = {
6504 .fh = NFS_FH(inode),
6505 .page = page,
6506 .bitmask = bitmask,
6507 .migration = 1, /* skip LOOKUP */
6508 };
6509 struct nfs4_fs_locations_res res = {
6510 .fs_locations = locations,
6511 .migration = 1,
6512 };
6513 struct rpc_message msg = {
6514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6515 .rpc_argp = &args,
6516 .rpc_resp = &res,
6517 .rpc_cred = cred,
6518 };
6519 int status;
6520
6521 nfs_fattr_init(&locations->fattr);
6522 locations->server = server;
6523 locations->nlocations = 0;
6524
6525 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6526 nfs4_set_sequence_privileged(&args.seq_args);
6527 status = nfs4_call_sync_sequence(clnt, server, &msg,
6528 &args.seq_args, &res.seq_res);
6529 if (status == NFS4_OK &&
6530 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6531 status = -NFS4ERR_LEASE_MOVED;
6532 return status;
6533}
6534
6535#endif /* CONFIG_NFS_V4_1 */
6536
6537/**
6538 * nfs4_proc_get_locations - discover locations for a migrated FSID
6539 * @inode: inode on FSID that is migrating
6540 * @locations: result of query
6541 * @page: buffer
6542 * @cred: credential to use for this operation
6543 *
6544 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6545 * operation failed, or a negative errno if a local error occurred.
6546 *
6547 * On success, "locations" is filled in, but if the server has
6548 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6549 * asserted.
6550 *
6551 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6552 * from this client that require migration recovery.
6553 */
6554int nfs4_proc_get_locations(struct inode *inode,
6555 struct nfs4_fs_locations *locations,
6556 struct page *page, struct rpc_cred *cred)
6557{
6558 struct nfs_server *server = NFS_SERVER(inode);
6559 struct nfs_client *clp = server->nfs_client;
6560 const struct nfs4_mig_recovery_ops *ops =
6561 clp->cl_mvops->mig_recovery_ops;
6562 struct nfs4_exception exception = { };
6563 int status;
6564
6565 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6566 (unsigned long long)server->fsid.major,
6567 (unsigned long long)server->fsid.minor,
6568 clp->cl_hostname);
6569 nfs_display_fhandle(NFS_FH(inode), __func__);
6570
6571 do {
6572 status = ops->get_locations(inode, locations, page, cred);
6573 if (status != -NFS4ERR_DELAY)
6574 break;
6575 nfs4_handle_exception(server, status, &exception);
6576 } while (exception.retry);
6577 return status;
6578}
6579
Chuck Lever44c99932013-10-17 14:13:30 -04006580/*
6581 * This operation also signals the server that this client is
6582 * performing "lease moved" recovery. The server can stop
6583 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6584 * is appended to this compound to identify the client ID which is
6585 * performing recovery.
6586 */
6587static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6588{
6589 struct nfs_server *server = NFS_SERVER(inode);
6590 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6591 struct rpc_clnt *clnt = server->client;
6592 struct nfs4_fsid_present_arg args = {
6593 .fh = NFS_FH(inode),
6594 .clientid = clp->cl_clientid,
6595 .renew = 1, /* append RENEW */
6596 };
6597 struct nfs4_fsid_present_res res = {
6598 .renew = 1,
6599 };
6600 struct rpc_message msg = {
6601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6602 .rpc_argp = &args,
6603 .rpc_resp = &res,
6604 .rpc_cred = cred,
6605 };
6606 unsigned long now = jiffies;
6607 int status;
6608
6609 res.fh = nfs_alloc_fhandle();
6610 if (res.fh == NULL)
6611 return -ENOMEM;
6612
6613 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6614 nfs4_set_sequence_privileged(&args.seq_args);
6615 status = nfs4_call_sync_sequence(clnt, server, &msg,
6616 &args.seq_args, &res.seq_res);
6617 nfs_free_fhandle(res.fh);
6618 if (status)
6619 return status;
6620
6621 do_renew_lease(clp, now);
6622 return 0;
6623}
6624
6625#ifdef CONFIG_NFS_V4_1
6626
6627/*
6628 * This operation also signals the server that this client is
6629 * performing "lease moved" recovery. The server can stop asserting
6630 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6631 * this operation is identified in the SEQUENCE operation in this
6632 * compound.
6633 */
6634static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6635{
6636 struct nfs_server *server = NFS_SERVER(inode);
6637 struct rpc_clnt *clnt = server->client;
6638 struct nfs4_fsid_present_arg args = {
6639 .fh = NFS_FH(inode),
6640 };
6641 struct nfs4_fsid_present_res res = {
6642 };
6643 struct rpc_message msg = {
6644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6645 .rpc_argp = &args,
6646 .rpc_resp = &res,
6647 .rpc_cred = cred,
6648 };
6649 int status;
6650
6651 res.fh = nfs_alloc_fhandle();
6652 if (res.fh == NULL)
6653 return -ENOMEM;
6654
6655 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6656 nfs4_set_sequence_privileged(&args.seq_args);
6657 status = nfs4_call_sync_sequence(clnt, server, &msg,
6658 &args.seq_args, &res.seq_res);
6659 nfs_free_fhandle(res.fh);
6660 if (status == NFS4_OK &&
6661 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6662 status = -NFS4ERR_LEASE_MOVED;
6663 return status;
6664}
6665
6666#endif /* CONFIG_NFS_V4_1 */
6667
6668/**
6669 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6670 * @inode: inode on FSID to check
6671 * @cred: credential to use for this operation
6672 *
6673 * Server indicates whether the FSID is present, moved, or not
6674 * recognized. This operation is necessary to clear a LEASE_MOVED
6675 * condition for this client ID.
6676 *
6677 * Returns NFS4_OK if the FSID is present on this server,
6678 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6679 * NFS4ERR code if some error occurred on the server, or a
6680 * negative errno if a local failure occurred.
6681 */
6682int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6683{
6684 struct nfs_server *server = NFS_SERVER(inode);
6685 struct nfs_client *clp = server->nfs_client;
6686 const struct nfs4_mig_recovery_ops *ops =
6687 clp->cl_mvops->mig_recovery_ops;
6688 struct nfs4_exception exception = { };
6689 int status;
6690
6691 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6692 (unsigned long long)server->fsid.major,
6693 (unsigned long long)server->fsid.minor,
6694 clp->cl_hostname);
6695 nfs_display_fhandle(NFS_FH(inode), __func__);
6696
6697 do {
6698 status = ops->fsid_present(inode, cred);
6699 if (status != -NFS4ERR_DELAY)
6700 break;
6701 nfs4_handle_exception(server, status, &exception);
6702 } while (exception.retry);
6703 return status;
6704}
6705
Andy Adamson5ec16a82013-08-08 10:57:55 -04006706/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006707 * If 'use_integrity' is true and the state managment nfs_client
6708 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
6709 * and the machine credential as per RFC3530bis and RFC5661 Security
6710 * Considerations sections. Otherwise, just use the user cred with the
6711 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04006712 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006713static 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 +00006714{
6715 int status;
6716 struct nfs4_secinfo_arg args = {
6717 .dir_fh = NFS_FH(dir),
6718 .name = name,
6719 };
6720 struct nfs4_secinfo_res res = {
6721 .flavors = flavors,
6722 };
6723 struct rpc_message msg = {
6724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
6725 .rpc_argp = &args,
6726 .rpc_resp = &res,
6727 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006728 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006729 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006730
6731 if (use_integrity) {
6732 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006733 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
6734 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006735 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006736
6737 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006738
6739 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
6740 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
6741
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006742 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
6743 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006744 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006745
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04006746 if (cred)
6747 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006748
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006749 return status;
6750}
6751
Bryan Schumaker72de53e2012-04-27 13:27:40 -04006752int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
6753 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006754{
6755 struct nfs4_exception exception = { };
6756 int err;
6757 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04006758 err = -NFS4ERR_WRONGSEC;
6759
6760 /* try to use integrity protection with machine cred */
6761 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
6762 err = _nfs4_proc_secinfo(dir, name, flavors, true);
6763
6764 /*
6765 * if unable to use integrity protection, or SECINFO with
6766 * integrity protection returns NFS4ERR_WRONGSEC (which is
6767 * disallowed by spec, but exists in deployed servers) use
6768 * the current filesystem's rpc_client and the user cred.
6769 */
6770 if (err == -NFS4ERR_WRONGSEC)
6771 err = _nfs4_proc_secinfo(dir, name, flavors, false);
6772
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006773 trace_nfs4_secinfo(dir, name, err);
6774 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006775 &exception);
6776 } while (exception.retry);
6777 return err;
6778}
6779
Andy Adamson557134a2009-04-01 09:21:53 -04006780#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04006781/*
Andy Adamson357f54d2010-12-14 10:11:57 -05006782 * Check the exchange flags returned by the server for invalid flags, having
6783 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
6784 * DS flags set.
6785 */
6786static int nfs4_check_cl_exchange_flags(u32 flags)
6787{
6788 if (flags & ~EXCHGID4_FLAG_MASK_R)
6789 goto out_inval;
6790 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
6791 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
6792 goto out_inval;
6793 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
6794 goto out_inval;
6795 return NFS_OK;
6796out_inval:
6797 return -NFS4ERR_INVAL;
6798}
6799
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006800static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006801nfs41_same_server_scope(struct nfs41_server_scope *a,
6802 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006803{
6804 if (a->server_scope_sz == b->server_scope_sz &&
6805 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
6806 return true;
6807
6808 return false;
6809}
6810
Andy Adamson02a95de2016-02-05 16:08:37 -05006811static void
6812nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
6813{
6814}
6815
6816static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
6817 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
6818};
6819
Andy Adamson357f54d2010-12-14 10:11:57 -05006820/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006821 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006822 *
6823 * The 4.1 client currently uses the same TCP connection for the
6824 * fore and backchannel.
6825 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006826static
6827int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
6828 struct rpc_xprt *xprt,
6829 struct nfs_client *clp,
6830 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006831{
6832 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006833 struct nfs41_bind_conn_to_session_args args = {
6834 .client = clp,
6835 .dir = NFS4_CDFC4_FORE_OR_BOTH,
6836 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006837 struct nfs41_bind_conn_to_session_res res;
6838 struct rpc_message msg = {
6839 .rpc_proc =
6840 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08006841 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006842 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04006843 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006844 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006845 struct rpc_task_setup task_setup_data = {
6846 .rpc_client = clnt,
6847 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05006848 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006849 .rpc_message = &msg,
6850 .flags = RPC_TASK_TIMEOUT,
6851 };
6852 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006853
6854 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006855
Trond Myklebust71a097c2015-02-18 09:27:18 -08006856 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
6857 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
6858 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006859
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006860 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
6861 if (xprt != rcu_access_pointer(clnt->cl_xprt))
6862 args.dir = NFS4_CDFC4_FORE;
6863
6864 task = rpc_run_task(&task_setup_data);
6865 if (!IS_ERR(task)) {
6866 status = task->tk_status;
6867 rpc_put_task(task);
6868 } else
6869 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006870 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006871 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08006872 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006873 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
6874 dprintk("NFS: %s: Session ID mismatch\n", __func__);
6875 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006876 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006877 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006878 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006879 dprintk("NFS: %s: Unexpected direction from server\n",
6880 __func__);
6881 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006882 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006883 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08006884 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006885 dprintk("NFS: %s: Server returned RDMA mode = true\n",
6886 __func__);
6887 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08006888 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006889 }
6890 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006891out:
6892 dprintk("<-- %s status= %d\n", __func__, status);
6893 return status;
6894}
6895
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05006896struct rpc_bind_conn_calldata {
6897 struct nfs_client *clp;
6898 struct rpc_cred *cred;
6899};
6900
6901static int
6902nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
6903 struct rpc_xprt *xprt,
6904 void *calldata)
6905{
6906 struct rpc_bind_conn_calldata *p = calldata;
6907
6908 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
6909}
6910
6911int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
6912{
6913 struct rpc_bind_conn_calldata data = {
6914 .clp = clp,
6915 .cred = cred,
6916 };
6917 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
6918 nfs4_proc_bind_conn_to_session_callback, &data);
6919}
6920
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04006921/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006922 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
6923 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04006924 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006925static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
6926 .how = SP4_MACH_CRED,
6927 .enforce.u.words = {
6928 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6929 1 << (OP_EXCHANGE_ID - 32) |
6930 1 << (OP_CREATE_SESSION - 32) |
6931 1 << (OP_DESTROY_SESSION - 32) |
6932 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006933 },
6934 .allow.u.words = {
6935 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006936 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006937 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006938 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006939 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04006940 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006941 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05006942 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04006943 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04006944 1 << (OP_FREE_STATEID - 32) |
6945 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04006946 }
6947};
6948
6949/*
6950 * Select the state protection mode for client `clp' given the server results
6951 * from exchange_id in `sp'.
6952 *
6953 * Returns 0 on success, negative errno otherwise.
6954 */
6955static int nfs4_sp4_select_mode(struct nfs_client *clp,
6956 struct nfs41_state_protection *sp)
6957{
6958 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
6959 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
6960 1 << (OP_EXCHANGE_ID - 32) |
6961 1 << (OP_CREATE_SESSION - 32) |
6962 1 << (OP_DESTROY_SESSION - 32) |
6963 1 << (OP_DESTROY_CLIENTID - 32)
6964 };
6965 unsigned int i;
6966
6967 if (sp->how == SP4_MACH_CRED) {
6968 /* Print state protect result */
6969 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
6970 for (i = 0; i <= LAST_NFS4_OP; i++) {
6971 if (test_bit(i, sp->enforce.u.longs))
6972 dfprintk(MOUNT, " enforce op %d\n", i);
6973 if (test_bit(i, sp->allow.u.longs))
6974 dfprintk(MOUNT, " allow op %d\n", i);
6975 }
6976
6977 /* make sure nothing is on enforce list that isn't supported */
6978 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
6979 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
6980 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
6981 return -EINVAL;
6982 }
6983 }
6984
6985 /*
6986 * Minimal mode - state operations are allowed to use machine
6987 * credential. Note this already happens by default, so the
6988 * client doesn't have to do anything more than the negotiation.
6989 *
6990 * NOTE: we don't care if EXCHANGE_ID is in the list -
6991 * we're already using the machine cred for exchange_id
6992 * and will never use a different cred.
6993 */
6994 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
6995 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
6996 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
6997 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
6998 dfprintk(MOUNT, "sp4_mach_cred:\n");
6999 dfprintk(MOUNT, " minimal mode enabled\n");
7000 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7001 } else {
7002 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7003 return -EINVAL;
7004 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007005
7006 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007007 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7008 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007009 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7010 dfprintk(MOUNT, " cleanup mode enabled\n");
7011 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7012 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007013
Andrew Elble99ade3c2015-12-02 09:39:51 -05007014 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7015 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7016 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7017 &clp->cl_sp4_flags);
7018 }
7019
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007020 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7021 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7022 dfprintk(MOUNT, " secinfo mode enabled\n");
7023 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7024 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007025
7026 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7027 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7028 dfprintk(MOUNT, " stateid mode enabled\n");
7029 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7030 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007031
7032 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7033 dfprintk(MOUNT, " write mode enabled\n");
7034 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7035 }
7036
7037 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7038 dfprintk(MOUNT, " commit mode enabled\n");
7039 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7040 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007041 }
7042
7043 return 0;
7044}
7045
7046/*
7047 * _nfs4_proc_exchange_id()
7048 *
7049 * Wrapper for EXCHANGE_ID operation.
7050 */
7051static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7052 u32 sp4_how)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007053{
7054 nfs4_verifier verifier;
7055 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05007056 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04007057 .client = clp,
Chuck Levercd3fade2013-10-17 14:14:04 -04007058#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebust4f0b4292013-05-20 12:24:03 -04007059 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
Chuck Levercd3fade2013-10-17 14:14:04 -04007060 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7061 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7062#else
7063 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7064 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7065#endif
Benny Halevy99fe60d2009-04-01 09:22:29 -04007066 };
7067 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04007068 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04007069 };
7070 int status;
7071 struct rpc_message msg = {
7072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
7073 .rpc_argp = &args,
7074 .rpc_resp = &res,
7075 .rpc_cred = cred,
7076 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04007077
Chuck Leverf0920752012-05-21 22:45:41 -04007078 nfs4_init_boot_verifier(clp, &verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007079
7080 status = nfs4_init_uniform_client_string(clp);
7081 if (status)
Jeff Layton3a6bb732015-06-09 19:43:57 -04007082 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007083
7084 dprintk("NFS call exchange_id auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007085 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04007086 clp->cl_owner_id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007087
Chuck Leveracdeb692012-05-21 22:46:16 -04007088 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007089 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04007090 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007091 status = -ENOMEM;
7092 goto out;
7093 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007094
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007095 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007096 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007097 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04007098 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04007099 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007100 }
7101
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007102 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04007103 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007104 status = -ENOMEM;
7105 goto out_server_scope;
7106 }
7107
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007108 switch (sp4_how) {
7109 case SP4_NONE:
7110 args.state_protect.how = SP4_NONE;
7111 break;
7112
7113 case SP4_MACH_CRED:
7114 args.state_protect = nfs4_sp4_mach_cred_request;
7115 break;
7116
7117 default:
7118 /* unsupported! */
7119 WARN_ON_ONCE(1);
7120 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007121 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007122 }
7123
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007124 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007125 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04007126 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04007127 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007128
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007129 if (status == 0)
7130 status = nfs4_sp4_select_mode(clp, &res.state_protect);
7131
Chuck Lever177313f2012-05-21 22:44:58 -04007132 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04007133 clp->cl_clientid = res.clientid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007134 clp->cl_exchange_flags = res.flags;
7135 /* Client ID is not confirmed */
7136 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
7137 clear_bit(NFS4_SESSION_ESTABLISHED,
7138 &clp->cl_session->session_state);
Trond Myklebust32b01312012-05-26 13:41:04 -04007139 clp->cl_seqid = res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007140 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007141
Chuck Leveracdeb692012-05-21 22:46:16 -04007142 kfree(clp->cl_serverowner);
7143 clp->cl_serverowner = res.server_owner;
7144 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007145
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007146 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007147 kfree(clp->cl_implid);
7148 clp->cl_implid = res.impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007149 res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007150
Chuck Lever177313f2012-05-21 22:44:58 -04007151 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007152 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007153 res.server_scope)) {
7154 dprintk("%s: server_scope mismatch detected\n",
7155 __func__);
7156 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007157 kfree(clp->cl_serverscope);
7158 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007159 }
7160
Chuck Lever177313f2012-05-21 22:44:58 -04007161 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007162 clp->cl_serverscope = res.server_scope;
Kinglong Mee6b559702015-07-01 11:54:53 +08007163 res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007164 }
Kinglong Mee6b559702015-07-01 11:54:53 +08007165 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007166
Kinglong Mee6b559702015-07-01 11:54:53 +08007167out_impl_id:
7168 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007169out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007170 kfree(res.server_scope);
Kinglong Mee6b559702015-07-01 11:54:53 +08007171out_server_owner:
7172 kfree(res.server_owner);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007173out:
Chuck Lever177313f2012-05-21 22:44:58 -04007174 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007175 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007176 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007177 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007178 clp->cl_implid->date.seconds,
7179 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007180 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007181 return status;
7182}
7183
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007184/*
7185 * nfs4_proc_exchange_id()
7186 *
7187 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7188 *
7189 * Since the clientid has expired, all compounds using sessions
7190 * associated with the stale clientid will be returning
7191 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7192 * be in some phase of session reset.
7193 *
7194 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7195 */
7196int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7197{
7198 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7199 int status;
7200
7201 /* try SP4_MACH_CRED if krb5i/p */
7202 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7203 authflavor == RPC_AUTH_GSS_KRB5P) {
7204 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
7205 if (!status)
7206 return 0;
7207 }
7208
7209 /* try SP4_NONE */
7210 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
7211}
7212
Trond Myklebust66245532012-05-25 17:18:09 -04007213static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7214 struct rpc_cred *cred)
7215{
7216 struct rpc_message msg = {
7217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7218 .rpc_argp = clp,
7219 .rpc_cred = cred,
7220 };
7221 int status;
7222
7223 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007224 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007225 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007226 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007227 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7228 return status;
7229}
7230
7231static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7232 struct rpc_cred *cred)
7233{
7234 unsigned int loop;
7235 int ret;
7236
7237 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7238 ret = _nfs4_proc_destroy_clientid(clp, cred);
7239 switch (ret) {
7240 case -NFS4ERR_DELAY:
7241 case -NFS4ERR_CLIENTID_BUSY:
7242 ssleep(1);
7243 break;
7244 default:
7245 return ret;
7246 }
7247 }
7248 return 0;
7249}
7250
7251int nfs4_destroy_clientid(struct nfs_client *clp)
7252{
7253 struct rpc_cred *cred;
7254 int ret = 0;
7255
7256 if (clp->cl_mvops->minor_version < 1)
7257 goto out;
7258 if (clp->cl_exchange_flags == 0)
7259 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007260 if (clp->cl_preserve_clid)
7261 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007262 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007263 ret = nfs4_proc_destroy_clientid(clp, cred);
7264 if (cred)
7265 put_rpccred(cred);
7266 switch (ret) {
7267 case 0:
7268 case -NFS4ERR_STALE_CLIENTID:
7269 clp->cl_exchange_flags = 0;
7270 }
7271out:
7272 return ret;
7273}
7274
Andy Adamson2050f0c2009-04-01 09:22:30 -04007275struct nfs4_get_lease_time_data {
7276 struct nfs4_get_lease_time_args *args;
7277 struct nfs4_get_lease_time_res *res;
7278 struct nfs_client *clp;
7279};
7280
7281static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7282 void *calldata)
7283{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007284 struct nfs4_get_lease_time_data *data =
7285 (struct nfs4_get_lease_time_data *)calldata;
7286
7287 dprintk("--> %s\n", __func__);
7288 /* just setup sequence, do not trigger session recovery
7289 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007290 nfs41_setup_sequence(data->clp->cl_session,
7291 &data->args->la_seq_args,
7292 &data->res->lr_seq_res,
7293 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007294 dprintk("<-- %s\n", __func__);
7295}
7296
7297/*
7298 * Called from nfs4_state_manager thread for session setup, so don't recover
7299 * from sequence operation or clientid errors.
7300 */
7301static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7302{
7303 struct nfs4_get_lease_time_data *data =
7304 (struct nfs4_get_lease_time_data *)calldata;
7305
7306 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007307 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7308 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007309 switch (task->tk_status) {
7310 case -NFS4ERR_DELAY:
7311 case -NFS4ERR_GRACE:
7312 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7313 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7314 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007315 /* fall through */
7316 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007317 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007318 return;
7319 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007320 dprintk("<-- %s\n", __func__);
7321}
7322
Trond Myklebust17280172012-03-11 13:11:00 -04007323static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007324 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7325 .rpc_call_done = nfs4_get_lease_time_done,
7326};
7327
7328int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7329{
7330 struct rpc_task *task;
7331 struct nfs4_get_lease_time_args args;
7332 struct nfs4_get_lease_time_res res = {
7333 .lr_fsinfo = fsinfo,
7334 };
7335 struct nfs4_get_lease_time_data data = {
7336 .args = &args,
7337 .res = &res,
7338 .clp = clp,
7339 };
7340 struct rpc_message msg = {
7341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7342 .rpc_argp = &args,
7343 .rpc_resp = &res,
7344 };
7345 struct rpc_task_setup task_setup = {
7346 .rpc_client = clp->cl_rpcclient,
7347 .rpc_message = &msg,
7348 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007349 .callback_data = &data,
7350 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007351 };
7352 int status;
7353
Chuck Levera9c92d62013-08-09 12:48:18 -04007354 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007355 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007356 dprintk("--> %s\n", __func__);
7357 task = rpc_run_task(&task_setup);
7358
7359 if (IS_ERR(task))
7360 status = PTR_ERR(task);
7361 else {
7362 status = task->tk_status;
7363 rpc_put_task(task);
7364 }
7365 dprintk("<-- %s return %d\n", __func__, status);
7366
7367 return status;
7368}
7369
Andy Adamsonfc931582009-04-01 09:22:31 -04007370/*
7371 * Initialize the values to be used by the client in CREATE_SESSION
7372 * If nfs4_init_session set the fore channel request and response sizes,
7373 * use them.
7374 *
7375 * Set the back channel max_resp_sz_cached to zero to force the client to
7376 * always set csa_cachethis to FALSE because the current implementation
7377 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7378 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007379static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7380 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007381{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007382 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007383 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007384
Andy Adamson18aad3d2013-06-26 12:21:49 -04007385 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7386 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7387
Andy Adamsonfc931582009-04-01 09:22:31 -04007388 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007389 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7390 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007391 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007392 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007393
7394 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007395 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007396 __func__,
7397 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007398 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007399
7400 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007401 args->bc_attrs.max_rqst_sz = max_bc_payload;
7402 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007403 args->bc_attrs.max_resp_sz_cached = 0;
7404 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebustf4f58ed2016-01-23 14:12:52 -05007405 args->bc_attrs.max_reqs = NFS41_BC_MAX_CALLBACKS;
Andy Adamsonfc931582009-04-01 09:22:31 -04007406
7407 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7408 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7409 __func__,
7410 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7411 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7412 args->bc_attrs.max_reqs);
7413}
7414
Trond Myklebust79969dd2015-02-18 11:30:18 -08007415static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7416 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007417{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007418 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007419 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007420
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007421 if (rcvd->max_resp_sz > sent->max_resp_sz)
7422 return -EINVAL;
7423 /*
7424 * Our requested max_ops is the minimum we need; we're not
7425 * prepared to break up compounds into smaller pieces than that.
7426 * So, no point even trying to continue if the server won't
7427 * cooperate:
7428 */
7429 if (rcvd->max_ops < sent->max_ops)
7430 return -EINVAL;
7431 if (rcvd->max_reqs == 0)
7432 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007433 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7434 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007435 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007436}
7437
Trond Myklebust79969dd2015-02-18 11:30:18 -08007438static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7439 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007440{
7441 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007442 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007443
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007444 if (!(res->flags & SESSION4_BACK_CHAN))
7445 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007446 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7447 return -EINVAL;
7448 if (rcvd->max_resp_sz < sent->max_resp_sz)
7449 return -EINVAL;
7450 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7451 return -EINVAL;
7452 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007453 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007454 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007455 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007456 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007457out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007458 return 0;
7459}
Andy Adamson8d353012009-04-01 09:22:32 -04007460
Andy Adamson8d353012009-04-01 09:22:32 -04007461static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007462 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007463{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007464 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007465
Trond Myklebust79969dd2015-02-18 11:30:18 -08007466 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007467 if (ret)
7468 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007469 return nfs4_verify_back_channel_attrs(args, res);
7470}
7471
7472static void nfs4_update_session(struct nfs4_session *session,
7473 struct nfs41_create_session_res *res)
7474{
7475 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007476 /* Mark client id and session as being confirmed */
7477 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7478 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007479 session->flags = res->flags;
7480 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007481 if (res->flags & SESSION4_BACK_CHAN)
7482 memcpy(&session->bc_attrs, &res->bc_attrs,
7483 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007484}
7485
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007486static int _nfs4_proc_create_session(struct nfs_client *clp,
7487 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007488{
7489 struct nfs4_session *session = clp->cl_session;
7490 struct nfs41_create_session_args args = {
7491 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007492 .clientid = clp->cl_clientid,
7493 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007494 .cb_program = NFS4_CALLBACK,
7495 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007496 struct nfs41_create_session_res res;
7497
Andy Adamsonfc931582009-04-01 09:22:31 -04007498 struct rpc_message msg = {
7499 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7500 .rpc_argp = &args,
7501 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007502 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007503 };
7504 int status;
7505
Chuck Lever6b26cc82016-05-02 14:40:40 -04007506 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007507 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007508
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007509 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007510 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007511
Trond Myklebust43095d32012-11-20 11:13:12 -05007512 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007513 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007514 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007515 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007516 if (clp->cl_seqid == res.seqid)
7517 clp->cl_seqid++;
7518 if (status)
7519 goto out;
7520 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007521 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007522out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007523 return status;
7524}
7525
7526/*
7527 * Issues a CREATE_SESSION operation to the server.
7528 * It is the responsibility of the caller to verify the session is
7529 * expired before calling this routine.
7530 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007531int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007532{
7533 int status;
7534 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007535 struct nfs4_session *session = clp->cl_session;
7536
7537 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7538
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007539 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007540 if (status)
7541 goto out;
7542
Andy Adamsonaacd5532011-11-09 13:58:21 -05007543 /* Init or reset the session slot tables */
7544 status = nfs4_setup_session_slot_tables(session);
7545 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007546 if (status)
7547 goto out;
7548
7549 ptr = (unsigned *)&session->sess_id.data[0];
7550 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7551 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007552out:
7553 dprintk("<-- %s\n", __func__);
7554 return status;
7555}
7556
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007557/*
7558 * Issue the over-the-wire RPC DESTROY_SESSION.
7559 * The caller must serialize access to this routine.
7560 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007561int nfs4_proc_destroy_session(struct nfs4_session *session,
7562 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007563{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007564 struct rpc_message msg = {
7565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7566 .rpc_argp = session,
7567 .rpc_cred = cred,
7568 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007569 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007570
7571 dprintk("--> nfs4_proc_destroy_session\n");
7572
7573 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007574 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7575 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007576
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007577 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007578 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007579
7580 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007581 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007582 "Session has been destroyed regardless...\n", status);
7583
7584 dprintk("<-- nfs4_proc_destroy_session\n");
7585 return status;
7586}
7587
Trond Myklebust7b38c362012-05-23 13:23:31 -04007588/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007589 * Renew the cl_session lease.
7590 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007591struct nfs4_sequence_data {
7592 struct nfs_client *clp;
7593 struct nfs4_sequence_args args;
7594 struct nfs4_sequence_res res;
7595};
7596
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007597static void nfs41_sequence_release(void *data)
7598{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007599 struct nfs4_sequence_data *calldata = data;
7600 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007601
Alexandros Batsakis71358402010-02-05 03:45:05 -08007602 if (atomic_read(&clp->cl_count) > 1)
7603 nfs4_schedule_state_renewal(clp);
7604 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007605 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007606}
7607
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007608static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7609{
7610 switch(task->tk_status) {
7611 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007612 rpc_delay(task, NFS4_POLL_RETRY_MAX);
7613 return -EAGAIN;
7614 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007615 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007616 }
7617 return 0;
7618}
7619
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007620static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007621{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007622 struct nfs4_sequence_data *calldata = data;
7623 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007624
Trond Myklebust14516c32010-07-31 14:29:06 -04007625 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
7626 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007627
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007628 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007629 if (task->tk_status < 0) {
7630 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007631 if (atomic_read(&clp->cl_count) == 1)
7632 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007633
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007634 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
7635 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007636 return;
7637 }
7638 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007639 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08007640out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007641 dprintk("<-- %s\n", __func__);
7642}
7643
7644static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
7645{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007646 struct nfs4_sequence_data *calldata = data;
7647 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007648 struct nfs4_sequence_args *args;
7649 struct nfs4_sequence_res *res;
7650
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007651 args = task->tk_msg.rpc_argp;
7652 res = task->tk_msg.rpc_resp;
7653
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007654 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007655}
7656
7657static const struct rpc_call_ops nfs41_sequence_ops = {
7658 .rpc_call_done = nfs41_sequence_call_done,
7659 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007660 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007661};
7662
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007663static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
7664 struct rpc_cred *cred,
7665 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007666{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007667 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007668 struct rpc_message msg = {
7669 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
7670 .rpc_cred = cred,
7671 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007672 struct rpc_task_setup task_setup_data = {
7673 .rpc_client = clp->cl_rpcclient,
7674 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007675 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04007676 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007677 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007678
Alexandros Batsakis71358402010-02-05 03:45:05 -08007679 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007680 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04007681 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007682 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08007683 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007684 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007685 }
Chuck Levera9c92d62013-08-09 12:48:18 -04007686 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007687 if (is_privileged)
7688 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007689 msg.rpc_argp = &calldata->args;
7690 msg.rpc_resp = &calldata->res;
7691 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007692 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007693
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007694 return rpc_run_task(&task_setup_data);
7695}
7696
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007697static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007698{
7699 struct rpc_task *task;
7700 int ret = 0;
7701
Trond Myklebust2f60ea62011-08-24 15:07:37 -04007702 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04007703 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007704 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007705 if (IS_ERR(task))
7706 ret = PTR_ERR(task);
7707 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08007708 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007709 dprintk("<-- %s status=%d\n", __func__, ret);
7710 return ret;
7711}
7712
7713static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
7714{
7715 struct rpc_task *task;
7716 int ret;
7717
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007718 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007719 if (IS_ERR(task)) {
7720 ret = PTR_ERR(task);
7721 goto out;
7722 }
7723 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04007724 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04007725 ret = task->tk_status;
7726 rpc_put_task(task);
7727out:
7728 dprintk("<-- %s status=%d\n", __func__, ret);
7729 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007730}
7731
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007732struct nfs4_reclaim_complete_data {
7733 struct nfs_client *clp;
7734 struct nfs41_reclaim_complete_args arg;
7735 struct nfs41_reclaim_complete_res res;
7736};
7737
7738static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
7739{
7740 struct nfs4_reclaim_complete_data *calldata = data;
7741
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007742 nfs41_setup_sequence(calldata->clp->cl_session,
7743 &calldata->arg.seq_args,
7744 &calldata->res.seq_res,
7745 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007746}
7747
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007748static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
7749{
7750 switch(task->tk_status) {
7751 case 0:
7752 case -NFS4ERR_COMPLETE_ALREADY:
7753 case -NFS4ERR_WRONG_CRED: /* What to do here? */
7754 break;
7755 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007756 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007757 /* fall through */
7758 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007759 return -EAGAIN;
7760 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05007761 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007762 }
7763 return 0;
7764}
7765
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007766static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
7767{
7768 struct nfs4_reclaim_complete_data *calldata = data;
7769 struct nfs_client *clp = calldata->clp;
7770 struct nfs4_sequence_res *res = &calldata->res.seq_res;
7771
7772 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007773 if (!nfs41_sequence_done(task, res))
7774 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007775
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007776 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04007777 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
7778 rpc_restart_call_prepare(task);
7779 return;
7780 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007781 dprintk("<-- %s\n", __func__);
7782}
7783
7784static void nfs4_free_reclaim_complete_data(void *data)
7785{
7786 struct nfs4_reclaim_complete_data *calldata = data;
7787
7788 kfree(calldata);
7789}
7790
7791static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
7792 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
7793 .rpc_call_done = nfs4_reclaim_complete_done,
7794 .rpc_release = nfs4_free_reclaim_complete_data,
7795};
7796
7797/*
7798 * Issue a global reclaim complete.
7799 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04007800static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
7801 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007802{
7803 struct nfs4_reclaim_complete_data *calldata;
7804 struct rpc_task *task;
7805 struct rpc_message msg = {
7806 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04007807 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007808 };
7809 struct rpc_task_setup task_setup_data = {
7810 .rpc_client = clp->cl_rpcclient,
7811 .rpc_message = &msg,
7812 .callback_ops = &nfs4_reclaim_complete_call_ops,
7813 .flags = RPC_TASK_ASYNC,
7814 };
7815 int status = -ENOMEM;
7816
7817 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04007818 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007819 if (calldata == NULL)
7820 goto out;
7821 calldata->clp = clp;
7822 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007823
Chuck Levera9c92d62013-08-09 12:48:18 -04007824 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007825 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007826 msg.rpc_argp = &calldata->arg;
7827 msg.rpc_resp = &calldata->res;
7828 task_setup_data.callback_data = calldata;
7829 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007830 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007831 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007832 goto out;
7833 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05007834 status = nfs4_wait_for_completion_rpc_task(task);
7835 if (status == 0)
7836 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007837 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02007838 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007839out:
7840 dprintk("<-- %s status=%d\n", __func__, status);
7841 return status;
7842}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007843
7844static void
7845nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
7846{
7847 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00007848 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007849 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007850
7851 dprintk("--> %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007852 nfs41_setup_sequence(session, &lgp->args.seq_args,
7853 &lgp->res.seq_res, task);
7854 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007855}
7856
7857static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
7858{
7859 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04007860
7861 dprintk("--> %s\n", __func__);
7862 nfs41_sequence_done(task, &lgp->res.seq_res);
7863 dprintk("<-- %s\n", __func__);
7864}
7865
7866static int
7867nfs4_layoutget_handle_exception(struct rpc_task *task,
7868 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
7869{
Trond Myklebustee314c22012-10-01 17:25:48 -07007870 struct inode *inode = lgp->args.inode;
7871 struct nfs_server *server = NFS_SERVER(inode);
7872 struct pnfs_layout_hdr *lo;
Jeff Layton183d9e72016-05-17 12:28:47 -04007873 int status = task->tk_status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007874
Boaz Harroshed7e5422014-01-22 20:34:54 +02007875 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007876
Jeff Layton183d9e72016-05-17 12:28:47 -04007877 switch (status) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007878 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07007879 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007880
7881 /*
7882 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
7883 * on the file. set tk_status to -ENODATA to tell upper layer to
7884 * retry go inband.
7885 */
7886 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04007887 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08007888 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007889 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07007890 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
7891 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
7892 */
7893 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04007894 status = -EOVERFLOW;
7895 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07007896 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02007897 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07007898 * (or clients) writing to the same RAID stripe except when
7899 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04007900 *
7901 * Treat it like we would RECALLCONFLICT -- we retry for a little
7902 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02007903 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007904 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04007905 if (lgp->args.minlength == 0) {
7906 status = -EOVERFLOW;
7907 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02007908 }
Jeff Layton183d9e72016-05-17 12:28:47 -04007909 /* Fallthrough */
7910 case -NFS4ERR_RECALLCONFLICT:
7911 nfs4_handle_exception(server, -NFS4ERR_RECALLCONFLICT,
7912 exception);
7913 status = -ERECALLCONFLICT;
7914 goto out;
Trond Myklebustee314c22012-10-01 17:25:48 -07007915 case -NFS4ERR_EXPIRED:
7916 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04007917 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07007918 spin_lock(&inode->i_lock);
Trond Myklebust2259f962015-09-20 13:30:30 -04007919 if (nfs4_stateid_match(&lgp->args.stateid,
7920 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007921 spin_unlock(&inode->i_lock);
7922 /* If the open stateid was bad, then recover it. */
Jeff Layton183d9e72016-05-17 12:28:47 -04007923 exception->state = lgp->args.ctx->state;
Trond Myklebust2259f962015-09-20 13:30:30 -04007924 break;
7925 }
7926 lo = NFS_I(inode)->layout;
7927 if (lo && nfs4_stateid_match(&lgp->args.stateid,
7928 &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07007929 LIST_HEAD(head);
7930
Christoph Hellwigdefb8462014-08-21 11:09:21 -05007931 /*
7932 * Mark the bad layout state as invalid, then retry
7933 * with the current stateid.
7934 */
Kinglong Mee8714d462015-09-20 23:04:22 +08007935 set_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags);
Jeff Layton6d597e12016-05-17 12:28:42 -04007936 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL, 0);
Trond Myklebustee314c22012-10-01 17:25:48 -07007937 spin_unlock(&inode->i_lock);
Trond Myklebustee314c22012-10-01 17:25:48 -07007938 pnfs_free_lseg_list(&head);
Trond Myklebust2259f962015-09-20 13:30:30 -04007939 } else
7940 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04007941 status = -EAGAIN;
7942 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007943 }
Jeff Layton183d9e72016-05-17 12:28:47 -04007944
7945 status = nfs4_handle_exception(server, status, exception);
7946 if (exception->retry)
7947 status = -EAGAIN;
Trond Myklebustee314c22012-10-01 17:25:48 -07007948out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007949 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04007950 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007951}
7952
Idan Kedar85541162012-08-02 11:47:10 +03007953static size_t max_response_pages(struct nfs_server *server)
7954{
7955 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
7956 return nfs_page_array_len(0, max_resp_sz);
7957}
7958
7959static void nfs4_free_pages(struct page **pages, size_t size)
7960{
7961 int i;
7962
7963 if (!pages)
7964 return;
7965
7966 for (i = 0; i < size; i++) {
7967 if (!pages[i])
7968 break;
7969 __free_page(pages[i]);
7970 }
7971 kfree(pages);
7972}
7973
7974static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
7975{
7976 struct page **pages;
7977 int i;
7978
7979 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
7980 if (!pages) {
7981 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
7982 return NULL;
7983 }
7984
7985 for (i = 0; i < size; i++) {
7986 pages[i] = alloc_page(gfp_flags);
7987 if (!pages[i]) {
7988 dprintk("%s: failed to allocate page\n", __func__);
7989 nfs4_free_pages(pages, size);
7990 return NULL;
7991 }
7992 }
7993
7994 return pages;
7995}
7996
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007997static void nfs4_layoutget_release(void *calldata)
7998{
7999 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008000 struct inode *inode = lgp->args.inode;
8001 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008002 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008003
8004 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008005 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008006 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008007 put_nfs_open_context(lgp->args.ctx);
8008 kfree(calldata);
8009 dprintk("<-- %s\n", __func__);
8010}
8011
8012static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8013 .rpc_call_prepare = nfs4_layoutget_prepare,
8014 .rpc_call_done = nfs4_layoutget_done,
8015 .rpc_release = nfs4_layoutget_release,
8016};
8017
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008018struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008019nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008020{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008021 struct inode *inode = lgp->args.inode;
8022 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008023 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008024 struct rpc_task *task;
8025 struct rpc_message msg = {
8026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8027 .rpc_argp = &lgp->args,
8028 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008029 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008030 };
8031 struct rpc_task_setup task_setup_data = {
8032 .rpc_client = server->client,
8033 .rpc_message = &msg,
8034 .callback_ops = &nfs4_layoutget_call_ops,
8035 .callback_data = lgp,
8036 .flags = RPC_TASK_ASYNC,
8037 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008038 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008039 struct nfs4_exception exception = {
8040 .inode = inode,
8041 .timeout = *timeout,
8042 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008043 int status = 0;
8044
8045 dprintk("--> %s\n", __func__);
8046
Peng Tao4bd5a982014-11-17 11:05:17 +08008047 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8048 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8049
Idan Kedar85541162012-08-02 11:47:10 +03008050 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8051 if (!lgp->args.layout.pages) {
8052 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008053 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008054 }
8055 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8056
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008057 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008058 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008059 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008060
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008061 task = rpc_run_task(&task_setup_data);
8062 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008063 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008064 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008065 if (status == 0) {
8066 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8067 *timeout = exception.timeout;
8068 }
8069
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008070 trace_nfs4_layoutget(lgp->args.ctx,
8071 &lgp->args.range,
8072 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008073 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008074 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008075
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008076 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8077 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008078 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008079 rpc_put_task(task);
8080 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008081 if (status)
8082 return ERR_PTR(status);
8083 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008084}
8085
Benny Halevycbe82602011-05-22 19:52:37 +03008086static void
8087nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8088{
8089 struct nfs4_layoutreturn *lrp = calldata;
8090
8091 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008092 nfs41_setup_sequence(lrp->clp->cl_session,
8093 &lrp->args.seq_args,
8094 &lrp->res.seq_res,
8095 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008096}
8097
8098static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8099{
8100 struct nfs4_layoutreturn *lrp = calldata;
8101 struct nfs_server *server;
8102
8103 dprintk("--> %s\n", __func__);
8104
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008105 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008106 return;
8107
8108 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008109 switch (task->tk_status) {
8110 default:
8111 task->tk_status = 0;
8112 case 0:
8113 break;
8114 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008115 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008116 break;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008117 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008118 return;
8119 }
Benny Halevycbe82602011-05-22 19:52:37 +03008120 dprintk("<-- %s\n", __func__);
8121}
8122
8123static void nfs4_layoutreturn_release(void *calldata)
8124{
8125 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008126 struct pnfs_layout_hdr *lo = lrp->args.layout;
Trond Myklebustc5d73712015-07-09 17:58:39 +02008127 LIST_HEAD(freeme);
Benny Halevycbe82602011-05-22 19:52:37 +03008128
8129 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04008130 spin_lock(&lo->plh_inode->i_lock);
Jeff Layton6d597e12016-05-17 12:28:42 -04008131 pnfs_mark_matching_lsegs_invalid(lo, &freeme, &lrp->args.range,
8132 be32_to_cpu(lrp->args.stateid.seqid));
Trond Myklebust0654cc72015-12-28 11:48:14 -05008133 pnfs_mark_layout_returned_if_empty(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008134 if (lrp->res.lrs_present)
8135 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Tom Haynesd67ae822014-12-11 17:02:04 -05008136 pnfs_clear_layoutreturn_waitbit(lo);
Trond Myklebust849b2862012-09-24 14:18:39 -04008137 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebustc5d73712015-07-09 17:58:39 +02008138 pnfs_free_lseg_list(&freeme);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04008139 pnfs_put_layout_hdr(lrp->args.layout);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008140 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008141 kfree(calldata);
8142 dprintk("<-- %s\n", __func__);
8143}
8144
8145static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8146 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8147 .rpc_call_done = nfs4_layoutreturn_done,
8148 .rpc_release = nfs4_layoutreturn_release,
8149};
8150
Peng Tao6c166052014-11-17 09:30:40 +08008151int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008152{
8153 struct rpc_task *task;
8154 struct rpc_message msg = {
8155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8156 .rpc_argp = &lrp->args,
8157 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008158 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008159 };
8160 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008161 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008162 .rpc_message = &msg,
8163 .callback_ops = &nfs4_layoutreturn_call_ops,
8164 .callback_data = lrp,
8165 };
Peng Tao6c166052014-11-17 09:30:40 +08008166 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008167
Andrew Elble99ade3c2015-12-02 09:39:51 -05008168 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8169 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8170 &task_setup_data.rpc_client, &msg);
8171
Benny Halevycbe82602011-05-22 19:52:37 +03008172 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008173 if (!sync) {
8174 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8175 if (!lrp->inode) {
8176 nfs4_layoutreturn_release(lrp);
8177 return -EAGAIN;
8178 }
8179 task_setup_data.flags |= RPC_TASK_ASYNC;
8180 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008181 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008182 task = rpc_run_task(&task_setup_data);
8183 if (IS_ERR(task))
8184 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008185 if (sync)
8186 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008187 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008188 dprintk("<-- %s status=%d\n", __func__, status);
8189 rpc_put_task(task);
8190 return status;
8191}
8192
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008193static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008194_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8195 struct pnfs_device *pdev,
8196 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008197{
8198 struct nfs4_getdeviceinfo_args args = {
8199 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008200 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8201 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008202 };
8203 struct nfs4_getdeviceinfo_res res = {
8204 .pdev = pdev,
8205 };
8206 struct rpc_message msg = {
8207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8208 .rpc_argp = &args,
8209 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008210 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008211 };
8212 int status;
8213
8214 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008215 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008216 if (res.notification & ~args.notify_types)
8217 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008218 if (res.notification != args.notify_types)
8219 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008220
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008221 dprintk("<-- %s status=%d\n", __func__, status);
8222
8223 return status;
8224}
8225
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008226int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8227 struct pnfs_device *pdev,
8228 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008229{
8230 struct nfs4_exception exception = { };
8231 int err;
8232
8233 do {
8234 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008235 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008236 &exception);
8237 } while (exception.retry);
8238 return err;
8239}
8240EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8241
Andy Adamson863a3c62011-03-23 13:27:54 +00008242static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8243{
8244 struct nfs4_layoutcommit_data *data = calldata;
8245 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008246 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00008247
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008248 nfs41_setup_sequence(session,
8249 &data->args.seq_args,
8250 &data->res.seq_res,
8251 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008252}
8253
8254static void
8255nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8256{
8257 struct nfs4_layoutcommit_data *data = calldata;
8258 struct nfs_server *server = NFS_SERVER(data->args.inode);
8259
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008260 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008261 return;
8262
8263 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008264 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8265 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8266 case -NFS4ERR_BADLAYOUT: /* no layout */
8267 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008268 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008269 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008270 break;
8271 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008272 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008273 rpc_restart_call_prepare(task);
8274 return;
8275 }
8276 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008277}
8278
8279static void nfs4_layoutcommit_release(void *calldata)
8280{
8281 struct nfs4_layoutcommit_data *data = calldata;
8282
Andy Adamsondb29c082011-07-30 20:52:38 -04008283 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008284 nfs_post_op_update_inode_force_wcc(data->args.inode,
8285 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008286 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008287 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008288 kfree(data);
8289}
8290
8291static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8292 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8293 .rpc_call_done = nfs4_layoutcommit_done,
8294 .rpc_release = nfs4_layoutcommit_release,
8295};
8296
8297int
Andy Adamsonef311532011-03-12 02:58:10 -05008298nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008299{
8300 struct rpc_message msg = {
8301 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8302 .rpc_argp = &data->args,
8303 .rpc_resp = &data->res,
8304 .rpc_cred = data->cred,
8305 };
8306 struct rpc_task_setup task_setup_data = {
8307 .task = &data->task,
8308 .rpc_client = NFS_CLIENT(data->args.inode),
8309 .rpc_message = &msg,
8310 .callback_ops = &nfs4_layoutcommit_ops,
8311 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008312 };
8313 struct rpc_task *task;
8314 int status = 0;
8315
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008316 dprintk("NFS: initiating layoutcommit call. sync %d "
8317 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008318 data->args.lastbytewritten,
8319 data->args.inode->i_ino);
8320
Trond Myklebust472e2592015-02-05 16:50:30 -05008321 if (!sync) {
8322 data->inode = nfs_igrab_and_active(data->args.inode);
8323 if (data->inode == NULL) {
8324 nfs4_layoutcommit_release(data);
8325 return -EAGAIN;
8326 }
8327 task_setup_data.flags = RPC_TASK_ASYNC;
8328 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008329 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008330 task = rpc_run_task(&task_setup_data);
8331 if (IS_ERR(task))
8332 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008333 if (sync)
8334 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008335 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008336 dprintk("%s: status %d\n", __func__, status);
8337 rpc_put_task(task);
8338 return status;
8339}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008340
Andy Adamson97431202013-08-08 10:57:56 -04008341/**
8342 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8343 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8344 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008345static int
8346_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008347 struct nfs_fsinfo *info,
8348 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008349{
8350 struct nfs41_secinfo_no_name_args args = {
8351 .style = SECINFO_STYLE_CURRENT_FH,
8352 };
8353 struct nfs4_secinfo_res res = {
8354 .flavors = flavors,
8355 };
8356 struct rpc_message msg = {
8357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8358 .rpc_argp = &args,
8359 .rpc_resp = &res,
8360 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008361 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008362 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008363 int status;
8364
8365 if (use_integrity) {
8366 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008367 cred = nfs4_get_clid_cred(server->nfs_client);
8368 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008369 }
8370
8371 dprintk("--> %s\n", __func__);
8372 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8373 &res.seq_res, 0);
8374 dprintk("<-- %s status=%d\n", __func__, status);
8375
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008376 if (cred)
8377 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008378
8379 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008380}
8381
8382static int
8383nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8384 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8385{
8386 struct nfs4_exception exception = { };
8387 int err;
8388 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008389 /* first try using integrity protection */
8390 err = -NFS4ERR_WRONGSEC;
8391
8392 /* try to use integrity protection with machine cred */
8393 if (_nfs4_is_integrity_protected(server->nfs_client))
8394 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8395 flavors, true);
8396
8397 /*
8398 * if unable to use integrity protection, or SECINFO with
8399 * integrity protection returns NFS4ERR_WRONGSEC (which is
8400 * disallowed by spec, but exists in deployed servers) use
8401 * the current filesystem's rpc_client and the user cred.
8402 */
8403 if (err == -NFS4ERR_WRONGSEC)
8404 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8405 flavors, false);
8406
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008407 switch (err) {
8408 case 0:
8409 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008410 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008411 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008412 default:
8413 err = nfs4_handle_exception(server, err, &exception);
8414 }
8415 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008416out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008417 return err;
8418}
8419
8420static int
8421nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8422 struct nfs_fsinfo *info)
8423{
8424 int err;
8425 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008426 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008427 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008428 struct nfs4_secinfo4 *secinfo;
8429 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008430
8431 page = alloc_page(GFP_KERNEL);
8432 if (!page) {
8433 err = -ENOMEM;
8434 goto out;
8435 }
8436
8437 flavors = page_address(page);
8438 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8439
8440 /*
8441 * Fall back on "guess and check" method if
8442 * the server doesn't support SECINFO_NO_NAME
8443 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008444 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008445 err = nfs4_find_root_sec(server, fhandle, info);
8446 goto out_freepage;
8447 }
8448 if (err)
8449 goto out_freepage;
8450
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008451 for (i = 0; i < flavors->num_flavors; i++) {
8452 secinfo = &flavors->flavors[i];
8453
8454 switch (secinfo->flavor) {
8455 case RPC_AUTH_NULL:
8456 case RPC_AUTH_UNIX:
8457 case RPC_AUTH_GSS:
8458 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8459 &secinfo->flavor_info);
8460 break;
8461 default:
8462 flavor = RPC_AUTH_MAXFLAVOR;
8463 break;
8464 }
8465
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008466 if (!nfs_auth_info_match(&server->auth_info, flavor))
8467 flavor = RPC_AUTH_MAXFLAVOR;
8468
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008469 if (flavor != RPC_AUTH_MAXFLAVOR) {
8470 err = nfs4_lookup_root_sec(server, fhandle,
8471 info, flavor);
8472 if (!err)
8473 break;
8474 }
8475 }
8476
8477 if (flavor == RPC_AUTH_MAXFLAVOR)
8478 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008479
8480out_freepage:
8481 put_page(page);
8482 if (err == -EACCES)
8483 return -EPERM;
8484out:
8485 return err;
8486}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008487
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008488static int _nfs41_test_stateid(struct nfs_server *server,
8489 nfs4_stateid *stateid,
8490 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008491{
8492 int status;
8493 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008494 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008495 };
8496 struct nfs41_test_stateid_res res;
8497 struct rpc_message msg = {
8498 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8499 .rpc_argp = &args,
8500 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008501 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008502 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008503 struct rpc_clnt *rpc_client = server->client;
8504
8505 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8506 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008507
Chuck Lever38527b12012-07-11 16:30:23 -04008508 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008509 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008510 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008511 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008512 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008513 if (status != NFS_OK) {
8514 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008515 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008516 }
8517 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008518 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008519}
8520
Chuck Lever38527b12012-07-11 16:30:23 -04008521/**
8522 * nfs41_test_stateid - perform a TEST_STATEID operation
8523 *
8524 * @server: server / transport on which to perform the operation
8525 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008526 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008527 *
8528 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8529 * Otherwise a negative NFS4ERR value is returned if the operation
8530 * failed or the state ID is not currently valid.
8531 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008532static int nfs41_test_stateid(struct nfs_server *server,
8533 nfs4_stateid *stateid,
8534 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008535{
8536 struct nfs4_exception exception = { };
8537 int err;
8538 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008539 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04008540 if (err != -NFS4ERR_DELAY)
8541 break;
8542 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008543 } while (exception.retry);
8544 return err;
8545}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008546
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008547struct nfs_free_stateid_data {
8548 struct nfs_server *server;
8549 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008550 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008551};
8552
8553static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8554{
8555 struct nfs_free_stateid_data *data = calldata;
8556 nfs41_setup_sequence(nfs4_get_session(data->server),
8557 &data->args.seq_args,
8558 &data->res.seq_res,
8559 task);
8560}
8561
8562static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8563{
8564 struct nfs_free_stateid_data *data = calldata;
8565
8566 nfs41_sequence_done(task, &data->res.seq_res);
8567
8568 switch (task->tk_status) {
8569 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008570 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008571 rpc_restart_call_prepare(task);
8572 }
8573}
8574
8575static void nfs41_free_stateid_release(void *calldata)
8576{
8577 kfree(calldata);
8578}
8579
Trond Myklebust17f26b12013-08-21 15:48:42 -04008580static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008581 .rpc_call_prepare = nfs41_free_stateid_prepare,
8582 .rpc_call_done = nfs41_free_stateid_done,
8583 .rpc_release = nfs41_free_stateid_release,
8584};
8585
8586static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
8587 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008588 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008589 bool privileged)
8590{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008591 struct rpc_message msg = {
8592 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008593 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008594 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008595 struct rpc_task_setup task_setup = {
8596 .rpc_client = server->client,
8597 .rpc_message = &msg,
8598 .callback_ops = &nfs41_free_stateid_ops,
8599 .flags = RPC_TASK_ASYNC,
8600 };
8601 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008602
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008603 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8604 &task_setup.rpc_client, &msg);
8605
Chuck Lever38527b12012-07-11 16:30:23 -04008606 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008607 data = kmalloc(sizeof(*data), GFP_NOFS);
8608 if (!data)
8609 return ERR_PTR(-ENOMEM);
8610 data->server = server;
8611 nfs4_stateid_copy(&data->args.stateid, stateid);
8612
8613 task_setup.callback_data = data;
8614
8615 msg.rpc_argp = &data->args;
8616 msg.rpc_resp = &data->res;
Chuck Levera9c92d62013-08-09 12:48:18 -04008617 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008618 if (privileged)
8619 nfs4_set_sequence_privileged(&data->args.seq_args);
8620
8621 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008622}
8623
Chuck Lever38527b12012-07-11 16:30:23 -04008624/**
8625 * nfs41_free_stateid - perform a FREE_STATEID operation
8626 *
8627 * @server: server / transport on which to perform the operation
8628 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008629 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008630 *
8631 * Returns NFS_OK if the server freed "stateid". Otherwise a
8632 * negative NFS4ERR value is returned.
8633 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008634static int nfs41_free_stateid(struct nfs_server *server,
8635 nfs4_stateid *stateid,
8636 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008637{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008638 struct rpc_task *task;
8639 int ret;
8640
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008641 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008642 if (IS_ERR(task))
8643 return PTR_ERR(task);
8644 ret = rpc_wait_for_completion_task(task);
8645 if (!ret)
8646 ret = task->tk_status;
8647 rpc_put_task(task);
8648 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008649}
Trond Myklebust36281ca2012-03-04 18:13:56 -05008650
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008651static void
8652nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008653{
8654 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008655 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008656
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008657 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008658 nfs4_free_lock_state(server, lsp);
8659 if (IS_ERR(task))
Jeff Laytonf1cdae82014-05-01 06:28:47 -04008660 return;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008661 rpc_put_task(task);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008662}
8663
Trond Myklebust36281ca2012-03-04 18:13:56 -05008664static bool nfs41_match_stateid(const nfs4_stateid *s1,
8665 const nfs4_stateid *s2)
8666{
Trond Myklebust93b717f2016-05-16 17:42:43 -04008667 if (s1->type != s2->type)
8668 return false;
8669
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008670 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008671 return false;
8672
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008673 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008674 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05008675 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05008676 return true;
8677
8678 return false;
8679}
8680
Andy Adamson557134a2009-04-01 09:21:53 -04008681#endif /* CONFIG_NFS_V4_1 */
8682
Trond Myklebust36281ca2012-03-04 18:13:56 -05008683static bool nfs4_match_stateid(const nfs4_stateid *s1,
8684 const nfs4_stateid *s2)
8685{
Trond Myklebustf597c532012-03-04 18:13:56 -05008686 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05008687}
8688
8689
Trond Myklebust17280172012-03-11 13:11:00 -04008690static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008691 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008692 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008693 .recover_open = nfs4_open_reclaim,
8694 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04008695 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04008696 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008697};
8698
Andy Adamson591d71c2009-04-01 09:22:47 -04008699#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008700static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008701 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
8702 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
8703 .recover_open = nfs4_open_reclaim,
8704 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05008705 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008706 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04008707 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04008708};
8709#endif /* CONFIG_NFS_V4_1 */
8710
Trond Myklebust17280172012-03-11 13:11:00 -04008711static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05008712 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05008713 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03008714 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008715 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04008716 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008717};
8718
Andy Adamson591d71c2009-04-01 09:22:47 -04008719#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008720static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04008721 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
8722 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04008723 .recover_open = nfs41_open_expired,
8724 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05008725 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04008726};
8727#endif /* CONFIG_NFS_V4_1 */
8728
Trond Myklebust17280172012-03-11 13:11:00 -04008729static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008730 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04008731 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008732 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04008733};
8734
8735#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04008736static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04008737 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04008738 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04008739 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04008740};
8741#endif
8742
Chuck Leverec011fe2013-10-17 14:12:39 -04008743static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008744 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008745 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008746};
8747
8748#if defined(CONFIG_NFS_V4_1)
8749static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04008750 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04008751 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04008752};
8753#endif /* CONFIG_NFS_V4_1 */
8754
Trond Myklebust97dc1352010-06-16 09:52:26 -04008755static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
8756 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008757 .init_caps = NFS_CAP_READDIRPLUS
8758 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008759 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008760 .init_client = nfs40_init_client,
8761 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008762 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008763 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008764 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008765 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008766 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008767 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
8768 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
8769 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008770 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008771};
8772
8773#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05008774static struct nfs_seqid *
8775nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
8776{
8777 return NULL;
8778}
8779
Trond Myklebust97dc1352010-06-16 09:52:26 -04008780static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
8781 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04008782 .init_caps = NFS_CAP_READDIRPLUS
8783 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04008784 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04008785 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008786 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008787 .init_client = nfs41_init_client,
8788 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05008789 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008790 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04008791 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008792 .alloc_seqid = nfs_alloc_no_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04008793 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04008794 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8795 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8796 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04008797 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04008798};
8799#endif
8800
Steve Dickson42c2c422013-05-22 12:50:38 -04008801#if defined(CONFIG_NFS_V4_2)
8802static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
8803 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04008804 .init_caps = NFS_CAP_READDIRPLUS
8805 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04008806 | NFS_CAP_POSIX_LOCK
8807 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04008808 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05008809 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04008810 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05008811 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04008812 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08008813 | NFS_CAP_LAYOUTSTATS
8814 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04008815 .init_client = nfs41_init_client,
8816 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04008817 .match_stateid = nfs41_match_stateid,
8818 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04008819 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04008820 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust63f5f792015-01-23 19:19:25 -05008821 .alloc_seqid = nfs_alloc_no_seqid,
Steve Dickson42c2c422013-05-22 12:50:38 -04008822 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
8823 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
8824 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08008825 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04008826};
8827#endif
8828
Trond Myklebust97dc1352010-06-16 09:52:26 -04008829const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
8830 [0] = &nfs_v4_0_minor_ops,
8831#if defined(CONFIG_NFS_V4_1)
8832 [1] = &nfs_v4_1_minor_ops,
8833#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04008834#if defined(CONFIG_NFS_V4_2)
8835 [2] = &nfs_v4_2_minor_ops,
8836#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04008837};
8838
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008839ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
8840{
8841 ssize_t error, error2;
8842
8843 error = generic_listxattr(dentry, list, size);
8844 if (error < 0)
8845 return error;
8846 if (list) {
8847 list += error;
8848 size -= error;
8849 }
8850
8851 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
8852 if (error2 < 0)
8853 return error2;
8854 return error + error2;
8855}
8856
Trond Myklebust17f26b12013-08-21 15:48:42 -04008857static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04008858 .create = nfs_create,
8859 .lookup = nfs_lookup,
8860 .atomic_open = nfs_atomic_open,
8861 .link = nfs_link,
8862 .unlink = nfs_unlink,
8863 .symlink = nfs_symlink,
8864 .mkdir = nfs_mkdir,
8865 .rmdir = nfs_rmdir,
8866 .mknod = nfs_mknod,
8867 .rename = nfs_rename,
8868 .permission = nfs_permission,
8869 .getattr = nfs_getattr,
8870 .setattr = nfs_setattr,
8871 .getxattr = generic_getxattr,
8872 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008873 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04008874 .removexattr = generic_removexattr,
8875};
8876
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08008877static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008878 .permission = nfs_permission,
8879 .getattr = nfs_getattr,
8880 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008881 .getxattr = generic_getxattr,
8882 .setxattr = generic_setxattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01008883 .listxattr = nfs4_listxattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008884 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008885};
8886
David Howells509de812006-08-22 20:06:11 -04008887const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008888 .version = 4, /* protocol version */
8889 .dentry_ops = &nfs4_dentry_operations,
8890 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00008891 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04008892 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008893 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04008894 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04008895 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008896 .getattr = nfs4_proc_getattr,
8897 .setattr = nfs4_proc_setattr,
8898 .lookup = nfs4_proc_lookup,
8899 .access = nfs4_proc_access,
8900 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008901 .create = nfs4_proc_create,
8902 .remove = nfs4_proc_remove,
8903 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04008904 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008905 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04008906 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04008907 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04008908 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008909 .link = nfs4_proc_link,
8910 .symlink = nfs4_proc_symlink,
8911 .mkdir = nfs4_proc_mkdir,
8912 .rmdir = nfs4_proc_remove,
8913 .readdir = nfs4_proc_readdir,
8914 .mknod = nfs4_proc_mknod,
8915 .statfs = nfs4_proc_statfs,
8916 .fsinfo = nfs4_proc_fsinfo,
8917 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04008918 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008919 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04008920 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008921 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05008922 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008923 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008924 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008925 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04008926 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05008927 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008928 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00008929 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04008930 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04008931 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04008932 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04008933 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04008934 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00008935 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04008936 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04008937 .create_server = nfs4_create_server,
8938 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008939};
8940
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008941static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01008942 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008943 .list = nfs4_xattr_list_nfs4_acl,
8944 .get = nfs4_xattr_get_nfs4_acl,
8945 .set = nfs4_xattr_set_nfs4_acl,
8946};
8947
8948const struct xattr_handler *nfs4_xattr_handlers[] = {
8949 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04008950#ifdef CONFIG_NFS_V4_SECURITY_LABEL
8951 &nfs4_xattr_nfs4_label_handler,
8952#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00008953 NULL
8954};
8955
Linus Torvalds1da177e2005-04-16 15:20:36 -07008956/*
8957 * Local variables:
8958 * c-basic-offset: 8
8959 * End:
8960 */