blob: 09c7e3b87c4698080c59f87b38cb75465f1c6b8c [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>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050054#include <linux/nfs_idmap.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"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050066#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000067#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebustc6d01c62013-08-09 11:51:26 -040069#include "nfs4trace.h"
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFSDBG_FACILITY NFSDBG_PROC
72
Trond Myklebust2066fe82006-09-15 08:30:46 -040073#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_POLL_RETRY_MAX (15*HZ)
75
Trond Myklebustcdd4e682006-01-03 09:55:12 +010076struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010077static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080078static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050080static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050081static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040082static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
83static 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 -040084static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
85 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -040086 struct nfs4_state *state, struct nfs4_label *ilabel,
87 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Trond Myklebustab7cb0d2013-05-20 11:20:27 -040089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
90 struct rpc_cred *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *,
92 struct rpc_cred *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040093#endif
David Quigleyaa9c2662013-05-22 12:50:44 -040094
95#ifdef CONFIG_NFS_V4_SECURITY_LABEL
96static inline struct nfs4_label *
97nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
98 struct iattr *sattr, struct nfs4_label *label)
99{
100 int err;
101
102 if (label == NULL)
103 return NULL;
104
105 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
106 return NULL;
107
108 if (NFS_SERVER(dir)->nfs_client->cl_minorversion < 2)
109 return NULL;
110
111 err = security_dentry_init_security(dentry, sattr->ia_mode,
112 &dentry->d_name, (void **)&label->label, &label->len);
113 if (err == 0)
114 return label;
115
116 return NULL;
117}
118static inline void
119nfs4_label_release_security(struct nfs4_label *label)
120{
121 if (label)
122 security_release_secctx(label->label, label->len);
123}
124static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
125{
126 if (label)
127 return server->attr_bitmask;
128
129 return server->attr_bitmask_nl;
130}
131#else
132static inline struct nfs4_label *
133nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
134 struct iattr *sattr, struct nfs4_label *l)
135{ return NULL; }
136static inline void
137nfs4_label_release_security(struct nfs4_label *label)
138{ return; }
139static inline u32 *
140nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
141{ return server->attr_bitmask; }
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500145static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Trond Myklebust52567b02009-10-23 14:46:42 -0400147 if (err >= -1000)
148 return err;
149 switch (err) {
150 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500151 case -NFS4ERR_LAYOUTTRYLATER:
152 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400153 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000154 case -NFS4ERR_WRONGSEC:
155 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800156 case -NFS4ERR_BADOWNER:
157 case -NFS4ERR_BADNAME:
158 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400159 case -NFS4ERR_SHARE_DENIED:
160 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400161 case -NFS4ERR_MINOR_VERS_MISMATCH:
162 return -EPROTONOSUPPORT;
Weston Andros Adamson6168f622012-09-10 14:00:46 -0400163 case -NFS4ERR_ACCESS:
164 return -EACCES;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400165 case -NFS4ERR_FILE_OPEN:
166 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400167 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700169 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400172 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*
176 * This is our standard bitmap for GETATTR requests.
177 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400178const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 FATTR4_WORD0_TYPE
180 | FATTR4_WORD0_CHANGE
181 | FATTR4_WORD0_SIZE
182 | FATTR4_WORD0_FSID
183 | FATTR4_WORD0_FILEID,
184 FATTR4_WORD1_MODE
185 | FATTR4_WORD1_NUMLINKS
186 | FATTR4_WORD1_OWNER
187 | FATTR4_WORD1_OWNER_GROUP
188 | FATTR4_WORD1_RAWDEV
189 | FATTR4_WORD1_SPACE_USED
190 | FATTR4_WORD1_TIME_ACCESS
191 | FATTR4_WORD1_TIME_METADATA
David Quigleyaa9c2662013-05-22 12:50:44 -0400192 | FATTR4_WORD1_TIME_MODIFY,
193#ifdef CONFIG_NFS_V4_SECURITY_LABEL
194 FATTR4_WORD2_SECURITY_LABEL
195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196};
197
Trond Myklebust1549210f2012-06-05 09:16:47 -0400198static const u32 nfs4_pnfs_open_bitmap[3] = {
199 FATTR4_WORD0_TYPE
200 | FATTR4_WORD0_CHANGE
201 | FATTR4_WORD0_SIZE
202 | FATTR4_WORD0_FSID
203 | FATTR4_WORD0_FILEID,
204 FATTR4_WORD1_MODE
205 | FATTR4_WORD1_NUMLINKS
206 | FATTR4_WORD1_OWNER
207 | FATTR4_WORD1_OWNER_GROUP
208 | FATTR4_WORD1_RAWDEV
209 | FATTR4_WORD1_SPACE_USED
210 | FATTR4_WORD1_TIME_ACCESS
211 | FATTR4_WORD1_TIME_METADATA
212 | FATTR4_WORD1_TIME_MODIFY,
213 FATTR4_WORD2_MDSTHRESHOLD
214};
215
Andy Adamsone23008e2012-10-02 21:07:32 -0400216static const u32 nfs4_open_noattr_bitmap[3] = {
217 FATTR4_WORD0_TYPE
218 | FATTR4_WORD0_CHANGE
219 | FATTR4_WORD0_FILEID,
220};
221
David Quigleya09df2c2013-05-22 12:50:41 -0400222const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 FATTR4_WORD0_FILES_AVAIL
224 | FATTR4_WORD0_FILES_FREE
225 | FATTR4_WORD0_FILES_TOTAL,
226 FATTR4_WORD1_SPACE_AVAIL
227 | FATTR4_WORD1_SPACE_FREE
228 | FATTR4_WORD1_SPACE_TOTAL
229};
230
David Quigleya09df2c2013-05-22 12:50:41 -0400231const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 FATTR4_WORD0_MAXLINK
233 | FATTR4_WORD0_MAXNAME,
234 0
235};
236
Fred Isamandae100c2011-07-30 20:52:37 -0400237const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 | FATTR4_WORD0_MAXREAD
239 | FATTR4_WORD0_MAXWRITE
240 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700241 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400242 | FATTR4_WORD1_FS_LAYOUT_TYPES,
243 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_fs_locations_bitmap[3] = {
Manoj Naik830b8e32006-06-09 09:34:25 -0400247 FATTR4_WORD0_TYPE
248 | FATTR4_WORD0_CHANGE
249 | FATTR4_WORD0_SIZE
250 | FATTR4_WORD0_FSID
251 | FATTR4_WORD0_FILEID
252 | FATTR4_WORD0_FS_LOCATIONS,
253 FATTR4_WORD1_MODE
254 | FATTR4_WORD1_NUMLINKS
255 | FATTR4_WORD1_OWNER
256 | FATTR4_WORD1_OWNER_GROUP
257 | FATTR4_WORD1_RAWDEV
258 | FATTR4_WORD1_SPACE_USED
259 | FATTR4_WORD1_TIME_ACCESS
260 | FATTR4_WORD1_TIME_METADATA
261 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400262 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400263};
264
Al Virobc4785c2006-10-19 23:28:51 -0700265static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 struct nfs4_readdir_arg *readdir)
267{
Al Viro0dbb4c62006-10-19 23:28:49 -0700268 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000271 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
273 return;
274 }
275
276 readdir->cookie = 0;
277 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
278 if (cookie == 2)
279 return;
280
281 /*
282 * NFSv4 servers do not return entries for '.' and '..'
283 * Therefore, we fake these entries here. We let '.'
284 * have cookie 0 and '..' have cookie 1. Note that
285 * when talking to the server, we always send cookie 0
286 * instead of 1 or 2.
287 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800288 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 if (cookie == 0) {
291 *p++ = xdr_one; /* next */
292 *p++ = xdr_zero; /* cookie, first word */
293 *p++ = xdr_one; /* cookie, second word */
294 *p++ = xdr_one; /* entry len */
295 memcpy(p, ".\0\0\0", 4); /* entry */
296 p++;
297 *p++ = xdr_one; /* bitmap length */
298 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
299 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400300 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
303 *p++ = xdr_one; /* next */
304 *p++ = xdr_zero; /* cookie, first word */
305 *p++ = xdr_two; /* cookie, second word */
306 *p++ = xdr_two; /* entry len */
307 memcpy(p, "..\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 */
Peter Staubach4e769b92007-08-03 15:07:10 -0400312 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 readdir->pgbase = (char *)p - (char *)start;
315 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800316 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Trond Myklebust65de8722008-12-23 15:21:44 -0500319static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
320{
321 int res = 0;
322
323 might_sleep();
324
325 if (*timeout <= 0)
326 *timeout = NFS4_POLL_RETRY_MIN;
327 if (*timeout > NFS4_POLL_RETRY_MAX)
328 *timeout = NFS4_POLL_RETRY_MAX;
Colin Cross416ad3c2013-05-06 23:50:06 +0000329 freezable_schedule_timeout_killable_unsafe(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500330 if (fatal_signal_pending(current))
331 res = -ERESTARTSYS;
332 *timeout <<= 1;
333 return res;
334}
335
336/* This is the error handling routine for processes that are allowed
337 * to sleep.
338 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800339static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500340{
341 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500342 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500343 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500344 int ret = errorcode;
345
346 exception->retry = 0;
347 switch(errorcode) {
348 case 0:
349 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500350 case -NFS4ERR_OPENMODE:
Bryan Schumaker011e2a72012-06-20 15:53:43 -0400351 if (inode && nfs4_have_delegation(inode, FMODE_READ)) {
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400352 nfs4_inode_return_delegation(inode);
Trond Myklebust3114ea72012-03-07 16:39:06 -0500353 exception->retry = 1;
354 return 0;
355 }
356 if (state == NULL)
357 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400358 ret = nfs4_schedule_stateid_recovery(server, state);
359 if (ret < 0)
360 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500361 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500362 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500363 case -NFS4ERR_ADMIN_REVOKED:
364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustb570a972013-04-12 15:04:51 -0400365 if (inode != NULL && nfs4_have_delegation(inode, FMODE_READ)) {
366 nfs_remove_bad_delegation(inode);
367 exception->retry = 1;
368 break;
369 }
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500370 if (state == NULL)
371 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400372 ret = nfs4_schedule_stateid_recovery(server, state);
373 if (ret < 0)
374 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500375 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400376 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -0400377 if (state != NULL) {
378 ret = nfs4_schedule_stateid_recovery(server, state);
379 if (ret < 0)
380 break;
381 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500382 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500383 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500384 nfs4_schedule_lease_recovery(clp);
385 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500386#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400387 case -NFS4ERR_BADSESSION:
388 case -NFS4ERR_BADSLOT:
389 case -NFS4ERR_BAD_HIGH_SLOT:
390 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
391 case -NFS4ERR_DEADSESSION:
392 case -NFS4ERR_SEQ_FALSE_RETRY:
393 case -NFS4ERR_SEQ_MISORDERED:
394 dprintk("%s ERROR: %d Reset session\n", __func__,
395 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400396 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400397 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500398#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500399 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500400 if (exception->timeout > HZ) {
401 /* We have retried a decent amount, time to
402 * fail
403 */
404 ret = -EBUSY;
405 break;
406 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 case -NFS4ERR_GRACE:
408 case -NFS4ERR_DELAY:
409 ret = nfs4_delay(server->client, &exception->timeout);
410 if (ret != 0)
411 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400412 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 case -NFS4ERR_OLD_STATEID:
414 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800415 break;
416 case -NFS4ERR_BADOWNER:
417 /* The following works around a Linux server bug! */
418 case -NFS4ERR_BADNAME:
419 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
420 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
421 exception->retry = 1;
422 printk(KERN_WARNING "NFS: v4 server %s "
423 "does not accept raw "
424 "uid/gids. "
425 "Reenabling the idmapper.\n",
426 server->nfs_client->cl_hostname);
427 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500428 }
429 /* We failed to handle the error */
430 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500431wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500432 ret = nfs4_wait_clnt_recover(clp);
433 if (ret == 0)
434 exception->retry = 1;
435 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500436}
437
438
Trond Myklebust452e9352010-07-31 14:29:06 -0400439static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 spin_lock(&clp->cl_lock);
442 if (time_before(clp->cl_last_renewal,timestamp))
443 clp->cl_last_renewal = timestamp;
444 spin_unlock(&clp->cl_lock);
445}
446
Trond Myklebust452e9352010-07-31 14:29:06 -0400447static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
448{
449 do_renew_lease(server->nfs_client, timestamp);
450}
451
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400452#if defined(CONFIG_NFS_V4_1)
453
Trond Myklebustd185a332010-06-16 09:52:25 -0400454static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400455{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500456 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400457 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500458 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400459
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400461 /* just wake up the next guy waiting since
462 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500463 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500464 return;
Andy Adamson13615872009-04-01 09:22:17 -0400465 }
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500466 tbl = res->sr_slot->table;
467 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500468
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500469 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500470 /* Be nice to the server: try to ensure that the last transmitted
471 * value for highest_user_slotid <= target_highest_slotid
472 */
473 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
474 send_new_highest_used_slotid = true;
475
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500476 if (nfs41_wake_and_assign_slot(tbl, res->sr_slot)) {
477 send_new_highest_used_slotid = false;
478 goto out_unlock;
479 }
Trond Myklebustf4af6e2a2012-11-20 14:17:32 -0500480 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500481
482 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
483 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500484out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500485 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400486 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500487 if (send_new_highest_used_slotid)
488 nfs41_server_notify_highest_slotid_update(session->clp);
Andy Adamson13615872009-04-01 09:22:17 -0400489}
490
Trond Myklebust14516c32010-07-31 14:29:06 -0400491static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500493 struct nfs4_session *session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500494 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500496 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500497 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400498
Bryan Schumaker468f8612011-04-18 15:57:32 -0400499 /* don't increment the sequence number if the task wasn't sent */
500 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 goto out;
502
Trond Myklebust933602e2012-11-16 12:12:38 -0500503 slot = res->sr_slot;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500504 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500505
Trond Myklebustac20d162012-12-15 15:36:07 -0500506 if (slot->interrupted) {
507 slot->interrupted = 0;
508 interrupted = true;
509 }
510
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400511 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500512 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400513 switch (res->sr_status) {
514 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400515 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust933602e2012-11-16 12:12:38 -0500516 ++slot->seq_nr;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500517 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500518 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500519 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500520 if (res->sr_status_flags != 0)
521 nfs4_schedule_lease_recovery(clp);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500522 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400523 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500524 case 1:
525 /*
526 * sr_status remains 1 if an RPC level error occurred.
527 * The server may or may not have processed the sequence
528 * operation..
529 * Mark the slot as having hosted an interrupted RPC call.
530 */
531 slot->interrupted = 1;
532 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400533 case -NFS4ERR_DELAY:
534 /* The server detected a resend of the RPC call and
535 * returned NFS4ERR_DELAY as per Section 2.10.6.2
536 * of RFC5661.
537 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500538 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400539 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500540 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500541 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400542 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500543 case -NFS4ERR_BADSLOT:
544 /*
545 * The slot id we used was probably retired. Try again
546 * using a different slot id.
547 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500548 goto retry_nowait;
549 case -NFS4ERR_SEQ_MISORDERED:
550 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500551 * Was the last operation on this sequence interrupted?
552 * If so, retry after bumping the sequence number.
553 */
554 if (interrupted) {
555 ++slot->seq_nr;
556 goto retry_nowait;
557 }
558 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500559 * Could this slot have been previously retired?
560 * If so, then the server may be expecting seq_nr = 1!
561 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500562 if (slot->seq_nr != 1) {
563 slot->seq_nr = 1;
564 goto retry_nowait;
565 }
566 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500567 case -NFS4ERR_SEQ_FALSE_RETRY:
568 ++slot->seq_nr;
569 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400570 default:
571 /* Just update the slot sequence no. */
Trond Myklebust933602e2012-11-16 12:12:38 -0500572 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400573 }
574out:
575 /* The session may be reset by one of the error handlers. */
576 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400577 nfs41_sequence_free_slot(res);
Trond Myklebust85563072012-12-11 10:31:12 -0500578 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500579retry_nowait:
580 if (rpc_restart_call_prepare(task)) {
581 task->tk_status = 0;
582 ret = 0;
583 }
584 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400585out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400586 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400587 goto out;
588 rpc_delay(task, NFS4_POLL_RETRY_MAX);
589 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400590}
591
Trond Myklebust14516c32010-07-31 14:29:06 -0400592static int nfs4_sequence_done(struct rpc_task *task,
593 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400594{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500595 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400596 return 1;
597 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400598}
599
Trond Myklebust9d12b212012-01-17 22:04:25 -0500600static void nfs41_init_sequence(struct nfs4_sequence_args *args,
601 struct nfs4_sequence_res *res, int cache_reply)
602{
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500603 args->sa_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500604 args->sa_cache_this = 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400605 args->sa_privileged = 0;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500606 if (cache_reply)
607 args->sa_cache_this = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -0500608 res->sr_slot = NULL;
609}
610
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400611static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
612{
613 args->sa_privileged = 1;
614}
615
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000616int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 struct nfs4_sequence_args *args,
618 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 struct rpc_task *task)
620{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400621 struct nfs4_slot *slot;
622 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400623
624 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400626 if (res->sr_slot != NULL)
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400627 goto out_success;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400629 tbl = &session->fc_slot_table;
630
Trond Myklebust69d206b2012-11-22 13:21:02 -0500631 task->tk_timeout = 0;
632
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400633 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson774d5f12013-05-20 14:13:50 -0400634 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400635 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500636 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500637 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400638 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400639 }
640
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500641 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500642 if (IS_ERR(slot)) {
643 /* If out of memory, try again in 1/4 second */
644 if (slot == ERR_PTR(-ENOMEM))
645 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400646 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400647 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400648 }
649 spin_unlock(&tbl->slot_tbl_lock);
650
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500651 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400652
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500653 dprintk("<-- %s slotid=%d seqid=%d\n", __func__,
654 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400655
Benny Halevydfb4f3092010-09-24 09:17:01 -0400656 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500657 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400658 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400659 /*
660 * sr_status is only set in decode_sequence, and so will remain
661 * set to 1 if an rpc level failure occurs.
662 */
663 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400664 trace_nfs4_setup_sequence(session, args);
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400665out_success:
666 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400668out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400669 /* Privileged tasks are queued with top priority */
670 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400671 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
672 NULL, RPC_PRIORITY_PRIVILEGED);
673 else
674 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400675 spin_unlock(&tbl->slot_tbl_lock);
676 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000678EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679
Trond Myklebust035168a2010-06-16 09:52:26 -0400680int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 struct rpc_task *task)
684{
Trond Myklebust035168a2010-06-16 09:52:26 -0400685 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 int ret = 0;
687
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400688 if (session == NULL) {
689 rpc_call_start(task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 goto out;
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400691 }
Trond Myklebust035168a2010-06-16 09:52:26 -0400692
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500693 dprintk("--> %s clp %p session %p sr_slot %d\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 __func__, session->clp, session, res->sr_slot ?
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500695 res->sr_slot->slot_nr : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400696
Trond Myklebust9d12b212012-01-17 22:04:25 -0500697 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698out:
699 dprintk("<-- %s status=%d\n", __func__, ret);
700 return ret;
701}
702
703struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400704 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400705 struct nfs4_sequence_args *seq_args;
706 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707};
708
709static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
710{
711 struct nfs41_call_sync_data *data = calldata;
Trond Myklebust6ba7db32012-10-22 20:07:20 -0400712 struct nfs4_session *session = nfs4_get_session(data->seq_server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713
Trond Myklebust035168a2010-06-16 09:52:26 -0400714 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
715
Trond Myklebustd9afbd12012-10-22 20:28:44 -0400716 nfs41_setup_sequence(session, data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400717}
718
Andy Adamson69ab40c2009-04-01 09:22:19 -0400719static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
720{
721 struct nfs41_call_sync_data *data = calldata;
722
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400724}
725
Trond Myklebust17280172012-03-11 13:11:00 -0400726static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400727 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400728 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400729};
730
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400735 struct nfs4_sequence_res *res)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400736{
737 int ret;
738 struct rpc_task *task;
739 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400740 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400741 .seq_args = args,
742 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400743 };
744 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400746 .rpc_message = msg,
747 .callback_ops = &nfs41_call_sync_ops,
748 .callback_data = &data
749 };
750
Andy Adamsonce5039c2009-04-01 09:22:13 -0400751 task = rpc_run_task(&task_setup);
752 if (IS_ERR(task))
753 ret = PTR_ERR(task);
754 else {
755 ret = task->tk_status;
756 rpc_put_task(task);
757 }
758 return ret;
759}
760
Trond Myklebustdf896452010-06-16 09:52:26 -0400761#else
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400762static
Trond Myklebust9d12b212012-01-17 22:04:25 -0500763void nfs41_init_sequence(struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res, int cache_reply)
765{
766}
767
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400768static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
769{
770}
771
772
Trond Myklebust14516c32010-07-31 14:29:06 -0400773static int nfs4_sequence_done(struct rpc_task *task,
774 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400775{
Trond Myklebust14516c32010-07-31 14:29:06 -0400776 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400777}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400778#endif /* CONFIG_NFS_V4_1 */
779
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400780static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000781int _nfs4_call_sync(struct rpc_clnt *clnt,
782 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400783 struct rpc_message *msg,
784 struct nfs4_sequence_args *args,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400785 struct nfs4_sequence_res *res)
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400786{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000787 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400788}
789
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400790static
Bryan Schumaker7c513052011-03-24 17:12:24 +0000791int nfs4_call_sync(struct rpc_clnt *clnt,
792 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000793 struct rpc_message *msg,
794 struct nfs4_sequence_args *args,
795 struct nfs4_sequence_res *res,
796 int cache_reply)
797{
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400798 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000799 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
Trond Myklebustfd0c0952012-11-01 14:43:38 -0400800 args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000801}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400802
Trond Myklebust38478b22006-05-25 01:40:57 -0400803static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Trond Myklebust38478b22006-05-25 01:40:57 -0400805 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Trond Myklebust38478b22006-05-25 01:40:57 -0400807 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400808 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700809 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400810 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700811 dir->i_version = cinfo->after;
David Howellsde242c02012-12-20 21:52:38 +0000812 nfs_fscache_invalidate(dir);
Trond Myklebust38478b22006-05-25 01:40:57 -0400813 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400817 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 struct nfs_openargs o_arg;
819 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100820 struct nfs_open_confirmargs c_arg;
821 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500822 struct nfs4_string owner_name;
823 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400825 struct nfs4_label *f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400827 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400829 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100831 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400832 unsigned int rpc_done : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -0400833 unsigned int is_recover : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100834 int rpc_status;
835 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836};
837
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400838static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
839 int err, struct nfs4_exception *exception)
840{
841 if (err != -EINVAL)
842 return false;
843 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
844 return false;
845 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
846 exception->retry = 1;
847 return true;
848}
849
850static enum open_claim_type4
851nfs4_map_atomic_open_claim(struct nfs_server *server,
852 enum open_claim_type4 claim)
853{
854 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
855 return claim;
856 switch (claim) {
857 default:
858 return claim;
859 case NFS4_OPEN_CLAIM_FH:
860 return NFS4_OPEN_CLAIM_NULL;
861 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
862 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
863 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
864 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
865 }
866}
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400867
868static void nfs4_init_opendata_res(struct nfs4_opendata *p)
869{
870 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -0400871 p->o_res.f_label = p->f_label;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400872 p->o_res.seqid = p->o_arg.seqid;
873 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400874 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -0400875 p->o_res.access_request = p->o_arg.access;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400876 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500877 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400878}
879
Al Viro82a2c1b2011-06-22 18:30:55 -0400880static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400882 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -0400883 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400884 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400885 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100886{
Al Viro82a2c1b2011-06-22 18:30:55 -0400887 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100888 struct inode *dir = parent->d_inode;
889 struct nfs_server *server = NFS_SERVER(dir);
890 struct nfs4_opendata *p;
891
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400892 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100893 if (p == NULL)
894 goto err;
David Quigley14c43f72013-05-22 12:50:43 -0400895
896 p->f_label = nfs4_label_alloc(server, gfp_mask);
897 if (IS_ERR(p->f_label))
898 goto err_free_p;
899
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400900 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100901 if (p->o_arg.seqid == NULL)
David Quigley14c43f72013-05-22 12:50:43 -0400902 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -0400903 nfs_sb_active(dentry->d_sb);
904 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100905 p->dir = parent;
906 p->owner = sp;
907 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 p->o_arg.open_flags = flags;
909 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700910 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
911 * will return permission denied for all bits until close */
912 if (!(flags & O_EXCL)) {
913 /* ask server to check for all possible rights as results
914 * are cached */
915 p->o_arg.access = NFS4_ACCESS_READ | NFS4_ACCESS_MODIFY |
916 NFS4_ACCESS_EXTEND | NFS4_ACCESS_EXECUTE;
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -0700917 }
David Howells7539bba2006-08-22 20:06:09 -0400918 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400919 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
920 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400921 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100922 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -0400923 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -0400924 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
David Quigley1775fd32013-05-22 12:50:42 -0400925 p->o_arg.label = label;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -0400926 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
927 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -0400928 case NFS4_OPEN_CLAIM_NULL:
929 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
930 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
931 p->o_arg.fh = NFS_FH(dir);
932 break;
933 case NFS4_OPEN_CLAIM_PREVIOUS:
934 case NFS4_OPEN_CLAIM_FH:
935 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
936 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
937 p->o_arg.fh = NFS_FH(dentry->d_inode);
938 }
Trond Myklebust536e43d2012-01-17 22:04:26 -0500939 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -0400940 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400941
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100942 p->o_arg.u.attrs = &p->attrs;
943 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500944
945 verf[0] = jiffies;
946 verf[1] = current->pid;
947 memcpy(p->o_arg.u.verifier.data, verf,
948 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100949 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100950 p->c_arg.fh = &p->o_res.fh;
951 p->c_arg.stateid = &p->o_res.stateid;
952 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400953 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400954 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100955 return p;
David Quigley14c43f72013-05-22 12:50:43 -0400956
957err_free_label:
958 nfs4_label_free(p->f_label);
959err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100960 kfree(p);
961err:
962 dput(parent);
963 return NULL;
964}
965
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400966static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100967{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400968 struct nfs4_opendata *p = container_of(kref,
969 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400970 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400971
972 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 if (p->state != NULL)
974 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400975 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -0400976
977 nfs4_label_free(p->f_label);
978
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400979 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400980 dput(p->dentry);
981 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500982 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400983 kfree(p);
984}
985
986static void nfs4_opendata_put(struct nfs4_opendata *p)
987{
988 if (p != NULL)
989 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100990}
991
Trond Myklebust06f814a2006-01-03 09:55:07 +0100992static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
993{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100994 int ret;
995
Trond Myklebust06f814a2006-01-03 09:55:07 +0100996 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100997 return ret;
998}
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001{
1002 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003
Trond Myklebust536e43d2012-01-17 22:04:26 -05001004 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 goto out;
1006 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001007 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001008 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1009 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 break;
1011 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001012 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1013 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014 break;
1015 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001016 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1017 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020 return ret;
1021}
1022
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001025 if (delegation == NULL)
1026 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -05001029 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001031 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1032 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -05001033 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 return 1;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001038{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001040 case FMODE_WRITE:
1041 state->n_wronly++;
1042 break;
1043 case FMODE_READ:
1044 state->n_rdonly++;
1045 break;
1046 case FMODE_READ|FMODE_WRITE:
1047 state->n_rdwr++;
1048 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001050}
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001053{
1054 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001055 nfs4_stateid_copy(&state->stateid, stateid);
1056 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust92b40e92013-04-20 01:25:45 -04001057 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001059 case FMODE_READ:
1060 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1061 break;
1062 case FMODE_WRITE:
1063 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1064 break;
1065 case FMODE_READ|FMODE_WRITE:
1066 set_bit(NFS_O_RDWR_STATE, &state->flags);
1067 }
1068}
1069
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001070static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -04001071{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001072 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001073 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001074 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001075}
1076
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001077static 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 -07001078{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001079 /*
1080 * Protect the call to nfs4_state_set_mode_locked and
1081 * serialise the stateid update
1082 */
1083 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001084 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001085 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001086 set_bit(NFS_DELEGATED_STATE, &state->flags);
1087 }
1088 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001090 write_sequnlock(&state->seqlock);
1091 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001093 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096static 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 -05001097{
1098 struct nfs_inode *nfsi = NFS_I(state->inode);
1099 struct nfs_delegation *deleg_cur;
1100 int ret = 0;
1101
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 rcu_read_lock();
1105 deleg_cur = rcu_dereference(nfsi->delegation);
1106 if (deleg_cur == NULL)
1107 goto no_delegation;
1108
1109 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001110 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001111 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001113 goto no_delegation_unlock;
1114
1115 if (delegation == NULL)
1116 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001117 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001118 goto no_delegation_unlock;
1119
Trond Myklebustb7391f42008-12-23 15:21:52 -05001120 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001122 ret = 1;
1123no_delegation_unlock:
1124 spin_unlock(&deleg_cur->lock);
1125no_delegation:
1126 rcu_read_unlock();
1127
1128 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001130 ret = 1;
1131 }
1132
1133 return ret;
1134}
1135
1136
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001138{
1139 struct nfs_delegation *delegation;
1140
1141 rcu_read_lock();
1142 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001144 rcu_read_unlock();
1145 return;
1146 }
1147 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001148 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001149}
1150
Trond Myklebust6ee41262007-07-08 14:11:36 -04001151static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001152{
1153 struct nfs4_state *state = opendata->state;
1154 struct nfs_inode *nfsi = NFS_I(state->inode);
1155 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001156 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001158 nfs4_stateid stateid;
1159 int ret = -EAGAIN;
1160
Trond Myklebustaac00a82007-07-05 19:02:21 -04001161 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001162 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001163 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001164 if (can_open_cached(state, fmode, open_mode)) {
1165 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001166 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001167 goto out_return_state;
1168 }
1169 spin_unlock(&state->owner->so_lock);
1170 }
Trond Myklebust34310432008-12-23 15:21:38 -05001171 rcu_read_lock();
1172 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001173 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001174 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001175 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001176 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001177 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001178 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001179 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001180 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001181 if (!opendata->is_recover) {
1182 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1183 if (ret != 0)
1184 goto out;
1185 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001186 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001187
1188 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001189 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001190 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001191 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001192out:
1193 return ERR_PTR(ret);
1194out_return_state:
1195 atomic_inc(&state->count);
1196 return state;
1197}
1198
Andy Adamsone23008e2012-10-02 21:07:32 -04001199static void
1200nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1201{
1202 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1203 struct nfs_delegation *delegation;
1204 int delegation_flags = 0;
1205
1206 rcu_read_lock();
1207 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1208 if (delegation)
1209 delegation_flags = delegation->flags;
1210 rcu_read_unlock();
1211 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1212 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1213 "returning a delegation for "
1214 "OPEN(CLAIM_DELEGATE_CUR)\n",
1215 clp->cl_hostname);
1216 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
1217 nfs_inode_set_delegation(state->inode,
1218 data->owner->so_cred,
1219 &data->o_res);
1220 else
1221 nfs_inode_reclaim_delegation(state->inode,
1222 data->owner->so_cred,
1223 &data->o_res);
1224}
1225
1226/*
1227 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1228 * and update the nfs4_state.
1229 */
1230static struct nfs4_state *
1231_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1232{
1233 struct inode *inode = data->state->inode;
1234 struct nfs4_state *state = data->state;
1235 int ret;
1236
1237 if (!data->rpc_done) {
1238 ret = data->rpc_status;
1239 goto err;
1240 }
1241
1242 ret = -ESTALE;
1243 if (!(data->f_attr.valid & NFS_ATTR_FATTR_TYPE) ||
1244 !(data->f_attr.valid & NFS_ATTR_FATTR_FILEID) ||
1245 !(data->f_attr.valid & NFS_ATTR_FATTR_CHANGE))
1246 goto err;
1247
1248 ret = -ENOMEM;
1249 state = nfs4_get_open_state(inode, data->owner);
1250 if (state == NULL)
1251 goto err;
1252
1253 ret = nfs_refresh_inode(inode, &data->f_attr);
1254 if (ret)
1255 goto err;
1256
David Quigleyaa9c2662013-05-22 12:50:44 -04001257 nfs_setsecurity(inode, &data->f_attr, data->f_label);
1258
Andy Adamsone23008e2012-10-02 21:07:32 -04001259 if (data->o_res.delegation_type != 0)
1260 nfs4_opendata_check_deleg(data, state);
1261 update_open_stateid(state, &data->o_res.stateid, NULL,
1262 data->o_arg.fmode);
1263
1264 return state;
1265err:
1266 return ERR_PTR(ret);
1267
1268}
1269
1270static struct nfs4_state *
1271_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001272{
1273 struct inode *inode;
1274 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001275 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001276
Trond Myklebustaac00a82007-07-05 19:02:21 -04001277 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001278 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001279 goto out;
1280 }
1281
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001282 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001283 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001284 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001285 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001286 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001287 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001288 goto err;
1289 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001290 state = nfs4_get_open_state(inode, data->owner);
1291 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001292 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001293 if (data->o_res.delegation_type != 0)
1294 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001295 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001297 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001298out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001299 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001300 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301err_put_inode:
1302 iput(inode);
1303err:
1304 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001305}
1306
Andy Adamsone23008e2012-10-02 21:07:32 -04001307static struct nfs4_state *
1308nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1309{
1310 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
1311 return _nfs4_opendata_reclaim_to_nfs4_state(data);
1312 return _nfs4_opendata_to_nfs4_state(data);
1313}
1314
Trond Myklebust864472e2006-01-03 09:55:15 +01001315static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1316{
1317 struct nfs_inode *nfsi = NFS_I(state->inode);
1318 struct nfs_open_context *ctx;
1319
1320 spin_lock(&state->inode->i_lock);
1321 list_for_each_entry(ctx, &nfsi->open_files, list) {
1322 if (ctx->state != state)
1323 continue;
1324 get_nfs_open_context(ctx);
1325 spin_unlock(&state->inode->i_lock);
1326 return ctx;
1327 }
1328 spin_unlock(&state->inode->i_lock);
1329 return ERR_PTR(-ENOENT);
1330}
1331
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001332static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1333 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001334{
1335 struct nfs4_opendata *opendata;
1336
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001337 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001338 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001339 if (opendata == NULL)
1340 return ERR_PTR(-ENOMEM);
1341 opendata->state = state;
1342 atomic_inc(&state->count);
1343 return opendata;
1344}
1345
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001346static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001347{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001348 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001349 int ret;
1350
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 opendata->o_arg.open_flags = 0;
1352 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001353 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1354 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1355 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001356 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001357 if (ret != 0)
1358 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001359 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 if (IS_ERR(newstate))
1361 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001362 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001363 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001364 return 0;
1365}
1366
1367static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1368{
Trond Myklebust864472e2006-01-03 09:55:15 +01001369 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001370 int ret;
1371
1372 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001373 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001374 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001375 smp_rmb();
1376 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001377 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001378 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001379 if (ret != 0)
1380 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001381 if (newstate != state)
1382 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001383 }
1384 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001386 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001387 if (ret != 0)
1388 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001389 if (newstate != state)
1390 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001391 }
1392 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001393 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001394 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001395 if (ret != 0)
1396 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001397 if (newstate != state)
1398 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001399 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001400 /*
1401 * We may have performed cached opens for all three recoveries.
1402 * Check if we need to update the current stateid.
1403 */
1404 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001405 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001406 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001407 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001408 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001409 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001410 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001411 return 0;
1412}
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * OPEN_RECLAIM:
1416 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001418static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001420 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001421 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 int status;
1424
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001425 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1426 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001427 if (IS_ERR(opendata))
1428 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001429 rcu_read_lock();
1430 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001431 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001432 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001433 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001434 opendata->o_arg.u.delegation_type = delegation_type;
1435 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001436 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 return status;
1438}
1439
Trond Myklebust539cd032007-06-05 11:46:42 -04001440static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct nfs_server *server = NFS_SERVER(state->inode);
1443 struct nfs4_exception exception = { };
1444 int err;
1445 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001446 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001447 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001448 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1449 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001450 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001451 break;
1452 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 } while (exception.retry);
1454 return err;
1455}
1456
Trond Myklebust864472e2006-01-03 09:55:15 +01001457static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1458{
1459 struct nfs_open_context *ctx;
1460 int ret;
1461
1462 ctx = nfs4_state_find_open_context(state);
1463 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001464 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001465 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001466 put_nfs_open_context(ctx);
1467 return ret;
1468}
1469
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001470static 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 -07001471{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001472 switch (err) {
1473 default:
1474 printk(KERN_ERR "NFS: %s: unhandled error "
1475 "%d.\n", __func__, err);
1476 case 0:
1477 case -ENOENT:
1478 case -ESTALE:
1479 break;
1480 case -NFS4ERR_BADSESSION:
1481 case -NFS4ERR_BADSLOT:
1482 case -NFS4ERR_BAD_HIGH_SLOT:
1483 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1484 case -NFS4ERR_DEADSESSION:
1485 set_bit(NFS_DELEGATED_STATE, &state->flags);
1486 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1487 return -EAGAIN;
1488 case -NFS4ERR_STALE_CLIENTID:
1489 case -NFS4ERR_STALE_STATEID:
1490 set_bit(NFS_DELEGATED_STATE, &state->flags);
1491 case -NFS4ERR_EXPIRED:
1492 /* Don't recall a delegation if it was lost */
1493 nfs4_schedule_lease_recovery(server->nfs_client);
1494 return -EAGAIN;
1495 case -NFS4ERR_DELEG_REVOKED:
1496 case -NFS4ERR_ADMIN_REVOKED:
1497 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001498 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001499 nfs_inode_find_state_and_recover(state->inode,
1500 stateid);
1501 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001502 return 0;
1503 case -NFS4ERR_DELAY:
1504 case -NFS4ERR_GRACE:
1505 set_bit(NFS_DELEGATED_STATE, &state->flags);
1506 ssleep(1);
1507 return -EAGAIN;
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001508 case -ENOMEM:
1509 case -NFS4ERR_DENIED:
1510 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1511 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return err;
1514}
1515
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04001516int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
1517{
1518 struct nfs_server *server = NFS_SERVER(state->inode);
1519 struct nfs4_opendata *opendata;
1520 int err;
1521
1522 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1523 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1524 if (IS_ERR(opendata))
1525 return PTR_ERR(opendata);
1526 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
1527 err = nfs4_open_recover(opendata, state);
1528 nfs4_opendata_put(opendata);
1529 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1530}
1531
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001532static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1533{
1534 struct nfs4_opendata *data = calldata;
1535
1536 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001537 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001538 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001539 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001540 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001542 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001543}
1544
1545static void nfs4_open_confirm_release(void *calldata)
1546{
1547 struct nfs4_opendata *data = calldata;
1548 struct nfs4_state *state = NULL;
1549
1550 /* If this request hasn't been cancelled, do nothing */
1551 if (data->cancelled == 0)
1552 goto out_free;
1553 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001554 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001555 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001556 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001557 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001558 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001559out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001560 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001561}
1562
1563static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001564 .rpc_call_done = nfs4_open_confirm_done,
1565 .rpc_release = nfs4_open_confirm_release,
1566};
1567
1568/*
1569 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1570 */
1571static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1572{
1573 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1574 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001575 struct rpc_message msg = {
1576 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1577 .rpc_argp = &data->c_arg,
1578 .rpc_resp = &data->c_res,
1579 .rpc_cred = data->owner->so_cred,
1580 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001581 struct rpc_task_setup task_setup_data = {
1582 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001583 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001584 .callback_ops = &nfs4_open_confirm_ops,
1585 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001587 .flags = RPC_TASK_ASYNC,
1588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int status;
1590
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001592 data->rpc_done = 0;
1593 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001594 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001595 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001596 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001597 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001598 status = nfs4_wait_for_completion_rpc_task(task);
1599 if (status != 0) {
1600 data->cancelled = 1;
1601 smp_wmb();
1602 } else
1603 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001604 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return status;
1606}
1607
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 struct nfs4_opendata *data = calldata;
1611 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04001612 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001613
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001614 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001615 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001616 /*
1617 * Check if we still need to send an OPEN call, or if we can use
1618 * a delegation instead.
1619 */
1620 if (data->state != NULL) {
1621 struct nfs_delegation *delegation;
1622
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001624 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625 rcu_read_lock();
1626 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001627 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
Trond Myklebustcd4c9be2013-04-23 14:46:25 -04001628 data->o_arg.claim != NFS4_OPEN_CLAIM_DELEG_CUR_FH &&
Trond Myklebust652f89f2011-12-09 19:05:58 -05001629 can_open_delegated(delegation, data->o_arg.fmode))
1630 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001631 rcu_read_unlock();
1632 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001633 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04001634 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust8188df12013-04-23 14:31:19 -04001635 switch (data->o_arg.claim) {
1636 case NFS4_OPEN_CLAIM_PREVIOUS:
1637 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1638 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001639 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04001640 case NFS4_OPEN_CLAIM_FH:
1641 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1643 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001644 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001645 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001646 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04001647 &data->o_res.seq_res,
1648 task) != 0)
1649 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04001650
1651 /* Set the create mode (note dependency on the session type) */
1652 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
1653 if (data->o_arg.open_flags & O_EXCL) {
1654 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
1655 if (nfs4_has_persistent_session(clp))
1656 data->o_arg.createmode = NFS4_CREATE_GUARDED;
1657 else if (clp->cl_mvops->minor_version > 0)
1658 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
1659 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04001660 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001661unlock_no_action:
1662 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001663out_no_action:
1664 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05001665out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05001666 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001667}
1668
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001669static void nfs4_open_done(struct rpc_task *task, void *calldata)
1670{
1671 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001674
Trond Myklebust14516c32010-07-31 14:29:06 -04001675 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1676 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07001679 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
1680 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001681 case S_IFREG:
1682 break;
1683 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001685 break;
1686 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001688 break;
1689 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07001691 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07001692 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001693 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001694 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1695 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001696 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001697 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001698}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700static void nfs4_open_release(void *calldata)
1701{
1702 struct nfs4_opendata *data = calldata;
1703 struct nfs4_state *state = NULL;
1704
1705 /* If this request hasn't been cancelled, do nothing */
1706 if (data->cancelled == 0)
1707 goto out_free;
1708 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001709 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001710 goto out_free;
1711 /* In case we need an open_confirm, no cleanup! */
1712 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1713 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001714 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001715 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001716 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001717out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001719}
1720
1721static const struct rpc_call_ops nfs4_open_ops = {
1722 .rpc_call_prepare = nfs4_open_prepare,
1723 .rpc_call_done = nfs4_open_done,
1724 .rpc_release = nfs4_open_release,
1725};
1726
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001727static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001728{
1729 struct inode *dir = data->dir->d_inode;
1730 struct nfs_server *server = NFS_SERVER(dir);
1731 struct nfs_openargs *o_arg = &data->o_arg;
1732 struct nfs_openres *o_res = &data->o_res;
1733 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001734 struct rpc_message msg = {
1735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1736 .rpc_argp = o_arg,
1737 .rpc_resp = o_res,
1738 .rpc_cred = data->owner->so_cred,
1739 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001740 struct rpc_task_setup task_setup_data = {
1741 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001742 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001743 .callback_ops = &nfs4_open_ops,
1744 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001745 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001746 .flags = RPC_TASK_ASYNC,
1747 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001748 int status;
1749
Trond Myklebust9d12b212012-01-17 22:04:25 -05001750 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001751 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001752 data->rpc_done = 0;
1753 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001755 data->is_recover = 0;
1756 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04001757 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001758 data->is_recover = 1;
1759 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04001760 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001761 if (IS_ERR(task))
1762 return PTR_ERR(task);
1763 status = nfs4_wait_for_completion_rpc_task(task);
1764 if (status != 0) {
1765 data->cancelled = 1;
1766 smp_wmb();
1767 } else
1768 status = data->rpc_status;
1769 rpc_put_task(task);
1770
1771 return status;
1772}
1773
1774static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1775{
1776 struct inode *dir = data->dir->d_inode;
1777 struct nfs_openres *o_res = &data->o_res;
1778 int status;
1779
1780 status = nfs4_run_open_task(data, 1);
1781 if (status != 0 || !data->rpc_done)
1782 return status;
1783
Trond Myklebust6926afd2012-01-07 13:22:46 -05001784 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1785
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001786 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1787 status = _nfs4_proc_open_confirm(data);
1788 if (status != 0)
1789 return status;
1790 }
1791
1792 return status;
1793}
1794
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001795static int nfs4_opendata_access(struct rpc_cred *cred,
1796 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001797 struct nfs4_state *state, fmode_t fmode,
1798 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001799{
1800 struct nfs_access_entry cache;
1801 u32 mask;
1802
1803 /* access call failed or for some reason the server doesn't
1804 * support any access modes -- defer access call until later */
1805 if (opendata->o_res.access_supported == 0)
1806 return 0;
1807
1808 mask = 0;
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001809 /* don't check MAY_WRITE - a newly created file may not have
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05001810 * write mode bits, but POSIX allows the creating process to write.
1811 * use openflags to check for exec, because fmode won't
1812 * always have FMODE_EXEC set when file open for exec. */
1813 if (openflags & __FMODE_EXEC) {
1814 /* ONLY check for exec rights */
1815 mask = MAY_EXEC;
1816 } else if (fmode & FMODE_READ)
1817 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001818
1819 cache.cred = cred;
1820 cache.jiffies = jiffies;
1821 nfs_access_set_mask(&cache, opendata->o_res.access_result);
1822 nfs_access_add_cache(state->inode, &cache);
1823
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07001824 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001825 return 0;
1826
1827 /* even though OPEN succeeded, access is denied. Close the file */
1828 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04001829 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04001830}
1831
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001832/*
1833 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1834 */
1835static int _nfs4_proc_open(struct nfs4_opendata *data)
1836{
1837 struct inode *dir = data->dir->d_inode;
1838 struct nfs_server *server = NFS_SERVER(dir);
1839 struct nfs_openargs *o_arg = &data->o_arg;
1840 struct nfs_openres *o_res = &data->o_res;
1841 int status;
1842
1843 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001844 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001846 if (status != 0) {
1847 if (status == -NFS4ERR_BADNAME &&
1848 !(o_arg->open_flags & O_CREAT))
1849 return -ENOENT;
1850 return status;
1851 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001852
Trond Myklebust6926afd2012-01-07 13:22:46 -05001853 nfs_fattr_map_and_free_names(server, &data->f_attr);
1854
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001855 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001856 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001857 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1858 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001860 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001862 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
David Quigley1775fd32013-05-22 12:50:42 -04001865 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001866 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867}
1868
Andy Adamsond83217c2011-03-01 01:34:17 +00001869static int nfs4_recover_expired_lease(struct nfs_server *server)
1870{
1871 return nfs4_client_recover_expired_lease(server->nfs_client);
1872}
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874/*
1875 * OPEN_EXPIRED:
1876 * reclaim state on the server after a network partition.
1877 * Assumes caller holds the appropriate lock
1878 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001879static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001881 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001882 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001884 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001885 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001886 if (IS_ERR(opendata))
1887 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001888 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001889 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001890 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001891 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001895static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001896{
Trond Myklebust539cd032007-06-05 11:46:42 -04001897 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001898 struct nfs4_exception exception = { };
1899 int err;
1900
1901 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001902 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001903 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001904 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1905 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001906 switch (err) {
1907 default:
1908 goto out;
1909 case -NFS4ERR_GRACE:
1910 case -NFS4ERR_DELAY:
1911 nfs4_handle_exception(server, err, &exception);
1912 err = 0;
1913 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001914 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001915out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001916 return err;
1917}
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 ctx = nfs4_state_find_open_context(state);
1925 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001926 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001927 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001928 put_nfs_open_context(ctx);
1929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001932#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001933static void nfs41_clear_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001934{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001935 struct nfs_server *server = NFS_SERVER(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001936 nfs4_stateid *stateid = &state->stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001937 struct nfs_delegation *delegation;
1938 struct rpc_cred *cred = NULL;
1939 int status = -NFS4ERR_BAD_STATEID;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001940
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001941 /* If a state reset has been done, test_stateid is unneeded */
1942 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1943 return;
1944
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001945 /* Get the delegation credential for use by test/free_stateid */
1946 rcu_read_lock();
1947 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1948 if (delegation != NULL &&
1949 nfs4_stateid_match(&delegation->stateid, stateid)) {
1950 cred = get_rpccred(delegation->cred);
1951 rcu_read_unlock();
1952 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001953 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001954 } else
1955 rcu_read_unlock();
1956
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001957 if (status != NFS_OK) {
1958 /* Free the stateid unless the server explicitly
1959 * informs us the stateid is unrecognized. */
1960 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001961 nfs41_free_stateid(server, stateid, cred);
Bryan Schumaker69388672012-09-26 15:25:52 -04001962 nfs_remove_bad_delegation(state->inode);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001963
Bryan Schumaker69388672012-09-26 15:25:52 -04001964 write_seqlock(&state->seqlock);
1965 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1966 write_sequnlock(&state->seqlock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001967 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1968 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001969
1970 if (cred != NULL)
1971 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001972}
1973
1974/**
1975 * nfs41_check_open_stateid - possibly free an open stateid
1976 *
1977 * @state: NFSv4 state for an inode
1978 *
1979 * Returns NFS_OK if recovery for this stateid is now finished.
1980 * Otherwise a negative NFS4ERR value is returned.
1981 */
1982static int nfs41_check_open_stateid(struct nfs4_state *state)
1983{
1984 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04001985 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001986 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001987 int status;
1988
1989 /* If a state reset has been done, test_stateid is unneeded */
1990 if ((test_bit(NFS_O_RDONLY_STATE, &state->flags) == 0) &&
1991 (test_bit(NFS_O_WRONLY_STATE, &state->flags) == 0) &&
1992 (test_bit(NFS_O_RDWR_STATE, &state->flags) == 0))
1993 return -NFS4ERR_BAD_STATEID;
1994
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04001995 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04001996 trace_nfs4_test_open_stateid(state, NULL, status);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04001997 if (status != NFS_OK) {
1998 /* Free the stateid unless the server explicitly
1999 * informs us the stateid is unrecognized. */
2000 if (status != -NFS4ERR_BAD_STATEID)
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002001 nfs41_free_stateid(server, stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002002
2003 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2004 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2005 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002006 clear_bit(NFS_OPEN_STATE, &state->flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002007 }
2008 return status;
2009}
2010
2011static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
Chuck Levereb64cf92012-07-11 16:30:05 -04002013 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002014
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002015 nfs41_clear_delegation_stateid(state);
2016 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002017 if (status != NFS_OK)
2018 status = nfs4_open_expired(sp, state);
2019 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002020}
2021#endif
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002024 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2025 * fields corresponding to attributes that were used to store the verifier.
2026 * Make sure we clobber those fields in the later setattr call
2027 */
2028static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
2029{
2030 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
2031 !(sattr->ia_valid & ATTR_ATIME_SET))
2032 sattr->ia_valid |= ATTR_ATIME;
2033
2034 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
2035 !(sattr->ia_valid & ATTR_MTIME_SET))
2036 sattr->ia_valid |= ATTR_MTIME;
2037}
2038
Trond Myklebustc21443c2013-02-07 14:26:21 -05002039static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2040 fmode_t fmode,
2041 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002042 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002043{
2044 struct nfs4_state_owner *sp = opendata->owner;
2045 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002046 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002047 struct nfs4_state *state;
2048 unsigned int seq;
2049 int ret;
2050
2051 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2052
2053 ret = _nfs4_proc_open(opendata);
2054 if (ret != 0)
2055 goto out;
2056
2057 state = nfs4_opendata_to_nfs4_state(opendata);
2058 ret = PTR_ERR(state);
2059 if (IS_ERR(state))
2060 goto out;
2061 if (server->caps & NFS_CAP_POSIX_LOCK)
2062 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
2063
Trond Myklebust275bb302013-05-29 13:11:28 -04002064 dentry = opendata->dentry;
2065 if (dentry->d_inode == NULL) {
2066 /* FIXME: Is this d_drop() ever needed? */
2067 d_drop(dentry);
2068 dentry = d_add_unique(dentry, igrab(state->inode));
2069 if (dentry == NULL) {
2070 dentry = opendata->dentry;
2071 } else if (dentry != ctx->dentry) {
2072 dput(ctx->dentry);
2073 ctx->dentry = dget(dentry);
2074 }
2075 nfs_set_verifier(dentry,
2076 nfs_save_change_attribute(opendata->dir->d_inode));
2077 }
2078
Trond Myklebustc21443c2013-02-07 14:26:21 -05002079 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2080 if (ret != 0)
2081 goto out;
2082
Trond Myklebust3efb9722013-05-29 13:17:04 -04002083 ctx->state = state;
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002084 if (dentry->d_inode == state->inode) {
2085 nfs_inode_attach_open_context(ctx);
2086 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2087 nfs4_schedule_stateid_recovery(server, state);
2088 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002089out:
2090 return ret;
2091}
2092
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002093/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002094 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Andy Adamson82be4172012-05-23 05:02:35 -04002096static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002097 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002098 int flags,
2099 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002100 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 struct nfs4_state_owner *sp;
2103 struct nfs4_state *state = NULL;
2104 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002105 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002106 struct dentry *dentry = ctx->dentry;
2107 struct rpc_cred *cred = ctx->cred;
2108 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2109 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002110 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002111 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002112 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002116 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2117 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2119 goto out_err;
2120 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002121 status = nfs4_recover_expired_lease(server);
2122 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002123 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04002124 if (dentry->d_inode != NULL)
2125 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002126 status = -ENOMEM;
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002127 if (dentry->d_inode)
2128 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002129 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002130 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002131 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002132 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
David Quigley14c43f72013-05-22 12:50:43 -04002134 if (label) {
2135 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2136 if (IS_ERR(olabel)) {
2137 status = PTR_ERR(olabel);
2138 goto err_opendata_put;
2139 }
2140 }
2141
Andy Adamson82be4172012-05-23 05:02:35 -04002142 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2143 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2144 if (!opendata->f_attr.mdsthreshold)
David Quigley14c43f72013-05-22 12:50:43 -04002145 goto err_free_label;
Trond Myklebust1549210f2012-06-05 09:16:47 -04002146 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002147 }
Al Viro82a2c1b2011-06-22 18:30:55 -04002148 if (dentry->d_inode != NULL)
2149 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002150
Trond Myklebust3efb9722013-05-29 13:17:04 -04002151 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002152 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002153 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002154 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002155
Trond Myklebust549b19c2013-04-16 18:42:34 -04002156 if ((opendata->o_arg.open_flags & O_EXCL) &&
2157 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002158 nfs4_exclusive_attrset(opendata, sattr);
2159
2160 nfs_fattr_init(opendata->o_res.f_attr);
2161 status = nfs4_do_setattr(state->inode, cred,
2162 opendata->o_res.f_attr, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002163 state, label, olabel);
2164 if (status == 0) {
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002165 nfs_setattr_update_inode(state->inode, sattr);
David Quigley1775fd32013-05-22 12:50:42 -04002166 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002167 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
David Quigley1775fd32013-05-22 12:50:42 -04002168 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002169 }
Andy Adamson82be4172012-05-23 05:02:35 -04002170
2171 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
2172 *ctx_th = opendata->f_attr.mdsthreshold;
2173 else
2174 kfree(opendata->f_attr.mdsthreshold);
2175 opendata->f_attr.mdsthreshold = NULL;
2176
David Quigley14c43f72013-05-22 12:50:43 -04002177 nfs4_label_free(olabel);
2178
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002179 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002182err_free_label:
2183 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002184err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04002185 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002186 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002187err_put_state_owner:
2188 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return status;
2191}
2192
2193
Andy Adamson82be4172012-05-23 05:02:35 -04002194static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002195 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002196 int flags,
2197 struct iattr *sattr,
Trond Myklebust959d9212013-06-28 16:29:51 -04002198 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002200 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct nfs4_exception exception = { };
2202 struct nfs4_state *res;
2203 int status;
2204
2205 do {
Trond Myklebust959d9212013-06-28 16:29:51 -04002206 status = _nfs4_do_open(dir, ctx, flags, sattr, label);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002207 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002208 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (status == 0)
2210 break;
2211 /* NOTE: BAD_SEQID means the server and client disagree about the
2212 * book-keeping w.r.t. state-changing operations
2213 * (OPEN/CLOSE/LOCK/LOCKU...)
2214 * It is actually a sign of a bug on the client or on the server.
2215 *
2216 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002217 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * have unhashed the old state_owner for us, and that we can
2219 * therefore safely retry using a new one. We should still warn
2220 * the user though...
2221 */
2222 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002223 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002224 " returned a bad sequence-id error!\n",
2225 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 exception.retry = 1;
2227 continue;
2228 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002229 /*
2230 * BAD_STATEID on OPEN means that the server cancelled our
2231 * state before it received the OPEN_CONFIRM.
2232 * Recover by retrying the request as per the discussion
2233 * on Page 181 of RFC3530.
2234 */
2235 if (status == -NFS4ERR_BAD_STATEID) {
2236 exception.retry = 1;
2237 continue;
2238 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002239 if (status == -EAGAIN) {
2240 /* We must have found a delegation */
2241 exception.retry = 1;
2242 continue;
2243 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002244 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2245 continue;
2246 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 status, &exception));
2248 } while (exception.retry);
2249 return res;
2250}
2251
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002252static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2253 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002254 struct nfs4_state *state, struct nfs4_label *ilabel,
2255 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002257 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002259 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .iap = sattr,
2261 .server = server,
2262 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002263 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 };
2265 struct nfs_setattrres res = {
2266 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002267 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 .server = server,
2269 };
2270 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2272 .rpc_argp = &arg,
2273 .rpc_resp = &res,
2274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01002276 unsigned long timestamp = jiffies;
Trond Myklebustee3ae842013-04-29 10:35:36 -04002277 fmode_t fmode;
2278 bool truncate;
Trond Myklebust65e43082005-08-16 11:49:44 -04002279 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
David Quigleyaa9c2662013-05-22 12:50:44 -04002281 arg.bitmask = nfs4_bitmask(server, ilabel);
2282 if (ilabel)
2283 arg.bitmask = nfs4_bitmask(server, olabel);
2284
Trond Myklebust0e574af2005-10-27 22:12:38 -04002285 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Trond Myklebustee3ae842013-04-29 10:35:36 -04002287 /* Servers should only apply open mode checks for file size changes */
2288 truncate = (sattr->ia_valid & ATTR_SIZE) ? true : false;
2289 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2290
2291 if (nfs4_copy_delegation_stateid(&arg.stateid, inode, fmode)) {
2292 /* Use that stateid */
2293 } else if (truncate && state != NULL && nfs4_valid_open_stateid(state)) {
Trond Myklebust2a369152012-08-13 18:54:45 -04002294 struct nfs_lockowner lockowner = {
2295 .l_owner = current->files,
2296 .l_pid = current->tgid,
2297 };
Trond Myklebust4fc87962012-03-08 17:42:01 -05002298 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
Trond Myklebust2a369152012-08-13 18:54:45 -04002299 &lockowner);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002300 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05002301 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Bryan Schumaker7c513052011-03-24 17:12:24 +00002303 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002304 if (status == 0 && state != NULL)
2305 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04002306 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002309static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2310 struct nfs_fattr *fattr, struct iattr *sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002311 struct nfs4_state *state, struct nfs4_label *ilabel,
2312 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002314 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002315 struct nfs4_exception exception = {
2316 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05002317 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05002318 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 int err;
2320 do {
David Quigley1775fd32013-05-22 12:50:42 -04002321 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state, ilabel, olabel);
Trond Myklebustc1578b72013-08-12 16:58:42 -04002322 trace_nfs4_setattr(inode, err);
Trond Myklebust451146b2012-04-18 16:29:11 -04002323 switch (err) {
2324 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002325 if (!(sattr->ia_valid & ATTR_SIZE)) {
2326 pr_warn_once("NFSv4: server %s is incorrectly "
2327 "applying open mode checks to "
2328 "a SETATTR that is not "
2329 "changing file size.\n",
2330 server->nfs_client->cl_hostname);
2331 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002332 if (state && !(state->state & FMODE_WRITE)) {
2333 err = -EBADF;
2334 if (sattr->ia_valid & ATTR_OPEN)
2335 err = -EACCES;
2336 goto out;
2337 }
2338 }
2339 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002341out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return err;
2343}
2344
2345struct nfs4_closedata {
2346 struct inode *inode;
2347 struct nfs4_state *state;
2348 struct nfs_closeargs arg;
2349 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002350 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002351 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002352 bool roc;
2353 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354};
2355
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002356static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002357{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002358 struct nfs4_closedata *calldata = data;
2359 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002360 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002361
Fred Isamanf7e89172011-01-06 11:36:32 +00002362 if (calldata->roc)
2363 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002364 nfs4_put_open_state(calldata->state);
2365 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002366 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002367 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002368 kfree(calldata);
2369}
2370
Trond Myklebust88069f72009-12-08 08:33:16 -05002371static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2372 fmode_t fmode)
2373{
2374 spin_lock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002375 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002376 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
2377 case FMODE_WRITE:
2378 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2379 break;
2380 case FMODE_READ:
2381 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2382 break;
2383 case 0:
2384 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2385 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2386 clear_bit(NFS_OPEN_STATE, &state->flags);
2387 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002388 spin_unlock(&state->owner->so_lock);
2389}
2390
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002391static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002393 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct nfs_server *server = NFS_SERVER(calldata->inode);
2396
Chuck Levera3ca5652012-03-01 17:00:40 -05002397 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002398 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2399 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002400 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* hmm. we are done with the inode, and in the process of freeing
2402 * the state_owner. we keep this around to process errors
2403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 switch (task->tk_status) {
2405 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002406 if (calldata->roc)
2407 pnfs_roc_set_barrier(state->inode,
2408 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002409 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002410 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002411 nfs4_close_clear_stateid_flags(state,
2412 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
2414 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002415 case -NFS4ERR_OLD_STATEID:
2416 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002418 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002419 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002421 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2422 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002424 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002425 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002426 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427}
2428
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002429static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002431 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002432 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002433 struct inode *inode = calldata->inode;
Trond Myklebust88069f72009-12-08 08:33:16 -05002434 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002435
Chuck Levera3ca5652012-03-01 17:00:40 -05002436 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002437 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002438 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04002439
Trond Myklebust88069f72009-12-08 08:33:16 -05002440 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2441 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002442 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002443 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002444 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002445 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002446 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2447 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2448 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002449 }
2450 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002451 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2452 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2453 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002454 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002455 }
Trond Myklebust5d422302013-03-14 16:57:48 -04002456 if (!nfs4_valid_open_stateid(state))
2457 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002458 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002459
2460 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002461 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002462 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07002463 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002464
Fred Isamanf7e89172011-01-06 11:36:32 +00002465 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002466 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002467 if (calldata->roc &&
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002468 pnfs_roc_drain(inode, &calldata->roc_barrier, task)) {
2469 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002470 goto out_wait;
Andy Adamsonb9536ad2013-04-11 09:28:45 -04002471 }
Fred Isamanf7e89172011-01-06 11:36:32 +00002472 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002473
Trond Myklebust516a6af2005-10-27 22:12:41 -04002474 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002475 calldata->timestamp = jiffies;
Trond Myklebust7fdab062012-09-20 20:15:57 -04002476 if (nfs4_setup_sequence(NFS_SERVER(inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002477 &calldata->arg.seq_args,
2478 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002479 task) != 0)
2480 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05002481 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002482 return;
2483out_no_action:
2484 task->tk_action = NULL;
2485out_wait:
2486 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002489static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002490 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002491 .rpc_call_done = nfs4_close_done,
2492 .rpc_release = nfs4_free_closedata,
2493};
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495/*
2496 * It is possible for data to be read/written from a mem-mapped file
2497 * after the sys_close call (which hits the vfs layer as a flush).
2498 * This means that we can't safely call nfsv4 close on a file until
2499 * the inode is cleared. This in turn means that we are not good
2500 * NFSv4 citizens - we do not indicate to the server to update the file's
2501 * share state even when we are done with one of the three share
2502 * stateid's in the inode.
2503 *
2504 * NOTE: Caller must be holding the sp->so_owner semaphore!
2505 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002506int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002508 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002510 struct nfs4_state_owner *sp = state->owner;
2511 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002512 struct rpc_message msg = {
2513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2514 .rpc_cred = state->owner->so_cred,
2515 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002516 struct rpc_task_setup task_setup_data = {
2517 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002518 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002519 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002520 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002521 .flags = RPC_TASK_ASYNC,
2522 };
Trond Myklebust95121352005-10-18 14:20:12 -07002523 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002525 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002527 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002528 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002529 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002531 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002532 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002534 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002535 if (calldata->arg.seqid == NULL)
2536 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002537 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002538 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002539 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002540 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002541 calldata->res.server = server;
Trond Myklebust1f7977c2012-09-20 20:31:51 -04002542 calldata->roc = pnfs_roc(state->inode);
Al Viro643168c2011-06-22 18:20:23 -04002543 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002544
Trond Myklebust1174dd12010-12-21 10:52:24 -05002545 msg.rpc_argp = &calldata->arg;
2546 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 task_setup_data.callback_data = calldata;
2548 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002549 if (IS_ERR(task))
2550 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002551 status = 0;
2552 if (wait)
2553 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002554 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002555 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002556out_free_calldata:
2557 kfree(calldata);
2558out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002559 nfs4_put_open_state(state);
2560 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002561 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Trond Myklebust2b484292010-09-17 10:56:51 -04002564static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002565nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04002568 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
2569
2570 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Trond Myklebust565277f2007-10-15 18:17:53 -04002572 /* Protect against concurrent sillydeletes */
Trond Myklebust959d9212013-06-28 16:29:51 -04002573 state = nfs4_do_open(dir, ctx, open_flags, attr, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002574
2575 nfs4_label_release_security(label);
2576
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002577 if (IS_ERR(state))
2578 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04002579 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Trond Myklebust1185a552009-12-03 15:54:02 -05002582static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002583{
2584 if (ctx->state == NULL)
2585 return;
2586 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002587 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002588 else
Al Viro643168c2011-06-22 18:20:23 -04002589 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2593{
Benny Halevy43652ad2009-04-01 09:21:54 -04002594 struct nfs4_server_caps_arg args = {
2595 .fhandle = fhandle,
2596 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct nfs4_server_caps_res res = {};
2598 struct rpc_message msg = {
2599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002600 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 .rpc_resp = &res,
2602 };
2603 int status;
2604
Bryan Schumaker7c513052011-03-24 17:12:24 +00002605 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (status == 0) {
2607 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002608 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2609 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2610 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2611 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2612 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2614 server->caps |= NFS_CAP_ACLS;
2615 if (res.has_links != 0)
2616 server->caps |= NFS_CAP_HARDLINKS;
2617 if (res.has_symlinks != 0)
2618 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002619 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2620 server->caps |= NFS_CAP_FILEID;
2621 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2622 server->caps |= NFS_CAP_MODE;
2623 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2624 server->caps |= NFS_CAP_NLINK;
2625 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2626 server->caps |= NFS_CAP_OWNER;
2627 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2628 server->caps |= NFS_CAP_OWNER_GROUP;
2629 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2630 server->caps |= NFS_CAP_ATIME;
2631 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2632 server->caps |= NFS_CAP_CTIME;
2633 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2634 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04002635#ifdef CONFIG_NFS_V4_SECURITY_LABEL
2636 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
2637 server->caps |= NFS_CAP_SECURITY_LABEL;
2638#endif
2639 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
2640 sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002641
David Quigleyaa9c2662013-05-22 12:50:44 -04002642 if (server->caps & NFS_CAP_SECURITY_LABEL) {
2643 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2644 res.attr_bitmask[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
2645 }
Trond Myklebusta65318b2009-03-11 14:10:28 -04002646 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2647 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2648 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002650 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return status;
2654}
2655
Trond Myklebust55a97592006-06-09 09:34:19 -04002656int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
2658 struct nfs4_exception exception = { };
2659 int err;
2660 do {
2661 err = nfs4_handle_exception(server,
2662 _nfs4_server_capabilities(server, fhandle),
2663 &exception);
2664 } while (exception.retry);
2665 return err;
2666}
2667
2668static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2669 struct nfs_fsinfo *info)
2670{
David Quigleyaa9c2662013-05-22 12:50:44 -04002671 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04002673 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 };
2675 struct nfs4_lookup_res res = {
2676 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002677 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 .fh = fhandle,
2679 };
2680 struct rpc_message msg = {
2681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2682 .rpc_argp = &args,
2683 .rpc_resp = &res,
2684 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002685
David Quigleyaa9c2662013-05-22 12:50:44 -04002686 bitmask[0] = nfs4_fattr_bitmap[0];
2687 bitmask[1] = nfs4_fattr_bitmap[1];
2688 /*
2689 * Process the label in the upcoming getfattr
2690 */
2691 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
2692
Trond Myklebust0e574af2005-10-27 22:12:38 -04002693 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002694 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2698 struct nfs_fsinfo *info)
2699{
2700 struct nfs4_exception exception = { };
2701 int err;
2702 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002703 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002704 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002705 switch (err) {
2706 case 0:
2707 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002708 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002709 default:
2710 err = nfs4_handle_exception(server, err, &exception);
2711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002713out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 return err;
2715}
2716
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002717static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2718 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2719{
Trond Myklebustc2190662013-08-26 19:23:04 -04002720 struct rpc_auth_create_args auth_args = {
2721 .pseudoflavor = flavor,
2722 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002723 struct rpc_auth *auth;
2724 int ret;
2725
Trond Myklebustc2190662013-08-26 19:23:04 -04002726 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08002727 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04002728 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002729 goto out;
2730 }
2731 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002732out:
2733 return ret;
2734}
2735
Chuck Lever9a744ba2013-03-16 15:56:02 -04002736/*
2737 * Retry pseudoroot lookup with various security flavors. We do this when:
2738 *
2739 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
2740 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
2741 *
2742 * Returns zero on success, or a negative NFS4ERR value, or a
2743 * negative errno value.
2744 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002745static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002746 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
Chuck Lever9a744ba2013-03-16 15:56:02 -04002748 /* Per 3530bis 15.33.5 */
2749 static const rpc_authflavor_t flav_array[] = {
2750 RPC_AUTH_GSS_KRB5P,
2751 RPC_AUTH_GSS_KRB5I,
2752 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04002753 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04002754 RPC_AUTH_NULL,
2755 };
2756 int status = -EPERM;
2757 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Chuck Lever9a744ba2013-03-16 15:56:02 -04002759 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002760 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002761 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002762 continue;
2763 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002764 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04002765
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002766 /*
2767 * -EACCESS could mean that the user doesn't have correct permissions
2768 * to access the mount. It could also mean that we tried to mount
2769 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2770 * existing mount programs don't handle -EACCES very well so it should
2771 * be mapped to -EPERM instead.
2772 */
2773 if (status == -EACCES)
2774 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002775 return status;
2776}
2777
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002778static int nfs4_do_find_root_sec(struct nfs_server *server,
2779 struct nfs_fh *fhandle, struct nfs_fsinfo *info)
2780{
2781 int mv = server->nfs_client->cl_minorversion;
2782 return nfs_v4_minor_ops[mv]->find_root_sec(server, fhandle, info);
2783}
2784
2785/**
2786 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
2787 * @server: initialized nfs_server handle
2788 * @fhandle: we fill in the pseudo-fs root file handle
2789 * @info: we fill in an FSINFO struct
2790 *
2791 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002792 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002793int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2794 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002795{
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002796 int status;
2797
2798 status = nfs4_lookup_root(server, fhandle, info);
2799 if ((status == -NFS4ERR_WRONGSEC) &&
2800 !(server->flags & NFS_MOUNT_SECFLAVOUR))
2801 status = nfs4_do_find_root_sec(server, fhandle, info);
2802
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (status == 0)
2804 status = nfs4_server_capabilities(server, fhandle);
2805 if (status == 0)
2806 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04002807
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002808 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
Bryan Schumakerbae36242012-05-10 15:07:31 -04002811static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2812 struct nfs_fsinfo *info)
2813{
2814 int error;
2815 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04002816 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002817
2818 error = nfs4_server_capabilities(server, mntfh);
2819 if (error < 0) {
2820 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2821 return error;
2822 }
2823
David Quigley14c43f72013-05-22 12:50:43 -04002824 label = nfs4_label_alloc(server, GFP_KERNEL);
2825 if (IS_ERR(label))
2826 return PTR_ERR(label);
2827
David Quigley1775fd32013-05-22 12:50:42 -04002828 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04002829 if (error < 0) {
2830 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04002831 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04002832 }
2833
2834 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2835 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2836 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2837
David Quigley14c43f72013-05-22 12:50:43 -04002838err_free_label:
2839 nfs4_label_free(label);
2840
Bryan Schumakerbae36242012-05-10 15:07:31 -04002841 return error;
2842}
2843
Manoj Naik6b97fd32006-06-09 09:34:29 -04002844/*
2845 * Get locations and (maybe) other attributes of a referral.
2846 * Note that we'll actually follow the referral later when
2847 * we detect fsid mismatch in inode revalidation
2848 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002849static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2850 const struct qstr *name, struct nfs_fattr *fattr,
2851 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002852{
2853 int status = -ENOMEM;
2854 struct page *page = NULL;
2855 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002856
2857 page = alloc_page(GFP_KERNEL);
2858 if (page == NULL)
2859 goto out;
2860 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2861 if (locations == NULL)
2862 goto out;
2863
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002864 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002865 if (status != 0)
2866 goto out;
2867 /* Make sure server returned a different fsid for the referral */
2868 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002869 dprintk("%s: server did not return a different fsid for"
2870 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002871 status = -EIO;
2872 goto out;
2873 }
Andy Adamson533eb462011-06-13 18:25:56 -04002874 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2875 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002876
Andy Adamson533eb462011-06-13 18:25:56 -04002877 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002878 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002879 memset(fhandle, 0, sizeof(struct nfs_fh));
2880out:
2881 if (page)
2882 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002883 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002884 return status;
2885}
2886
David Quigley1775fd32013-05-22 12:50:42 -04002887static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2888 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889{
2890 struct nfs4_getattr_arg args = {
2891 .fh = fhandle,
2892 .bitmask = server->attr_bitmask,
2893 };
2894 struct nfs4_getattr_res res = {
2895 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002896 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 .server = server,
2898 };
2899 struct rpc_message msg = {
2900 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2901 .rpc_argp = &args,
2902 .rpc_resp = &res,
2903 };
David Quigleyaa9c2662013-05-22 12:50:44 -04002904
2905 args.bitmask = nfs4_bitmask(server, label);
2906
Trond Myklebust0e574af2005-10-27 22:12:38 -04002907 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002908 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
David Quigley1775fd32013-05-22 12:50:42 -04002911static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
2912 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 struct nfs4_exception exception = { };
2915 int err;
2916 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04002917 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
2918 trace_nfs4_getattr(server, fhandle, fattr, err);
2919 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 &exception);
2921 } while (exception.retry);
2922 return err;
2923}
2924
2925/*
2926 * The file is not closed if it is opened due to the a request to change
2927 * the size of the file. The open call will not be needed once the
2928 * VFS layer lookup-intents are implemented.
2929 *
2930 * Close is called when the inode is destroyed.
2931 * If we haven't opened the file for O_WRONLY, we
2932 * need to in the size_change case to obtain a stateid.
2933 *
2934 * Got race?
2935 * Because OPEN is always done by name in nfsv4, it is
2936 * possible that we opened a different file by the same
2937 * name. We can recognize this race condition, but we
2938 * can't do anything about it besides returning an error.
2939 *
2940 * This will be fixed with VFS changes (lookup-intent).
2941 */
2942static int
2943nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2944 struct iattr *sattr)
2945{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002946 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002947 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002948 struct nfs4_state *state = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04002949 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int status;
2951
Benny Halevy8a1636c2010-07-14 15:43:57 -04002952 if (pnfs_ld_layoutret_on_setattr(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04002953 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04002954
Trond Myklebust0e574af2005-10-27 22:12:38 -04002955 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Andy Adamson26699402012-05-30 16:12:24 -04002957 /* Deal with open(O_TRUNC) */
2958 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03002959 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04002960
2961 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03002962 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04002963 return 0;
2964
Trond Myklebustd5308382005-11-04 15:33:38 -05002965 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002966 if (sattr->ia_valid & ATTR_FILE) {
2967 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002968
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002969 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002970 if (ctx) {
2971 cred = ctx->cred;
2972 state = ctx->state;
2973 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002974 }
2975
David Quigley14c43f72013-05-22 12:50:43 -04002976 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
2977 if (IS_ERR(label))
2978 return PTR_ERR(label);
2979
2980 status = nfs4_do_setattr(inode, cred, fattr, sattr, state, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04002981 if (status == 0) {
Trond Myklebust65e43082005-08-16 11:49:44 -04002982 nfs_setattr_update_inode(inode, sattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04002983 nfs_setsecurity(inode, fattr, label);
2984 }
David Quigley14c43f72013-05-22 12:50:43 -04002985 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return status;
2987}
2988
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002989static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2990 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04002991 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04002992{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002993 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002994 int status;
2995 struct nfs4_lookup_arg args = {
2996 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002997 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002998 .name = name,
2999 };
3000 struct nfs4_lookup_res res = {
3001 .server = server,
3002 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003003 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003004 .fh = fhandle,
3005 };
3006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3008 .rpc_argp = &args,
3009 .rpc_resp = &res,
3010 };
3011
David Quigleyaa9c2662013-05-22 12:50:44 -04003012 args.bitmask = nfs4_bitmask(server, label);
3013
David Howells2b3de442006-08-22 20:06:09 -04003014 nfs_fattr_init(fattr);
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003017 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 dprintk("NFS reply lookup: %d\n", status);
3019 return status;
3020}
3021
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003022static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003023{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003024 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003025 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003026 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3027 fattr->nlink = 2;
3028}
3029
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003030static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
3031 struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003032 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003033{
3034 struct nfs4_exception exception = { };
3035 struct rpc_clnt *client = *clnt;
3036 int err;
3037 do {
David Quigley1775fd32013-05-22 12:50:42 -04003038 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003039 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003040 switch (err) {
3041 case -NFS4ERR_BADNAME:
3042 err = -ENOENT;
3043 goto out;
3044 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003045 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003046 goto out;
3047 case -NFS4ERR_WRONGSEC:
3048 err = -EPERM;
3049 if (client != *clnt)
3050 goto out;
3051
3052 client = nfs4_create_sec_client(client, dir, name);
3053 if (IS_ERR(client))
3054 return PTR_ERR(client);
3055
3056 exception.retry = 1;
3057 break;
3058 default:
3059 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3060 }
3061 } while (exception.retry);
3062
3063out:
3064 if (err == 0)
3065 *clnt = client;
3066 else if (client != *clnt)
3067 rpc_shutdown_client(client);
3068
3069 return err;
3070}
3071
Bryan Schumaker80a16b22012-04-27 13:27:46 -04003072static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003073 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3074 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003076 int status;
3077 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003078
David Quigley1775fd32013-05-22 12:50:42 -04003079 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003080 if (client != NFS_CLIENT(dir)) {
3081 rpc_shutdown_client(client);
3082 nfs_fixup_secinfo_attributes(fattr);
3083 }
3084 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003087struct rpc_clnt *
3088nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
3089 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3090{
3091 int status;
3092 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
3093
David Quigley1775fd32013-05-22 12:50:42 -04003094 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003095 if (status < 0) {
3096 rpc_shutdown_client(client);
3097 return ERR_PTR(status);
3098 }
3099 return client;
3100}
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3103{
Trond Myklebust76b32992007-08-10 17:45:11 -04003104 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 struct nfs4_accessargs args = {
3106 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003107 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003109 struct nfs4_accessres res = {
3110 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003111 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 struct rpc_message msg = {
3113 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3114 .rpc_argp = &args,
3115 .rpc_resp = &res,
3116 .rpc_cred = entry->cred,
3117 };
3118 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003119 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 /*
3122 * Determine which access bits we want to ask for...
3123 */
3124 if (mode & MAY_READ)
3125 args.access |= NFS4_ACCESS_READ;
3126 if (S_ISDIR(inode->i_mode)) {
3127 if (mode & MAY_WRITE)
3128 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3129 if (mode & MAY_EXEC)
3130 args.access |= NFS4_ACCESS_LOOKUP;
3131 } else {
3132 if (mode & MAY_WRITE)
3133 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3134 if (mode & MAY_EXEC)
3135 args.access |= NFS4_ACCESS_EXECUTE;
3136 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003137
3138 res.fattr = nfs_alloc_fattr();
3139 if (res.fattr == NULL)
3140 return -ENOMEM;
3141
Bryan Schumaker7c513052011-03-24 17:12:24 +00003142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003144 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003145 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003147 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return status;
3149}
3150
3151static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3152{
3153 struct nfs4_exception exception = { };
3154 int err;
3155 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003156 err = _nfs4_proc_access(inode, entry);
3157 trace_nfs4_access(inode, err);
3158 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 &exception);
3160 } while (exception.retry);
3161 return err;
3162}
3163
3164/*
3165 * TODO: For the time being, we don't try to get any attributes
3166 * along with any of the zero-copy operations READ, READDIR,
3167 * READLINK, WRITE.
3168 *
3169 * In the case of the first three, we want to put the GETATTR
3170 * after the read-type operation -- this is because it is hard
3171 * to predict the length of a GETATTR response in v4, and thus
3172 * align the READ data correctly. This means that the GETATTR
3173 * may end up partially falling into the page cache, and we should
3174 * shift it into the 'tail' of the xdr_buf before processing.
3175 * To do this efficiently, we need to know the total length
3176 * of data received, which doesn't seem to be available outside
3177 * of the RPC layer.
3178 *
3179 * In the case of WRITE, we also want to put the GETATTR after
3180 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003181 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 *
3183 * Both of these changes to the XDR layer would in fact be quite
3184 * minor, but I decided to leave them for a subsequent patch.
3185 */
3186static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3187 unsigned int pgbase, unsigned int pglen)
3188{
3189 struct nfs4_readlink args = {
3190 .fh = NFS_FH(inode),
3191 .pgbase = pgbase,
3192 .pglen = pglen,
3193 .pages = &page,
3194 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003195 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 struct rpc_message msg = {
3197 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3198 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003199 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 };
3201
Bryan Schumaker7c513052011-03-24 17:12:24 +00003202 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 -07003203}
3204
3205static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3206 unsigned int pgbase, unsigned int pglen)
3207{
3208 struct nfs4_exception exception = { };
3209 int err;
3210 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003211 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3212 trace_nfs4_readlink(inode, err);
3213 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 &exception);
3215 } while (exception.retry);
3216 return err;
3217}
3218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003220 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222static int
3223nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003224 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
David Quigleyaa9c2662013-05-22 12:50:44 -04003226 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003227 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 int status = 0;
3230
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003231 ctx = alloc_nfs_open_context(dentry, FMODE_READ);
3232 if (IS_ERR(ctx))
3233 return PTR_ERR(ctx);
3234
David Quigleyaa9c2662013-05-22 12:50:44 -04003235 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3236
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003237 sattr->ia_mode &= ~current_umask();
Trond Myklebust959d9212013-06-28 16:29:51 -04003238 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (IS_ERR(state)) {
3240 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003241 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003244 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003245 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return status;
3247}
3248
3249static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
3250{
Trond Myklebust16e42952005-10-27 22:12:44 -04003251 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003252 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003254 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003256 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003257 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003258 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3261 .rpc_argp = &args,
3262 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 };
Trond Myklebust778d2812012-04-27 13:48:19 -04003264 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003265
Bryan Schumaker7c513052011-03-24 17:12:24 +00003266 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003267 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04003268 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return status;
3270}
3271
3272static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
3273{
3274 struct nfs4_exception exception = { };
3275 int err;
3276 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003277 err = _nfs4_proc_remove(dir, name);
3278 trace_nfs4_remove(dir, name, err);
3279 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 &exception);
3281 } while (exception.retry);
3282 return err;
3283}
3284
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003285static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003287 struct nfs_server *server = NFS_SERVER(dir);
3288 struct nfs_removeargs *args = msg->rpc_argp;
3289 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003291 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003293 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003294
3295 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003298static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3299{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003300 nfs4_setup_sequence(NFS_SERVER(data->dir),
3301 &data->args.seq_args,
3302 &data->res.seq_res,
3303 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304}
3305
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003306static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003308 struct nfs_unlinkdata *data = task->tk_calldata;
3309 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003310
Trond Myklebust14516c32010-07-31 14:29:06 -04003311 if (!nfs4_sequence_done(task, &res->seq_res))
3312 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003313 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003314 return 0;
3315 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003316 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
Jeff Laytond3d41522010-09-17 17:31:57 -04003319static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
3320{
3321 struct nfs_server *server = NFS_SERVER(dir);
3322 struct nfs_renameargs *arg = msg->rpc_argp;
3323 struct nfs_renameres *res = msg->rpc_resp;
3324
3325 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04003326 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05003327 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04003328}
3329
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04003330static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
3331{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003332 nfs4_setup_sequence(NFS_SERVER(data->old_dir),
3333 &data->args.seq_args,
3334 &data->res.seq_res,
3335 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04003336}
3337
3338static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
3339 struct inode *new_dir)
3340{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003341 struct nfs_renamedata *data = task->tk_calldata;
3342 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04003343
3344 if (!nfs4_sequence_done(task, &res->seq_res))
3345 return 0;
3346 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
3347 return 0;
3348
3349 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003350 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04003351 return 1;
3352}
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3355 struct inode *new_dir, struct qstr *new_name)
3356{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003357 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04003358 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 .old_dir = NFS_FH(old_dir),
3360 .new_dir = NFS_FH(new_dir),
3361 .old_name = old_name,
3362 .new_name = new_name,
3363 };
Jeff Laytone8582a82010-09-17 17:31:06 -04003364 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003365 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04003366 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 struct rpc_message msg = {
3368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
3369 .rpc_argp = &arg,
3370 .rpc_resp = &res,
3371 };
Trond Myklebust011fff72010-04-16 16:22:49 -04003372 int status = -ENOMEM;
David Quigley14c43f72013-05-22 12:50:43 -04003373
Bryan Schumaker7c513052011-03-24 17:12:24 +00003374 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (!status) {
3376 update_changeattr(old_dir, &res.old_cinfo);
3377 update_changeattr(new_dir, &res.new_cinfo);
3378 }
3379 return status;
3380}
3381
3382static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
3383 struct inode *new_dir, struct qstr *new_name)
3384{
3385 struct nfs4_exception exception = { };
3386 int err;
3387 do {
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04003388 err = _nfs4_proc_rename(old_dir, old_name,
3389 new_dir, new_name);
3390 trace_nfs4_rename(old_dir, old_name, new_dir, new_name, err);
3391 err = nfs4_handle_exception(NFS_SERVER(old_dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 &exception);
3393 } while (exception.retry);
3394 return err;
3395}
3396
3397static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3398{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003399 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 struct nfs4_link_arg arg = {
3401 .fh = NFS_FH(inode),
3402 .dir_fh = NFS_FH(dir),
3403 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003404 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003406 struct nfs4_link_res res = {
3407 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04003408 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003409 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
3412 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003413 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 };
Trond Myklebust136f2622010-04-16 16:22:49 -04003415 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416
Trond Myklebust136f2622010-04-16 16:22:49 -04003417 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04003418 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04003419 goto out;
3420
David Quigley14c43f72013-05-22 12:50:43 -04003421 res.label = nfs4_label_alloc(server, GFP_KERNEL);
3422 if (IS_ERR(res.label)) {
3423 status = PTR_ERR(res.label);
3424 goto out;
3425 }
David Quigleyaa9c2662013-05-22 12:50:44 -04003426 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04003427
Bryan Schumaker7c513052011-03-24 17:12:24 +00003428 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003429 if (!status) {
3430 update_changeattr(dir, &res.cinfo);
David Quigleyaa9c2662013-05-22 12:50:44 -04003431 status = nfs_post_op_update_inode(inode, res.fattr);
3432 if (!status)
3433 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003434 }
David Quigley14c43f72013-05-22 12:50:43 -04003435
3436
3437 nfs4_label_free(res.label);
3438
Trond Myklebust136f2622010-04-16 16:22:49 -04003439out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003440 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return status;
3442}
3443
3444static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3445{
3446 struct nfs4_exception exception = { };
3447 int err;
3448 do {
3449 err = nfs4_handle_exception(NFS_SERVER(inode),
3450 _nfs4_proc_link(inode, dir, name),
3451 &exception);
3452 } while (exception.retry);
3453 return err;
3454}
3455
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003456struct nfs4_createdata {
3457 struct rpc_message msg;
3458 struct nfs4_create_arg arg;
3459 struct nfs4_create_res res;
3460 struct nfs_fh fh;
3461 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003462 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003463};
3464
3465static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3466 struct qstr *name, struct iattr *sattr, u32 ftype)
3467{
3468 struct nfs4_createdata *data;
3469
3470 data = kzalloc(sizeof(*data), GFP_KERNEL);
3471 if (data != NULL) {
3472 struct nfs_server *server = NFS_SERVER(dir);
3473
David Quigley14c43f72013-05-22 12:50:43 -04003474 data->label = nfs4_label_alloc(server, GFP_KERNEL);
3475 if (IS_ERR(data->label))
3476 goto out_free;
3477
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003478 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3479 data->msg.rpc_argp = &data->arg;
3480 data->msg.rpc_resp = &data->res;
3481 data->arg.dir_fh = NFS_FH(dir);
3482 data->arg.server = server;
3483 data->arg.name = name;
3484 data->arg.attrs = sattr;
3485 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04003486 data->arg.bitmask = nfs4_bitmask(server, data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003487 data->res.server = server;
3488 data->res.fh = &data->fh;
3489 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003490 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003491 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003492 }
3493 return data;
David Quigley14c43f72013-05-22 12:50:43 -04003494out_free:
3495 kfree(data);
3496 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003497}
3498
3499static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3500{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003501 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003502 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003503 if (status == 0) {
3504 update_changeattr(dir, &data->res.dir_cinfo);
David Quigley1775fd32013-05-22 12:50:42 -04003505 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003506 }
3507 return status;
3508}
3509
3510static void nfs4_free_createdata(struct nfs4_createdata *data)
3511{
David Quigley14c43f72013-05-22 12:50:43 -04003512 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003513 kfree(data);
3514}
3515
Chuck Lever4f390c12006-08-22 20:06:22 -04003516static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003517 struct page *page, unsigned int len, struct iattr *sattr,
3518 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003520 struct nfs4_createdata *data;
3521 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Chuck Lever94a6d752006-08-22 20:06:23 -04003523 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003524 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003525
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003526 status = -ENOMEM;
3527 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3528 if (data == NULL)
3529 goto out;
3530
3531 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3532 data->arg.u.symlink.pages = &page;
3533 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04003534 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003536 status = nfs4_do_create(dir, dentry, data);
3537
3538 nfs4_free_createdata(data);
3539out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 return status;
3541}
3542
Chuck Lever4f390c12006-08-22 20:06:22 -04003543static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003544 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003547 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04003549
3550 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003553 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
3554 trace_nfs4_symlink(dir, &dentry->d_name, err);
3555 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 &exception);
3557 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003558
3559 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return err;
3561}
3562
3563static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04003564 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003566 struct nfs4_createdata *data;
3567 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003569 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3570 if (data == NULL)
3571 goto out;
3572
David Quigley1775fd32013-05-22 12:50:42 -04003573 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003574 status = nfs4_do_create(dir, dentry, data);
3575
3576 nfs4_free_createdata(data);
3577out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return status;
3579}
3580
3581static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3582 struct iattr *sattr)
3583{
3584 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003585 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003587
David Quigleyaa9c2662013-05-22 12:50:44 -04003588 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3589
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003590 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003592 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
3593 trace_nfs4_mkdir(dir, &dentry->d_name, err);
3594 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 &exception);
3596 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003597 nfs4_label_release_security(label);
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 return err;
3600}
3601
3602static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003603 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct inode *dir = dentry->d_inode;
3606 struct nfs4_readdir_arg args = {
3607 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003608 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 .pgbase = 0,
3610 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003611 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003612 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 };
3614 struct nfs4_readdir_res res;
3615 struct rpc_message msg = {
3616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3617 .rpc_argp = &args,
3618 .rpc_resp = &res,
3619 .rpc_cred = cred,
3620 };
3621 int status;
3622
Harvey Harrison3110ff82008-05-02 13:42:44 -07003623 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003624 dentry->d_parent->d_name.name,
3625 dentry->d_name.name,
3626 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003627 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003629 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 -05003630 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04003631 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003632 status += args.pgbase;
3633 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003634
3635 nfs_invalidate_atime(dir);
3636
Harvey Harrison3110ff82008-05-02 13:42:44 -07003637 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return status;
3639}
3640
3641static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003642 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643{
3644 struct nfs4_exception exception = { };
3645 int err;
3646 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003647 err = _nfs4_proc_readdir(dentry, cred, cookie,
3648 pages, count, plus);
3649 trace_nfs4_readdir(dentry->d_inode, err);
3650 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 &exception);
3652 } while (exception.retry);
3653 return err;
3654}
3655
3656static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04003657 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003659 struct nfs4_createdata *data;
3660 int mode = sattr->ia_mode;
3661 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003663 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3664 if (data == NULL)
3665 goto out;
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003668 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003670 data->arg.ftype = NF4BLK;
3671 data->arg.u.device.specdata1 = MAJOR(rdev);
3672 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003675 data->arg.ftype = NF4CHR;
3676 data->arg.u.device.specdata1 = MAJOR(rdev);
3677 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003678 } else if (!S_ISSOCK(mode)) {
3679 status = -EINVAL;
3680 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
David Quigley1775fd32013-05-22 12:50:42 -04003682
David Quigleyaa9c2662013-05-22 12:50:44 -04003683 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003684 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04003685out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003686 nfs4_free_createdata(data);
3687out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 return status;
3689}
3690
3691static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3692 struct iattr *sattr, dev_t rdev)
3693{
3694 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04003695 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003697
David Quigleyaa9c2662013-05-22 12:50:44 -04003698 label = nfs4_label_init_security(dir, dentry, sattr, &l);
3699
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003700 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003702 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
3703 trace_nfs4_mknod(dir, &dentry->d_name, err);
3704 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 &exception);
3706 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04003707
3708 nfs4_label_release_security(label);
3709
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return err;
3711}
3712
3713static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3714 struct nfs_fsstat *fsstat)
3715{
3716 struct nfs4_statfs_arg args = {
3717 .fh = fhandle,
3718 .bitmask = server->attr_bitmask,
3719 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003720 struct nfs4_statfs_res res = {
3721 .fsstat = fsstat,
3722 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 struct rpc_message msg = {
3724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3725 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003726 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 };
3728
Trond Myklebust0e574af2005-10-27 22:12:38 -04003729 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003730 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
3733static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3734{
3735 struct nfs4_exception exception = { };
3736 int err;
3737 do {
3738 err = nfs4_handle_exception(server,
3739 _nfs4_proc_statfs(server, fhandle, fsstat),
3740 &exception);
3741 } while (exception.retry);
3742 return err;
3743}
3744
3745static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3746 struct nfs_fsinfo *fsinfo)
3747{
3748 struct nfs4_fsinfo_arg args = {
3749 .fh = fhandle,
3750 .bitmask = server->attr_bitmask,
3751 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003752 struct nfs4_fsinfo_res res = {
3753 .fsinfo = fsinfo,
3754 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 struct rpc_message msg = {
3756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3757 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003758 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 };
3760
Bryan Schumaker7c513052011-03-24 17:12:24 +00003761 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
3764static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3765{
3766 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04003767 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 int err;
3769
3770 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04003771 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003772 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04003773 if (err == 0) {
3774 struct nfs_client *clp = server->nfs_client;
3775
3776 spin_lock(&clp->cl_lock);
3777 clp->cl_lease_time = fsinfo->lease_time * HZ;
3778 clp->cl_last_renewal = now;
3779 spin_unlock(&clp->cl_lock);
3780 break;
3781 }
3782 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 } while (exception.retry);
3784 return err;
3785}
3786
3787static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3788{
Bryan Schumakere38eb652012-06-20 15:53:40 -04003789 int error;
3790
Trond Myklebust0e574af2005-10-27 22:12:38 -04003791 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04003792 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08003793 if (error == 0) {
3794 /* block layout checks this! */
3795 server->pnfs_blksize = fsinfo->blksize;
Bryan Schumakere38eb652012-06-20 15:53:40 -04003796 set_pnfs_layoutdriver(server, fhandle, fsinfo->layouttype);
Peng Taodc182542012-08-24 00:27:49 +08003797 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04003798
3799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800}
3801
3802static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3803 struct nfs_pathconf *pathconf)
3804{
3805 struct nfs4_pathconf_arg args = {
3806 .fh = fhandle,
3807 .bitmask = server->attr_bitmask,
3808 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003809 struct nfs4_pathconf_res res = {
3810 .pathconf = pathconf,
3811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3814 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003815 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 };
3817
3818 /* None of the pathconf attributes are mandatory to implement */
3819 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3820 memset(pathconf, 0, sizeof(*pathconf));
3821 return 0;
3822 }
3823
Trond Myklebust0e574af2005-10-27 22:12:38 -04003824 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003825 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
3828static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3829 struct nfs_pathconf *pathconf)
3830{
3831 struct nfs4_exception exception = { };
3832 int err;
3833
3834 do {
3835 err = nfs4_handle_exception(server,
3836 _nfs4_proc_pathconf(server, fhandle, pathconf),
3837 &exception);
3838 } while (exception.retry);
3839 return err;
3840}
3841
Trond Myklebust5521abf2013-03-16 20:54:34 -04003842int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04003843 const struct nfs_open_context *ctx,
3844 const struct nfs_lock_context *l_ctx,
3845 fmode_t fmode)
3846{
3847 const struct nfs_lockowner *lockowner = NULL;
3848
3849 if (l_ctx != NULL)
3850 lockowner = &l_ctx->lockowner;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003851 return nfs4_select_rw_stateid(stateid, ctx->state, fmode, lockowner);
Trond Myklebust9b206142013-03-17 15:52:00 -04003852}
3853EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
3854
Trond Myklebust5521abf2013-03-16 20:54:34 -04003855static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
3856 const struct nfs_open_context *ctx,
3857 const struct nfs_lock_context *l_ctx,
3858 fmode_t fmode)
3859{
3860 nfs4_stateid current_stateid;
3861
3862 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode))
3863 return false;
3864 return nfs4_stateid_match(stateid, &current_stateid);
3865}
3866
3867static bool nfs4_error_stateid_expired(int err)
3868{
3869 switch (err) {
3870 case -NFS4ERR_DELEG_REVOKED:
3871 case -NFS4ERR_ADMIN_REVOKED:
3872 case -NFS4ERR_BAD_STATEID:
3873 case -NFS4ERR_STALE_STATEID:
3874 case -NFS4ERR_OLD_STATEID:
3875 case -NFS4ERR_OPENMODE:
3876 case -NFS4ERR_EXPIRED:
3877 return true;
3878 }
3879 return false;
3880}
3881
Benny Halevyd20581a2011-05-22 19:52:03 +03003882void __nfs4_read_done_cb(struct nfs_read_data *data)
3883{
Fred Isamancd841602012-04-20 14:47:44 -04003884 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003885}
3886
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003887static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
Fred Isamancd841602012-04-20 14:47:44 -04003889 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003891 trace_nfs4_read(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003892 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003893 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003894 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003896
Benny Halevyd20581a2011-05-22 19:52:03 +03003897 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003899 renew_lease(server, data->timestamp);
3900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901}
3902
Trond Myklebust5521abf2013-03-16 20:54:34 -04003903static bool nfs4_read_stateid_changed(struct rpc_task *task,
3904 struct nfs_readargs *args)
3905{
3906
3907 if (!nfs4_error_stateid_expired(task->tk_status) ||
3908 nfs4_stateid_is_current(&args->stateid,
3909 args->context,
3910 args->lock_context,
3911 FMODE_READ))
3912 return false;
3913 rpc_restart_call_prepare(task);
3914 return true;
3915}
3916
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003917static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3918{
3919
3920 dprintk("--> %s\n", __func__);
3921
3922 if (!nfs4_sequence_done(task, &data->res.seq_res))
3923 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003924 if (nfs4_read_stateid_changed(task, &data->args))
3925 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003926 return data->read_done_cb ? data->read_done_cb(task, data) :
3927 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003928}
3929
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003930static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003933 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003934 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003935 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936}
3937
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003938static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3939{
Trond Myklebust9b206142013-03-17 15:52:00 -04003940 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04003941 &data->args.seq_args,
3942 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04003943 task))
3944 return;
3945 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
3946 data->args.lock_context, FMODE_READ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947}
3948
Fred Isamanb029bc92011-03-03 15:13:42 +00003949static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950{
Fred Isamancd841602012-04-20 14:47:44 -04003951 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Trond Myklebustcc668ab2013-08-14 15:31:28 -04003953 trace_nfs4_write(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003954 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003955 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003956 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003958 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003960 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003961 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
3964
Trond Myklebust5521abf2013-03-16 20:54:34 -04003965static bool nfs4_write_stateid_changed(struct rpc_task *task,
3966 struct nfs_writeargs *args)
3967{
3968
3969 if (!nfs4_error_stateid_expired(task->tk_status) ||
3970 nfs4_stateid_is_current(&args->stateid,
3971 args->context,
3972 args->lock_context,
3973 FMODE_WRITE))
3974 return false;
3975 rpc_restart_call_prepare(task);
3976 return true;
3977}
3978
Fred Isamanb029bc92011-03-03 15:13:42 +00003979static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3980{
3981 if (!nfs4_sequence_done(task, &data->res.seq_res))
3982 return -EAGAIN;
Trond Myklebust5521abf2013-03-16 20:54:34 -04003983 if (nfs4_write_stateid_changed(task, &data->args))
3984 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003985 return data->write_done_cb ? data->write_done_cb(task, data) :
3986 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003987}
3988
Trond Myklebust5a37f852012-04-28 14:55:16 -04003989static
3990bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
Fred Isamana69aef12011-03-03 15:13:47 +00003991{
Trond Myklebust5a37f852012-04-28 14:55:16 -04003992 const struct nfs_pgio_header *hdr = data->header;
3993
3994 /* Don't request attributes for pNFS or O_DIRECT writes */
3995 if (data->ds_clp != NULL || hdr->dreq != NULL)
3996 return false;
3997 /* Otherwise, request attributes if and only if we don't hold
3998 * a delegation
3999 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004000 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004001}
Fred Isamana69aef12011-03-03 15:13:47 +00004002
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004003static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Fred Isamancd841602012-04-20 14:47:44 -04004005 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004006
Trond Myklebust5a37f852012-04-28 14:55:16 -04004007 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00004008 data->args.bitmask = NULL;
4009 data->res.fattr = NULL;
4010 } else
4011 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004012
Fred Isamanb029bc92011-03-03 15:13:42 +00004013 if (!data->write_done_cb)
4014 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004015 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 data->timestamp = jiffies;
4017
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004018 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004019 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04004022static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
4023{
Trond Myklebust9b206142013-03-17 15:52:00 -04004024 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004025 &data->args.seq_args,
4026 &data->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004027 task))
4028 return;
4029 nfs4_set_rw_stateid(&data->args.stateid, data->args.context,
4030 data->args.lock_context, FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031}
4032
Fred Isaman0b7c0152012-04-20 14:47:39 -04004033static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4034{
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004035 nfs4_setup_sequence(NFS_SERVER(data->inode),
4036 &data->args.seq_args,
4037 &data->res.seq_res,
4038 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Fred Isaman0b7c0152012-04-20 14:47:39 -04004041static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004044
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004045 trace_nfs4_commit(data, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004046 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004047 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004048 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004050 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051}
4052
Fred Isaman0b7c0152012-04-20 14:47:39 -04004053static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004054{
4055 if (!nfs4_sequence_done(task, &data->res.seq_res))
4056 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004057 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004058}
4059
Fred Isaman0b7c0152012-04-20 14:47:39 -04004060static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004062 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004063
Fred Isaman0b7c0152012-04-20 14:47:39 -04004064 if (data->commit_done_cb == NULL)
4065 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004066 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004067 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05004068 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004071struct nfs4_renewdata {
4072 struct nfs_client *client;
4073 unsigned long timestamp;
4074};
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076/*
4077 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4078 * standalone procedure for queueing an asynchronous RENEW.
4079 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004080static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004081{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004082 struct nfs4_renewdata *data = calldata;
4083 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004084
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004085 if (atomic_read(&clp->cl_count) > 1)
4086 nfs4_schedule_state_renewal(clp);
4087 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004088 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004089}
4090
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004091static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004093 struct nfs4_renewdata *data = calldata;
4094 struct nfs_client *clp = data->client;
4095 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004097 trace_nfs4_renew_async(clp, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04004099 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004100 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4101 return;
4102 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004103 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004104 return;
4105 }
4106 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004108 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004111static const struct rpc_call_ops nfs4_renew_ops = {
4112 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004113 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004114};
4115
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004116static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117{
4118 struct rpc_message msg = {
4119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4120 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004121 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004123 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004125 if (renew_flags == 0)
4126 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004127 if (!atomic_inc_not_zero(&clp->cl_count))
4128 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004129 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004130 if (data == NULL)
4131 return -ENOMEM;
4132 data->client = clp;
4133 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004134 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004135 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136}
4137
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004138static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139{
4140 struct rpc_message msg = {
4141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4142 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004143 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 };
4145 unsigned long now = jiffies;
4146 int status;
4147
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004148 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 if (status < 0)
4150 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004151 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 return 0;
4153}
4154
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004155static inline int nfs4_server_supports_acls(struct nfs_server *server)
4156{
4157 return (server->caps & NFS_CAP_ACLS)
4158 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
4159 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
4160}
4161
Trond Myklebust21f498c2012-08-24 10:59:25 -04004162/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4163 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004164 * the stack.
4165 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004166#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004167
Neil Hormane9e3d722011-03-04 19:26:03 -05004168static int buf_to_pages_noslab(const void *buf, size_t buflen,
4169 struct page **pages, unsigned int *pgbase)
4170{
4171 struct page *newpage, **spages;
4172 int rc = 0;
4173 size_t len;
4174 spages = pages;
4175
4176 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004177 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004178 newpage = alloc_page(GFP_KERNEL);
4179
4180 if (newpage == NULL)
4181 goto unwind;
4182 memcpy(page_address(newpage), buf, len);
4183 buf += len;
4184 buflen -= len;
4185 *pages++ = newpage;
4186 rc++;
4187 } while (buflen != 0);
4188
4189 return rc;
4190
4191unwind:
4192 for(; rc > 0; rc--)
4193 __free_page(spages[rc-1]);
4194 return -ENOMEM;
4195}
4196
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004197struct nfs4_cached_acl {
4198 int cached;
4199 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004200 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004201};
4202
4203static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004204{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004205 struct nfs_inode *nfsi = NFS_I(inode);
4206
4207 spin_lock(&inode->i_lock);
4208 kfree(nfsi->nfs4_acl);
4209 nfsi->nfs4_acl = acl;
4210 spin_unlock(&inode->i_lock);
4211}
4212
4213static void nfs4_zap_acl_attr(struct inode *inode)
4214{
4215 nfs4_set_cached_acl(inode, NULL);
4216}
4217
4218static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4219{
4220 struct nfs_inode *nfsi = NFS_I(inode);
4221 struct nfs4_cached_acl *acl;
4222 int ret = -ENOENT;
4223
4224 spin_lock(&inode->i_lock);
4225 acl = nfsi->nfs4_acl;
4226 if (acl == NULL)
4227 goto out;
4228 if (buf == NULL) /* user is just asking for length */
4229 goto out_len;
4230 if (acl->cached == 0)
4231 goto out;
4232 ret = -ERANGE; /* see getxattr(2) man page */
4233 if (acl->len > buflen)
4234 goto out;
4235 memcpy(buf, acl->data, acl->len);
4236out_len:
4237 ret = acl->len;
4238out:
4239 spin_unlock(&inode->i_lock);
4240 return ret;
4241}
4242
Sachin Prabhu5794d212012-04-17 14:36:40 +01004243static 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 +00004244{
4245 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004246 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004247
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004248 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004249 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004250 if (acl == NULL)
4251 goto out;
4252 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004253 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004254 } else {
4255 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4256 if (acl == NULL)
4257 goto out;
4258 acl->cached = 0;
4259 }
4260 acl->len = acl_len;
4261out:
4262 nfs4_set_cached_acl(inode, acl);
4263}
4264
Andy Adamsonbf118a32011-12-07 11:55:27 -05004265/*
4266 * The getxattr API returns the required buffer length when called with a
4267 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4268 * the required buf. On a NULL buf, we send a page of data to the server
4269 * guessing that the ACL request can be serviced by a page. If so, we cache
4270 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4271 * the cache. If not so, we throw away the page, and cache the required
4272 * length. The next getxattr call will then produce another round trip to
4273 * the server, this time with the input buf of the required size.
4274 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004275static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004276{
Andy Adamsonbf118a32011-12-07 11:55:27 -05004277 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004278 struct nfs_getaclargs args = {
4279 .fh = NFS_FH(inode),
4280 .acl_pages = pages,
4281 .acl_len = buflen,
4282 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004283 struct nfs_getaclres res = {
4284 .acl_len = buflen,
4285 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004286 struct rpc_message msg = {
4287 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4288 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004289 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004290 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004291 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
4292 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004293
Andy Adamsonbf118a32011-12-07 11:55:27 -05004294 /* As long as we're doing a round trip to the server anyway,
4295 * let's be prepared for a page of acl data. */
4296 if (npages == 0)
4297 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004298 if (npages > ARRAY_SIZE(pages))
4299 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004300
Andy Adamsonbf118a32011-12-07 11:55:27 -05004301 for (i = 0; i < npages; i++) {
4302 pages[i] = alloc_page(GFP_KERNEL);
4303 if (!pages[i])
4304 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004305 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004306
4307 /* for decoding across pages */
4308 res.acl_scratch = alloc_page(GFP_KERNEL);
4309 if (!res.acl_scratch)
4310 goto out_free;
4311
Andy Adamsonbf118a32011-12-07 11:55:27 -05004312 args.acl_len = npages * PAGE_SIZE;
4313 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004314
Peng Taode040be2012-01-10 22:42:47 +08004315 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004316 __func__, buf, buflen, npages, args.acl_len);
4317 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4318 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004319 if (ret)
4320 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004321
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004322 /* Handle the case where the passed-in buffer is too short */
4323 if (res.acl_flags & NFS4_ACL_TRUNC) {
4324 /* Did the user only issue a request for the acl length? */
4325 if (buf == NULL)
4326 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004327 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004328 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004329 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004330 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004331 if (buf) {
4332 if (res.acl_len > buflen) {
4333 ret = -ERANGE;
4334 goto out_free;
4335 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004336 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01004337 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004338out_ok:
4339 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004340out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05004341 for (i = 0; i < npages; i++)
4342 if (pages[i])
4343 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05004344 if (res.acl_scratch)
4345 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004346 return ret;
4347}
4348
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004349static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
4350{
4351 struct nfs4_exception exception = { };
4352 ssize_t ret;
4353 do {
4354 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04004355 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004356 if (ret >= 0)
4357 break;
4358 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
4359 } while (exception.retry);
4360 return ret;
4361}
4362
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004363static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
4364{
4365 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004366 int ret;
4367
4368 if (!nfs4_server_supports_acls(server))
4369 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004370 ret = nfs_revalidate_inode(server, inode);
4371 if (ret < 0)
4372 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004373 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
4374 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004375 ret = nfs4_read_cached_acl(inode, buf, buflen);
4376 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05004377 /* -ENOENT is returned if there is no ACL or if there is an ACL
4378 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004379 return ret;
4380 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004381}
4382
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004383static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004384{
4385 struct nfs_server *server = NFS_SERVER(inode);
4386 struct page *pages[NFS4ACL_MAXPAGES];
4387 struct nfs_setaclargs arg = {
4388 .fh = NFS_FH(inode),
4389 .acl_pages = pages,
4390 .acl_len = buflen,
4391 };
Benny Halevy73c403a2009-04-01 09:22:01 -04004392 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004393 struct rpc_message msg = {
4394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
4395 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04004396 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004397 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04004398 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05004399 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004400
4401 if (!nfs4_server_supports_acls(server))
4402 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004403 if (npages > ARRAY_SIZE(pages))
4404 return -ERANGE;
Neil Hormane9e3d722011-03-04 19:26:03 -05004405 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
4406 if (i < 0)
4407 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04004408 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004409 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05004410
4411 /*
4412 * Free each page after tx, so the only ref left is
4413 * held by the network stack
4414 */
4415 for (; i > 0; i--)
4416 put_page(pages[i-1]);
4417
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00004418 /*
4419 * Acl update can result in inode attribute update.
4420 * so mark the attribute cache invalid.
4421 */
4422 spin_lock(&inode->i_lock);
4423 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
4424 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04004425 nfs_access_zap_cache(inode);
4426 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004427 return ret;
4428}
4429
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004430static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
4431{
4432 struct nfs4_exception exception = { };
4433 int err;
4434 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004435 err = __nfs4_proc_set_acl(inode, buf, buflen);
4436 trace_nfs4_set_acl(inode, err);
4437 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b4289c2006-08-24 12:27:15 -04004438 &exception);
4439 } while (exception.retry);
4440 return err;
4441}
4442
David Quigleyaa9c2662013-05-22 12:50:44 -04004443#ifdef CONFIG_NFS_V4_SECURITY_LABEL
4444static int _nfs4_get_security_label(struct inode *inode, void *buf,
4445 size_t buflen)
4446{
4447 struct nfs_server *server = NFS_SERVER(inode);
4448 struct nfs_fattr fattr;
4449 struct nfs4_label label = {0, 0, buflen, buf};
4450
4451 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4452 struct nfs4_getattr_arg args = {
4453 .fh = NFS_FH(inode),
4454 .bitmask = bitmask,
4455 };
4456 struct nfs4_getattr_res res = {
4457 .fattr = &fattr,
4458 .label = &label,
4459 .server = server,
4460 };
4461 struct rpc_message msg = {
4462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4463 .rpc_argp = &args,
4464 .rpc_resp = &res,
4465 };
4466 int ret;
4467
4468 nfs_fattr_init(&fattr);
4469
4470 ret = rpc_call_sync(server->client, &msg, 0);
4471 if (ret)
4472 return ret;
4473 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
4474 return -ENOENT;
4475 if (buflen < label.len)
4476 return -ERANGE;
4477 return 0;
4478}
4479
4480static int nfs4_get_security_label(struct inode *inode, void *buf,
4481 size_t buflen)
4482{
4483 struct nfs4_exception exception = { };
4484 int err;
4485
4486 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4487 return -EOPNOTSUPP;
4488
4489 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004490 err = _nfs4_get_security_label(inode, buf, buflen);
4491 trace_nfs4_get_security_label(inode, err);
4492 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004493 &exception);
4494 } while (exception.retry);
4495 return err;
4496}
4497
4498static int _nfs4_do_set_security_label(struct inode *inode,
4499 struct nfs4_label *ilabel,
4500 struct nfs_fattr *fattr,
4501 struct nfs4_label *olabel)
4502{
4503
4504 struct iattr sattr = {0};
4505 struct nfs_server *server = NFS_SERVER(inode);
4506 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
4507 struct nfs_setattrargs args = {
4508 .fh = NFS_FH(inode),
4509 .iap = &sattr,
4510 .server = server,
4511 .bitmask = bitmask,
4512 .label = ilabel,
4513 };
4514 struct nfs_setattrres res = {
4515 .fattr = fattr,
4516 .label = olabel,
4517 .server = server,
4518 };
4519 struct rpc_message msg = {
4520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
4521 .rpc_argp = &args,
4522 .rpc_resp = &res,
4523 };
4524 int status;
4525
4526 nfs4_stateid_copy(&args.stateid, &zero_stateid);
4527
4528 status = rpc_call_sync(server->client, &msg, 0);
4529 if (status)
4530 dprintk("%s failed: %d\n", __func__, status);
4531
4532 return status;
4533}
4534
4535static int nfs4_do_set_security_label(struct inode *inode,
4536 struct nfs4_label *ilabel,
4537 struct nfs_fattr *fattr,
4538 struct nfs4_label *olabel)
4539{
4540 struct nfs4_exception exception = { };
4541 int err;
4542
4543 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004544 err = _nfs4_do_set_security_label(inode, ilabel,
4545 fattr, olabel);
4546 trace_nfs4_set_security_label(inode, err);
4547 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04004548 &exception);
4549 } while (exception.retry);
4550 return err;
4551}
4552
4553static int
4554nfs4_set_security_label(struct dentry *dentry, const void *buf, size_t buflen)
4555{
4556 struct nfs4_label ilabel, *olabel = NULL;
4557 struct nfs_fattr fattr;
4558 struct rpc_cred *cred;
4559 struct inode *inode = dentry->d_inode;
4560 int status;
4561
4562 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
4563 return -EOPNOTSUPP;
4564
4565 nfs_fattr_init(&fattr);
4566
4567 ilabel.pi = 0;
4568 ilabel.lfs = 0;
4569 ilabel.label = (char *)buf;
4570 ilabel.len = buflen;
4571
4572 cred = rpc_lookup_cred();
4573 if (IS_ERR(cred))
4574 return PTR_ERR(cred);
4575
4576 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4577 if (IS_ERR(olabel)) {
4578 status = -PTR_ERR(olabel);
4579 goto out;
4580 }
4581
4582 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
4583 if (status == 0)
4584 nfs_setsecurity(inode, &fattr, olabel);
4585
4586 nfs4_label_free(olabel);
4587out:
4588 put_rpccred(cred);
4589 return status;
4590}
4591#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
4592
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004595nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004597 struct nfs_client *clp = server->nfs_client;
4598
4599 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 return 0;
4601 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004602 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004603 case -NFS4ERR_ADMIN_REVOKED:
4604 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04004605 if (state == NULL)
4606 break;
4607 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004608 case -NFS4ERR_OPENMODE:
4609 if (state == NULL)
4610 break;
Trond Myklebust5d422302013-03-14 16:57:48 -04004611 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4612 goto stateid_invalid;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004613 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004614 case -NFS4ERR_EXPIRED:
Trond Myklebust5d422302013-03-14 16:57:48 -04004615 if (state != NULL) {
4616 if (nfs4_schedule_stateid_recovery(server, state) < 0)
4617 goto stateid_invalid;
4618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004620 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004621 nfs4_schedule_lease_recovery(clp);
4622 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04004623#if defined(CONFIG_NFS_V4_1)
4624 case -NFS4ERR_BADSESSION:
4625 case -NFS4ERR_BADSLOT:
4626 case -NFS4ERR_BAD_HIGH_SLOT:
4627 case -NFS4ERR_DEADSESSION:
4628 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4629 case -NFS4ERR_SEQ_FALSE_RETRY:
4630 case -NFS4ERR_SEQ_MISORDERED:
4631 dprintk("%s ERROR %d, Reset session\n", __func__,
4632 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04004633 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04004634 task->tk_status = 0;
4635 return -EAGAIN;
4636#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04004638 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05004639 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 rpc_delay(task, NFS4_POLL_RETRY_MAX);
4641 task->tk_status = 0;
4642 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004643 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 case -NFS4ERR_OLD_STATEID:
4645 task->tk_status = 0;
4646 return -EAGAIN;
4647 }
4648 task->tk_status = nfs4_map_errors(task->tk_status);
4649 return 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04004650stateid_invalid:
4651 task->tk_status = -EIO;
4652 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004653wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004654 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004655 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
4656 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
4657 task->tk_status = 0;
4658 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
Chuck Leverf0920752012-05-21 22:45:41 -04004661static void nfs4_init_boot_verifier(const struct nfs_client *clp,
4662 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05004663{
4664 __be32 verf[2];
4665
Chuck Lever2c820d92012-05-21 22:45:33 -04004666 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
4667 /* An impossible timestamp guarantees this value
4668 * will never match a generated boot time. */
4669 verf[0] = 0;
Trond Myklebust17f26b12013-08-21 15:48:42 -04004670 verf[1] = cpu_to_be32(NSEC_PER_SEC + 1);
Chuck Lever2c820d92012-05-21 22:45:33 -04004671 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04004672 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Trond Myklebust17f26b12013-08-21 15:48:42 -04004673 verf[0] = cpu_to_be32(nn->boot_time.tv_sec);
4674 verf[1] = cpu_to_be32(nn->boot_time.tv_nsec);
Chuck Lever2c820d92012-05-21 22:45:33 -04004675 }
Chuck Levercd937102012-03-02 17:14:31 -05004676 memcpy(bootverf->data, verf, sizeof(bootverf->data));
4677}
4678
Chuck Levere984a552012-09-14 17:24:21 -04004679static unsigned int
4680nfs4_init_nonuniform_client_string(const struct nfs_client *clp,
4681 char *buf, size_t len)
4682{
4683 unsigned int result;
4684
4685 rcu_read_lock();
4686 result = scnprintf(buf, len, "Linux NFSv4.0 %s/%s %s",
4687 clp->cl_ipaddr,
4688 rpc_peeraddr2str(clp->cl_rpcclient,
4689 RPC_DISPLAY_ADDR),
4690 rpc_peeraddr2str(clp->cl_rpcclient,
4691 RPC_DISPLAY_PROTO));
4692 rcu_read_unlock();
4693 return result;
4694}
4695
4696static unsigned int
4697nfs4_init_uniform_client_string(const struct nfs_client *clp,
4698 char *buf, size_t len)
4699{
Trond Myklebust55b59292013-07-24 16:36:35 -04004700 const char *nodename = clp->cl_rpcclient->cl_nodename;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004701
4702 if (nfs4_client_id_uniquifier[0] != '\0')
Trond Myklebust55b59292013-07-24 16:36:35 -04004703 return scnprintf(buf, len, "Linux NFSv%u.%u %s/%s",
4704 clp->rpc_ops->version,
4705 clp->cl_minorversion,
4706 nfs4_client_id_uniquifier,
4707 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004708 return scnprintf(buf, len, "Linux NFSv%u.%u %s",
4709 clp->rpc_ops->version, clp->cl_minorversion,
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04004710 nodename);
Chuck Levere984a552012-09-14 17:24:21 -04004711}
4712
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004713/**
4714 * nfs4_proc_setclientid - Negotiate client ID
4715 * @clp: state data structure
4716 * @program: RPC program for NFSv4 callback service
4717 * @port: IP port number for NFS4 callback service
4718 * @cred: RPC credential to use for this call
4719 * @res: where to place the result
4720 *
4721 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4722 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004723int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
4724 unsigned short port, struct rpc_cred *cred,
4725 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726{
4727 nfs4_verifier sc_verifier;
4728 struct nfs4_setclientid setclientid = {
4729 .sc_verifier = &sc_verifier,
4730 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00004731 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 };
4733 struct rpc_message msg = {
4734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
4735 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004736 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004737 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004739 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
Chuck Leverde734832012-07-11 16:30:50 -04004741 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04004742 nfs4_init_boot_verifier(clp, &sc_verifier);
Chuck Levere984a552012-09-14 17:24:21 -04004743 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
4744 setclientid.sc_name_len =
4745 nfs4_init_uniform_client_string(clp,
4746 setclientid.sc_name,
4747 sizeof(setclientid.sc_name));
4748 else
4749 setclientid.sc_name_len =
4750 nfs4_init_nonuniform_client_string(clp,
4751 setclientid.sc_name,
4752 sizeof(setclientid.sc_name));
Chuck Leverde734832012-07-11 16:30:50 -04004753 /* cb_client4 */
Chuck Levere984a552012-09-14 17:24:21 -04004754 rcu_read_lock();
Chuck Leverde734832012-07-11 16:30:50 -04004755 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Djalal Harounife2d5392013-06-18 17:58:12 +01004756 sizeof(setclientid.sc_netid), "%s",
Chuck Leverd4d3c502007-12-10 14:57:09 -05004757 rpc_peeraddr2str(clp->cl_rpcclient,
4758 RPC_DISPLAY_NETID));
Chuck Leverde734832012-07-11 16:30:50 -04004759 rcu_read_unlock();
4760 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05004761 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 clp->cl_ipaddr, port >> 8, port & 255);
4763
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004764 dprintk("NFS call setclientid auth=%s, '%.*s'\n",
4765 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4766 setclientid.sc_name_len, setclientid.sc_name);
4767 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004768 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004769 dprintk("NFS reply setclientid: %d\n", status);
4770 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004773/**
4774 * nfs4_proc_setclientid_confirm - Confirm client ID
4775 * @clp: state data structure
4776 * @res: result of a previous SETCLIENTID
4777 * @cred: RPC credential to use for this call
4778 *
4779 * Returns zero, a negative errno, or a negative NFS4ERR status code.
4780 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004781int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004782 struct nfs4_setclientid_res *arg,
4783 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 struct rpc_message msg = {
4786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004787 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004788 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 int status;
4791
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004792 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
4793 clp->cl_rpcclient->cl_auth->au_ops->au_name,
4794 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004795 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004796 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04004797 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 return status;
4799}
4800
Trond Myklebustfe650402006-01-03 09:55:18 +01004801struct nfs4_delegreturndata {
4802 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004803 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004804 struct nfs_fh fh;
4805 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004806 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004807 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004808 int rpc_status;
4809};
4810
Trond Myklebustfe650402006-01-03 09:55:18 +01004811static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4812{
4813 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004814
Trond Myklebust14516c32010-07-31 14:29:06 -04004815 if (!nfs4_sequence_done(task, &data->res.seq_res))
4816 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004817
Trond Myklebustca8acf82013-08-13 10:36:56 -04004818 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004819 switch (task->tk_status) {
4820 case -NFS4ERR_STALE_STATEID:
4821 case -NFS4ERR_EXPIRED:
4822 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004823 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004824 break;
4825 default:
4826 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4827 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004828 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004829 return;
4830 }
4831 }
4832 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004833}
4834
4835static void nfs4_delegreturn_release(void *calldata)
4836{
Trond Myklebustfe650402006-01-03 09:55:18 +01004837 kfree(calldata);
4838}
4839
Andy Adamson938e1012009-04-01 09:22:28 -04004840#if defined(CONFIG_NFS_V4_1)
4841static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4842{
4843 struct nfs4_delegreturndata *d_data;
4844
4845 d_data = (struct nfs4_delegreturndata *)data;
4846
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004847 nfs4_setup_sequence(d_data->res.server,
4848 &d_data->args.seq_args,
4849 &d_data->res.seq_res,
4850 task);
Andy Adamson938e1012009-04-01 09:22:28 -04004851}
4852#endif /* CONFIG_NFS_V4_1 */
4853
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004854static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004855#if defined(CONFIG_NFS_V4_1)
4856 .rpc_call_prepare = nfs4_delegreturn_prepare,
4857#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004858 .rpc_call_done = nfs4_delegreturn_done,
4859 .rpc_release = nfs4_delegreturn_release,
4860};
4861
Trond Myklebuste6f81072008-01-24 18:14:34 -05004862static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004863{
4864 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004865 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004866 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004867 struct rpc_message msg = {
4868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4869 .rpc_cred = cred,
4870 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004871 struct rpc_task_setup task_setup_data = {
4872 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004873 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004874 .callback_ops = &nfs4_delegreturn_ops,
4875 .flags = RPC_TASK_ASYNC,
4876 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004877 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004878
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004879 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004880 if (data == NULL)
4881 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004882 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004883 data->args.fhandle = &data->fh;
4884 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004885 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004886 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004887 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004888 data->res.fattr = &data->fattr;
4889 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004890 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004891 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004892 data->rpc_status = 0;
4893
Trond Myklebustc970aa82007-07-14 15:39:59 -04004894 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004895 msg.rpc_argp = &data->args;
4896 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004897 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004898 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004899 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004900 if (!issync)
4901 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004902 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004903 if (status != 0)
4904 goto out;
4905 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004906 if (status == 0)
4907 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4908 else
4909 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004910out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004911 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004912 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913}
4914
Trond Myklebuste6f81072008-01-24 18:14:34 -05004915int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916{
4917 struct nfs_server *server = NFS_SERVER(inode);
4918 struct nfs4_exception exception = { };
4919 int err;
4920 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004921 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Trond Myklebustca8acf82013-08-13 10:36:56 -04004922 trace_nfs4_delegreturn(inode, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 switch (err) {
4924 case -NFS4ERR_STALE_STATEID:
4925 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 case 0:
4927 return 0;
4928 }
4929 err = nfs4_handle_exception(server, err, &exception);
4930 } while (exception.retry);
4931 return err;
4932}
4933
4934#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4935#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4936
4937/*
4938 * sleep, with exponential backoff, and retry the LOCK operation.
4939 */
4940static unsigned long
4941nfs4_set_lock_task_retry(unsigned long timeout)
4942{
Colin Cross416ad3c2013-05-06 23:50:06 +00004943 freezable_schedule_timeout_killable_unsafe(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 timeout <<= 1;
4945 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4946 return NFS4_LOCK_MAXTIMEOUT;
4947 return timeout;
4948}
4949
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4951{
4952 struct inode *inode = state->inode;
4953 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004954 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004955 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004957 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004959 struct nfs_lockt_res res = {
4960 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 };
4962 struct rpc_message msg = {
4963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4964 .rpc_argp = &arg,
4965 .rpc_resp = &res,
4966 .rpc_cred = state->owner->so_cred,
4967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 struct nfs4_lock_state *lsp;
4969 int status;
4970
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004971 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004972 status = nfs4_set_lock_state(state, request);
4973 if (status != 0)
4974 goto out;
4975 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004976 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004977 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004978 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004979 switch (status) {
4980 case 0:
4981 request->fl_type = F_UNLCK;
4982 break;
4983 case -NFS4ERR_DENIED:
4984 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004986 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004987out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 return status;
4989}
4990
4991static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4992{
4993 struct nfs4_exception exception = { };
4994 int err;
4995
4996 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04004997 err = _nfs4_proc_getlk(state, cmd, request);
4998 trace_nfs4_get_lock(request, state, cmd, err);
4999 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 &exception);
5001 } while (exception.retry);
5002 return err;
5003}
5004
5005static int do_vfs_lock(struct file *file, struct file_lock *fl)
5006{
5007 int res = 0;
5008 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
5009 case FL_POSIX:
5010 res = posix_lock_file_wait(file, fl);
5011 break;
5012 case FL_FLOCK:
5013 res = flock_lock_file_wait(file, fl);
5014 break;
5015 default:
5016 BUG();
5017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 return res;
5019}
5020
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005021struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005022 struct nfs_locku_args arg;
5023 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005024 struct nfs4_lock_state *lsp;
5025 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005026 struct file_lock fl;
5027 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005028 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005029};
5030
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005031static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5032 struct nfs_open_context *ctx,
5033 struct nfs4_lock_state *lsp,
5034 struct nfs_seqid *seqid)
5035{
5036 struct nfs4_unlockdata *p;
5037 struct inode *inode = lsp->ls_state->inode;
5038
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005039 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005040 if (p == NULL)
5041 return NULL;
5042 p->arg.fh = NFS_FH(inode);
5043 p->arg.fl = &p->fl;
5044 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005045 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005046 p->arg.stateid = &lsp->ls_stateid;
5047 p->lsp = lsp;
5048 atomic_inc(&lsp->ls_count);
5049 /* Ensure we don't close file until we're done freeing locks! */
5050 p->ctx = get_nfs_open_context(ctx);
5051 memcpy(&p->fl, fl, sizeof(p->fl));
5052 p->server = NFS_SERVER(inode);
5053 return p;
5054}
5055
Trond Myklebust06f814a2006-01-03 09:55:07 +01005056static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005057{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005058 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005059 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005060 nfs4_put_lock_state(calldata->lsp);
5061 put_nfs_open_context(calldata->ctx);
5062 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005063}
5064
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005065static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005066{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005067 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005068
Trond Myklebust14516c32010-07-31 14:29:06 -04005069 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5070 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005071 switch (task->tk_status) {
5072 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05005073 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
5074 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005075 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005076 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005077 case -NFS4ERR_BAD_STATEID:
5078 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005079 case -NFS4ERR_STALE_STATEID:
5080 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005081 break;
5082 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005083 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005084 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005085 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005086 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005087}
5088
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005089static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005090{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005091 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005093 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005094 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04005095 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005096 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005097 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005098 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005099 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005100 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04005101 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005102 &calldata->res.seq_res,
5103 task) != 0)
5104 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005105 return;
5106out_no_action:
5107 task->tk_action = NULL;
5108out_wait:
5109 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005112static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005113 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005114 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005115 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005116};
5117
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005118static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5119 struct nfs_open_context *ctx,
5120 struct nfs4_lock_state *lsp,
5121 struct nfs_seqid *seqid)
5122{
5123 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005124 struct rpc_message msg = {
5125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5126 .rpc_cred = ctx->cred,
5127 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005128 struct rpc_task_setup task_setup_data = {
5129 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005130 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005131 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005132 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005133 .flags = RPC_TASK_ASYNC,
5134 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005135
Frank Filz137d6ac2007-07-09 15:32:29 -07005136 /* Ensure this is an unlock - when canceling a lock, the
5137 * canceled lock is passed in, and it won't be an unlock.
5138 */
5139 fl->fl_type = F_UNLCK;
5140
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005141 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5142 if (data == NULL) {
5143 nfs_free_seqid(seqid);
5144 return ERR_PTR(-ENOMEM);
5145 }
5146
Trond Myklebust9d12b212012-01-17 22:04:25 -05005147 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005148 msg.rpc_argp = &data->arg;
5149 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005150 task_setup_data.callback_data = data;
5151 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005152}
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5155{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005156 struct inode *inode = state->inode;
5157 struct nfs4_state_owner *sp = state->owner;
5158 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005159 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005160 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005161 struct rpc_task *task;
5162 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005163 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Trond Myklebust9b073572006-06-29 16:38:34 -04005165 status = nfs4_set_lock_state(state, request);
5166 /* Unlock _before_ we do the RPC call */
5167 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005168 /* Exclude nfs_delegation_claim_locks() */
5169 mutex_lock(&sp->so_delegreturn_mutex);
5170 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005171 down_read(&nfsi->rwsem);
5172 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
5173 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005174 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005175 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005176 }
5177 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005178 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005179 if (status != 0)
5180 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005181 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005182 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005183 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5184 goto out;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005185 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005186 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005187 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04005188 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005189 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005190 status = PTR_ERR(task);
5191 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005192 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005193 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005194 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005195out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005196 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005197 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005198 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199}
5200
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005201struct nfs4_lockdata {
5202 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005203 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005204 struct nfs4_lock_state *lsp;
5205 struct nfs_open_context *ctx;
5206 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005207 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005208 int rpc_status;
5209 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005210 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005211};
5212
5213static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005214 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
5215 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005216{
5217 struct nfs4_lockdata *p;
5218 struct inode *inode = lsp->ls_state->inode;
5219 struct nfs_server *server = NFS_SERVER(inode);
5220
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005221 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005222 if (p == NULL)
5223 return NULL;
5224
5225 p->arg.fh = NFS_FH(inode);
5226 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005227 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005228 if (p->arg.open_seqid == NULL)
5229 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005230 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005231 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005232 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005233 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04005234 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005235 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005236 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005237 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005238 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04005239 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005240 atomic_inc(&lsp->ls_count);
5241 p->ctx = get_nfs_open_context(ctx);
5242 memcpy(&p->fl, fl, sizeof(p->fl));
5243 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005244out_free_seqid:
5245 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005246out_free:
5247 kfree(p);
5248 return NULL;
5249}
5250
5251static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
5252{
5253 struct nfs4_lockdata *data = calldata;
5254 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
Harvey Harrison3110ff82008-05-02 13:42:44 -07005256 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005257 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005258 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005259 /* Do we need to do an open_to_lock_owner? */
5260 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005261 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005262 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005263 }
Trond Myklebust8e472f32013-04-20 01:30:53 -04005264 data->arg.open_stateid = &state->open_stateid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005265 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005266 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005267 } else
5268 data->arg.new_lock_owner = 0;
Trond Myklebust5d422302013-03-14 16:57:48 -04005269 if (!nfs4_valid_open_stateid(state)) {
5270 data->rpc_status = -EBADF;
5271 task->tk_action = NULL;
5272 goto out_release_open_seqid;
5273 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005274 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04005275 if (nfs4_setup_sequence(data->server,
5276 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005277 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005278 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04005279 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04005280out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005281 nfs_release_seqid(data->arg.open_seqid);
5282out_release_lock_seqid:
5283 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005284out_wait:
5285 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005286 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005287}
5288
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005289static void nfs4_lock_done(struct rpc_task *task, void *calldata)
5290{
5291 struct nfs4_lockdata *data = calldata;
5292
Harvey Harrison3110ff82008-05-02 13:42:44 -07005293 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005294
Trond Myklebust14516c32010-07-31 14:29:06 -04005295 if (!nfs4_sequence_done(task, &data->res.seq_res))
5296 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04005297
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005298 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005299 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005300 if (data->rpc_status == 0)
5301 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
5302 else
5303 goto out;
5304 }
5305 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05005306 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005307 set_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags);
Al Viro3d4ff432011-06-22 18:40:12 -04005308 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005309 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005310out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07005311 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005312}
5313
5314static void nfs4_lock_release(void *calldata)
5315{
5316 struct nfs4_lockdata *data = calldata;
5317
Harvey Harrison3110ff82008-05-02 13:42:44 -07005318 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05005319 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005320 if (data->cancelled != 0) {
5321 struct rpc_task *task;
5322 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
5323 data->arg.lock_seqid);
5324 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08005325 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005326 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005327 } else
5328 nfs_free_seqid(data->arg.lock_seqid);
5329 nfs4_put_lock_state(data->lsp);
5330 put_nfs_open_context(data->ctx);
5331 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005332 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005333}
5334
5335static const struct rpc_call_ops nfs4_lock_ops = {
5336 .rpc_call_prepare = nfs4_lock_prepare,
5337 .rpc_call_done = nfs4_lock_done,
5338 .rpc_release = nfs4_lock_release,
5339};
5340
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005341static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
5342{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005343 switch (error) {
5344 case -NFS4ERR_ADMIN_REVOKED:
5345 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05005346 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005347 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04005348 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05005349 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005350 break;
5351 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05005352 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05005353 case -NFS4ERR_EXPIRED:
5354 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005355 };
5356}
5357
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005358static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005359{
5360 struct nfs4_lockdata *data;
5361 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005362 struct rpc_message msg = {
5363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
5364 .rpc_cred = state->owner->so_cred,
5365 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005366 struct rpc_task_setup task_setup_data = {
5367 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005368 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005369 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005370 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005371 .flags = RPC_TASK_ASYNC,
5372 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005373 int ret;
5374
Harvey Harrison3110ff82008-05-02 13:42:44 -07005375 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005376 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005377 fl->fl_u.nfs4_fl.owner,
5378 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005379 if (data == NULL)
5380 return -ENOMEM;
5381 if (IS_SETLKW(cmd))
5382 data->arg.block = 1;
Trond Myklebust9d12b212012-01-17 22:04:25 -05005383 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005384 msg.rpc_argp = &data->arg;
5385 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005386 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04005387 if (recovery_type > NFS_LOCK_NEW) {
5388 if (recovery_type == NFS_LOCK_RECLAIM)
5389 data->arg.reclaim = NFS_LOCK_RECLAIM;
5390 nfs4_set_sequence_privileged(&data->arg.seq_args);
5391 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04005392 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005393 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005394 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005395 ret = nfs4_wait_for_completion_rpc_task(task);
5396 if (ret == 0) {
5397 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05005398 if (ret)
5399 nfs4_handle_setlk_error(data->server, data->lsp,
5400 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005401 } else
5402 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005403 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005404 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406}
5407
5408static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
5409{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005410 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005411 struct nfs4_exception exception = {
5412 .inode = state->inode,
5413 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005414 int err;
5415
5416 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005417 /* Cache the lock if possible... */
5418 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5419 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005420 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005421 trace_nfs4_lock_reclaim(request, state, F_SETLK, err);
Trond Myklebust168667c2010-10-19 19:47:49 -04005422 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00005423 break;
5424 nfs4_handle_exception(server, err, &exception);
5425 } while (exception.retry);
5426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427}
5428
5429static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
5430{
Trond Myklebust202b50d2005-06-22 17:16:29 +00005431 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04005432 struct nfs4_exception exception = {
5433 .inode = state->inode,
5434 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00005435 int err;
5436
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005437 err = nfs4_set_lock_state(state, request);
5438 if (err != 0)
5439 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00005440 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04005441 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
5442 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005443 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005444 trace_nfs4_lock_expired(request, state, F_SETLK, err);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005445 switch (err) {
5446 default:
5447 goto out;
5448 case -NFS4ERR_GRACE:
5449 case -NFS4ERR_DELAY:
5450 nfs4_handle_exception(server, err, &exception);
5451 err = 0;
5452 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00005453 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05005454out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00005455 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456}
5457
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005458#if defined(CONFIG_NFS_V4_1)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005459/**
5460 * nfs41_check_expired_locks - possibly free a lock stateid
5461 *
5462 * @state: NFSv4 state for an inode
5463 *
5464 * Returns NFS_OK if recovery for this stateid is now finished.
5465 * Otherwise a negative NFS4ERR value is returned.
5466 */
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005467static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005468{
Chuck Levereb64cf92012-07-11 16:30:05 -04005469 int status, ret = -NFS4ERR_BAD_STATEID;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005470 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005471 struct nfs_server *server = NFS_SERVER(state->inode);
5472
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005473 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
Trond Myklebust795a88c2012-09-10 13:26:49 -04005474 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005475 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
5476
5477 status = nfs41_test_stateid(server,
5478 &lsp->ls_stateid,
5479 cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04005480 trace_nfs4_test_lock_stateid(state, lsp, status);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005481 if (status != NFS_OK) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04005482 /* Free the stateid unless the server
5483 * informs us the stateid is unrecognized. */
Chuck Lever89af2732012-07-11 16:29:56 -04005484 if (status != -NFS4ERR_BAD_STATEID)
5485 nfs41_free_stateid(server,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04005486 &lsp->ls_stateid,
5487 cred);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005488 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05005489 ret = status;
5490 }
5491 }
5492 };
5493
5494 return ret;
5495}
5496
5497static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
5498{
5499 int status = NFS_OK;
5500
5501 if (test_bit(LK_STATE_IN_USE, &state->flags))
5502 status = nfs41_check_expired_locks(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04005503 if (status != NFS_OK)
5504 status = nfs4_lock_expired(state, request);
5505 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04005506}
5507#endif
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5510{
Trond Myklebust9a99af492013-02-04 20:17:49 -05005511 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005512 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005513 unsigned char fl_flags = request->fl_flags;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005514 unsigned int seq;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005515 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
Trond Myklebust8e469eb2010-01-26 15:42:30 -05005517 if ((fl_flags & FL_POSIX) &&
5518 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
5519 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005520 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005521 status = nfs4_set_lock_state(state, request);
5522 if (status != 0)
5523 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005524 request->fl_flags |= FL_ACCESS;
5525 status = do_vfs_lock(request->fl_file, request);
5526 if (status < 0)
5527 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005528 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005529 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04005530 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005531 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005532 request->fl_flags = fl_flags & ~FL_SLEEP;
5533 status = do_vfs_lock(request->fl_file, request);
5534 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005535 }
Trond Myklebust9a99af492013-02-04 20:17:49 -05005536 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
5537 up_read(&nfsi->rwsem);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08005538 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005539 if (status != 0)
Trond Myklebust9a99af492013-02-04 20:17:49 -05005540 goto out;
5541 down_read(&nfsi->rwsem);
5542 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) {
5543 status = -NFS4ERR_DELAY;
Trond Myklebust01c3b862006-06-29 16:38:39 -04005544 goto out_unlock;
Trond Myklebust9a99af492013-02-04 20:17:49 -05005545 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005546 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04005547 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005548 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05005549 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
5550 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005551out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05005552 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04005553out:
5554 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 return status;
5556}
5557
5558static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5559{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005560 struct nfs4_exception exception = {
5561 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04005562 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05005563 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 int err;
5565
5566 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07005567 err = _nfs4_proc_setlk(state, cmd, request);
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005568 trace_nfs4_set_lock(request, state, cmd, err);
Trond Myklebust965b5d62009-06-17 13:22:59 -07005569 if (err == -NFS4ERR_DENIED)
5570 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07005572 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 } while (exception.retry);
5574 return err;
5575}
5576
5577static int
5578nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
5579{
5580 struct nfs_open_context *ctx;
5581 struct nfs4_state *state;
5582 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
5583 int status;
5584
5585 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005586 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 state = ctx->state;
5588
5589 if (request->fl_start < 0 || request->fl_end < 0)
5590 return -EINVAL;
5591
Trond Myklebustd9531262009-07-21 19:22:38 -04005592 if (IS_GETLK(cmd)) {
5593 if (state != NULL)
5594 return nfs4_proc_getlk(state, F_GETLK, request);
5595 return 0;
5596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
5598 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
5599 return -EINVAL;
5600
Trond Myklebustd9531262009-07-21 19:22:38 -04005601 if (request->fl_type == F_UNLCK) {
5602 if (state != NULL)
5603 return nfs4_proc_unlck(state, cmd, request);
5604 return 0;
5605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Trond Myklebustd9531262009-07-21 19:22:38 -04005607 if (state == NULL)
5608 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04005609 /*
5610 * Don't rely on the VFS having checked the file open mode,
5611 * since it won't do this for flock() locks.
5612 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04005613 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04005614 case F_RDLCK:
5615 if (!(filp->f_mode & FMODE_READ))
5616 return -EBADF;
5617 break;
5618 case F_WRLCK:
5619 if (!(filp->f_mode & FMODE_WRITE))
5620 return -EBADF;
5621 }
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 do {
5624 status = nfs4_proc_setlk(state, cmd, request);
5625 if ((status != -EAGAIN) || IS_SETLK(cmd))
5626 break;
5627 timeout = nfs4_set_lock_task_retry(timeout);
5628 status = -ERESTARTSYS;
5629 if (signalled())
5630 break;
5631 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 return status;
5633}
5634
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005635int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05005636{
5637 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05005638 int err;
5639
5640 err = nfs4_set_lock_state(state, fl);
5641 if (err != 0)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005642 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04005643 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e632013-04-01 15:56:46 -04005644 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05005645}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005646
Trond Myklebustcf470c32012-03-07 13:49:12 -05005647struct nfs_release_lockowner_data {
5648 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005649 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005650 struct nfs_release_lockowner_args args;
5651};
5652
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005653static void nfs4_release_lockowner_release(void *calldata)
5654{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005655 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005656 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005657 kfree(calldata);
5658}
5659
Trond Myklebust17280172012-03-11 13:11:00 -04005660static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005661 .rpc_release = nfs4_release_lockowner_release,
5662};
5663
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04005664static int nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005665{
Trond Myklebustcf470c32012-03-07 13:49:12 -05005666 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005667 struct rpc_message msg = {
5668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
5669 };
5670
5671 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05005672 return -EINVAL;
5673 data = kmalloc(sizeof(*data), GFP_NOFS);
5674 if (!data)
5675 return -ENOMEM;
5676 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04005677 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05005678 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
5679 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
5680 data->args.lock_owner.s_dev = server->s_dev;
5681 msg.rpc_argp = &data->args;
5682 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
5683 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04005684}
5685
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005686#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
5687
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005688static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
5689 const void *buf, size_t buflen,
5690 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005691{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005692 if (strcmp(key, "") != 0)
5693 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005694
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005695 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005696}
5697
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005698static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
5699 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005700{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005701 if (strcmp(key, "") != 0)
5702 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005703
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005704 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005705}
5706
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005707static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
5708 size_t list_len, const char *name,
5709 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005710{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005711 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005712
J. Bruce Fields096455a2006-03-20 23:23:42 -05005713 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
5714 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005715
5716 if (list && len <= list_len)
5717 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005718 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005719}
5720
David Quigleyc9bccef2013-05-22 12:50:45 -04005721#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5722static inline int nfs4_server_supports_labels(struct nfs_server *server)
5723{
5724 return server->caps & NFS_CAP_SECURITY_LABEL;
5725}
5726
5727static int nfs4_xattr_set_nfs4_label(struct dentry *dentry, const char *key,
5728 const void *buf, size_t buflen,
5729 int flags, int type)
5730{
5731 if (security_ismaclabel(key))
5732 return nfs4_set_security_label(dentry, buf, buflen);
5733
5734 return -EOPNOTSUPP;
5735}
5736
5737static int nfs4_xattr_get_nfs4_label(struct dentry *dentry, const char *key,
5738 void *buf, size_t buflen, int type)
5739{
5740 if (security_ismaclabel(key))
5741 return nfs4_get_security_label(dentry->d_inode, buf, buflen);
5742 return -EOPNOTSUPP;
5743}
5744
5745static size_t nfs4_xattr_list_nfs4_label(struct dentry *dentry, char *list,
5746 size_t list_len, const char *name,
5747 size_t name_len, int type)
5748{
5749 size_t len = 0;
5750
5751 if (nfs_server_capable(dentry->d_inode, NFS_CAP_SECURITY_LABEL)) {
5752 len = security_inode_listsecurity(dentry->d_inode, NULL, 0);
5753 if (list && len <= list_len)
5754 security_inode_listsecurity(dentry->d_inode, list, len);
5755 }
5756 return len;
5757}
5758
5759static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
5760 .prefix = XATTR_SECURITY_PREFIX,
5761 .list = nfs4_xattr_list_nfs4_label,
5762 .get = nfs4_xattr_get_nfs4_label,
5763 .set = nfs4_xattr_set_nfs4_label,
5764};
5765#endif
5766
5767
Andy Adamson533eb462011-06-13 18:25:56 -04005768/*
5769 * nfs_fhget will use either the mounted_on_fileid or the fileid
5770 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005771static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
5772{
Andy Adamson533eb462011-06-13 18:25:56 -04005773 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
5774 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
5775 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05005776 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005777 return;
5778
5779 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05005780 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04005781 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
5782 fattr->nlink = 2;
5783}
5784
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005785static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5786 const struct qstr *name,
5787 struct nfs4_fs_locations *fs_locations,
5788 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04005789{
5790 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04005791 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04005792 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005793 };
5794 struct nfs4_fs_locations_arg args = {
5795 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05005796 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005797 .page = page,
5798 .bitmask = bitmask,
5799 };
Benny Halevy22958462009-04-01 09:22:02 -04005800 struct nfs4_fs_locations_res res = {
5801 .fs_locations = fs_locations,
5802 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04005803 struct rpc_message msg = {
5804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
5805 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005806 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005807 };
5808 int status;
5809
Harvey Harrison3110ff82008-05-02 13:42:44 -07005810 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005811
5812 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5813 * is not supported */
5814 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5815 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5816 else
5817 bitmask[0] |= FATTR4_WORD0_FILEID;
5818
Trond Myklebustc228fd32007-01-13 02:28:11 -05005819 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005820 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005821 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005822 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005823 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005824 return status;
5825}
5826
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005827int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5828 const struct qstr *name,
5829 struct nfs4_fs_locations *fs_locations,
5830 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005831{
5832 struct nfs4_exception exception = { };
5833 int err;
5834 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005835 err = _nfs4_proc_fs_locations(client, dir, name,
5836 fs_locations, page);
5837 trace_nfs4_get_fs_locations(dir, name, err);
5838 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005839 &exception);
5840 } while (exception.retry);
5841 return err;
5842}
5843
Andy Adamson5ec16a82013-08-08 10:57:55 -04005844/**
5845 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
5846 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
5847 */
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005848static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5849{
5850 int status;
5851 struct nfs4_secinfo_arg args = {
5852 .dir_fh = NFS_FH(dir),
5853 .name = name,
5854 };
5855 struct nfs4_secinfo_res res = {
5856 .flavors = flavors,
5857 };
5858 struct rpc_message msg = {
5859 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5860 .rpc_argp = &args,
5861 .rpc_resp = &res,
5862 };
Andy Adamson5ec16a82013-08-08 10:57:55 -04005863 struct rpc_clnt *clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005864
5865 dprintk("NFS call secinfo %s\n", name->name);
Andy Adamson5ec16a82013-08-08 10:57:55 -04005866 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005867 dprintk("NFS reply secinfo: %d\n", status);
5868 return status;
5869}
5870
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005871int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5872 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005873{
5874 struct nfs4_exception exception = { };
5875 int err;
5876 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005877 err = _nfs4_proc_secinfo(dir, name, flavors);
5878 trace_nfs4_secinfo(dir, name, err);
5879 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005880 &exception);
5881 } while (exception.retry);
5882 return err;
5883}
5884
Andy Adamson557134a2009-04-01 09:21:53 -04005885#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005886/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005887 * Check the exchange flags returned by the server for invalid flags, having
5888 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5889 * DS flags set.
5890 */
5891static int nfs4_check_cl_exchange_flags(u32 flags)
5892{
5893 if (flags & ~EXCHGID4_FLAG_MASK_R)
5894 goto out_inval;
5895 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5896 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5897 goto out_inval;
5898 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5899 goto out_inval;
5900 return NFS_OK;
5901out_inval:
5902 return -NFS4ERR_INVAL;
5903}
5904
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005905static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005906nfs41_same_server_scope(struct nfs41_server_scope *a,
5907 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005908{
5909 if (a->server_scope_sz == b->server_scope_sz &&
5910 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5911 return true;
5912
5913 return false;
5914}
5915
Andy Adamson357f54d2010-12-14 10:11:57 -05005916/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005917 * nfs4_proc_bind_conn_to_session()
5918 *
5919 * The 4.1 client currently uses the same TCP connection for the
5920 * fore and backchannel.
5921 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005922int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005923{
5924 int status;
5925 struct nfs41_bind_conn_to_session_res res;
5926 struct rpc_message msg = {
5927 .rpc_proc =
5928 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5929 .rpc_argp = clp,
5930 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005931 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005932 };
5933
5934 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005935
5936 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5937 if (unlikely(res.session == NULL)) {
5938 status = -ENOMEM;
5939 goto out;
5940 }
5941
5942 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005943 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005944 if (status == 0) {
5945 if (memcmp(res.session->sess_id.data,
5946 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5947 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5948 status = -EIO;
5949 goto out_session;
5950 }
5951 if (res.dir != NFS4_CDFS4_BOTH) {
5952 dprintk("NFS: %s: Unexpected direction from server\n",
5953 __func__);
5954 status = -EIO;
5955 goto out_session;
5956 }
5957 if (res.use_conn_in_rdma_mode) {
5958 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5959 __func__);
5960 status = -EIO;
5961 goto out_session;
5962 }
5963 }
5964out_session:
5965 kfree(res.session);
5966out:
5967 dprintk("<-- %s status= %d\n", __func__, status);
5968 return status;
5969}
5970
5971/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005972 * nfs4_proc_exchange_id()
5973 *
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005974 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5975 *
Benny Halevy99fe60d2009-04-01 09:22:29 -04005976 * Since the clientid has expired, all compounds using sessions
5977 * associated with the stale clientid will be returning
5978 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5979 * be in some phase of session reset.
5980 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005981int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005982{
5983 nfs4_verifier verifier;
5984 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005985 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005986 .client = clp,
Trond Myklebust4f0b4292013-05-20 12:24:03 -04005987 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
5988 EXCHGID4_FLAG_BIND_PRINC_STATEID,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005989 };
5990 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005991 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005992 };
5993 int status;
5994 struct rpc_message msg = {
5995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5996 .rpc_argp = &args,
5997 .rpc_resp = &res,
5998 .rpc_cred = cred,
5999 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04006000
Chuck Leverf0920752012-05-21 22:45:41 -04006001 nfs4_init_boot_verifier(clp, &verifier);
Chuck Levere984a552012-09-14 17:24:21 -04006002 args.id_len = nfs4_init_uniform_client_string(clp, args.id,
6003 sizeof(args.id));
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006004 dprintk("NFS call exchange_id auth=%s, '%.*s'\n",
6005 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6006 args.id_len, args.id);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006007
Chuck Leveracdeb692012-05-21 22:46:16 -04006008 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006009 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04006010 if (unlikely(res.server_owner == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006011 status = -ENOMEM;
6012 goto out;
6013 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006014
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006015 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006016 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006017 if (unlikely(res.server_scope == NULL)) {
Benny Halevy99fe60d2009-04-01 09:22:29 -04006018 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04006019 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04006020 }
6021
Trond Myklebustbbafffd2012-05-24 16:31:39 -04006022 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04006023 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006024 status = -ENOMEM;
6025 goto out_server_scope;
6026 }
6027
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006028 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006029 trace_nfs4_exchange_id(clp, status);
Chuck Lever177313f2012-05-21 22:44:58 -04006030 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04006031 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006032
Chuck Lever177313f2012-05-21 22:44:58 -04006033 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04006034 clp->cl_clientid = res.clientid;
6035 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
6036 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
6037 clp->cl_seqid = res.seqid;
6038
Chuck Leveracdeb692012-05-21 22:46:16 -04006039 kfree(clp->cl_serverowner);
6040 clp->cl_serverowner = res.server_owner;
6041 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04006042
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006043 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04006044 kfree(clp->cl_implid);
6045 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006046
Chuck Lever177313f2012-05-21 22:44:58 -04006047 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006048 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006049 res.server_scope)) {
6050 dprintk("%s: server_scope mismatch detected\n",
6051 __func__);
6052 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006053 kfree(clp->cl_serverscope);
6054 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04006055 }
6056
Chuck Lever177313f2012-05-21 22:44:58 -04006057 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04006058 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006059 goto out;
6060 }
Trond Myklebust32b01312012-05-26 13:41:04 -04006061 } else
6062 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006063
Chuck Leveracdeb692012-05-21 22:46:16 -04006064out_server_owner:
6065 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006066out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05006067 kfree(res.server_scope);
6068out:
Chuck Lever177313f2012-05-21 22:44:58 -04006069 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006070 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05006071 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006072 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04006073 clp->cl_implid->date.seconds,
6074 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006075 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04006076 return status;
6077}
6078
Trond Myklebust66245532012-05-25 17:18:09 -04006079static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
6080 struct rpc_cred *cred)
6081{
6082 struct rpc_message msg = {
6083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
6084 .rpc_argp = clp,
6085 .rpc_cred = cred,
6086 };
6087 int status;
6088
6089 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006090 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04006091 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04006092 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04006093 "DESTROY_CLIENTID.", status, clp->cl_hostname);
6094 return status;
6095}
6096
6097static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
6098 struct rpc_cred *cred)
6099{
6100 unsigned int loop;
6101 int ret;
6102
6103 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
6104 ret = _nfs4_proc_destroy_clientid(clp, cred);
6105 switch (ret) {
6106 case -NFS4ERR_DELAY:
6107 case -NFS4ERR_CLIENTID_BUSY:
6108 ssleep(1);
6109 break;
6110 default:
6111 return ret;
6112 }
6113 }
6114 return 0;
6115}
6116
6117int nfs4_destroy_clientid(struct nfs_client *clp)
6118{
6119 struct rpc_cred *cred;
6120 int ret = 0;
6121
6122 if (clp->cl_mvops->minor_version < 1)
6123 goto out;
6124 if (clp->cl_exchange_flags == 0)
6125 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04006126 if (clp->cl_preserve_clid)
6127 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04006128 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04006129 ret = nfs4_proc_destroy_clientid(clp, cred);
6130 if (cred)
6131 put_rpccred(cred);
6132 switch (ret) {
6133 case 0:
6134 case -NFS4ERR_STALE_CLIENTID:
6135 clp->cl_exchange_flags = 0;
6136 }
6137out:
6138 return ret;
6139}
6140
Andy Adamson2050f0c2009-04-01 09:22:30 -04006141struct nfs4_get_lease_time_data {
6142 struct nfs4_get_lease_time_args *args;
6143 struct nfs4_get_lease_time_res *res;
6144 struct nfs_client *clp;
6145};
6146
6147static void nfs4_get_lease_time_prepare(struct rpc_task *task,
6148 void *calldata)
6149{
Andy Adamson2050f0c2009-04-01 09:22:30 -04006150 struct nfs4_get_lease_time_data *data =
6151 (struct nfs4_get_lease_time_data *)calldata;
6152
6153 dprintk("--> %s\n", __func__);
6154 /* just setup sequence, do not trigger session recovery
6155 since we're invoked within one */
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006156 nfs41_setup_sequence(data->clp->cl_session,
6157 &data->args->la_seq_args,
6158 &data->res->lr_seq_res,
6159 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006160 dprintk("<-- %s\n", __func__);
6161}
6162
6163/*
6164 * Called from nfs4_state_manager thread for session setup, so don't recover
6165 * from sequence operation or clientid errors.
6166 */
6167static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
6168{
6169 struct nfs4_get_lease_time_data *data =
6170 (struct nfs4_get_lease_time_data *)calldata;
6171
6172 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006173 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
6174 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04006175 switch (task->tk_status) {
6176 case -NFS4ERR_DELAY:
6177 case -NFS4ERR_GRACE:
6178 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
6179 rpc_delay(task, NFS4_POLL_RETRY_MIN);
6180 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006181 /* fall through */
6182 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006183 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006184 return;
6185 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04006186 dprintk("<-- %s\n", __func__);
6187}
6188
Trond Myklebust17280172012-03-11 13:11:00 -04006189static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04006190 .rpc_call_prepare = nfs4_get_lease_time_prepare,
6191 .rpc_call_done = nfs4_get_lease_time_done,
6192};
6193
6194int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
6195{
6196 struct rpc_task *task;
6197 struct nfs4_get_lease_time_args args;
6198 struct nfs4_get_lease_time_res res = {
6199 .lr_fsinfo = fsinfo,
6200 };
6201 struct nfs4_get_lease_time_data data = {
6202 .args = &args,
6203 .res = &res,
6204 .clp = clp,
6205 };
6206 struct rpc_message msg = {
6207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
6208 .rpc_argp = &args,
6209 .rpc_resp = &res,
6210 };
6211 struct rpc_task_setup task_setup = {
6212 .rpc_client = clp->cl_rpcclient,
6213 .rpc_message = &msg,
6214 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006215 .callback_data = &data,
6216 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04006217 };
6218 int status;
6219
Trond Myklebust9d12b212012-01-17 22:04:25 -05006220 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006221 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04006222 dprintk("--> %s\n", __func__);
6223 task = rpc_run_task(&task_setup);
6224
6225 if (IS_ERR(task))
6226 status = PTR_ERR(task);
6227 else {
6228 status = task->tk_status;
6229 rpc_put_task(task);
6230 }
6231 dprintk("<-- %s return %d\n", __func__, status);
6232
6233 return status;
6234}
6235
Andy Adamsonfc931582009-04-01 09:22:31 -04006236/*
6237 * Initialize the values to be used by the client in CREATE_SESSION
6238 * If nfs4_init_session set the fore channel request and response sizes,
6239 * use them.
6240 *
6241 * Set the back channel max_resp_sz_cached to zero to force the client to
6242 * always set csa_cachethis to FALSE because the current implementation
6243 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
6244 */
6245static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
6246{
Andy Adamson18aad3d2013-06-26 12:21:49 -04006247 unsigned int max_rqst_sz, max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006248
Andy Adamson18aad3d2013-06-26 12:21:49 -04006249 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
6250 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
6251
Andy Adamsonfc931582009-04-01 09:22:31 -04006252 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04006253 args->fc_attrs.max_rqst_sz = max_rqst_sz;
6254 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04006255 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05006256 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04006257
6258 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05006259 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04006260 __func__,
6261 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05006262 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04006263
6264 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04006265 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
6266 args->bc_attrs.max_resp_sz = PAGE_SIZE;
6267 args->bc_attrs.max_resp_sz_cached = 0;
6268 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
6269 args->bc_attrs.max_reqs = 1;
6270
6271 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
6272 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
6273 __func__,
6274 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
6275 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
6276 args->bc_attrs.max_reqs);
6277}
6278
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006279static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04006280{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006281 struct nfs4_channel_attrs *sent = &args->fc_attrs;
6282 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
6283
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006284 if (rcvd->max_resp_sz > sent->max_resp_sz)
6285 return -EINVAL;
6286 /*
6287 * Our requested max_ops is the minimum we need; we're not
6288 * prepared to break up compounds into smaller pieces than that.
6289 * So, no point even trying to continue if the server won't
6290 * cooperate:
6291 */
6292 if (rcvd->max_ops < sent->max_ops)
6293 return -EINVAL;
6294 if (rcvd->max_reqs == 0)
6295 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006296 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
6297 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006298 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04006299}
6300
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006301static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
6302{
6303 struct nfs4_channel_attrs *sent = &args->bc_attrs;
6304 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04006305
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006306 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
6307 return -EINVAL;
6308 if (rcvd->max_resp_sz < sent->max_resp_sz)
6309 return -EINVAL;
6310 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
6311 return -EINVAL;
6312 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006313 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006314 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04006315 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006316 return -EINVAL;
6317 return 0;
6318}
Andy Adamson8d353012009-04-01 09:22:32 -04006319
Andy Adamson8d353012009-04-01 09:22:32 -04006320static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
6321 struct nfs4_session *session)
6322{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006323 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04006324
J. Bruce Fields43c2e882010-10-02 15:19:01 -04006325 ret = nfs4_verify_fore_channel_attrs(args, session);
6326 if (ret)
6327 return ret;
6328 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04006329}
6330
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006331static int _nfs4_proc_create_session(struct nfs_client *clp,
6332 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006333{
6334 struct nfs4_session *session = clp->cl_session;
6335 struct nfs41_create_session_args args = {
6336 .client = clp,
6337 .cb_program = NFS4_CALLBACK,
6338 };
6339 struct nfs41_create_session_res res = {
6340 .client = clp,
6341 };
6342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
6344 .rpc_argp = &args,
6345 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006346 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04006347 };
6348 int status;
6349
6350 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04006351 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04006352
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006353 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006354 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006355
Trond Myklebust43095d32012-11-20 11:13:12 -05006356 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04006357 /* Verify the session's negotiated channel_attrs values */
6358 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04006359 /* Increment the clientid slot sequence id */
6360 clp->cl_seqid++;
6361 }
6362
6363 return status;
6364}
6365
6366/*
6367 * Issues a CREATE_SESSION operation to the server.
6368 * It is the responsibility of the caller to verify the session is
6369 * expired before calling this routine.
6370 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006371int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04006372{
6373 int status;
6374 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04006375 struct nfs4_session *session = clp->cl_session;
6376
6377 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
6378
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006379 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04006380 if (status)
6381 goto out;
6382
Andy Adamsonaacd5532011-11-09 13:58:21 -05006383 /* Init or reset the session slot tables */
6384 status = nfs4_setup_session_slot_tables(session);
6385 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04006386 if (status)
6387 goto out;
6388
6389 ptr = (unsigned *)&session->sess_id.data[0];
6390 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
6391 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04006392out:
6393 dprintk("<-- %s\n", __func__);
6394 return status;
6395}
6396
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006397/*
6398 * Issue the over-the-wire RPC DESTROY_SESSION.
6399 * The caller must serialize access to this routine.
6400 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006401int nfs4_proc_destroy_session(struct nfs4_session *session,
6402 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006403{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04006404 struct rpc_message msg = {
6405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
6406 .rpc_argp = session,
6407 .rpc_cred = cred,
6408 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006409 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006410
6411 dprintk("--> nfs4_proc_destroy_session\n");
6412
6413 /* session is still being setup */
6414 if (session->clp->cl_cons_state != NFS_CS_READY)
6415 return status;
6416
Trond Myklebust1bd714f2011-04-24 14:29:33 -04006417 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006418 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006419
6420 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04006421 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04006422 "Session has been destroyed regardless...\n", status);
6423
6424 dprintk("<-- nfs4_proc_destroy_session\n");
6425 return status;
6426}
6427
Trond Myklebust7b38c362012-05-23 13:23:31 -04006428/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006429 * Renew the cl_session lease.
6430 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006431struct nfs4_sequence_data {
6432 struct nfs_client *clp;
6433 struct nfs4_sequence_args args;
6434 struct nfs4_sequence_res res;
6435};
6436
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006437static void nfs41_sequence_release(void *data)
6438{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006439 struct nfs4_sequence_data *calldata = data;
6440 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006441
Alexandros Batsakis71358402010-02-05 03:45:05 -08006442 if (atomic_read(&clp->cl_count) > 1)
6443 nfs4_schedule_state_renewal(clp);
6444 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006445 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006446}
6447
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006448static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6449{
6450 switch(task->tk_status) {
6451 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
6453 return -EAGAIN;
6454 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006455 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006456 }
6457 return 0;
6458}
6459
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006460static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006461{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006462 struct nfs4_sequence_data *calldata = data;
6463 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006464
Trond Myklebust14516c32010-07-31 14:29:06 -04006465 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
6466 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006467
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006468 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006469 if (task->tk_status < 0) {
6470 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006471 if (atomic_read(&clp->cl_count) == 1)
6472 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006473
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006474 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
6475 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006476 return;
6477 }
6478 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006479 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08006480out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006481 dprintk("<-- %s\n", __func__);
6482}
6483
6484static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
6485{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006486 struct nfs4_sequence_data *calldata = data;
6487 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006488 struct nfs4_sequence_args *args;
6489 struct nfs4_sequence_res *res;
6490
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006491 args = task->tk_msg.rpc_argp;
6492 res = task->tk_msg.rpc_resp;
6493
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006494 nfs41_setup_sequence(clp->cl_session, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006495}
6496
6497static const struct rpc_call_ops nfs41_sequence_ops = {
6498 .rpc_call_done = nfs41_sequence_call_done,
6499 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08006500 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006501};
6502
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006503static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
6504 struct rpc_cred *cred,
6505 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006506{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006507 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006508 struct rpc_message msg = {
6509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
6510 .rpc_cred = cred,
6511 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006512 struct rpc_task_setup task_setup_data = {
6513 .rpc_client = clp->cl_rpcclient,
6514 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006515 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04006516 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006517 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006518
Alexandros Batsakis71358402010-02-05 03:45:05 -08006519 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006520 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04006521 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006522 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08006523 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006524 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006525 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05006526 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006527 if (is_privileged)
6528 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04006529 msg.rpc_argp = &calldata->args;
6530 msg.rpc_resp = &calldata->res;
6531 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006532 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006533
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006534 return rpc_run_task(&task_setup_data);
6535}
6536
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006537static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006538{
6539 struct rpc_task *task;
6540 int ret = 0;
6541
Trond Myklebust2f60ea62011-08-24 15:07:37 -04006542 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
6543 return 0;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006544 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006545 if (IS_ERR(task))
6546 ret = PTR_ERR(task);
6547 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08006548 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006549 dprintk("<-- %s status=%d\n", __func__, ret);
6550 return ret;
6551}
6552
6553static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
6554{
6555 struct rpc_task *task;
6556 int ret;
6557
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006558 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006559 if (IS_ERR(task)) {
6560 ret = PTR_ERR(task);
6561 goto out;
6562 }
6563 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05006564 if (!ret) {
6565 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
6566
6567 if (task->tk_status == 0)
6568 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006569 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05006570 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04006571 rpc_put_task(task);
6572out:
6573 dprintk("<-- %s status=%d\n", __func__, ret);
6574 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04006575}
6576
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006577struct nfs4_reclaim_complete_data {
6578 struct nfs_client *clp;
6579 struct nfs41_reclaim_complete_args arg;
6580 struct nfs41_reclaim_complete_res res;
6581};
6582
6583static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
6584{
6585 struct nfs4_reclaim_complete_data *calldata = data;
6586
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006587 nfs41_setup_sequence(calldata->clp->cl_session,
6588 &calldata->arg.seq_args,
6589 &calldata->res.seq_res,
6590 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006591}
6592
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006593static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
6594{
6595 switch(task->tk_status) {
6596 case 0:
6597 case -NFS4ERR_COMPLETE_ALREADY:
6598 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6599 break;
6600 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006601 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006602 /* fall through */
6603 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006604 return -EAGAIN;
6605 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006606 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006607 }
6608 return 0;
6609}
6610
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006611static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6612{
6613 struct nfs4_reclaim_complete_data *calldata = data;
6614 struct nfs_client *clp = calldata->clp;
6615 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6616
6617 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006618 if (!nfs41_sequence_done(task, res))
6619 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006620
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006621 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006622 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6623 rpc_restart_call_prepare(task);
6624 return;
6625 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006626 dprintk("<-- %s\n", __func__);
6627}
6628
6629static void nfs4_free_reclaim_complete_data(void *data)
6630{
6631 struct nfs4_reclaim_complete_data *calldata = data;
6632
6633 kfree(calldata);
6634}
6635
6636static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6637 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6638 .rpc_call_done = nfs4_reclaim_complete_done,
6639 .rpc_release = nfs4_free_reclaim_complete_data,
6640};
6641
6642/*
6643 * Issue a global reclaim complete.
6644 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04006645static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
6646 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006647{
6648 struct nfs4_reclaim_complete_data *calldata;
6649 struct rpc_task *task;
6650 struct rpc_message msg = {
6651 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04006652 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006653 };
6654 struct rpc_task_setup task_setup_data = {
6655 .rpc_client = clp->cl_rpcclient,
6656 .rpc_message = &msg,
6657 .callback_ops = &nfs4_reclaim_complete_call_ops,
6658 .flags = RPC_TASK_ASYNC,
6659 };
6660 int status = -ENOMEM;
6661
6662 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006663 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006664 if (calldata == NULL)
6665 goto out;
6666 calldata->clp = clp;
6667 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006668
Trond Myklebust9d12b212012-01-17 22:04:25 -05006669 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006670 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006671 msg.rpc_argp = &calldata->arg;
6672 msg.rpc_resp = &calldata->res;
6673 task_setup_data.callback_data = calldata;
6674 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006675 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006676 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006677 goto out;
6678 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006679 status = nfs4_wait_for_completion_rpc_task(task);
6680 if (status == 0)
6681 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006682 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006683 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006684out:
6685 dprintk("<-- %s status=%d\n", __func__, status);
6686 return status;
6687}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006688
6689static void
6690nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6691{
6692 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006693 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006694 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006695
6696 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006697 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6698 * right now covering the LAYOUTGET we are about to send.
6699 * However, that is not so catastrophic, and there seems
6700 * to be no way to prevent it completely.
6701 */
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006702 if (nfs41_setup_sequence(session, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006703 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006704 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006705 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6706 NFS_I(lgp->args.inode)->layout,
6707 lgp->args.ctx->state)) {
6708 rpc_exit(task, NFS4_OK);
Fred Isamancf7d63f2011-01-06 11:36:25 +00006709 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006710}
6711
6712static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6713{
6714 struct nfs4_layoutget *lgp = calldata;
Trond Myklebustee314c22012-10-01 17:25:48 -07006715 struct inode *inode = lgp->args.inode;
6716 struct nfs_server *server = NFS_SERVER(inode);
6717 struct pnfs_layout_hdr *lo;
6718 struct nfs4_state *state = NULL;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006719 unsigned long timeo, giveup;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006720
6721 dprintk("--> %s\n", __func__);
6722
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006723 if (!nfs41_sequence_done(task, &lgp->res.seq_res))
Trond Myklebustee314c22012-10-01 17:25:48 -07006724 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006725
6726 switch (task->tk_status) {
6727 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07006728 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006729 case -NFS4ERR_LAYOUTTRYLATER:
6730 case -NFS4ERR_RECALLCONFLICT:
Weston Andros Adamson30005122013-02-28 20:30:10 -05006731 timeo = rpc_get_timeout(task->tk_client);
6732 giveup = lgp->args.timestamp + timeo;
6733 if (time_after(giveup, jiffies))
6734 task->tk_status = -NFS4ERR_DELAY;
Trond Myklebustee314c22012-10-01 17:25:48 -07006735 break;
6736 case -NFS4ERR_EXPIRED:
6737 case -NFS4ERR_BAD_STATEID:
6738 spin_lock(&inode->i_lock);
6739 lo = NFS_I(inode)->layout;
6740 if (!lo || list_empty(&lo->plh_segs)) {
6741 spin_unlock(&inode->i_lock);
6742 /* If the open stateid was bad, then recover it. */
6743 state = lgp->args.ctx->state;
6744 } else {
6745 LIST_HEAD(head);
6746
6747 pnfs_mark_matching_lsegs_invalid(lo, &head, NULL);
6748 spin_unlock(&inode->i_lock);
6749 /* Mark the bad layout state as invalid, then
6750 * retry using the open stateid. */
6751 pnfs_free_lseg_list(&head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006752 }
6753 }
Trond Myklebustee314c22012-10-01 17:25:48 -07006754 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
6755 rpc_restart_call_prepare(task);
6756out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006757 dprintk("<-- %s\n", __func__);
6758}
6759
Idan Kedar85541162012-08-02 11:47:10 +03006760static size_t max_response_pages(struct nfs_server *server)
6761{
6762 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
6763 return nfs_page_array_len(0, max_resp_sz);
6764}
6765
6766static void nfs4_free_pages(struct page **pages, size_t size)
6767{
6768 int i;
6769
6770 if (!pages)
6771 return;
6772
6773 for (i = 0; i < size; i++) {
6774 if (!pages[i])
6775 break;
6776 __free_page(pages[i]);
6777 }
6778 kfree(pages);
6779}
6780
6781static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
6782{
6783 struct page **pages;
6784 int i;
6785
6786 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
6787 if (!pages) {
6788 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
6789 return NULL;
6790 }
6791
6792 for (i = 0; i < size; i++) {
6793 pages[i] = alloc_page(gfp_flags);
6794 if (!pages[i]) {
6795 dprintk("%s: failed to allocate page\n", __func__);
6796 nfs4_free_pages(pages, size);
6797 return NULL;
6798 }
6799 }
6800
6801 return pages;
6802}
6803
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006804static void nfs4_layoutget_release(void *calldata)
6805{
6806 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006807 struct inode *inode = lgp->args.inode;
6808 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006809 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006810
6811 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03006812 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006813 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006814 put_nfs_open_context(lgp->args.ctx);
6815 kfree(calldata);
6816 dprintk("<-- %s\n", __func__);
6817}
6818
6819static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6820 .rpc_call_prepare = nfs4_layoutget_prepare,
6821 .rpc_call_done = nfs4_layoutget_done,
6822 .rpc_release = nfs4_layoutget_release,
6823};
6824
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006825struct pnfs_layout_segment *
6826nfs4_proc_layoutget(struct nfs4_layoutget *lgp, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006827{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006828 struct inode *inode = lgp->args.inode;
6829 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03006830 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006831 struct rpc_task *task;
6832 struct rpc_message msg = {
6833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6834 .rpc_argp = &lgp->args,
6835 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04006836 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006837 };
6838 struct rpc_task_setup task_setup_data = {
6839 .rpc_client = server->client,
6840 .rpc_message = &msg,
6841 .callback_ops = &nfs4_layoutget_call_ops,
6842 .callback_data = lgp,
6843 .flags = RPC_TASK_ASYNC,
6844 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006845 struct pnfs_layout_segment *lseg = NULL;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006846 int status = 0;
6847
6848 dprintk("--> %s\n", __func__);
6849
Idan Kedar85541162012-08-02 11:47:10 +03006850 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
6851 if (!lgp->args.layout.pages) {
6852 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006853 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03006854 }
6855 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
Weston Andros Adamson30005122013-02-28 20:30:10 -05006856 lgp->args.timestamp = jiffies;
Idan Kedar85541162012-08-02 11:47:10 +03006857
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006858 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006859 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006860 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05006861
6862 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
6863 pnfs_get_layout_hdr(NFS_I(inode)->layout);
6864
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006865 task = rpc_run_task(&task_setup_data);
6866 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006867 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006868 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006869 if (status == 0)
6870 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006871 trace_nfs4_layoutget(lgp->args.ctx,
6872 &lgp->args.range,
6873 &lgp->res.range,
6874 status);
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05006875 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
6876 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006877 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006878 rpc_put_task(task);
6879 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04006880 if (status)
6881 return ERR_PTR(status);
6882 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006883}
6884
Benny Halevycbe82602011-05-22 19:52:37 +03006885static void
6886nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6887{
6888 struct nfs4_layoutreturn *lrp = calldata;
6889
6890 dprintk("--> %s\n", __func__);
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006891 nfs41_setup_sequence(lrp->clp->cl_session,
6892 &lrp->args.seq_args,
6893 &lrp->res.seq_res,
6894 task);
Benny Halevycbe82602011-05-22 19:52:37 +03006895}
6896
6897static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6898{
6899 struct nfs4_layoutreturn *lrp = calldata;
6900 struct nfs_server *server;
6901
6902 dprintk("--> %s\n", __func__);
6903
Trond Myklebust6ba7db32012-10-22 20:07:20 -04006904 if (!nfs41_sequence_done(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03006905 return;
6906
6907 server = NFS_SERVER(lrp->args.inode);
6908 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006909 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006910 return;
6911 }
Benny Halevycbe82602011-05-22 19:52:37 +03006912 dprintk("<-- %s\n", __func__);
6913}
6914
6915static void nfs4_layoutreturn_release(void *calldata)
6916{
6917 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04006918 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006919
6920 dprintk("--> %s\n", __func__);
Trond Myklebust849b2862012-09-24 14:18:39 -04006921 spin_lock(&lo->plh_inode->i_lock);
6922 if (lrp->res.lrs_present)
6923 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
6924 lo->plh_block_lgets--;
6925 spin_unlock(&lo->plh_inode->i_lock);
Trond Myklebust70c3bd22012-09-18 20:51:13 -04006926 pnfs_put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006927 kfree(calldata);
6928 dprintk("<-- %s\n", __func__);
6929}
6930
6931static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6932 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6933 .rpc_call_done = nfs4_layoutreturn_done,
6934 .rpc_release = nfs4_layoutreturn_release,
6935};
6936
6937int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6938{
6939 struct rpc_task *task;
6940 struct rpc_message msg = {
6941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6942 .rpc_argp = &lrp->args,
6943 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04006944 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03006945 };
6946 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04006947 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03006948 .rpc_message = &msg,
6949 .callback_ops = &nfs4_layoutreturn_call_ops,
6950 .callback_data = lrp,
6951 };
6952 int status;
6953
6954 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006955 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006956 task = rpc_run_task(&task_setup_data);
6957 if (IS_ERR(task))
6958 return PTR_ERR(task);
6959 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04006960 trace_nfs4_layoutreturn(lrp->args.inode, status);
Benny Halevycbe82602011-05-22 19:52:37 +03006961 dprintk("<-- %s status=%d\n", __func__, status);
6962 rpc_put_task(task);
6963 return status;
6964}
6965
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006966/*
6967 * Retrieve the list of Data Server devices from the MDS.
6968 */
6969static int _nfs4_getdevicelist(struct nfs_server *server,
6970 const struct nfs_fh *fh,
6971 struct pnfs_devicelist *devlist)
6972{
6973 struct nfs4_getdevicelist_args args = {
6974 .fh = fh,
6975 .layoutclass = server->pnfs_curr_ld->id,
6976 };
6977 struct nfs4_getdevicelist_res res = {
6978 .devlist = devlist,
6979 };
6980 struct rpc_message msg = {
6981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6982 .rpc_argp = &args,
6983 .rpc_resp = &res,
6984 };
6985 int status;
6986
6987 dprintk("--> %s\n", __func__);
6988 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6989 &res.seq_res, 0);
6990 dprintk("<-- %s status=%d\n", __func__, status);
6991 return status;
6992}
6993
6994int nfs4_proc_getdevicelist(struct nfs_server *server,
6995 const struct nfs_fh *fh,
6996 struct pnfs_devicelist *devlist)
6997{
6998 struct nfs4_exception exception = { };
6999 int err;
7000
7001 do {
7002 err = nfs4_handle_exception(server,
7003 _nfs4_getdevicelist(server, fh, devlist),
7004 &exception);
7005 } while (exception.retry);
7006
7007 dprintk("%s: err=%d, num_devs=%u\n", __func__,
7008 err, devlist->num_devs);
7009
7010 return err;
7011}
7012EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
7013
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007014static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007015_nfs4_proc_getdeviceinfo(struct nfs_server *server,
7016 struct pnfs_device *pdev,
7017 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007018{
7019 struct nfs4_getdeviceinfo_args args = {
7020 .pdev = pdev,
7021 };
7022 struct nfs4_getdeviceinfo_res res = {
7023 .pdev = pdev,
7024 };
7025 struct rpc_message msg = {
7026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
7027 .rpc_argp = &args,
7028 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007029 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007030 };
7031 int status;
7032
7033 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00007034 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007035 dprintk("<-- %s status=%d\n", __func__, status);
7036
7037 return status;
7038}
7039
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007040int nfs4_proc_getdeviceinfo(struct nfs_server *server,
7041 struct pnfs_device *pdev,
7042 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007043{
7044 struct nfs4_exception exception = { };
7045 int err;
7046
7047 do {
7048 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04007049 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04007050 &exception);
7051 } while (exception.retry);
7052 return err;
7053}
7054EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
7055
Andy Adamson863a3c62011-03-23 13:27:54 +00007056static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
7057{
7058 struct nfs4_layoutcommit_data *data = calldata;
7059 struct nfs_server *server = NFS_SERVER(data->args.inode);
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007060 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamson863a3c62011-03-23 13:27:54 +00007061
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007062 nfs41_setup_sequence(session,
7063 &data->args.seq_args,
7064 &data->res.seq_res,
7065 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00007066}
7067
7068static void
7069nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
7070{
7071 struct nfs4_layoutcommit_data *data = calldata;
7072 struct nfs_server *server = NFS_SERVER(data->args.inode);
7073
Trond Myklebust6ba7db32012-10-22 20:07:20 -04007074 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00007075 return;
7076
7077 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007078 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
7079 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
7080 case -NFS4ERR_BADLAYOUT: /* no layout */
7081 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00007082 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007083 break;
7084 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00007085 nfs_post_op_update_inode_force_wcc(data->args.inode,
7086 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04007087 break;
7088 default:
7089 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
7090 rpc_restart_call_prepare(task);
7091 return;
7092 }
7093 }
Andy Adamson863a3c62011-03-23 13:27:54 +00007094}
7095
7096static void nfs4_layoutcommit_release(void *calldata)
7097{
7098 struct nfs4_layoutcommit_data *data = calldata;
7099
Andy Adamsondb29c082011-07-30 20:52:38 -04007100 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00007101 put_rpccred(data->cred);
7102 kfree(data);
7103}
7104
7105static const struct rpc_call_ops nfs4_layoutcommit_ops = {
7106 .rpc_call_prepare = nfs4_layoutcommit_prepare,
7107 .rpc_call_done = nfs4_layoutcommit_done,
7108 .rpc_release = nfs4_layoutcommit_release,
7109};
7110
7111int
Andy Adamsonef311532011-03-12 02:58:10 -05007112nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00007113{
7114 struct rpc_message msg = {
7115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
7116 .rpc_argp = &data->args,
7117 .rpc_resp = &data->res,
7118 .rpc_cred = data->cred,
7119 };
7120 struct rpc_task_setup task_setup_data = {
7121 .task = &data->task,
7122 .rpc_client = NFS_CLIENT(data->args.inode),
7123 .rpc_message = &msg,
7124 .callback_ops = &nfs4_layoutcommit_ops,
7125 .callback_data = data,
7126 .flags = RPC_TASK_ASYNC,
7127 };
7128 struct rpc_task *task;
7129 int status = 0;
7130
7131 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
7132 "lbw: %llu inode %lu\n",
7133 data->task.tk_pid, sync,
7134 data->args.lastbytewritten,
7135 data->args.inode->i_ino);
7136
Trond Myklebust9d12b212012-01-17 22:04:25 -05007137 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00007138 task = rpc_run_task(&task_setup_data);
7139 if (IS_ERR(task))
7140 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05007141 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00007142 goto out;
7143 status = nfs4_wait_for_completion_rpc_task(task);
7144 if (status != 0)
7145 goto out;
7146 status = task->tk_status;
Trond Myklebust1037e6e2013-08-14 16:36:51 -04007147 trace_nfs4_layoutcommit(data->args.inode, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00007148out:
7149 dprintk("%s: status %d\n", __func__, status);
7150 rpc_put_task(task);
7151 return status;
7152}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007153
Andy Adamson97431202013-08-08 10:57:56 -04007154/**
7155 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
7156 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
7157 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007158static int
7159_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7160 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7161{
7162 struct nfs41_secinfo_no_name_args args = {
7163 .style = SECINFO_STYLE_CURRENT_FH,
7164 };
7165 struct nfs4_secinfo_res res = {
7166 .flavors = flavors,
7167 };
7168 struct rpc_message msg = {
7169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
7170 .rpc_argp = &args,
7171 .rpc_resp = &res,
7172 };
Andy Adamson97431202013-08-08 10:57:56 -04007173 return nfs4_call_sync(server->nfs_client->cl_rpcclient, server, &msg,
7174 &args.seq_args, &res.seq_res, 0);
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007175}
7176
7177static int
7178nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
7179 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
7180{
7181 struct nfs4_exception exception = { };
7182 int err;
7183 do {
7184 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7185 switch (err) {
7186 case 0:
7187 case -NFS4ERR_WRONGSEC:
7188 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007189 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007190 default:
7191 err = nfs4_handle_exception(server, err, &exception);
7192 }
7193 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04007194out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007195 return err;
7196}
7197
7198static int
7199nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
7200 struct nfs_fsinfo *info)
7201{
7202 int err;
7203 struct page *page;
7204 rpc_authflavor_t flavor;
7205 struct nfs4_secinfo_flavors *flavors;
7206
7207 page = alloc_page(GFP_KERNEL);
7208 if (!page) {
7209 err = -ENOMEM;
7210 goto out;
7211 }
7212
7213 flavors = page_address(page);
7214 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
7215
7216 /*
7217 * Fall back on "guess and check" method if
7218 * the server doesn't support SECINFO_NO_NAME
7219 */
7220 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
7221 err = nfs4_find_root_sec(server, fhandle, info);
7222 goto out_freepage;
7223 }
7224 if (err)
7225 goto out_freepage;
7226
7227 flavor = nfs_find_best_sec(flavors);
7228 if (err == 0)
7229 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
7230
7231out_freepage:
7232 put_page(page);
7233 if (err == -EACCES)
7234 return -EPERM;
7235out:
7236 return err;
7237}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007238
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007239static int _nfs41_test_stateid(struct nfs_server *server,
7240 nfs4_stateid *stateid,
7241 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007242{
7243 int status;
7244 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007245 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007246 };
7247 struct nfs41_test_stateid_res res;
7248 struct rpc_message msg = {
7249 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
7250 .rpc_argp = &args,
7251 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007252 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04007253 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05007254
Chuck Lever38527b12012-07-11 16:30:23 -04007255 dprintk("NFS call test_stateid %p\n", stateid);
Trond Myklebust9d12b212012-01-17 22:04:25 -05007256 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007257 nfs4_set_sequence_privileged(&args.seq_args);
7258 status = nfs4_call_sync_sequence(server->client, server, &msg,
7259 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04007260 if (status != NFS_OK) {
7261 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04007262 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04007263 }
7264 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04007265 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04007266}
7267
Chuck Lever38527b12012-07-11 16:30:23 -04007268/**
7269 * nfs41_test_stateid - perform a TEST_STATEID operation
7270 *
7271 * @server: server / transport on which to perform the operation
7272 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007273 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007274 *
7275 * Returns NFS_OK if the server recognizes that "stateid" is valid.
7276 * Otherwise a negative NFS4ERR value is returned if the operation
7277 * failed or the state ID is not currently valid.
7278 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007279static int nfs41_test_stateid(struct nfs_server *server,
7280 nfs4_stateid *stateid,
7281 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04007282{
7283 struct nfs4_exception exception = { };
7284 int err;
7285 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007286 err = _nfs41_test_stateid(server, stateid, cred);
Chuck Lever377e5072012-07-11 16:29:45 -04007287 if (err != -NFS4ERR_DELAY)
7288 break;
7289 nfs4_handle_exception(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04007290 } while (exception.retry);
7291 return err;
7292}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007293
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007294struct nfs_free_stateid_data {
7295 struct nfs_server *server;
7296 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007297 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007298};
7299
7300static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
7301{
7302 struct nfs_free_stateid_data *data = calldata;
7303 nfs41_setup_sequence(nfs4_get_session(data->server),
7304 &data->args.seq_args,
7305 &data->res.seq_res,
7306 task);
7307}
7308
7309static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
7310{
7311 struct nfs_free_stateid_data *data = calldata;
7312
7313 nfs41_sequence_done(task, &data->res.seq_res);
7314
7315 switch (task->tk_status) {
7316 case -NFS4ERR_DELAY:
7317 if (nfs4_async_handle_error(task, data->server, NULL) == -EAGAIN)
7318 rpc_restart_call_prepare(task);
7319 }
7320}
7321
7322static void nfs41_free_stateid_release(void *calldata)
7323{
7324 kfree(calldata);
7325}
7326
Trond Myklebust17f26b12013-08-21 15:48:42 -04007327static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007328 .rpc_call_prepare = nfs41_free_stateid_prepare,
7329 .rpc_call_done = nfs41_free_stateid_done,
7330 .rpc_release = nfs41_free_stateid_release,
7331};
7332
7333static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
7334 nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007335 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007336 bool privileged)
7337{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007340 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007341 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007342 struct rpc_task_setup task_setup = {
7343 .rpc_client = server->client,
7344 .rpc_message = &msg,
7345 .callback_ops = &nfs41_free_stateid_ops,
7346 .flags = RPC_TASK_ASYNC,
7347 };
7348 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007349
Chuck Lever38527b12012-07-11 16:30:23 -04007350 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007351 data = kmalloc(sizeof(*data), GFP_NOFS);
7352 if (!data)
7353 return ERR_PTR(-ENOMEM);
7354 data->server = server;
7355 nfs4_stateid_copy(&data->args.stateid, stateid);
7356
7357 task_setup.callback_data = data;
7358
7359 msg.rpc_argp = &data->args;
7360 msg.rpc_resp = &data->res;
7361 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
7362 if (privileged)
7363 nfs4_set_sequence_privileged(&data->args.seq_args);
7364
7365 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007366}
7367
Chuck Lever38527b12012-07-11 16:30:23 -04007368/**
7369 * nfs41_free_stateid - perform a FREE_STATEID operation
7370 *
7371 * @server: server / transport on which to perform the operation
7372 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007373 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04007374 *
7375 * Returns NFS_OK if the server freed "stateid". Otherwise a
7376 * negative NFS4ERR value is returned.
7377 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007378static int nfs41_free_stateid(struct nfs_server *server,
7379 nfs4_stateid *stateid,
7380 struct rpc_cred *cred)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007381{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007382 struct rpc_task *task;
7383 int ret;
7384
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007385 task = _nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04007386 if (IS_ERR(task))
7387 return PTR_ERR(task);
7388 ret = rpc_wait_for_completion_task(task);
7389 if (!ret)
7390 ret = task->tk_status;
7391 rpc_put_task(task);
7392 return ret;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04007393}
Trond Myklebust36281ca2012-03-04 18:13:56 -05007394
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007395static int nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
7396{
7397 struct rpc_task *task;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007398 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007399
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04007400 task = _nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007401 nfs4_free_lock_state(server, lsp);
7402 if (IS_ERR(task))
7403 return PTR_ERR(task);
7404 rpc_put_task(task);
7405 return 0;
7406}
7407
Trond Myklebust36281ca2012-03-04 18:13:56 -05007408static bool nfs41_match_stateid(const nfs4_stateid *s1,
7409 const nfs4_stateid *s2)
7410{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007411 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007412 return false;
7413
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007414 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007415 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05007416 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05007417 return true;
7418
7419 return false;
7420}
7421
Andy Adamson557134a2009-04-01 09:21:53 -04007422#endif /* CONFIG_NFS_V4_1 */
7423
Trond Myklebust36281ca2012-03-04 18:13:56 -05007424static bool nfs4_match_stateid(const nfs4_stateid *s1,
7425 const nfs4_stateid *s2)
7426{
Trond Myklebustf597c532012-03-04 18:13:56 -05007427 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05007428}
7429
7430
Trond Myklebust17280172012-03-11 13:11:00 -04007431static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 .recover_open = nfs4_open_reclaim,
7435 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04007436 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04007437 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438};
7439
Andy Adamson591d71c2009-04-01 09:22:47 -04007440#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007441static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007442 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
7443 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
7444 .recover_open = nfs4_open_reclaim,
7445 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05007446 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05007447 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04007448 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04007449};
7450#endif /* CONFIG_NFS_V4_1 */
7451
Trond Myklebust17280172012-03-11 13:11:00 -04007452static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05007453 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05007454 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007455 .recover_open = nfs4_open_expired,
7456 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04007457 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007458};
7459
Andy Adamson591d71c2009-04-01 09:22:47 -04007460#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007461static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04007462 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
7463 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007464 .recover_open = nfs41_open_expired,
7465 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05007466 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04007467};
7468#endif /* CONFIG_NFS_V4_1 */
7469
Trond Myklebust17280172012-03-11 13:11:00 -04007470static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007471 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04007472 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007473 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04007474};
7475
7476#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04007477static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04007478 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04007479 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04007480 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04007481};
7482#endif
7483
Trond Myklebust97dc1352010-06-16 09:52:26 -04007484static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
7485 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007486 .init_caps = NFS_CAP_READDIRPLUS
7487 | NFS_CAP_ATOMIC_OPEN
7488 | NFS_CAP_CHANGE_ATTR
7489 | NFS_CAP_POSIX_LOCK,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007490 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007491 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007492 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007493 .free_lock_state = nfs4_release_lockowner,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007494 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
7495 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
7496 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007497};
7498
7499#if defined(CONFIG_NFS_V4_1)
7500static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
7501 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04007502 .init_caps = NFS_CAP_READDIRPLUS
7503 | NFS_CAP_ATOMIC_OPEN
7504 | NFS_CAP_CHANGE_ATTR
Trond Myklebust3b664862013-03-17 15:31:15 -04007505 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04007506 | NFS_CAP_STATEID_NFSV41
7507 | NFS_CAP_ATOMIC_OPEN_V1,
Bryan Schumaker104287c2012-11-12 14:13:13 -05007508 .call_sync = nfs4_call_sync_sequence,
Trond Myklebust36281ca2012-03-04 18:13:56 -05007509 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04007510 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04007511 .free_lock_state = nfs41_free_lock_state,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04007512 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7513 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7514 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04007515};
7516#endif
7517
Steve Dickson42c2c422013-05-22 12:50:38 -04007518#if defined(CONFIG_NFS_V4_2)
7519static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
7520 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04007521 .init_caps = NFS_CAP_READDIRPLUS
7522 | NFS_CAP_ATOMIC_OPEN
7523 | NFS_CAP_CHANGE_ATTR
7524 | NFS_CAP_POSIX_LOCK
7525 | NFS_CAP_STATEID_NFSV41
7526 | NFS_CAP_ATOMIC_OPEN_V1,
Steve Dickson42c2c422013-05-22 12:50:38 -04007527 .call_sync = nfs4_call_sync_sequence,
7528 .match_stateid = nfs41_match_stateid,
7529 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04007530 .free_lock_state = nfs41_free_lock_state,
Steve Dickson42c2c422013-05-22 12:50:38 -04007531 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
7532 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
7533 .state_renewal_ops = &nfs41_state_renewal_ops,
7534};
7535#endif
7536
Trond Myklebust97dc1352010-06-16 09:52:26 -04007537const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
7538 [0] = &nfs_v4_0_minor_ops,
7539#if defined(CONFIG_NFS_V4_1)
7540 [1] = &nfs_v4_1_minor_ops,
7541#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04007542#if defined(CONFIG_NFS_V4_2)
7543 [2] = &nfs_v4_2_minor_ops,
7544#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04007545};
7546
Trond Myklebust17f26b12013-08-21 15:48:42 -04007547static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04007548 .create = nfs_create,
7549 .lookup = nfs_lookup,
7550 .atomic_open = nfs_atomic_open,
7551 .link = nfs_link,
7552 .unlink = nfs_unlink,
7553 .symlink = nfs_symlink,
7554 .mkdir = nfs_mkdir,
7555 .rmdir = nfs_rmdir,
7556 .mknod = nfs_mknod,
7557 .rename = nfs_rename,
7558 .permission = nfs_permission,
7559 .getattr = nfs_getattr,
7560 .setattr = nfs_setattr,
7561 .getxattr = generic_getxattr,
7562 .setxattr = generic_setxattr,
7563 .listxattr = generic_listxattr,
7564 .removexattr = generic_removexattr,
7565};
7566
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08007567static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007568 .permission = nfs_permission,
7569 .getattr = nfs_getattr,
7570 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007571 .getxattr = generic_getxattr,
7572 .setxattr = generic_setxattr,
7573 .listxattr = generic_listxattr,
7574 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007575};
7576
David Howells509de812006-08-22 20:06:11 -04007577const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007578 .version = 4, /* protocol version */
7579 .dentry_ops = &nfs4_dentry_operations,
7580 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007581 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04007582 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007583 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04007584 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04007585 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007586 .getattr = nfs4_proc_getattr,
7587 .setattr = nfs4_proc_setattr,
7588 .lookup = nfs4_proc_lookup,
7589 .access = nfs4_proc_access,
7590 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007591 .create = nfs4_proc_create,
7592 .remove = nfs4_proc_remove,
7593 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04007594 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007595 .unlink_done = nfs4_proc_unlink_done,
7596 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04007597 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04007598 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04007599 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007600 .link = nfs4_proc_link,
7601 .symlink = nfs4_proc_symlink,
7602 .mkdir = nfs4_proc_mkdir,
7603 .rmdir = nfs4_proc_remove,
7604 .readdir = nfs4_proc_readdir,
7605 .mknod = nfs4_proc_mknod,
7606 .statfs = nfs4_proc_statfs,
7607 .fsinfo = nfs4_proc_fsinfo,
7608 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04007609 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007610 .decode_dirent = nfs4_decode_dirent,
7611 .read_setup = nfs4_proc_read_setup,
Bryan Schumaker1abb50882012-06-20 15:53:47 -04007612 .read_pageio_init = pnfs_pageio_init_read,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04007613 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05007614 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007615 .write_setup = nfs4_proc_write_setup,
Bryan Schumaker57208fa2012-06-20 15:53:48 -04007616 .write_pageio_init = pnfs_pageio_init_write,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04007617 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007618 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04007620 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05007621 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007622 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00007623 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04007624 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04007625 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04007626 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04007627 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04007628 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00007629 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04007630 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04007631 .create_server = nfs4_create_server,
7632 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633};
7634
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007635static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
7636 .prefix = XATTR_NAME_NFSV4_ACL,
7637 .list = nfs4_xattr_list_nfs4_acl,
7638 .get = nfs4_xattr_get_nfs4_acl,
7639 .set = nfs4_xattr_set_nfs4_acl,
7640};
7641
7642const struct xattr_handler *nfs4_xattr_handlers[] = {
7643 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04007644#ifdef CONFIG_NFS_V4_SECURITY_LABEL
7645 &nfs4_xattr_nfs4_label_handler,
7646#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00007647 NULL
7648};
7649
Linus Torvalds1da177e2005-04-16 15:20:36 -07007650/*
7651 * Local variables:
7652 * c-basic-offset: 8
7653 * End:
7654 */