blob: af2db2c49c5fb24367e86e272a4fb32124e71d1e [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>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Trond Myklebust6926afd2012-01-07 13:22:46 -050055#include <linux/nfs_idmap.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040056#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000057#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050058#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010059#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebust4ce79712005-06-22 17:16:21 +000061#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050063#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050064#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040065#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040066#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040067#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69#define NFSDBG_FACILITY NFSDBG_PROC
70
Trond Myklebust2066fe82006-09-15 08:30:46 -040071#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFS4_POLL_RETRY_MAX (15*HZ)
73
Trond Myklebusta78cb572009-08-09 15:06:19 -040074#define NFS4_MAX_LOOP_ON_RECOVER (10)
75
Trond Myklebustef159e92012-02-06 19:50:40 -050076static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
77
Trond Myklebustcdd4e682006-01-03 09:55:12 +010078struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010079static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080080static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050082static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050083static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040084static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040085static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040086static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
87 struct nfs_fattr *fattr, struct iattr *sattr,
88 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040089#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050090static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
91static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050094static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Trond Myklebust52567b02009-10-23 14:46:42 -040096 if (err >= -1000)
97 return err;
98 switch (err) {
99 case -NFS4ERR_RESOURCE:
100 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000101 case -NFS4ERR_WRONGSEC:
102 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800103 case -NFS4ERR_BADOWNER:
104 case -NFS4ERR_BADNAME:
105 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400106 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700108 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400111 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
114/*
115 * This is our standard bitmap for GETATTR requests.
116 */
117const u32 nfs4_fattr_bitmap[2] = {
118 FATTR4_WORD0_TYPE
119 | FATTR4_WORD0_CHANGE
120 | FATTR4_WORD0_SIZE
121 | FATTR4_WORD0_FSID
122 | FATTR4_WORD0_FILEID,
123 FATTR4_WORD1_MODE
124 | FATTR4_WORD1_NUMLINKS
125 | FATTR4_WORD1_OWNER
126 | FATTR4_WORD1_OWNER_GROUP
127 | FATTR4_WORD1_RAWDEV
128 | FATTR4_WORD1_SPACE_USED
129 | FATTR4_WORD1_TIME_ACCESS
130 | FATTR4_WORD1_TIME_METADATA
131 | FATTR4_WORD1_TIME_MODIFY
132};
133
134const u32 nfs4_statfs_bitmap[2] = {
135 FATTR4_WORD0_FILES_AVAIL
136 | FATTR4_WORD0_FILES_FREE
137 | FATTR4_WORD0_FILES_TOTAL,
138 FATTR4_WORD1_SPACE_AVAIL
139 | FATTR4_WORD1_SPACE_FREE
140 | FATTR4_WORD1_SPACE_TOTAL
141};
142
Trond Myklebust4ce79712005-06-22 17:16:21 +0000143const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 FATTR4_WORD0_MAXLINK
145 | FATTR4_WORD0_MAXNAME,
146 0
147};
148
Fred Isamandae100c2011-07-30 20:52:37 -0400149const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 | FATTR4_WORD0_MAXREAD
151 | FATTR4_WORD0_MAXWRITE
152 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700153 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400154 | FATTR4_WORD1_FS_LAYOUT_TYPES,
155 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156};
157
Manoj Naik830b8e32006-06-09 09:34:25 -0400158const u32 nfs4_fs_locations_bitmap[2] = {
159 FATTR4_WORD0_TYPE
160 | FATTR4_WORD0_CHANGE
161 | FATTR4_WORD0_SIZE
162 | FATTR4_WORD0_FSID
163 | FATTR4_WORD0_FILEID
164 | FATTR4_WORD0_FS_LOCATIONS,
165 FATTR4_WORD1_MODE
166 | FATTR4_WORD1_NUMLINKS
167 | FATTR4_WORD1_OWNER
168 | FATTR4_WORD1_OWNER_GROUP
169 | FATTR4_WORD1_RAWDEV
170 | FATTR4_WORD1_SPACE_USED
171 | FATTR4_WORD1_TIME_ACCESS
172 | FATTR4_WORD1_TIME_METADATA
173 | FATTR4_WORD1_TIME_MODIFY
174 | FATTR4_WORD1_MOUNTED_ON_FILEID
175};
176
Al Virobc4785c2006-10-19 23:28:51 -0700177static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs4_readdir_arg *readdir)
179{
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 BUG_ON(readdir->count < 80);
183 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000184 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
186 return;
187 }
188
189 readdir->cookie = 0;
190 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
191 if (cookie == 2)
192 return;
193
194 /*
195 * NFSv4 servers do not return entries for '.' and '..'
196 * Therefore, we fake these entries here. We let '.'
197 * have cookie 0 and '..' have cookie 1. Note that
198 * when talking to the server, we always send cookie 0
199 * instead of 1 or 2.
200 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800201 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 if (cookie == 0) {
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_one; /* cookie, second word */
207 *p++ = xdr_one; /* entry len */
208 memcpy(p, ".\0\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 *p++ = xdr_one; /* next */
217 *p++ = xdr_zero; /* cookie, first word */
218 *p++ = xdr_two; /* cookie, second word */
219 *p++ = xdr_two; /* entry len */
220 memcpy(p, "..\0\0", 4); /* entry */
221 p++;
222 *p++ = xdr_one; /* bitmap length */
223 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
224 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400225 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 readdir->pgbase = (char *)p - (char *)start;
228 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800229 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Trond Myklebust65de8722008-12-23 15:21:44 -0500232static int nfs4_wait_clnt_recover(struct nfs_client *clp)
233{
234 int res;
235
236 might_sleep();
237
Trond Myklebuste005e802008-12-23 15:21:48 -0500238 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400239 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500240 return res;
241}
242
243static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
244{
245 int res = 0;
246
247 might_sleep();
248
249 if (*timeout <= 0)
250 *timeout = NFS4_POLL_RETRY_MIN;
251 if (*timeout > NFS4_POLL_RETRY_MAX)
252 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100253 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500254 if (fatal_signal_pending(current))
255 res = -ERESTARTSYS;
256 *timeout <<= 1;
257 return res;
258}
259
260/* This is the error handling routine for processes that are allowed
261 * to sleep.
262 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800263static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500264{
265 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500267 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 int ret = errorcode;
269
270 exception->retry = 0;
271 switch(errorcode) {
272 case 0:
273 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500274 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400275 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500276 nfs_inode_return_delegation(inode);
277 exception->retry = 1;
278 return 0;
279 }
280 if (state == NULL)
281 break;
282 nfs4_schedule_stateid_recovery(server, state);
283 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500284 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500285 case -NFS4ERR_ADMIN_REVOKED:
286 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500287 if (state == NULL)
288 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400289 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_stateid_recovery(server, state);
291 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400292 case -NFS4ERR_EXPIRED:
293 if (state != NULL)
294 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500297 nfs4_schedule_lease_recovery(clp);
298 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500299#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400300 case -NFS4ERR_BADSESSION:
301 case -NFS4ERR_BADSLOT:
302 case -NFS4ERR_BAD_HIGH_SLOT:
303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
304 case -NFS4ERR_DEADSESSION:
305 case -NFS4ERR_SEQ_FALSE_RETRY:
306 case -NFS4ERR_SEQ_MISORDERED:
307 dprintk("%s ERROR: %d Reset session\n", __func__,
308 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400309 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Andy Adamson4745e312009-04-01 09:22:42 -0400310 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500311 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500312#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500313 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500314 if (exception->timeout > HZ) {
315 /* We have retried a decent amount, time to
316 * fail
317 */
318 ret = -EBUSY;
319 break;
320 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 case -NFS4ERR_GRACE:
322 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500323 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 ret = nfs4_delay(server->client, &exception->timeout);
325 if (ret != 0)
326 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400327 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500328 case -NFS4ERR_OLD_STATEID:
329 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800330 break;
331 case -NFS4ERR_BADOWNER:
332 /* The following works around a Linux server bug! */
333 case -NFS4ERR_BADNAME:
334 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
335 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
336 exception->retry = 1;
337 printk(KERN_WARNING "NFS: v4 server %s "
338 "does not accept raw "
339 "uid/gids. "
340 "Reenabling the idmapper.\n",
341 server->nfs_client->cl_hostname);
342 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500343 }
344 /* We failed to handle the error */
345 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500346wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500347 ret = nfs4_wait_clnt_recover(clp);
348 if (ret == 0)
349 exception->retry = 1;
350 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500351}
352
353
Trond Myklebust452e9352010-07-31 14:29:06 -0400354static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_lock(&clp->cl_lock);
357 if (time_before(clp->cl_last_renewal,timestamp))
358 clp->cl_last_renewal = timestamp;
359 spin_unlock(&clp->cl_lock);
360}
361
Trond Myklebust452e9352010-07-31 14:29:06 -0400362static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
363{
364 do_renew_lease(server->nfs_client, timestamp);
365}
366
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400367#if defined(CONFIG_NFS_V4_1)
368
Benny Halevy510b8172009-04-01 09:22:14 -0400369/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400370 * nfs4_free_slot - free a slot and efficiently update slot table.
371 *
372 * freeing a slot is trivially done by clearing its respective bit
373 * in the bitmap.
374 * If the freed slotid equals highest_used_slotid we want to update it
375 * so that the server would be able to size down the slot table if needed,
376 * otherwise we know that the highest_used_slotid is still in use.
377 * When updating highest_used_slotid there may be "holes" in the bitmap
378 * so we need to scan down from highest_used_slotid to 0 looking for the now
379 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500380 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500381 *
382 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 */
384static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500385nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400386{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500387 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400388 /* clear used bit in bitmap */
389 __clear_bit(slotid, tbl->used_slots);
390
391 /* update highest_used_slotid when it is freed */
392 if (slotid == tbl->highest_used_slotid) {
393 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500394 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400395 tbl->highest_used_slotid = slotid;
396 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500397 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400398 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500399 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
400 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400401}
402
Trond Myklebust961a8282012-01-17 22:57:37 -0500403bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
404{
405 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
406 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400407}
408
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800409/*
Andy Adamson42acd022011-01-06 02:04:34 +0000410 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800411 */
Andy Adamson42acd022011-01-06 02:04:34 +0000412static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800413{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500415 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
416 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800417 return;
418 }
419
Trond Myklebust45d43c22012-02-06 19:38:51 -0500420 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800421 return;
422
Andy Adamson42acd022011-01-06 02:04:34 +0000423 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
424 complete(&ses->fc_slot_table.complete);
425}
426
427/*
428 * Signal state manager thread if session back channel is drained
429 */
430void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
431{
432 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500433 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000434 return;
435 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
436 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800437}
438
Trond Myklebustd185a332010-06-16 09:52:25 -0400439static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400440{
441 struct nfs4_slot_table *tbl;
442
Trond Myklebustd185a332010-06-16 09:52:25 -0400443 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400444 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400445 /* just wake up the next guy waiting since
446 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500447 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500448 return;
Andy Adamson13615872009-04-01 09:22:17 -0400449 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500450
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500451 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500452 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000453 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500454 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400455 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400456}
457
Trond Myklebust14516c32010-07-31 14:29:06 -0400458static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459{
460 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462
463 /*
464 * sr_status remains 1 if an RPC level error occurred. The server
465 * may or may not have processed the sequence operation..
466 * Proceed as if the server received and processed the sequence
467 * operation.
468 */
469 if (res->sr_status == 1)
470 res->sr_status = NFS_OK;
471
Bryan Schumaker468f8612011-04-18 15:57:32 -0400472 /* don't increment the sequence number if the task wasn't sent */
473 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 goto out;
475
Andy Adamson691daf32009-12-04 15:55:39 -0500476 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 switch (res->sr_status) {
478 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400483 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500484 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500485 if (res->sr_status_flags != 0)
486 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 break;
488 case -NFS4ERR_DELAY:
489 /* The server detected a resend of the RPC call and
490 * returned NFS4ERR_DELAY as per Section 2.10.6.2
491 * of RFC5661.
492 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200493 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400494 __func__,
495 res->sr_slot - res->sr_session->fc_slot_table.slots,
496 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400497 goto out_retry;
498 default:
499 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400500 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400501 }
502out:
503 /* The session may be reset by one of the error handlers. */
504 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400505 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400506 return 1;
507out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400508 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400509 goto out;
510 rpc_delay(task, NFS4_POLL_RETRY_MAX);
511 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400512}
513
Trond Myklebust14516c32010-07-31 14:29:06 -0400514static int nfs4_sequence_done(struct rpc_task *task,
515 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400516{
Trond Myklebust14516c32010-07-31 14:29:06 -0400517 if (res->sr_session == NULL)
518 return 1;
519 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400520}
521
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400522/*
Benny Halevy510b8172009-04-01 09:22:14 -0400523 * nfs4_find_slot - efficiently look for a free slot
524 *
525 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
526 * If found, we mark the slot as used, update the highest_used_slotid,
527 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500528 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400529 *
530 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400531 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500532static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800533nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400534{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500535 u32 slotid;
536 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400537
Trond Myklebust45d43c22012-02-06 19:38:51 -0500538 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400539 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
540 tbl->max_slots);
541 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
542 if (slotid >= tbl->max_slots)
543 goto out;
544 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500545 if (slotid > tbl->highest_used_slotid ||
546 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400547 tbl->highest_used_slotid = slotid;
548 ret_id = slotid;
549out:
550 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
551 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400552 return ret_id;
553}
554
Trond Myklebust9d12b212012-01-17 22:04:25 -0500555static void nfs41_init_sequence(struct nfs4_sequence_args *args,
556 struct nfs4_sequence_res *res, int cache_reply)
557{
558 args->sa_session = NULL;
559 args->sa_cache_this = 0;
560 if (cache_reply)
561 args->sa_cache_this = 1;
562 res->sr_session = NULL;
563 res->sr_slot = NULL;
564}
565
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000566int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 struct rpc_task *task)
570{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 struct nfs4_slot *slot;
572 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500573 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574
575 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400577 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 return 0;
579
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580 tbl = &session->fc_slot_table;
581
582 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400583 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800584 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500585 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500586 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400587 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500588 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500589 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400590 }
591
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800592 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
593 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
594 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
595 spin_unlock(&tbl->slot_tbl_lock);
596 dprintk("%s enforce FIFO order\n", __func__);
597 return -EAGAIN;
598 }
599
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800600 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500601 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400602 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
603 spin_unlock(&tbl->slot_tbl_lock);
604 dprintk("<-- %s: no free slots\n", __func__);
605 return -EAGAIN;
606 }
607 spin_unlock(&tbl->slot_tbl_lock);
608
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800609 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400610 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400611 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400613
614 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
615
Benny Halevy99fe60d2009-04-01 09:22:29 -0400616 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400617 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400618 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400619 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400620 /*
621 * sr_status is only set in decode_sequence, and so will remain
622 * set to 1 if an rpc level failure occurs.
623 */
624 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 return 0;
626}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000627EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628
Trond Myklebust035168a2010-06-16 09:52:26 -0400629int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *args,
631 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct rpc_task *task)
633{
Trond Myklebust035168a2010-06-16 09:52:26 -0400634 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635 int ret = 0;
636
Trond Myklebust9d12b212012-01-17 22:04:25 -0500637 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400639
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200640 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400641 __func__, session->clp, session, res->sr_slot ?
642 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400643
Trond Myklebust9d12b212012-01-17 22:04:25 -0500644 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645out:
646 dprintk("<-- %s status=%d\n", __func__, ret);
647 return ret;
648}
649
650struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400651 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 struct nfs4_sequence_args *seq_args;
653 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654};
655
656static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust035168a2010-06-16 09:52:26 -0400660 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
661
662 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500663 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 return;
665 rpc_call_start(task);
666}
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
669{
670 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
671 nfs41_call_sync_prepare(task, calldata);
672}
673
Andy Adamson69ab40c2009-04-01 09:22:19 -0400674static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
675{
676 struct nfs41_call_sync_data *data = calldata;
677
Trond Myklebust14516c32010-07-31 14:29:06 -0400678 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400679}
680
Trond Myklebust17280172012-03-11 13:11:00 -0400681static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400683 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684};
685
Trond Myklebust17280172012-03-11 13:11:00 -0400686static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800687 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
688 .rpc_call_done = nfs41_call_sync_done,
689};
690
Bryan Schumaker7c513052011-03-24 17:12:24 +0000691static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
692 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400693 struct rpc_message *msg,
694 struct nfs4_sequence_args *args,
695 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800696 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697{
698 int ret;
699 struct rpc_task *task;
700 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400701 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400702 .seq_args = args,
703 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400704 };
705 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400707 .rpc_message = msg,
708 .callback_ops = &nfs41_call_sync_ops,
709 .callback_data = &data
710 };
711
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800712 if (privileged)
713 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400714 task = rpc_run_task(&task_setup);
715 if (IS_ERR(task))
716 ret = PTR_ERR(task);
717 else {
718 ret = task->tk_status;
719 rpc_put_task(task);
720 }
721 return ret;
722}
723
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724int _nfs4_call_sync_session(struct rpc_clnt *clnt,
725 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
729 int cache_reply)
730{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500731 nfs41_init_sequence(args, res, cache_reply);
732 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733}
734
Trond Myklebustdf896452010-06-16 09:52:26 -0400735#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500736static inline
737void nfs41_init_sequence(struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res, int cache_reply)
739{
740}
741
Trond Myklebust14516c32010-07-31 14:29:06 -0400742static int nfs4_sequence_done(struct rpc_task *task,
743 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400744{
Trond Myklebust14516c32010-07-31 14:29:06 -0400745 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400746}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747#endif /* CONFIG_NFS_V4_1 */
748
Bryan Schumaker7c513052011-03-24 17:12:24 +0000749int _nfs4_call_sync(struct rpc_clnt *clnt,
750 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400751 struct rpc_message *msg,
752 struct nfs4_sequence_args *args,
753 struct nfs4_sequence_res *res,
754 int cache_reply)
755{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500756 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000757 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400758}
759
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000760static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000761int nfs4_call_sync(struct rpc_clnt *clnt,
762 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000763 struct rpc_message *msg,
764 struct nfs4_sequence_args *args,
765 struct nfs4_sequence_res *res,
766 int cache_reply)
767{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000768 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
769 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000770}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400771
Trond Myklebust38478b22006-05-25 01:40:57 -0400772static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Trond Myklebust38478b22006-05-25 01:40:57 -0400774 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Trond Myklebust38478b22006-05-25 01:40:57 -0400776 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -0400777 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700778 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400779 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700780 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400781 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100784struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400785 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 struct nfs_openargs o_arg;
787 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100788 struct nfs_open_confirmargs c_arg;
789 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500790 struct nfs4_string owner_name;
791 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400794 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400796 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100798 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400799 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100800 int rpc_status;
801 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802};
803
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400804
805static void nfs4_init_opendata_res(struct nfs4_opendata *p)
806{
807 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400808 p->o_res.seqid = p->o_arg.seqid;
809 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400810 p->o_res.server = p->o_arg.server;
811 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500812 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400813}
814
Al Viro82a2c1b2011-06-22 18:30:55 -0400815static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400817 const struct iattr *attrs,
818 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819{
Al Viro82a2c1b2011-06-22 18:30:55 -0400820 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 struct inode *dir = parent->d_inode;
822 struct nfs_server *server = NFS_SERVER(dir);
823 struct nfs4_opendata *p;
824
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400825 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 if (p == NULL)
827 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400828 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 if (p->o_arg.seqid == NULL)
830 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400831 nfs_sb_active(dentry->d_sb);
832 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 p->dir = parent;
834 p->owner = sp;
835 atomic_inc(&sp->so_count);
836 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837 p->o_arg.open_flags = flags;
838 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400839 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400840 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
841 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400842 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843 p->o_arg.server = server;
844 p->o_arg.bitmask = server->attr_bitmask;
845 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500846 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500847 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400848
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100849 p->o_arg.u.attrs = &p->attrs;
850 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500851
852 verf[0] = jiffies;
853 verf[1] = current->pid;
854 memcpy(p->o_arg.u.verifier.data, verf,
855 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100857 p->c_arg.fh = &p->o_res.fh;
858 p->c_arg.stateid = &p->o_res.stateid;
859 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400860 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400861 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862 return p;
863err_free:
864 kfree(p);
865err:
866 dput(parent);
867 return NULL;
868}
869
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400870static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100871{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400872 struct nfs4_opendata *p = container_of(kref,
873 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400874 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400875
876 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877 if (p->state != NULL)
878 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400879 nfs4_put_state_owner(p->owner);
880 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400881 dput(p->dentry);
882 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500883 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400884 kfree(p);
885}
886
887static void nfs4_opendata_put(struct nfs4_opendata *p)
888{
889 if (p != NULL)
890 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100891}
892
Trond Myklebust06f814a2006-01-03 09:55:07 +0100893static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
894{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100895 int ret;
896
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100898 return ret;
899}
900
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400902{
903 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904
Trond Myklebust536e43d2012-01-17 22:04:26 -0500905 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 goto out;
907 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400908 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500909 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
910 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400911 break;
912 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500913 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
914 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400915 break;
916 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500917 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
918 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400919 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400921 return ret;
922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400925{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500926 if (delegation == NULL)
927 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400929 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500930 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400931 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500932 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400933 return 1;
934}
935
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100937{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100939 case FMODE_WRITE:
940 state->n_wronly++;
941 break;
942 case FMODE_READ:
943 state->n_rdonly++;
944 break;
945 case FMODE_READ|FMODE_WRITE:
946 state->n_rdwr++;
947 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100949}
950
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400952{
953 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500954 nfs4_stateid_copy(&state->stateid, stateid);
955 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400957 case FMODE_READ:
958 set_bit(NFS_O_RDONLY_STATE, &state->flags);
959 break;
960 case FMODE_WRITE:
961 set_bit(NFS_O_WRONLY_STATE, &state->flags);
962 break;
963 case FMODE_READ|FMODE_WRITE:
964 set_bit(NFS_O_RDWR_STATE, &state->flags);
965 }
966}
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400969{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400970 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400972 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400973}
974
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975static 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 -0700976{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400977 /*
978 * Protect the call to nfs4_state_set_mode_locked and
979 * serialise the stateid update
980 */
981 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500983 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400984 set_bit(NFS_DELEGATED_STATE, &state->flags);
985 }
986 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400988 write_sequnlock(&state->seqlock);
989 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700991 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994static 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 -0500995{
996 struct nfs_inode *nfsi = NFS_I(state->inode);
997 struct nfs_delegation *deleg_cur;
998 int ret = 0;
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001001
1002 rcu_read_lock();
1003 deleg_cur = rcu_dereference(nfsi->delegation);
1004 if (deleg_cur == NULL)
1005 goto no_delegation;
1006
1007 spin_lock(&deleg_cur->lock);
1008 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001010 goto no_delegation_unlock;
1011
1012 if (delegation == NULL)
1013 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001014 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001015 goto no_delegation_unlock;
1016
Trond Myklebustb7391f42008-12-23 15:21:52 -05001017 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001019 ret = 1;
1020no_delegation_unlock:
1021 spin_unlock(&deleg_cur->lock);
1022no_delegation:
1023 rcu_read_unlock();
1024
1025 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001027 ret = 1;
1028 }
1029
1030 return ret;
1031}
1032
1033
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001035{
1036 struct nfs_delegation *delegation;
1037
1038 rcu_read_lock();
1039 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 rcu_read_unlock();
1042 return;
1043 }
1044 rcu_read_unlock();
1045 nfs_inode_return_delegation(inode);
1046}
1047
Trond Myklebust6ee41262007-07-08 14:11:36 -04001048static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049{
1050 struct nfs4_state *state = opendata->state;
1051 struct nfs_inode *nfsi = NFS_I(state->inode);
1052 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001053 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 nfs4_stateid stateid;
1056 int ret = -EAGAIN;
1057
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001059 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001060 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 if (can_open_cached(state, fmode, open_mode)) {
1062 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 goto out_return_state;
1065 }
1066 spin_unlock(&state->owner->so_lock);
1067 }
Trond Myklebust34310432008-12-23 15:21:38 -05001068 rcu_read_lock();
1069 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001070 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001071 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001072 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001073 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001075 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001076 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001077 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001078 if (ret != 0)
1079 goto out;
1080 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001081
1082 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001083 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001084 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001086out:
1087 return ERR_PTR(ret);
1088out_return_state:
1089 atomic_inc(&state->count);
1090 return state;
1091}
1092
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001093static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1094{
1095 struct inode *inode;
1096 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001097 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001098 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001099
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001101 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001102 goto out;
1103 }
1104
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001106 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001110 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111 goto err;
1112 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001113 state = nfs4_get_open_state(inode, data->owner);
1114 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001115 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001116 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001117 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001118 int delegation_flags = 0;
1119
Trond Myklebust003707c2007-07-05 18:07:55 -04001120 rcu_read_lock();
1121 delegation = rcu_dereference(NFS_I(inode)->delegation);
1122 if (delegation)
1123 delegation_flags = delegation->flags;
1124 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001125 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1126 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1127 "returning a delegation for "
1128 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001129 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001130 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001131 nfs_inode_set_delegation(state->inode,
1132 data->owner->so_cred,
1133 &data->o_res);
1134 else
1135 nfs_inode_reclaim_delegation(state->inode,
1136 data->owner->so_cred,
1137 &data->o_res);
1138 }
Trond Myklebust34310432008-12-23 15:21:38 -05001139
1140 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001142 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001143out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001144 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001145err_put_inode:
1146 iput(inode);
1147err:
1148 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001149}
1150
Trond Myklebust864472e2006-01-03 09:55:15 +01001151static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1152{
1153 struct nfs_inode *nfsi = NFS_I(state->inode);
1154 struct nfs_open_context *ctx;
1155
1156 spin_lock(&state->inode->i_lock);
1157 list_for_each_entry(ctx, &nfsi->open_files, list) {
1158 if (ctx->state != state)
1159 continue;
1160 get_nfs_open_context(ctx);
1161 spin_unlock(&state->inode->i_lock);
1162 return ctx;
1163 }
1164 spin_unlock(&state->inode->i_lock);
1165 return ERR_PTR(-ENOENT);
1166}
1167
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001168static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1169{
1170 struct nfs4_opendata *opendata;
1171
Al Viro3d4ff432011-06-22 18:40:12 -04001172 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001173 if (opendata == NULL)
1174 return ERR_PTR(-ENOMEM);
1175 opendata->state = state;
1176 atomic_inc(&state->count);
1177 return opendata;
1178}
1179
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001180static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001181{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 int ret;
1184
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001185 opendata->o_arg.open_flags = 0;
1186 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1188 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1189 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001190 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 if (ret != 0)
1192 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001194 if (IS_ERR(newstate))
1195 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001196 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 return 0;
1199}
1200
1201static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1202{
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 int ret;
1205
1206 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001207 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 smp_rmb();
1209 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001210 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001211 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 if (ret != 0)
1213 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001214 if (newstate != state)
1215 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 }
1217 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001218 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001219 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 if (ret != 0)
1221 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001222 if (newstate != state)
1223 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 }
1225 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001226 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001227 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 if (ret != 0)
1229 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001230 if (newstate != state)
1231 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001233 /*
1234 * We may have performed cached opens for all three recoveries.
1235 * Check if we need to update the current stateid.
1236 */
1237 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001238 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001239 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001240 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001241 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001242 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001243 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001244 return 0;
1245}
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/*
1248 * OPEN_RECLAIM:
1249 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001251static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001253 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001255 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 int status;
1257
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001258 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1259 if (IS_ERR(opendata))
1260 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1262 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001263 rcu_read_lock();
1264 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001265 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001266 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001267 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 opendata->o_arg.u.delegation_type = delegation_type;
1269 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return status;
1272}
1273
Trond Myklebust539cd032007-06-05 11:46:42 -04001274static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct nfs_server *server = NFS_SERVER(state->inode);
1277 struct nfs4_exception exception = { };
1278 int err;
1279 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001280 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001281 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001282 break;
1283 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 } while (exception.retry);
1285 return err;
1286}
1287
Trond Myklebust864472e2006-01-03 09:55:15 +01001288static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1289{
1290 struct nfs_open_context *ctx;
1291 int ret;
1292
1293 ctx = nfs4_state_find_open_context(state);
1294 if (IS_ERR(ctx))
1295 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001296 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001297 put_nfs_open_context(ctx);
1298 return ret;
1299}
1300
Trond Myklebust13437e12007-07-06 15:10:43 -04001301static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001303 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001304 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001306 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1307 if (IS_ERR(opendata))
1308 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001309 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001310 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001311 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Trond Myklebust13437e12007-07-06 15:10:43 -04001316int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
1318 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001319 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 int err;
1321 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001322 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 switch (err) {
1324 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001325 case -ENOENT:
1326 case -ESTALE:
1327 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001328 case -NFS4ERR_BADSESSION:
1329 case -NFS4ERR_BADSLOT:
1330 case -NFS4ERR_BAD_HIGH_SLOT:
1331 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1332 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04001333 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001334 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case -NFS4ERR_STALE_CLIENTID:
1336 case -NFS4ERR_STALE_STATEID:
1337 case -NFS4ERR_EXPIRED:
1338 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001339 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001340 goto out;
1341 case -ERESTARTSYS:
1342 /*
1343 * The show must go on: exit, but mark the
1344 * stateid as needing recovery.
1345 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001346 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001347 case -NFS4ERR_ADMIN_REVOKED:
1348 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001349 nfs_inode_find_state_and_recover(state->inode,
1350 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001351 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001352 case -EKEYEXPIRED:
1353 /*
1354 * User RPCSEC_GSS context has expired.
1355 * We cannot recover this stateid now, so
1356 * skip it and allow recovery thread to
1357 * proceed.
1358 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001359 case -ENOMEM:
1360 err = 0;
1361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 err = nfs4_handle_exception(server, err, &exception);
1364 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001365out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return err;
1367}
1368
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001369static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1370{
1371 struct nfs4_opendata *data = calldata;
1372
1373 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001374 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001375 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001376 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001378 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001379 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001380}
1381
1382static void nfs4_open_confirm_release(void *calldata)
1383{
1384 struct nfs4_opendata *data = calldata;
1385 struct nfs4_state *state = NULL;
1386
1387 /* If this request hasn't been cancelled, do nothing */
1388 if (data->cancelled == 0)
1389 goto out_free;
1390 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001391 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001393 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001394 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001395 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001397 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398}
1399
1400static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 .rpc_call_done = nfs4_open_confirm_done,
1402 .rpc_release = nfs4_open_confirm_release,
1403};
1404
1405/*
1406 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1407 */
1408static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1409{
1410 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1411 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412 struct rpc_message msg = {
1413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1414 .rpc_argp = &data->c_arg,
1415 .rpc_resp = &data->c_res,
1416 .rpc_cred = data->owner->so_cred,
1417 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 struct rpc_task_setup task_setup_data = {
1419 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001420 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001421 .callback_ops = &nfs4_open_confirm_ops,
1422 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001423 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001424 .flags = RPC_TASK_ASYNC,
1425 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int status;
1427
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001428 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001429 data->rpc_done = 0;
1430 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001432 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001433 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001435 status = nfs4_wait_for_completion_rpc_task(task);
1436 if (status != 0) {
1437 data->cancelled = 1;
1438 smp_wmb();
1439 } else
1440 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001441 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return status;
1443}
1444
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 struct nfs4_opendata *data = calldata;
1448 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1451 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001452 /*
1453 * Check if we still need to send an OPEN call, or if we can use
1454 * a delegation instead.
1455 */
1456 if (data->state != NULL) {
1457 struct nfs_delegation *delegation;
1458
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001460 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001461 rcu_read_lock();
1462 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001463 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1464 can_open_delegated(delegation, data->o_arg.fmode))
1465 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001466 rcu_read_unlock();
1467 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001468 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001469 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001470 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001471 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001472 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1473 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001474 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001475 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001476 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001477 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001478 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001479 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001480 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001481unlock_no_action:
1482 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001483out_no_action:
1484 task->tk_action = NULL;
1485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001488static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1489{
1490 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1491 nfs4_open_prepare(task, calldata);
1492}
1493
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494static void nfs4_open_done(struct rpc_task *task, void *calldata)
1495{
1496 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001499
Trond Myklebust14516c32010-07-31 14:29:06 -04001500 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1501 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503 if (task->tk_status == 0) {
1504 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001505 case S_IFREG:
1506 break;
1507 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001509 break;
1510 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001512 break;
1513 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001515 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001516 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001517 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1518 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001519 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001520 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001522
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523static void nfs4_open_release(void *calldata)
1524{
1525 struct nfs4_opendata *data = calldata;
1526 struct nfs4_state *state = NULL;
1527
1528 /* If this request hasn't been cancelled, do nothing */
1529 if (data->cancelled == 0)
1530 goto out_free;
1531 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001532 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 goto out_free;
1534 /* In case we need an open_confirm, no cleanup! */
1535 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1536 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001538 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001539 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001541 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542}
1543
1544static const struct rpc_call_ops nfs4_open_ops = {
1545 .rpc_call_prepare = nfs4_open_prepare,
1546 .rpc_call_done = nfs4_open_done,
1547 .rpc_release = nfs4_open_release,
1548};
1549
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001550static const struct rpc_call_ops nfs4_recover_open_ops = {
1551 .rpc_call_prepare = nfs4_recover_open_prepare,
1552 .rpc_call_done = nfs4_open_done,
1553 .rpc_release = nfs4_open_release,
1554};
1555
1556static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557{
1558 struct inode *dir = data->dir->d_inode;
1559 struct nfs_server *server = NFS_SERVER(dir);
1560 struct nfs_openargs *o_arg = &data->o_arg;
1561 struct nfs_openres *o_res = &data->o_res;
1562 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001563 struct rpc_message msg = {
1564 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1565 .rpc_argp = o_arg,
1566 .rpc_resp = o_res,
1567 .rpc_cred = data->owner->so_cred,
1568 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001569 struct rpc_task_setup task_setup_data = {
1570 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001571 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001572 .callback_ops = &nfs4_open_ops,
1573 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001574 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001575 .flags = RPC_TASK_ASYNC,
1576 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001577 int status;
1578
Trond Myklebust9d12b212012-01-17 22:04:25 -05001579 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001580 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001581 data->rpc_done = 0;
1582 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001583 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001584 if (isrecover)
1585 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001586 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001587 if (IS_ERR(task))
1588 return PTR_ERR(task);
1589 status = nfs4_wait_for_completion_rpc_task(task);
1590 if (status != 0) {
1591 data->cancelled = 1;
1592 smp_wmb();
1593 } else
1594 status = data->rpc_status;
1595 rpc_put_task(task);
1596
1597 return status;
1598}
1599
1600static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1601{
1602 struct inode *dir = data->dir->d_inode;
1603 struct nfs_openres *o_res = &data->o_res;
1604 int status;
1605
1606 status = nfs4_run_open_task(data, 1);
1607 if (status != 0 || !data->rpc_done)
1608 return status;
1609
Trond Myklebust6926afd2012-01-07 13:22:46 -05001610 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1611
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001612 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1613 status = _nfs4_proc_open_confirm(data);
1614 if (status != 0)
1615 return status;
1616 }
1617
1618 return status;
1619}
1620
1621/*
1622 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1623 */
1624static int _nfs4_proc_open(struct nfs4_opendata *data)
1625{
1626 struct inode *dir = data->dir->d_inode;
1627 struct nfs_server *server = NFS_SERVER(dir);
1628 struct nfs_openargs *o_arg = &data->o_arg;
1629 struct nfs_openres *o_res = &data->o_res;
1630 int status;
1631
1632 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001633 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001634 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001635 if (status != 0) {
1636 if (status == -NFS4ERR_BADNAME &&
1637 !(o_arg->open_flags & O_CREAT))
1638 return -ENOENT;
1639 return status;
1640 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001641
Trond Myklebust6926afd2012-01-07 13:22:46 -05001642 nfs_fattr_map_and_free_names(server, &data->f_attr);
1643
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001644 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001645 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001646 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1647 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001649 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001651 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001654 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001655 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Andy Adamsond83217c2011-03-01 01:34:17 +00001658static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001659{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001660 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001661 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001662
Trond Myklebusta78cb572009-08-09 15:06:19 -04001663 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001664 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001665 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001666 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001667 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1668 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001669 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001670 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001671 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001672 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001673 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001674}
1675
Andy Adamsond83217c2011-03-01 01:34:17 +00001676static int nfs4_recover_expired_lease(struct nfs_server *server)
1677{
1678 return nfs4_client_recover_expired_lease(server->nfs_client);
1679}
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681/*
1682 * OPEN_EXPIRED:
1683 * reclaim state on the server after a network partition.
1684 * Assumes caller holds the appropriate lock
1685 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001686static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001688 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001691 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1692 if (IS_ERR(opendata))
1693 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001694 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001695 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001696 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001697 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001701static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001702{
Trond Myklebust539cd032007-06-05 11:46:42 -04001703 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001704 struct nfs4_exception exception = { };
1705 int err;
1706
1707 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001708 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001709 switch (err) {
1710 default:
1711 goto out;
1712 case -NFS4ERR_GRACE:
1713 case -NFS4ERR_DELAY:
1714 nfs4_handle_exception(server, err, &exception);
1715 err = 0;
1716 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001717 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001718out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001719 return err;
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1723{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001725 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Trond Myklebust864472e2006-01-03 09:55:15 +01001727 ctx = nfs4_state_find_open_context(state);
1728 if (IS_ERR(ctx))
1729 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001730 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001731 put_nfs_open_context(ctx);
1732 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001735#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001736static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001737{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001738 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001739 struct nfs_server *server = NFS_SERVER(state->inode);
1740
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001741 if (state->flags & flags) {
1742 status = nfs41_test_stateid(server, stateid);
1743 if (status != NFS_OK) {
1744 nfs41_free_stateid(server, stateid);
1745 state->flags &= ~flags;
1746 }
1747 }
1748 return status;
1749}
1750
1751static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1752{
1753 int deleg_status, open_status;
1754 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1755 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1756
1757 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1758 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1759
1760 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1761 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001762 return nfs4_open_expired(sp, state);
1763}
1764#endif
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001767 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1768 * fields corresponding to attributes that were used to store the verifier.
1769 * Make sure we clobber those fields in the later setattr call
1770 */
1771static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1772{
1773 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1774 !(sattr->ia_valid & ATTR_ATIME_SET))
1775 sattr->ia_valid |= ATTR_ATIME;
1776
1777 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1778 !(sattr->ia_valid & ATTR_MTIME_SET))
1779 sattr->ia_valid |= ATTR_MTIME;
1780}
1781
1782/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001783 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 */
Andy Adamson82be4172012-05-23 05:02:35 -04001785static int _nfs4_do_open(struct inode *dir,
1786 struct dentry *dentry,
1787 fmode_t fmode,
1788 int flags,
1789 struct iattr *sattr,
1790 struct rpc_cred *cred,
1791 struct nfs4_state **res,
1792 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 struct nfs4_state_owner *sp;
1795 struct nfs4_state *state = NULL;
1796 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001797 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001798 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001802 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1803 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1805 goto out_err;
1806 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001807 status = nfs4_recover_expired_lease(server);
1808 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001809 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001810 if (dentry->d_inode != NULL)
1811 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001812 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001813 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001814 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001815 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Andy Adamson82be4172012-05-23 05:02:35 -04001817 if (ctx_th && server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
1818 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
1819 if (!opendata->f_attr.mdsthreshold)
1820 goto err_opendata_put;
1821 }
Al Viro82a2c1b2011-06-22 18:30:55 -04001822 if (dentry->d_inode != NULL)
1823 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001824
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001825 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001827 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001829 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001830 status = PTR_ERR(state);
1831 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001832 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001833 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001834 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001835
1836 if (opendata->o_arg.open_flags & O_EXCL) {
1837 nfs4_exclusive_attrset(opendata, sattr);
1838
1839 nfs_fattr_init(opendata->o_res.f_attr);
1840 status = nfs4_do_setattr(state->inode, cred,
1841 opendata->o_res.f_attr, sattr,
1842 state);
1843 if (status == 0)
1844 nfs_setattr_update_inode(state->inode, sattr);
1845 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1846 }
Andy Adamson82be4172012-05-23 05:02:35 -04001847
1848 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server))
1849 *ctx_th = opendata->f_attr.mdsthreshold;
1850 else
1851 kfree(opendata->f_attr.mdsthreshold);
1852 opendata->f_attr.mdsthreshold = NULL;
1853
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001854 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 *res = state;
1857 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001858err_opendata_put:
Andy Adamson82be4172012-05-23 05:02:35 -04001859 kfree(opendata->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001860 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001861err_put_state_owner:
1862 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 *res = NULL;
1865 return status;
1866}
1867
1868
Andy Adamson82be4172012-05-23 05:02:35 -04001869static struct nfs4_state *nfs4_do_open(struct inode *dir,
1870 struct dentry *dentry,
1871 fmode_t fmode,
1872 int flags,
1873 struct iattr *sattr,
1874 struct rpc_cred *cred,
1875 struct nfs4_threshold **ctx_th)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 struct nfs4_exception exception = { };
1878 struct nfs4_state *res;
1879 int status;
1880
1881 do {
Andy Adamson82be4172012-05-23 05:02:35 -04001882 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred,
1883 &res, ctx_th);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (status == 0)
1885 break;
1886 /* NOTE: BAD_SEQID means the server and client disagree about the
1887 * book-keeping w.r.t. state-changing operations
1888 * (OPEN/CLOSE/LOCK/LOCKU...)
1889 * It is actually a sign of a bug on the client or on the server.
1890 *
1891 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001892 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * have unhashed the old state_owner for us, and that we can
1894 * therefore safely retry using a new one. We should still warn
1895 * the user though...
1896 */
1897 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001898 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001899 " returned a bad sequence-id error!\n",
1900 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 exception.retry = 1;
1902 continue;
1903 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001904 /*
1905 * BAD_STATEID on OPEN means that the server cancelled our
1906 * state before it received the OPEN_CONFIRM.
1907 * Recover by retrying the request as per the discussion
1908 * on Page 181 of RFC3530.
1909 */
1910 if (status == -NFS4ERR_BAD_STATEID) {
1911 exception.retry = 1;
1912 continue;
1913 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001914 if (status == -EAGAIN) {
1915 /* We must have found a delegation */
1916 exception.retry = 1;
1917 continue;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1920 status, &exception));
1921 } while (exception.retry);
1922 return res;
1923}
1924
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001925static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1926 struct nfs_fattr *fattr, struct iattr *sattr,
1927 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001929 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001931 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 .iap = sattr,
1933 .server = server,
1934 .bitmask = server->attr_bitmask,
1935 };
1936 struct nfs_setattrres res = {
1937 .fattr = fattr,
1938 .server = server,
1939 };
1940 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001941 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1942 .rpc_argp = &arg,
1943 .rpc_resp = &res,
1944 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001946 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001947 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Trond Myklebust0e574af2005-10-27 22:12:38 -04001949 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Trond Myklebust4fc87962012-03-08 17:42:01 -05001951 if (state != NULL) {
1952 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1953 current->files, current->tgid);
1954 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1955 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001956 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001957 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001958 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Bryan Schumaker7c513052011-03-24 17:12:24 +00001960 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001961 if (status == 0 && state != NULL)
1962 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001963 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001966static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1967 struct nfs_fattr *fattr, struct iattr *sattr,
1968 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001970 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001971 struct nfs4_exception exception = {
1972 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001973 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int err;
1976 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001977 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1978 switch (err) {
1979 case -NFS4ERR_OPENMODE:
1980 if (state && !(state->state & FMODE_WRITE)) {
1981 err = -EBADF;
1982 if (sattr->ia_valid & ATTR_OPEN)
1983 err = -EACCES;
1984 goto out;
1985 }
1986 }
1987 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001989out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return err;
1991}
1992
1993struct nfs4_closedata {
1994 struct inode *inode;
1995 struct nfs4_state *state;
1996 struct nfs_closeargs arg;
1997 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001998 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001999 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00002000 bool roc;
2001 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002};
2003
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002004static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002005{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002006 struct nfs4_closedata *calldata = data;
2007 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002008 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002009
Fred Isamanf7e89172011-01-06 11:36:32 +00002010 if (calldata->roc)
2011 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07002012 nfs4_put_open_state(calldata->state);
2013 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002014 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04002015 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002016 kfree(calldata);
2017}
2018
Trond Myklebust88069f72009-12-08 08:33:16 -05002019static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
2020 fmode_t fmode)
2021{
2022 spin_lock(&state->owner->so_lock);
2023 if (!(fmode & FMODE_READ))
2024 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2025 if (!(fmode & FMODE_WRITE))
2026 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2027 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2028 spin_unlock(&state->owner->so_lock);
2029}
2030
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002031static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002033 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct nfs_server *server = NFS_SERVER(calldata->inode);
2036
Chuck Levera3ca5652012-03-01 17:00:40 -05002037 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002038 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2039 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* hmm. we are done with the inode, and in the process of freeing
2041 * the state_owner. we keep this around to process errors
2042 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 switch (task->tk_status) {
2044 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002045 if (calldata->roc)
2046 pnfs_roc_set_barrier(state->inode,
2047 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002048 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002049 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002050 nfs4_close_clear_stateid_flags(state,
2051 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 break;
2053 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002054 case -NFS4ERR_OLD_STATEID:
2055 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002057 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002058 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002060 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2061 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002063 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002064 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002065 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002068static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002070 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002071 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002072 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002073
Chuck Levera3ca5652012-03-01 17:00:40 -05002074 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002075 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002076 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002077
Trond Myklebust88069f72009-12-08 08:33:16 -05002078 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2079 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002080 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002081 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002082 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002083 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002084 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2085 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2086 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002087 }
2088 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002089 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2090 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2091 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002092 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002093 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002094 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002095
2096 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002097 /* Note: exit _without_ calling nfs4_close_done */
2098 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002099 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002100 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002101
Fred Isamanf7e89172011-01-06 11:36:32 +00002102 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002103 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002104 if (calldata->roc &&
2105 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2106 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2107 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002108 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002109 }
2110 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002111
Trond Myklebust516a6af2005-10-27 22:12:41 -04002112 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002113 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002114 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002115 &calldata->arg.seq_args,
2116 &calldata->res.seq_res,
2117 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002118 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002119 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002120out:
2121 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002124static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002125 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002126 .rpc_call_done = nfs4_close_done,
2127 .rpc_release = nfs4_free_closedata,
2128};
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130/*
2131 * It is possible for data to be read/written from a mem-mapped file
2132 * after the sys_close call (which hits the vfs layer as a flush).
2133 * This means that we can't safely call nfsv4 close on a file until
2134 * the inode is cleared. This in turn means that we are not good
2135 * NFSv4 citizens - we do not indicate to the server to update the file's
2136 * share state even when we are done with one of the three share
2137 * stateid's in the inode.
2138 *
2139 * NOTE: Caller must be holding the sp->so_owner semaphore!
2140 */
Al Viro643168c2011-06-22 18:20:23 -04002141int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002143 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002145 struct nfs4_state_owner *sp = state->owner;
2146 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002147 struct rpc_message msg = {
2148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2149 .rpc_cred = state->owner->so_cred,
2150 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002151 struct rpc_task_setup task_setup_data = {
2152 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002153 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002154 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002155 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002156 .flags = RPC_TASK_ASYNC,
2157 };
Trond Myklebust95121352005-10-18 14:20:12 -07002158 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002160 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002162 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002163 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002164 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002166 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002167 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002169 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002170 if (calldata->arg.seqid == NULL)
2171 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002172 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002173 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002174 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002175 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002176 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002177 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002178 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002179
Trond Myklebust1174dd12010-12-21 10:52:24 -05002180 msg.rpc_argp = &calldata->arg;
2181 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002182 task_setup_data.callback_data = calldata;
2183 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002184 if (IS_ERR(task))
2185 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002186 status = 0;
2187 if (wait)
2188 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002189 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002190 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002191out_free_calldata:
2192 kfree(calldata);
2193out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002194 if (roc)
2195 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002196 nfs4_put_open_state(state);
2197 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002198 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
Trond Myklebust2b484292010-09-17 10:56:51 -04002201static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002202nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 struct nfs4_state *state;
2205
Trond Myklebust565277f2007-10-15 18:17:53 -04002206 /* Protect against concurrent sillydeletes */
Andy Adamson82be4172012-05-23 05:02:35 -04002207 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr,
2208 ctx->cred, &ctx->mdsthreshold);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002209 if (IS_ERR(state))
2210 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002211 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002212 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
Trond Myklebust1185a552009-12-03 15:54:02 -05002215static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002216{
2217 if (ctx->state == NULL)
2218 return;
2219 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002220 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002221 else
Al Viro643168c2011-06-22 18:20:23 -04002222 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2226{
Benny Halevy43652ad2009-04-01 09:21:54 -04002227 struct nfs4_server_caps_arg args = {
2228 .fhandle = fhandle,
2229 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 struct nfs4_server_caps_res res = {};
2231 struct rpc_message msg = {
2232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002233 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 .rpc_resp = &res,
2235 };
2236 int status;
2237
Bryan Schumaker7c513052011-03-24 17:12:24 +00002238 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (status == 0) {
2240 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002241 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2242 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2243 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2244 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2245 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2247 server->caps |= NFS_CAP_ACLS;
2248 if (res.has_links != 0)
2249 server->caps |= NFS_CAP_HARDLINKS;
2250 if (res.has_symlinks != 0)
2251 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002252 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2253 server->caps |= NFS_CAP_FILEID;
2254 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2255 server->caps |= NFS_CAP_MODE;
2256 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2257 server->caps |= NFS_CAP_NLINK;
2258 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2259 server->caps |= NFS_CAP_OWNER;
2260 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2261 server->caps |= NFS_CAP_OWNER_GROUP;
2262 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2263 server->caps |= NFS_CAP_ATIME;
2264 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2265 server->caps |= NFS_CAP_CTIME;
2266 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2267 server->caps |= NFS_CAP_MTIME;
2268
Trond Myklebusta65318b2009-03-11 14:10:28 -04002269 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2270 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2271 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002273 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return status;
2277}
2278
Trond Myklebust55a97592006-06-09 09:34:19 -04002279int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 struct nfs4_exception exception = { };
2282 int err;
2283 do {
2284 err = nfs4_handle_exception(server,
2285 _nfs4_server_capabilities(server, fhandle),
2286 &exception);
2287 } while (exception.retry);
2288 return err;
2289}
2290
2291static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2292 struct nfs_fsinfo *info)
2293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 struct nfs4_lookup_root_arg args = {
2295 .bitmask = nfs4_fattr_bitmap,
2296 };
2297 struct nfs4_lookup_res res = {
2298 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002299 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 .fh = fhandle,
2301 };
2302 struct rpc_message msg = {
2303 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2304 .rpc_argp = &args,
2305 .rpc_resp = &res,
2306 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002307
Trond Myklebust0e574af2005-10-27 22:12:38 -04002308 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002309 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2313 struct nfs_fsinfo *info)
2314{
2315 struct nfs4_exception exception = { };
2316 int err;
2317 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002318 err = _nfs4_lookup_root(server, fhandle, info);
2319 switch (err) {
2320 case 0:
2321 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002322 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002323 default:
2324 err = nfs4_handle_exception(server, err, &exception);
2325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return err;
2329}
2330
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002331static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2332 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2333{
2334 struct rpc_auth *auth;
2335 int ret;
2336
2337 auth = rpcauth_create(flavor, server->client);
2338 if (!auth) {
2339 ret = -EIO;
2340 goto out;
2341 }
2342 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002343out:
2344 return ret;
2345}
2346
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002347static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002348 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002350 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002351 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002353 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2354 flav_array[len] = RPC_AUTH_NULL;
2355 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002356
2357 for (i = 0; i < len; i++) {
2358 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002359 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002360 continue;
2361 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002362 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002363 /*
2364 * -EACCESS could mean that the user doesn't have correct permissions
2365 * to access the mount. It could also mean that we tried to mount
2366 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2367 * existing mount programs don't handle -EACCES very well so it should
2368 * be mapped to -EPERM instead.
2369 */
2370 if (status == -EACCES)
2371 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002372 return status;
2373}
2374
2375/*
2376 * get the file handle for the "/" directory on the server
2377 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002378int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2379 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002380{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002381 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002382 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002383 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2384 /*
2385 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2386 * by nfs4_map_errors() as this function exits.
2387 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002388 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (status == 0)
2390 status = nfs4_server_capabilities(server, fhandle);
2391 if (status == 0)
2392 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002393 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Bryan Schumakerbae36242012-05-10 15:07:31 -04002396static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2397 struct nfs_fsinfo *info)
2398{
2399 int error;
2400 struct nfs_fattr *fattr = info->fattr;
2401
2402 error = nfs4_server_capabilities(server, mntfh);
2403 if (error < 0) {
2404 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2405 return error;
2406 }
2407
2408 error = nfs4_proc_getattr(server, mntfh, fattr);
2409 if (error < 0) {
2410 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2411 return error;
2412 }
2413
2414 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2415 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2416 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2417
2418 return error;
2419}
2420
Manoj Naik6b97fd32006-06-09 09:34:29 -04002421/*
2422 * Get locations and (maybe) other attributes of a referral.
2423 * Note that we'll actually follow the referral later when
2424 * we detect fsid mismatch in inode revalidation
2425 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002426static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2427 const struct qstr *name, struct nfs_fattr *fattr,
2428 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002429{
2430 int status = -ENOMEM;
2431 struct page *page = NULL;
2432 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002433
2434 page = alloc_page(GFP_KERNEL);
2435 if (page == NULL)
2436 goto out;
2437 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2438 if (locations == NULL)
2439 goto out;
2440
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002441 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002442 if (status != 0)
2443 goto out;
2444 /* Make sure server returned a different fsid for the referral */
2445 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002446 dprintk("%s: server did not return a different fsid for"
2447 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002448 status = -EIO;
2449 goto out;
2450 }
Andy Adamson533eb462011-06-13 18:25:56 -04002451 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2452 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002453
Andy Adamson533eb462011-06-13 18:25:56 -04002454 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002455 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002456 memset(fhandle, 0, sizeof(struct nfs_fh));
2457out:
2458 if (page)
2459 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002460 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002461 return status;
2462}
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2465{
2466 struct nfs4_getattr_arg args = {
2467 .fh = fhandle,
2468 .bitmask = server->attr_bitmask,
2469 };
2470 struct nfs4_getattr_res res = {
2471 .fattr = fattr,
2472 .server = server,
2473 };
2474 struct rpc_message msg = {
2475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2476 .rpc_argp = &args,
2477 .rpc_resp = &res,
2478 };
2479
Trond Myklebust0e574af2005-10-27 22:12:38 -04002480 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002481 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2485{
2486 struct nfs4_exception exception = { };
2487 int err;
2488 do {
2489 err = nfs4_handle_exception(server,
2490 _nfs4_proc_getattr(server, fhandle, fattr),
2491 &exception);
2492 } while (exception.retry);
2493 return err;
2494}
2495
2496/*
2497 * The file is not closed if it is opened due to the a request to change
2498 * the size of the file. The open call will not be needed once the
2499 * VFS layer lookup-intents are implemented.
2500 *
2501 * Close is called when the inode is destroyed.
2502 * If we haven't opened the file for O_WRONLY, we
2503 * need to in the size_change case to obtain a stateid.
2504 *
2505 * Got race?
2506 * Because OPEN is always done by name in nfsv4, it is
2507 * possible that we opened a different file by the same
2508 * name. We can recognize this race condition, but we
2509 * can't do anything about it besides returning an error.
2510 *
2511 * This will be fixed with VFS changes (lookup-intent).
2512 */
2513static int
2514nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2515 struct iattr *sattr)
2516{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002517 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002518 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002519 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int status;
2521
Benny Halevy8a1636c2010-07-14 15:43:57 -04002522 if (pnfs_ld_layoutret_on_setattr(inode))
2523 pnfs_return_layout(inode);
2524
Trond Myklebust0e574af2005-10-27 22:12:38 -04002525 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Trond Myklebustd5308382005-11-04 15:33:38 -05002527 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002528 if (sattr->ia_valid & ATTR_FILE) {
2529 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002530
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002531 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002532 if (ctx) {
2533 cred = ctx->cred;
2534 state = ctx->state;
2535 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002536 }
2537
Trond Myklebust536e43d2012-01-17 22:04:26 -05002538 /* Deal with open(O_TRUNC) */
2539 if (sattr->ia_valid & ATTR_OPEN)
2540 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2541
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002542 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002543 if (status == 0)
2544 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return status;
2546}
2547
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002548static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2549 const struct qstr *name, struct nfs_fh *fhandle,
2550 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002551{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002552 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002553 int status;
2554 struct nfs4_lookup_arg args = {
2555 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002556 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002557 .name = name,
2558 };
2559 struct nfs4_lookup_res res = {
2560 .server = server,
2561 .fattr = fattr,
2562 .fh = fhandle,
2563 };
2564 struct rpc_message msg = {
2565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2566 .rpc_argp = &args,
2567 .rpc_resp = &res,
2568 };
2569
2570 nfs_fattr_init(fattr);
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002573 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 dprintk("NFS reply lookup: %d\n", status);
2575 return status;
2576}
2577
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002578static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002579{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002580 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002581 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002582 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2583 fattr->nlink = 2;
2584}
2585
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002586static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2587 struct qstr *name, struct nfs_fh *fhandle,
2588 struct nfs_fattr *fattr)
2589{
2590 struct nfs4_exception exception = { };
2591 struct rpc_clnt *client = *clnt;
2592 int err;
2593 do {
2594 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2595 switch (err) {
2596 case -NFS4ERR_BADNAME:
2597 err = -ENOENT;
2598 goto out;
2599 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002600 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002601 goto out;
2602 case -NFS4ERR_WRONGSEC:
2603 err = -EPERM;
2604 if (client != *clnt)
2605 goto out;
2606
2607 client = nfs4_create_sec_client(client, dir, name);
2608 if (IS_ERR(client))
2609 return PTR_ERR(client);
2610
2611 exception.retry = 1;
2612 break;
2613 default:
2614 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2615 }
2616 } while (exception.retry);
2617
2618out:
2619 if (err == 0)
2620 *clnt = client;
2621 else if (client != *clnt)
2622 rpc_shutdown_client(client);
2623
2624 return err;
2625}
2626
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002627static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002628 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002630 int status;
2631 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002632
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002633 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2634 if (client != NFS_CLIENT(dir)) {
2635 rpc_shutdown_client(client);
2636 nfs_fixup_secinfo_attributes(fattr);
2637 }
2638 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002641struct rpc_clnt *
2642nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2643 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2644{
2645 int status;
2646 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2647
2648 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2649 if (status < 0) {
2650 rpc_shutdown_client(client);
2651 return ERR_PTR(status);
2652 }
2653 return client;
2654}
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2657{
Trond Myklebust76b32992007-08-10 17:45:11 -04002658 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 struct nfs4_accessargs args = {
2660 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002661 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002663 struct nfs4_accessres res = {
2664 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002665 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2668 .rpc_argp = &args,
2669 .rpc_resp = &res,
2670 .rpc_cred = entry->cred,
2671 };
2672 int mode = entry->mask;
2673 int status;
2674
2675 /*
2676 * Determine which access bits we want to ask for...
2677 */
2678 if (mode & MAY_READ)
2679 args.access |= NFS4_ACCESS_READ;
2680 if (S_ISDIR(inode->i_mode)) {
2681 if (mode & MAY_WRITE)
2682 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2683 if (mode & MAY_EXEC)
2684 args.access |= NFS4_ACCESS_LOOKUP;
2685 } else {
2686 if (mode & MAY_WRITE)
2687 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2688 if (mode & MAY_EXEC)
2689 args.access |= NFS4_ACCESS_EXECUTE;
2690 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002691
2692 res.fattr = nfs_alloc_fattr();
2693 if (res.fattr == NULL)
2694 return -ENOMEM;
2695
Bryan Schumaker7c513052011-03-24 17:12:24 +00002696 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (!status) {
2698 entry->mask = 0;
2699 if (res.access & NFS4_ACCESS_READ)
2700 entry->mask |= MAY_READ;
2701 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2702 entry->mask |= MAY_WRITE;
2703 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2704 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002705 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002707 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return status;
2709}
2710
2711static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2712{
2713 struct nfs4_exception exception = { };
2714 int err;
2715 do {
2716 err = nfs4_handle_exception(NFS_SERVER(inode),
2717 _nfs4_proc_access(inode, entry),
2718 &exception);
2719 } while (exception.retry);
2720 return err;
2721}
2722
2723/*
2724 * TODO: For the time being, we don't try to get any attributes
2725 * along with any of the zero-copy operations READ, READDIR,
2726 * READLINK, WRITE.
2727 *
2728 * In the case of the first three, we want to put the GETATTR
2729 * after the read-type operation -- this is because it is hard
2730 * to predict the length of a GETATTR response in v4, and thus
2731 * align the READ data correctly. This means that the GETATTR
2732 * may end up partially falling into the page cache, and we should
2733 * shift it into the 'tail' of the xdr_buf before processing.
2734 * To do this efficiently, we need to know the total length
2735 * of data received, which doesn't seem to be available outside
2736 * of the RPC layer.
2737 *
2738 * In the case of WRITE, we also want to put the GETATTR after
2739 * the operation -- in this case because we want to make sure
2740 * we get the post-operation mtime and size. This means that
2741 * we can't use xdr_encode_pages() as written: we need a variant
2742 * of it which would leave room in the 'tail' iovec.
2743 *
2744 * Both of these changes to the XDR layer would in fact be quite
2745 * minor, but I decided to leave them for a subsequent patch.
2746 */
2747static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2748 unsigned int pgbase, unsigned int pglen)
2749{
2750 struct nfs4_readlink args = {
2751 .fh = NFS_FH(inode),
2752 .pgbase = pgbase,
2753 .pglen = pglen,
2754 .pages = &page,
2755 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002756 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 struct rpc_message msg = {
2758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2759 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002760 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 };
2762
Bryan Schumaker7c513052011-03-24 17:12:24 +00002763 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 -07002764}
2765
2766static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2767 unsigned int pgbase, unsigned int pglen)
2768{
2769 struct nfs4_exception exception = { };
2770 int err;
2771 do {
2772 err = nfs4_handle_exception(NFS_SERVER(inode),
2773 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2774 &exception);
2775 } while (exception.retry);
2776 return err;
2777}
2778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779/*
2780 * Got race?
2781 * We will need to arrange for the VFS layer to provide an atomic open.
2782 * Until then, this create/open method is prone to inefficiency and race
2783 * conditions due to the lookup, create, and open VFS calls from sys_open()
2784 * placed on the wire.
2785 *
2786 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2787 * The file will be opened again in the subsequent VFS open call
2788 * (nfs4_proc_file_open).
2789 *
2790 * The open for read will just hang around to be used by any process that
2791 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2792 */
2793
2794static int
2795nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002796 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
Al Viro82a2c1b2011-06-22 18:30:55 -04002798 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002800 struct rpc_cred *cred = NULL;
2801 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 int status = 0;
2803
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002804 if (ctx != NULL) {
2805 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002806 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002807 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002809 sattr->ia_mode &= ~current_umask();
Andy Adamson82be4172012-05-23 05:02:35 -04002810 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred, NULL);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002811 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (IS_ERR(state)) {
2813 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002816 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002817 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002818 if (ctx != NULL)
2819 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002820 else
Al Viro643168c2011-06-22 18:20:23 -04002821 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822out:
2823 return status;
2824}
2825
2826static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2827{
Trond Myklebust16e42952005-10-27 22:12:44 -04002828 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002829 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002831 .name.len = name->len,
2832 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002834 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002835 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002836 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2839 .rpc_argp = &args,
2840 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002842 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002843
Bryan Schumaker7c513052011-03-24 17:12:24 +00002844 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002845 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002846 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2851{
2852 struct nfs4_exception exception = { };
2853 int err;
2854 do {
2855 err = nfs4_handle_exception(NFS_SERVER(dir),
2856 _nfs4_proc_remove(dir, name),
2857 &exception);
2858 } while (exception.retry);
2859 return err;
2860}
2861
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002862static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002864 struct nfs_server *server = NFS_SERVER(dir);
2865 struct nfs_removeargs *args = msg->rpc_argp;
2866 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002868 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002870 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002873static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2874{
2875 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2876 &data->args.seq_args,
2877 &data->res.seq_res,
2878 task))
2879 return;
2880 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002883static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002885 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2886
Trond Myklebust14516c32010-07-31 14:29:06 -04002887 if (!nfs4_sequence_done(task, &res->seq_res))
2888 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002889 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002890 return 0;
2891 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002892 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Jeff Laytond3d41522010-09-17 17:31:57 -04002895static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2896{
2897 struct nfs_server *server = NFS_SERVER(dir);
2898 struct nfs_renameargs *arg = msg->rpc_argp;
2899 struct nfs_renameres *res = msg->rpc_resp;
2900
2901 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002902 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002903 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002904}
2905
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002906static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2907{
2908 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2909 &data->args.seq_args,
2910 &data->res.seq_res,
2911 task))
2912 return;
2913 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002914}
2915
2916static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2917 struct inode *new_dir)
2918{
2919 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2920
2921 if (!nfs4_sequence_done(task, &res->seq_res))
2922 return 0;
2923 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2924 return 0;
2925
2926 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002927 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002928 return 1;
2929}
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2932 struct inode *new_dir, struct qstr *new_name)
2933{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002934 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002935 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 .old_dir = NFS_FH(old_dir),
2937 .new_dir = NFS_FH(new_dir),
2938 .old_name = old_name,
2939 .new_name = new_name,
2940 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002941 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002942 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002943 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 struct rpc_message msg = {
2945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2946 .rpc_argp = &arg,
2947 .rpc_resp = &res,
2948 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002949 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Bryan Schumaker7c513052011-03-24 17:12:24 +00002951 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (!status) {
2953 update_changeattr(old_dir, &res.old_cinfo);
2954 update_changeattr(new_dir, &res.new_cinfo);
2955 }
2956 return status;
2957}
2958
2959static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2960 struct inode *new_dir, struct qstr *new_name)
2961{
2962 struct nfs4_exception exception = { };
2963 int err;
2964 do {
2965 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2966 _nfs4_proc_rename(old_dir, old_name,
2967 new_dir, new_name),
2968 &exception);
2969 } while (exception.retry);
2970 return err;
2971}
2972
2973static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2974{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002975 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct nfs4_link_arg arg = {
2977 .fh = NFS_FH(inode),
2978 .dir_fh = NFS_FH(dir),
2979 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002980 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002982 struct nfs4_link_res res = {
2983 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002984 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct rpc_message msg = {
2986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2987 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002988 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002990 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Trond Myklebust136f2622010-04-16 16:22:49 -04002992 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04002993 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04002994 goto out;
2995
Bryan Schumaker7c513052011-03-24 17:12:24 +00002996 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002997 if (!status) {
2998 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002999 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04003000 }
Trond Myklebust136f2622010-04-16 16:22:49 -04003001out:
Trond Myklebust136f2622010-04-16 16:22:49 -04003002 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return status;
3004}
3005
3006static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
3007{
3008 struct nfs4_exception exception = { };
3009 int err;
3010 do {
3011 err = nfs4_handle_exception(NFS_SERVER(inode),
3012 _nfs4_proc_link(inode, dir, name),
3013 &exception);
3014 } while (exception.retry);
3015 return err;
3016}
3017
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003018struct nfs4_createdata {
3019 struct rpc_message msg;
3020 struct nfs4_create_arg arg;
3021 struct nfs4_create_res res;
3022 struct nfs_fh fh;
3023 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003024};
3025
3026static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
3027 struct qstr *name, struct iattr *sattr, u32 ftype)
3028{
3029 struct nfs4_createdata *data;
3030
3031 data = kzalloc(sizeof(*data), GFP_KERNEL);
3032 if (data != NULL) {
3033 struct nfs_server *server = NFS_SERVER(dir);
3034
3035 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3036 data->msg.rpc_argp = &data->arg;
3037 data->msg.rpc_resp = &data->res;
3038 data->arg.dir_fh = NFS_FH(dir);
3039 data->arg.server = server;
3040 data->arg.name = name;
3041 data->arg.attrs = sattr;
3042 data->arg.ftype = ftype;
3043 data->arg.bitmask = server->attr_bitmask;
3044 data->res.server = server;
3045 data->res.fh = &data->fh;
3046 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 }
3049 return data;
3050}
3051
3052static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3053{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003054 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003055 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003056 if (status == 0) {
3057 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3059 }
3060 return status;
3061}
3062
3063static void nfs4_free_createdata(struct nfs4_createdata *data)
3064{
3065 kfree(data);
3066}
3067
Chuck Lever4f390c12006-08-22 20:06:22 -04003068static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003069 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 struct nfs4_createdata *data;
3072 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Chuck Lever94a6d752006-08-22 20:06:23 -04003074 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003075 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003076
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003077 status = -ENOMEM;
3078 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3079 if (data == NULL)
3080 goto out;
3081
3082 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3083 data->arg.u.symlink.pages = &page;
3084 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003086 status = nfs4_do_create(dir, dentry, data);
3087
3088 nfs4_free_createdata(data);
3089out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return status;
3091}
3092
Chuck Lever4f390c12006-08-22 20:06:22 -04003093static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003094 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 struct nfs4_exception exception = { };
3097 int err;
3098 do {
3099 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003100 _nfs4_proc_symlink(dir, dentry, page,
3101 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 &exception);
3103 } while (exception.retry);
3104 return err;
3105}
3106
3107static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3108 struct iattr *sattr)
3109{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003110 struct nfs4_createdata *data;
3111 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003113 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3114 if (data == NULL)
3115 goto out;
3116
3117 status = nfs4_do_create(dir, dentry, data);
3118
3119 nfs4_free_createdata(data);
3120out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return status;
3122}
3123
3124static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3125 struct iattr *sattr)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003129
3130 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 do {
3132 err = nfs4_handle_exception(NFS_SERVER(dir),
3133 _nfs4_proc_mkdir(dir, dentry, sattr),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003140 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141{
3142 struct inode *dir = dentry->d_inode;
3143 struct nfs4_readdir_arg args = {
3144 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003145 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 .pgbase = 0,
3147 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003148 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003149 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 };
3151 struct nfs4_readdir_res res;
3152 struct rpc_message msg = {
3153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3154 .rpc_argp = &args,
3155 .rpc_resp = &res,
3156 .rpc_cred = cred,
3157 };
3158 int status;
3159
Harvey Harrison3110ff82008-05-02 13:42:44 -07003160 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003161 dentry->d_parent->d_name.name,
3162 dentry->d_name.name,
3163 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3165 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003166 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 -05003167 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003169 status += args.pgbase;
3170 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003171
3172 nfs_invalidate_atime(dir);
3173
Harvey Harrison3110ff82008-05-02 13:42:44 -07003174 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return status;
3176}
3177
3178static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003179 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 struct nfs4_exception exception = { };
3182 int err;
3183 do {
3184 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3185 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003186 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 &exception);
3188 } while (exception.retry);
3189 return err;
3190}
3191
3192static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3193 struct iattr *sattr, dev_t rdev)
3194{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003195 struct nfs4_createdata *data;
3196 int mode = sattr->ia_mode;
3197 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
3199 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3200 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003201
3202 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3203 if (data == NULL)
3204 goto out;
3205
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003207 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003209 data->arg.ftype = NF4BLK;
3210 data->arg.u.device.specdata1 = MAJOR(rdev);
3211 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 }
3213 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003214 data->arg.ftype = NF4CHR;
3215 data->arg.u.device.specdata1 = MAJOR(rdev);
3216 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003219 status = nfs4_do_create(dir, dentry, data);
3220
3221 nfs4_free_createdata(data);
3222out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 return status;
3224}
3225
3226static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3227 struct iattr *sattr, dev_t rdev)
3228{
3229 struct nfs4_exception exception = { };
3230 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003231
3232 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 do {
3234 err = nfs4_handle_exception(NFS_SERVER(dir),
3235 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3236 &exception);
3237 } while (exception.retry);
3238 return err;
3239}
3240
3241static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3242 struct nfs_fsstat *fsstat)
3243{
3244 struct nfs4_statfs_arg args = {
3245 .fh = fhandle,
3246 .bitmask = server->attr_bitmask,
3247 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003248 struct nfs4_statfs_res res = {
3249 .fsstat = fsstat,
3250 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 struct rpc_message msg = {
3252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3253 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003254 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 };
3256
Trond Myklebust0e574af2005-10-27 22:12:38 -04003257 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003258 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
3261static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3262{
3263 struct nfs4_exception exception = { };
3264 int err;
3265 do {
3266 err = nfs4_handle_exception(server,
3267 _nfs4_proc_statfs(server, fhandle, fsstat),
3268 &exception);
3269 } while (exception.retry);
3270 return err;
3271}
3272
3273static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3274 struct nfs_fsinfo *fsinfo)
3275{
3276 struct nfs4_fsinfo_arg args = {
3277 .fh = fhandle,
3278 .bitmask = server->attr_bitmask,
3279 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003280 struct nfs4_fsinfo_res res = {
3281 .fsinfo = fsinfo,
3282 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 struct rpc_message msg = {
3284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3285 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003286 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 };
3288
Bryan Schumaker7c513052011-03-24 17:12:24 +00003289 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290}
3291
3292static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3293{
3294 struct nfs4_exception exception = { };
3295 int err;
3296
3297 do {
3298 err = nfs4_handle_exception(server,
3299 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3300 &exception);
3301 } while (exception.retry);
3302 return err;
3303}
3304
3305static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3306{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003307 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3309}
3310
3311static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3312 struct nfs_pathconf *pathconf)
3313{
3314 struct nfs4_pathconf_arg args = {
3315 .fh = fhandle,
3316 .bitmask = server->attr_bitmask,
3317 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003318 struct nfs4_pathconf_res res = {
3319 .pathconf = pathconf,
3320 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct rpc_message msg = {
3322 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3323 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003324 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 };
3326
3327 /* None of the pathconf attributes are mandatory to implement */
3328 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3329 memset(pathconf, 0, sizeof(*pathconf));
3330 return 0;
3331 }
3332
Trond Myklebust0e574af2005-10-27 22:12:38 -04003333 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003334 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
3337static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3338 struct nfs_pathconf *pathconf)
3339{
3340 struct nfs4_exception exception = { };
3341 int err;
3342
3343 do {
3344 err = nfs4_handle_exception(server,
3345 _nfs4_proc_pathconf(server, fhandle, pathconf),
3346 &exception);
3347 } while (exception.retry);
3348 return err;
3349}
3350
Benny Halevyd20581a2011-05-22 19:52:03 +03003351void __nfs4_read_done_cb(struct nfs_read_data *data)
3352{
Fred Isamancd841602012-04-20 14:47:44 -04003353 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003354}
3355
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003356static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Fred Isamancd841602012-04-20 14:47:44 -04003358 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003360 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003361 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003362 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003364
Benny Halevyd20581a2011-05-22 19:52:03 +03003365 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003367 renew_lease(server, data->timestamp);
3368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369}
3370
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003371static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3372{
3373
3374 dprintk("--> %s\n", __func__);
3375
3376 if (!nfs4_sequence_done(task, &data->res.seq_res))
3377 return -EAGAIN;
3378
Benny Halevyd20581a2011-05-22 19:52:03 +03003379 return data->read_done_cb ? data->read_done_cb(task, data) :
3380 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003381}
3382
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003383static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003386 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003387 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003388 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003391static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3392{
Fred Isamancd841602012-04-20 14:47:44 -04003393 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003394 &data->args.seq_args,
3395 &data->res.seq_res,
3396 task))
3397 return;
3398 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399}
3400
Fred Isamanb029bc92011-03-03 15:13:42 +00003401static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402{
Fred Isamancd841602012-04-20 14:47:44 -04003403 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003405 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003406 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003407 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003409 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003411 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003412 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003413 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
Fred Isamanb029bc92011-03-03 15:13:42 +00003416static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3417{
3418 if (!nfs4_sequence_done(task, &data->res.seq_res))
3419 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003420 return data->write_done_cb ? data->write_done_cb(task, data) :
3421 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003422}
3423
Trond Myklebust5a37f852012-04-28 14:55:16 -04003424static
3425bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3426{
3427 const struct nfs_pgio_header *hdr = data->header;
3428
3429 /* Don't request attributes for pNFS or O_DIRECT writes */
3430 if (data->ds_clp != NULL || hdr->dreq != NULL)
3431 return false;
3432 /* Otherwise, request attributes if and only if we don't hold
3433 * a delegation
3434 */
3435 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3436}
3437
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003438static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Fred Isamancd841602012-04-20 14:47:44 -04003440 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003441
Trond Myklebust5a37f852012-04-28 14:55:16 -04003442 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003443 data->args.bitmask = NULL;
3444 data->res.fattr = NULL;
3445 } else
3446 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003447
Fred Isamanb029bc92011-03-03 15:13:42 +00003448 if (!data->write_done_cb)
3449 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003450 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 data->timestamp = jiffies;
3452
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003453 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003454 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455}
3456
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003457static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3458{
Fred Isamancd841602012-04-20 14:47:44 -04003459 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003460 &data->args.seq_args,
3461 &data->res.seq_res,
3462 task))
3463 return;
3464 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465}
3466
Fred Isaman0b7c0152012-04-20 14:47:39 -04003467static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3468{
3469 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3470 &data->args.seq_args,
3471 &data->res.seq_res,
3472 task))
3473 return;
3474 rpc_call_start(task);
3475}
3476
3477static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003480
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003481 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003482 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003483 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
Fred Isaman0b7c0152012-04-20 14:47:39 -04003488static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003489{
3490 if (!nfs4_sequence_done(task, &data->res.seq_res))
3491 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003492 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003493}
3494
Fred Isaman0b7c0152012-04-20 14:47:39 -04003495static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003497 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003498
Fred Isaman0b7c0152012-04-20 14:47:39 -04003499 if (data->commit_done_cb == NULL)
3500 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003501 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003502 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003503 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003506struct nfs4_renewdata {
3507 struct nfs_client *client;
3508 unsigned long timestamp;
3509};
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511/*
3512 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3513 * standalone procedure for queueing an asynchronous RENEW.
3514 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003515static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003516{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003517 struct nfs4_renewdata *data = calldata;
3518 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003519
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003520 if (atomic_read(&clp->cl_count) > 1)
3521 nfs4_schedule_state_renewal(clp);
3522 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003523 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003524}
3525
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003526static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003528 struct nfs4_renewdata *data = calldata;
3529 struct nfs_client *clp = data->client;
3530 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003533 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003534 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3535 return;
3536 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003537 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003538 return;
3539 }
3540 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003542 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543}
3544
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003545static const struct rpc_call_ops nfs4_renew_ops = {
3546 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003547 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003548};
3549
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003550static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551{
3552 struct rpc_message msg = {
3553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3554 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003555 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003557 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003559 if (renew_flags == 0)
3560 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003561 if (!atomic_inc_not_zero(&clp->cl_count))
3562 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003563 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003564 if (data == NULL)
3565 return -ENOMEM;
3566 data->client = clp;
3567 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003569 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003572static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
3574 struct rpc_message msg = {
3575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3576 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003577 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 };
3579 unsigned long now = jiffies;
3580 int status;
3581
3582 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3583 if (status < 0)
3584 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003585 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 return 0;
3587}
3588
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003589static inline int nfs4_server_supports_acls(struct nfs_server *server)
3590{
3591 return (server->caps & NFS_CAP_ACLS)
3592 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3593 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3594}
3595
3596/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3597 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3598 * the stack.
3599 */
3600#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3601
Neil Hormane9e3d722011-03-04 19:26:03 -05003602static int buf_to_pages_noslab(const void *buf, size_t buflen,
3603 struct page **pages, unsigned int *pgbase)
3604{
3605 struct page *newpage, **spages;
3606 int rc = 0;
3607 size_t len;
3608 spages = pages;
3609
3610 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003611 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003612 newpage = alloc_page(GFP_KERNEL);
3613
3614 if (newpage == NULL)
3615 goto unwind;
3616 memcpy(page_address(newpage), buf, len);
3617 buf += len;
3618 buflen -= len;
3619 *pages++ = newpage;
3620 rc++;
3621 } while (buflen != 0);
3622
3623 return rc;
3624
3625unwind:
3626 for(; rc > 0; rc--)
3627 __free_page(spages[rc-1]);
3628 return -ENOMEM;
3629}
3630
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003631struct nfs4_cached_acl {
3632 int cached;
3633 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003634 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003635};
3636
3637static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003638{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003639 struct nfs_inode *nfsi = NFS_I(inode);
3640
3641 spin_lock(&inode->i_lock);
3642 kfree(nfsi->nfs4_acl);
3643 nfsi->nfs4_acl = acl;
3644 spin_unlock(&inode->i_lock);
3645}
3646
3647static void nfs4_zap_acl_attr(struct inode *inode)
3648{
3649 nfs4_set_cached_acl(inode, NULL);
3650}
3651
3652static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3653{
3654 struct nfs_inode *nfsi = NFS_I(inode);
3655 struct nfs4_cached_acl *acl;
3656 int ret = -ENOENT;
3657
3658 spin_lock(&inode->i_lock);
3659 acl = nfsi->nfs4_acl;
3660 if (acl == NULL)
3661 goto out;
3662 if (buf == NULL) /* user is just asking for length */
3663 goto out_len;
3664 if (acl->cached == 0)
3665 goto out;
3666 ret = -ERANGE; /* see getxattr(2) man page */
3667 if (acl->len > buflen)
3668 goto out;
3669 memcpy(buf, acl->data, acl->len);
3670out_len:
3671 ret = acl->len;
3672out:
3673 spin_unlock(&inode->i_lock);
3674 return ret;
3675}
3676
Sachin Prabhu5794d212012-04-17 14:36:40 +01003677static 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 +00003678{
3679 struct nfs4_cached_acl *acl;
3680
Sachin Prabhu5794d212012-04-17 14:36:40 +01003681 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003682 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3683 if (acl == NULL)
3684 goto out;
3685 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003686 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003687 } else {
3688 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3689 if (acl == NULL)
3690 goto out;
3691 acl->cached = 0;
3692 }
3693 acl->len = acl_len;
3694out:
3695 nfs4_set_cached_acl(inode, acl);
3696}
3697
Andy Adamsonbf118a32011-12-07 11:55:27 -05003698/*
3699 * The getxattr API returns the required buffer length when called with a
3700 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3701 * the required buf. On a NULL buf, we send a page of data to the server
3702 * guessing that the ACL request can be serviced by a page. If so, we cache
3703 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3704 * the cache. If not so, we throw away the page, and cache the required
3705 * length. The next getxattr call will then produce another round trip to
3706 * the server, this time with the input buf of the required size.
3707 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003708static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003709{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003710 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003711 struct nfs_getaclargs args = {
3712 .fh = NFS_FH(inode),
3713 .acl_pages = pages,
3714 .acl_len = buflen,
3715 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003716 struct nfs_getaclres res = {
3717 .acl_len = buflen,
3718 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003719 struct rpc_message msg = {
3720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3721 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003722 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003723 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003724 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003725
Andy Adamsonbf118a32011-12-07 11:55:27 -05003726 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3727 /* As long as we're doing a round trip to the server anyway,
3728 * let's be prepared for a page of acl data. */
3729 if (npages == 0)
3730 npages = 1;
3731
Sachin Prabhu5a006892012-04-17 14:35:39 +01003732 /* Add an extra page to handle the bitmap returned */
3733 npages++;
3734
Andy Adamsonbf118a32011-12-07 11:55:27 -05003735 for (i = 0; i < npages; i++) {
3736 pages[i] = alloc_page(GFP_KERNEL);
3737 if (!pages[i])
3738 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003739 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003740
3741 /* for decoding across pages */
3742 res.acl_scratch = alloc_page(GFP_KERNEL);
3743 if (!res.acl_scratch)
3744 goto out_free;
3745
Andy Adamsonbf118a32011-12-07 11:55:27 -05003746 args.acl_len = npages * PAGE_SIZE;
3747 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003748
Andy Adamsonbf118a32011-12-07 11:55:27 -05003749 /* Let decode_getfacl know not to fail if the ACL data is larger than
3750 * the page we send as a guess */
3751 if (buf == NULL)
3752 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003753
Peng Taode040be2012-01-10 22:42:47 +08003754 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 __func__, buf, buflen, npages, args.acl_len);
3756 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3757 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003758 if (ret)
3759 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003760
3761 acl_len = res.acl_len - res.acl_data_offset;
3762 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003763 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003764 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003765 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003767 if (buf) {
3768 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003769 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003771 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003772 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003774 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003775out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003776 for (i = 0; i < npages; i++)
3777 if (pages[i])
3778 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003779 if (res.acl_scratch)
3780 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781 return ret;
3782}
3783
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003784static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3785{
3786 struct nfs4_exception exception = { };
3787 ssize_t ret;
3788 do {
3789 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3790 if (ret >= 0)
3791 break;
3792 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3793 } while (exception.retry);
3794 return ret;
3795}
3796
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003797static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3798{
3799 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003800 int ret;
3801
3802 if (!nfs4_server_supports_acls(server))
3803 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003804 ret = nfs_revalidate_inode(server, inode);
3805 if (ret < 0)
3806 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003807 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3808 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003809 ret = nfs4_read_cached_acl(inode, buf, buflen);
3810 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003811 /* -ENOENT is returned if there is no ACL or if there is an ACL
3812 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003813 return ret;
3814 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003815}
3816
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003817static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003818{
3819 struct nfs_server *server = NFS_SERVER(inode);
3820 struct page *pages[NFS4ACL_MAXPAGES];
3821 struct nfs_setaclargs arg = {
3822 .fh = NFS_FH(inode),
3823 .acl_pages = pages,
3824 .acl_len = buflen,
3825 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003826 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003827 struct rpc_message msg = {
3828 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3829 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003830 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003831 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003832 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833
3834 if (!nfs4_server_supports_acls(server))
3835 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003836 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3837 if (i < 0)
3838 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003839 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003840 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003841
3842 /*
3843 * Free each page after tx, so the only ref left is
3844 * held by the network stack
3845 */
3846 for (; i > 0; i--)
3847 put_page(pages[i-1]);
3848
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003849 /*
3850 * Acl update can result in inode attribute update.
3851 * so mark the attribute cache invalid.
3852 */
3853 spin_lock(&inode->i_lock);
3854 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3855 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003856 nfs_access_zap_cache(inode);
3857 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003858 return ret;
3859}
3860
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003861static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3862{
3863 struct nfs4_exception exception = { };
3864 int err;
3865 do {
3866 err = nfs4_handle_exception(NFS_SERVER(inode),
3867 __nfs4_proc_set_acl(inode, buf, buflen),
3868 &exception);
3869 } while (exception.retry);
3870 return err;
3871}
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003874nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003876 struct nfs_client *clp = server->nfs_client;
3877
3878 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return 0;
3880 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003881 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003882 case -NFS4ERR_ADMIN_REVOKED:
3883 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003884 if (state == NULL)
3885 break;
3886 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003887 case -NFS4ERR_OPENMODE:
3888 if (state == NULL)
3889 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003890 nfs4_schedule_stateid_recovery(server, state);
3891 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003892 case -NFS4ERR_EXPIRED:
3893 if (state != NULL)
3894 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003896 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003897 nfs4_schedule_lease_recovery(clp);
3898 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003899#if defined(CONFIG_NFS_V4_1)
3900 case -NFS4ERR_BADSESSION:
3901 case -NFS4ERR_BADSLOT:
3902 case -NFS4ERR_BAD_HIGH_SLOT:
3903 case -NFS4ERR_DEADSESSION:
3904 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3905 case -NFS4ERR_SEQ_FALSE_RETRY:
3906 case -NFS4ERR_SEQ_MISORDERED:
3907 dprintk("%s ERROR %d, Reset session\n", __func__,
3908 task->tk_status);
Trond Myklebust9f594792012-05-27 13:02:53 -04003909 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
Andy Adamson4745e312009-04-01 09:22:42 -04003910 task->tk_status = 0;
3911 return -EAGAIN;
3912#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003914 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003915 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003916 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3918 task->tk_status = 0;
3919 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003920 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 case -NFS4ERR_OLD_STATEID:
3922 task->tk_status = 0;
3923 return -EAGAIN;
3924 }
3925 task->tk_status = nfs4_map_errors(task->tk_status);
3926 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003927wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003928 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003929 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3930 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3931 task->tk_status = 0;
3932 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933}
3934
Chuck Leverf0920752012-05-21 22:45:41 -04003935static void nfs4_init_boot_verifier(const struct nfs_client *clp,
3936 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05003937{
3938 __be32 verf[2];
3939
Chuck Lever2c820d92012-05-21 22:45:33 -04003940 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
3941 /* An impossible timestamp guarantees this value
3942 * will never match a generated boot time. */
3943 verf[0] = 0;
3944 verf[1] = (__be32)(NSEC_PER_SEC + 1);
3945 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04003946 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
3947 verf[0] = (__be32)nn->boot_time.tv_sec;
3948 verf[1] = (__be32)nn->boot_time.tv_nsec;
Chuck Lever2c820d92012-05-21 22:45:33 -04003949 }
Chuck Levercd937102012-03-02 17:14:31 -05003950 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3951}
3952
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003953int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3954 unsigned short port, struct rpc_cred *cred,
3955 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 nfs4_verifier sc_verifier;
3958 struct nfs4_setclientid setclientid = {
3959 .sc_verifier = &sc_verifier,
3960 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003961 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 };
3963 struct rpc_message msg = {
3964 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3965 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003966 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003967 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 int loop = 0;
3970 int status;
3971
Chuck Leverf0920752012-05-21 22:45:41 -04003972 nfs4_init_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003975 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003977 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003978 clp->cl_ipaddr,
3979 rpc_peeraddr2str(clp->cl_rpcclient,
3980 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003981 rpc_peeraddr2str(clp->cl_rpcclient,
3982 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003983 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 clp->cl_id_uniquifier);
3985 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003986 sizeof(setclientid.sc_netid),
3987 rpc_peeraddr2str(clp->cl_rpcclient,
3988 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003990 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003992 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003994 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (status != -NFS4ERR_CLID_INUSE)
3996 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003997 if (loop != 0) {
3998 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004000 }
4001 ++loop;
4002 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004 return status;
4005}
4006
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004007int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004008 struct nfs4_setclientid_res *arg,
4009 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
4011 struct nfs_fsinfo fsinfo;
4012 struct rpc_message msg = {
4013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004014 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004016 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 };
4018 unsigned long now;
4019 int status;
4020
4021 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004022 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (status == 0) {
4024 spin_lock(&clp->cl_lock);
4025 clp->cl_lease_time = fsinfo.lease_time * HZ;
4026 clp->cl_last_renewal = now;
4027 spin_unlock(&clp->cl_lock);
4028 }
4029 return status;
4030}
4031
Trond Myklebustfe650402006-01-03 09:55:18 +01004032struct nfs4_delegreturndata {
4033 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004034 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004035 struct nfs_fh fh;
4036 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004037 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004038 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004039 int rpc_status;
4040};
4041
Trond Myklebustfe650402006-01-03 09:55:18 +01004042static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4043{
4044 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004045
Trond Myklebust14516c32010-07-31 14:29:06 -04004046 if (!nfs4_sequence_done(task, &data->res.seq_res))
4047 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004048
Ricardo Labiaga79708862009-12-07 09:23:21 -05004049 switch (task->tk_status) {
4050 case -NFS4ERR_STALE_STATEID:
4051 case -NFS4ERR_EXPIRED:
4052 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004053 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004054 break;
4055 default:
4056 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4057 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004058 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004059 return;
4060 }
4061 }
4062 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004063}
4064
4065static void nfs4_delegreturn_release(void *calldata)
4066{
Trond Myklebustfe650402006-01-03 09:55:18 +01004067 kfree(calldata);
4068}
4069
Andy Adamson938e1012009-04-01 09:22:28 -04004070#if defined(CONFIG_NFS_V4_1)
4071static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4072{
4073 struct nfs4_delegreturndata *d_data;
4074
4075 d_data = (struct nfs4_delegreturndata *)data;
4076
Trond Myklebust035168a2010-06-16 09:52:26 -04004077 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004078 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004079 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004080 return;
4081 rpc_call_start(task);
4082}
4083#endif /* CONFIG_NFS_V4_1 */
4084
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004085static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004086#if defined(CONFIG_NFS_V4_1)
4087 .rpc_call_prepare = nfs4_delegreturn_prepare,
4088#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004089 .rpc_call_done = nfs4_delegreturn_done,
4090 .rpc_release = nfs4_delegreturn_release,
4091};
4092
Trond Myklebuste6f81072008-01-24 18:14:34 -05004093static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004094{
4095 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004096 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004097 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004098 struct rpc_message msg = {
4099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4100 .rpc_cred = cred,
4101 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004102 struct rpc_task_setup task_setup_data = {
4103 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004104 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004105 .callback_ops = &nfs4_delegreturn_ops,
4106 .flags = RPC_TASK_ASYNC,
4107 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004108 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004109
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004110 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004111 if (data == NULL)
4112 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004113 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004114 data->args.fhandle = &data->fh;
4115 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004116 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004117 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004118 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004119 data->res.fattr = &data->fattr;
4120 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004121 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004122 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004123 data->rpc_status = 0;
4124
Trond Myklebustc970aa82007-07-14 15:39:59 -04004125 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004126 msg.rpc_argp = &data->args;
4127 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004128 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004129 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004130 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004131 if (!issync)
4132 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004133 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004134 if (status != 0)
4135 goto out;
4136 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004137 if (status == 0)
4138 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4139 else
4140 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004141out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004142 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004143 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144}
4145
Trond Myklebuste6f81072008-01-24 18:14:34 -05004146int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147{
4148 struct nfs_server *server = NFS_SERVER(inode);
4149 struct nfs4_exception exception = { };
4150 int err;
4151 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004152 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 switch (err) {
4154 case -NFS4ERR_STALE_STATEID:
4155 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 case 0:
4157 return 0;
4158 }
4159 err = nfs4_handle_exception(server, err, &exception);
4160 } while (exception.retry);
4161 return err;
4162}
4163
4164#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4165#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4166
4167/*
4168 * sleep, with exponential backoff, and retry the LOCK operation.
4169 */
4170static unsigned long
4171nfs4_set_lock_task_retry(unsigned long timeout)
4172{
Jeff Laytond3103102011-12-01 22:44:39 +01004173 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 timeout <<= 1;
4175 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4176 return NFS4_LOCK_MAXTIMEOUT;
4177 return timeout;
4178}
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4181{
4182 struct inode *inode = state->inode;
4183 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004184 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004185 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004187 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004189 struct nfs_lockt_res res = {
4190 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 };
4192 struct rpc_message msg = {
4193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4194 .rpc_argp = &arg,
4195 .rpc_resp = &res,
4196 .rpc_cred = state->owner->so_cred,
4197 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 struct nfs4_lock_state *lsp;
4199 int status;
4200
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004201 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004202 status = nfs4_set_lock_state(state, request);
4203 if (status != 0)
4204 goto out;
4205 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004206 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004207 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004208 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004209 switch (status) {
4210 case 0:
4211 request->fl_type = F_UNLCK;
4212 break;
4213 case -NFS4ERR_DENIED:
4214 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004216 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return status;
4219}
4220
4221static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4222{
4223 struct nfs4_exception exception = { };
4224 int err;
4225
4226 do {
4227 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4228 _nfs4_proc_getlk(state, cmd, request),
4229 &exception);
4230 } while (exception.retry);
4231 return err;
4232}
4233
4234static int do_vfs_lock(struct file *file, struct file_lock *fl)
4235{
4236 int res = 0;
4237 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4238 case FL_POSIX:
4239 res = posix_lock_file_wait(file, fl);
4240 break;
4241 case FL_FLOCK:
4242 res = flock_lock_file_wait(file, fl);
4243 break;
4244 default:
4245 BUG();
4246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 return res;
4248}
4249
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004250struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004251 struct nfs_locku_args arg;
4252 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004253 struct nfs4_lock_state *lsp;
4254 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004255 struct file_lock fl;
4256 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004257 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004258};
4259
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004260static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4261 struct nfs_open_context *ctx,
4262 struct nfs4_lock_state *lsp,
4263 struct nfs_seqid *seqid)
4264{
4265 struct nfs4_unlockdata *p;
4266 struct inode *inode = lsp->ls_state->inode;
4267
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004268 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004269 if (p == NULL)
4270 return NULL;
4271 p->arg.fh = NFS_FH(inode);
4272 p->arg.fl = &p->fl;
4273 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004274 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004275 p->arg.stateid = &lsp->ls_stateid;
4276 p->lsp = lsp;
4277 atomic_inc(&lsp->ls_count);
4278 /* Ensure we don't close file until we're done freeing locks! */
4279 p->ctx = get_nfs_open_context(ctx);
4280 memcpy(&p->fl, fl, sizeof(p->fl));
4281 p->server = NFS_SERVER(inode);
4282 return p;
4283}
4284
Trond Myklebust06f814a2006-01-03 09:55:07 +01004285static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004286{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004287 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004288 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004289 nfs4_put_lock_state(calldata->lsp);
4290 put_nfs_open_context(calldata->ctx);
4291 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004292}
4293
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004294static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004295{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004296 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004297
Trond Myklebust14516c32010-07-31 14:29:06 -04004298 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4299 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004300 switch (task->tk_status) {
4301 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004302 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4303 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004304 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004305 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004306 case -NFS4ERR_BAD_STATEID:
4307 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004308 case -NFS4ERR_STALE_STATEID:
4309 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004310 break;
4311 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004312 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004313 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004314 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004315}
4316
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004317static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004318{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004319 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004321 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004322 return;
4323 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004324 /* Note: exit _without_ running nfs4_locku_done */
4325 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004326 return;
4327 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004328 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004329 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004330 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004331 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004332 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004333 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004336static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004337 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004338 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004339 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004340};
4341
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004342static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4343 struct nfs_open_context *ctx,
4344 struct nfs4_lock_state *lsp,
4345 struct nfs_seqid *seqid)
4346{
4347 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004348 struct rpc_message msg = {
4349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4350 .rpc_cred = ctx->cred,
4351 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004352 struct rpc_task_setup task_setup_data = {
4353 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004354 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004355 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004356 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004357 .flags = RPC_TASK_ASYNC,
4358 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004359
Frank Filz137d6ac2007-07-09 15:32:29 -07004360 /* Ensure this is an unlock - when canceling a lock, the
4361 * canceled lock is passed in, and it won't be an unlock.
4362 */
4363 fl->fl_type = F_UNLCK;
4364
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004365 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4366 if (data == NULL) {
4367 nfs_free_seqid(seqid);
4368 return ERR_PTR(-ENOMEM);
4369 }
4370
Trond Myklebust9d12b212012-01-17 22:04:25 -05004371 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004372 msg.rpc_argp = &data->arg;
4373 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004374 task_setup_data.callback_data = data;
4375 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004376}
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4379{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004380 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004381 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004382 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004383 struct rpc_task *task;
4384 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004385 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Trond Myklebust9b073572006-06-29 16:38:34 -04004387 status = nfs4_set_lock_state(state, request);
4388 /* Unlock _before_ we do the RPC call */
4389 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004390 down_read(&nfsi->rwsem);
4391 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4392 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004393 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004394 }
4395 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004396 if (status != 0)
4397 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004398 /* Is this a delegated lock? */
4399 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004400 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004401 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004402 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004403 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004404 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004405 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004406 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407 status = PTR_ERR(task);
4408 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004409 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004410 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004411 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004412out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004413 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004414 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417struct nfs4_lockdata {
4418 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004419 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004420 struct nfs4_lock_state *lsp;
4421 struct nfs_open_context *ctx;
4422 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004423 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004424 int rpc_status;
4425 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004426 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004427};
4428
4429static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004430 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4431 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004432{
4433 struct nfs4_lockdata *p;
4434 struct inode *inode = lsp->ls_state->inode;
4435 struct nfs_server *server = NFS_SERVER(inode);
4436
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004437 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004438 if (p == NULL)
4439 return NULL;
4440
4441 p->arg.fh = NFS_FH(inode);
4442 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004443 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004444 if (p->arg.open_seqid == NULL)
4445 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004446 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004448 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004449 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004450 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004451 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004452 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004453 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004455 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004456 atomic_inc(&lsp->ls_count);
4457 p->ctx = get_nfs_open_context(ctx);
4458 memcpy(&p->fl, fl, sizeof(p->fl));
4459 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004460out_free_seqid:
4461 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004462out_free:
4463 kfree(p);
4464 return NULL;
4465}
4466
4467static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4468{
4469 struct nfs4_lockdata *data = calldata;
4470 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Harvey Harrison3110ff82008-05-02 13:42:44 -07004472 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004473 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4474 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004475 /* Do we need to do an open_to_lock_owner? */
4476 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004477 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4478 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004479 data->arg.open_stateid = &state->stateid;
4480 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004481 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004482 } else
4483 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004484 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004485 if (nfs4_setup_sequence(data->server,
4486 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004487 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004488 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004489 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004490 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004491}
4492
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004493static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4494{
4495 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4496 nfs4_lock_prepare(task, calldata);
4497}
4498
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004499static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4500{
4501 struct nfs4_lockdata *data = calldata;
4502
Harvey Harrison3110ff82008-05-02 13:42:44 -07004503 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004504
Trond Myklebust14516c32010-07-31 14:29:06 -04004505 if (!nfs4_sequence_done(task, &data->res.seq_res))
4506 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004507
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004509 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004510 if (data->rpc_status == 0)
4511 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4512 else
4513 goto out;
4514 }
4515 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004516 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004518 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004519 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004521 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004522}
4523
4524static void nfs4_lock_release(void *calldata)
4525{
4526 struct nfs4_lockdata *data = calldata;
4527
Harvey Harrison3110ff82008-05-02 13:42:44 -07004528 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004529 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004530 if (data->cancelled != 0) {
4531 struct rpc_task *task;
4532 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4533 data->arg.lock_seqid);
4534 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004535 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004536 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004537 } else
4538 nfs_free_seqid(data->arg.lock_seqid);
4539 nfs4_put_lock_state(data->lsp);
4540 put_nfs_open_context(data->ctx);
4541 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004542 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004543}
4544
4545static const struct rpc_call_ops nfs4_lock_ops = {
4546 .rpc_call_prepare = nfs4_lock_prepare,
4547 .rpc_call_done = nfs4_lock_done,
4548 .rpc_release = nfs4_lock_release,
4549};
4550
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004551static const struct rpc_call_ops nfs4_recover_lock_ops = {
4552 .rpc_call_prepare = nfs4_recover_lock_prepare,
4553 .rpc_call_done = nfs4_lock_done,
4554 .rpc_release = nfs4_lock_release,
4555};
4556
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004557static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4558{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004559 switch (error) {
4560 case -NFS4ERR_ADMIN_REVOKED:
4561 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004562 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004563 if (new_lock_owner != 0 ||
4564 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004565 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004566 break;
4567 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004568 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004569 case -NFS4ERR_EXPIRED:
4570 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004571 };
4572}
4573
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004574static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004575{
4576 struct nfs4_lockdata *data;
4577 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004578 struct rpc_message msg = {
4579 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4580 .rpc_cred = state->owner->so_cred,
4581 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004582 struct rpc_task_setup task_setup_data = {
4583 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004584 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004585 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004586 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004587 .flags = RPC_TASK_ASYNC,
4588 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004589 int ret;
4590
Harvey Harrison3110ff82008-05-02 13:42:44 -07004591 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004592 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004593 fl->fl_u.nfs4_fl.owner,
4594 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004595 if (data == NULL)
4596 return -ENOMEM;
4597 if (IS_SETLKW(cmd))
4598 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004599 if (recovery_type > NFS_LOCK_NEW) {
4600 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004601 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004602 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4603 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004604 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004605 msg.rpc_argp = &data->arg;
4606 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004607 task_setup_data.callback_data = data;
4608 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004609 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004610 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004611 ret = nfs4_wait_for_completion_rpc_task(task);
4612 if (ret == 0) {
4613 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004614 if (ret)
4615 nfs4_handle_setlk_error(data->server, data->lsp,
4616 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004617 } else
4618 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004619 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004620 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004621 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
4624static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4625{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004626 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004627 struct nfs4_exception exception = {
4628 .inode = state->inode,
4629 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004630 int err;
4631
4632 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004633 /* Cache the lock if possible... */
4634 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4635 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004636 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004637 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004638 break;
4639 nfs4_handle_exception(server, err, &exception);
4640 } while (exception.retry);
4641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642}
4643
4644static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4645{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004646 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004647 struct nfs4_exception exception = {
4648 .inode = state->inode,
4649 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004650 int err;
4651
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004652 err = nfs4_set_lock_state(state, request);
4653 if (err != 0)
4654 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004655 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004656 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4657 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004658 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004659 switch (err) {
4660 default:
4661 goto out;
4662 case -NFS4ERR_GRACE:
4663 case -NFS4ERR_DELAY:
4664 nfs4_handle_exception(server, err, &exception);
4665 err = 0;
4666 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004667 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004668out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004669 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670}
4671
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004672#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004673static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004674{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004675 int status, ret = NFS_OK;
4676 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004677 struct nfs_server *server = NFS_SERVER(state->inode);
4678
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004679 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4680 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4681 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4682 if (status != NFS_OK) {
4683 nfs41_free_stateid(server, &lsp->ls_stateid);
4684 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4685 ret = status;
4686 }
4687 }
4688 };
4689
4690 return ret;
4691}
4692
4693static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4694{
4695 int status = NFS_OK;
4696
4697 if (test_bit(LK_STATE_IN_USE, &state->flags))
4698 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004699 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004700 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004701 return nfs4_lock_expired(state, request);
4702}
4703#endif
4704
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4706{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004707 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004708 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004709 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004711 if ((fl_flags & FL_POSIX) &&
4712 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4713 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004714 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004715 status = nfs4_set_lock_state(state, request);
4716 if (status != 0)
4717 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004718 request->fl_flags |= FL_ACCESS;
4719 status = do_vfs_lock(request->fl_file, request);
4720 if (status < 0)
4721 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004722 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004723 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004724 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004725 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004726 request->fl_flags = fl_flags & ~FL_SLEEP;
4727 status = do_vfs_lock(request->fl_file, request);
4728 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004729 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004730 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004731 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004732 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004733 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004734 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004735 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004736 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4737 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004738out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004739 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004740out:
4741 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 return status;
4743}
4744
4745static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4746{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004747 struct nfs4_exception exception = {
4748 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004749 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004750 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 int err;
4752
4753 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004754 err = _nfs4_proc_setlk(state, cmd, request);
4755 if (err == -NFS4ERR_DENIED)
4756 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004758 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 } while (exception.retry);
4760 return err;
4761}
4762
4763static int
4764nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4765{
4766 struct nfs_open_context *ctx;
4767 struct nfs4_state *state;
4768 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4769 int status;
4770
4771 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004772 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 state = ctx->state;
4774
4775 if (request->fl_start < 0 || request->fl_end < 0)
4776 return -EINVAL;
4777
Trond Myklebustd9531262009-07-21 19:22:38 -04004778 if (IS_GETLK(cmd)) {
4779 if (state != NULL)
4780 return nfs4_proc_getlk(state, F_GETLK, request);
4781 return 0;
4782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
4784 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4785 return -EINVAL;
4786
Trond Myklebustd9531262009-07-21 19:22:38 -04004787 if (request->fl_type == F_UNLCK) {
4788 if (state != NULL)
4789 return nfs4_proc_unlck(state, cmd, request);
4790 return 0;
4791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Trond Myklebustd9531262009-07-21 19:22:38 -04004793 if (state == NULL)
4794 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004795 /*
4796 * Don't rely on the VFS having checked the file open mode,
4797 * since it won't do this for flock() locks.
4798 */
4799 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4800 case F_RDLCK:
4801 if (!(filp->f_mode & FMODE_READ))
4802 return -EBADF;
4803 break;
4804 case F_WRLCK:
4805 if (!(filp->f_mode & FMODE_WRITE))
4806 return -EBADF;
4807 }
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 do {
4810 status = nfs4_proc_setlk(state, cmd, request);
4811 if ((status != -EAGAIN) || IS_SETLK(cmd))
4812 break;
4813 timeout = nfs4_set_lock_task_retry(timeout);
4814 status = -ERESTARTSYS;
4815 if (signalled())
4816 break;
4817 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 return status;
4819}
4820
Trond Myklebust888e6942005-11-04 15:38:11 -05004821int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4822{
4823 struct nfs_server *server = NFS_SERVER(state->inode);
4824 struct nfs4_exception exception = { };
4825 int err;
4826
4827 err = nfs4_set_lock_state(state, fl);
4828 if (err != 0)
4829 goto out;
4830 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004831 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004832 switch (err) {
4833 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004834 printk(KERN_ERR "NFS: %s: unhandled error "
4835 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004836 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004837 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004838 goto out;
4839 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004840 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004841 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004842 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004843 nfs4_schedule_lease_recovery(server->nfs_client);
4844 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004845 case -NFS4ERR_BADSESSION:
4846 case -NFS4ERR_BADSLOT:
4847 case -NFS4ERR_BAD_HIGH_SLOT:
4848 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4849 case -NFS4ERR_DEADSESSION:
Trond Myklebust9f594792012-05-27 13:02:53 -04004850 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004851 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004852 case -ERESTARTSYS:
4853 /*
4854 * The show must go on: exit, but mark the
4855 * stateid as needing recovery.
4856 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004857 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004858 case -NFS4ERR_ADMIN_REVOKED:
4859 case -NFS4ERR_BAD_STATEID:
4860 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004861 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004862 err = 0;
4863 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004864 case -EKEYEXPIRED:
4865 /*
4866 * User RPCSEC_GSS context has expired.
4867 * We cannot recover this stateid now, so
4868 * skip it and allow recovery thread to
4869 * proceed.
4870 */
4871 err = 0;
4872 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004873 case -ENOMEM:
4874 case -NFS4ERR_DENIED:
4875 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4876 err = 0;
4877 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004878 case -NFS4ERR_DELAY:
4879 break;
4880 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004881 err = nfs4_handle_exception(server, err, &exception);
4882 } while (exception.retry);
4883out:
4884 return err;
4885}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004886
Trond Myklebustcf470c32012-03-07 13:49:12 -05004887struct nfs_release_lockowner_data {
4888 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004889 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004890 struct nfs_release_lockowner_args args;
4891};
4892
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004893static void nfs4_release_lockowner_release(void *calldata)
4894{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004895 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004896 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004897 kfree(calldata);
4898}
4899
Trond Myklebust17280172012-03-11 13:11:00 -04004900static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004901 .rpc_release = nfs4_release_lockowner_release,
4902};
4903
Trond Myklebustcf470c32012-03-07 13:49:12 -05004904int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004905{
4906 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004907 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004908 struct rpc_message msg = {
4909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4910 };
4911
4912 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004913 return -EINVAL;
4914 data = kmalloc(sizeof(*data), GFP_NOFS);
4915 if (!data)
4916 return -ENOMEM;
4917 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004918 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004919 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4920 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4921 data->args.lock_owner.s_dev = server->s_dev;
4922 msg.rpc_argp = &data->args;
4923 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4924 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004925}
4926
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004927#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4928
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004929static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4930 const void *buf, size_t buflen,
4931 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004932{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004933 if (strcmp(key, "") != 0)
4934 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004935
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004936 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004937}
4938
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004939static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4940 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004941{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004942 if (strcmp(key, "") != 0)
4943 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004944
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004945 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004946}
4947
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004948static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4949 size_t list_len, const char *name,
4950 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004951{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004952 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004953
J. Bruce Fields096455a2006-03-20 23:23:42 -05004954 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4955 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004956
4957 if (list && len <= list_len)
4958 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004959 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004960}
4961
Andy Adamson533eb462011-06-13 18:25:56 -04004962/*
4963 * nfs_fhget will use either the mounted_on_fileid or the fileid
4964 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004965static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4966{
Andy Adamson533eb462011-06-13 18:25:56 -04004967 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4968 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4969 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004970 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004971 return;
4972
4973 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004974 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004975 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4976 fattr->nlink = 2;
4977}
4978
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004979static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4980 const struct qstr *name,
4981 struct nfs4_fs_locations *fs_locations,
4982 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004983{
4984 struct nfs_server *server = NFS_SERVER(dir);
4985 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004986 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004987 };
4988 struct nfs4_fs_locations_arg args = {
4989 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004990 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004991 .page = page,
4992 .bitmask = bitmask,
4993 };
Benny Halevy22958462009-04-01 09:22:02 -04004994 struct nfs4_fs_locations_res res = {
4995 .fs_locations = fs_locations,
4996 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4999 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04005000 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04005001 };
5002 int status;
5003
Harvey Harrison3110ff82008-05-02 13:42:44 -07005004 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005005
5006 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5007 * is not supported */
5008 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5009 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5010 else
5011 bitmask[0] |= FATTR4_WORD0_FILEID;
5012
Trond Myklebustc228fd32007-01-13 02:28:11 -05005013 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005014 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005015 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005016 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005017 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005018 return status;
5019}
5020
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005021int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5022 const struct qstr *name,
5023 struct nfs4_fs_locations *fs_locations,
5024 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005025{
5026 struct nfs4_exception exception = { };
5027 int err;
5028 do {
5029 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005030 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005031 &exception);
5032 } while (exception.retry);
5033 return err;
5034}
5035
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005036static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5037{
5038 int status;
5039 struct nfs4_secinfo_arg args = {
5040 .dir_fh = NFS_FH(dir),
5041 .name = name,
5042 };
5043 struct nfs4_secinfo_res res = {
5044 .flavors = flavors,
5045 };
5046 struct rpc_message msg = {
5047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5048 .rpc_argp = &args,
5049 .rpc_resp = &res,
5050 };
5051
5052 dprintk("NFS call secinfo %s\n", name->name);
5053 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5054 dprintk("NFS reply secinfo: %d\n", status);
5055 return status;
5056}
5057
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005058int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5059 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005060{
5061 struct nfs4_exception exception = { };
5062 int err;
5063 do {
5064 err = nfs4_handle_exception(NFS_SERVER(dir),
5065 _nfs4_proc_secinfo(dir, name, flavors),
5066 &exception);
5067 } while (exception.retry);
5068 return err;
5069}
5070
Andy Adamson557134a2009-04-01 09:21:53 -04005071#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005072/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005073 * Check the exchange flags returned by the server for invalid flags, having
5074 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5075 * DS flags set.
5076 */
5077static int nfs4_check_cl_exchange_flags(u32 flags)
5078{
5079 if (flags & ~EXCHGID4_FLAG_MASK_R)
5080 goto out_inval;
5081 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5082 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5083 goto out_inval;
5084 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5085 goto out_inval;
5086 return NFS_OK;
5087out_inval:
5088 return -NFS4ERR_INVAL;
5089}
5090
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005091static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005092nfs41_same_server_scope(struct nfs41_server_scope *a,
5093 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005094{
5095 if (a->server_scope_sz == b->server_scope_sz &&
5096 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5097 return true;
5098
5099 return false;
5100}
5101
Andy Adamson357f54d2010-12-14 10:11:57 -05005102/*
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005103 * nfs4_proc_bind_conn_to_session()
5104 *
5105 * The 4.1 client currently uses the same TCP connection for the
5106 * fore and backchannel.
5107 */
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005108int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005109{
5110 int status;
5111 struct nfs41_bind_conn_to_session_res res;
5112 struct rpc_message msg = {
5113 .rpc_proc =
5114 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
5115 .rpc_argp = clp,
5116 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04005117 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04005118 };
5119
5120 dprintk("--> %s\n", __func__);
5121 BUG_ON(clp == NULL);
5122
5123 res.session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
5124 if (unlikely(res.session == NULL)) {
5125 status = -ENOMEM;
5126 goto out;
5127 }
5128
5129 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5130 if (status == 0) {
5131 if (memcmp(res.session->sess_id.data,
5132 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
5133 dprintk("NFS: %s: Session ID mismatch\n", __func__);
5134 status = -EIO;
5135 goto out_session;
5136 }
5137 if (res.dir != NFS4_CDFS4_BOTH) {
5138 dprintk("NFS: %s: Unexpected direction from server\n",
5139 __func__);
5140 status = -EIO;
5141 goto out_session;
5142 }
5143 if (res.use_conn_in_rdma_mode) {
5144 dprintk("NFS: %s: Server returned RDMA mode = true\n",
5145 __func__);
5146 status = -EIO;
5147 goto out_session;
5148 }
5149 }
5150out_session:
5151 kfree(res.session);
5152out:
5153 dprintk("<-- %s status= %d\n", __func__, status);
5154 return status;
5155}
5156
5157/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005158 * nfs4_proc_exchange_id()
5159 *
5160 * Since the clientid has expired, all compounds using sessions
5161 * associated with the stale clientid will be returning
5162 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5163 * be in some phase of session reset.
5164 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005165int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005166{
5167 nfs4_verifier verifier;
5168 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005169 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005170 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005171 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005172 };
5173 struct nfs41_exchange_id_res res = {
Trond Myklebust32b01312012-05-26 13:41:04 -04005174 0
Benny Halevy99fe60d2009-04-01 09:22:29 -04005175 };
5176 int status;
5177 struct rpc_message msg = {
5178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5179 .rpc_argp = &args,
5180 .rpc_resp = &res,
5181 .rpc_cred = cred,
5182 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005183
5184 dprintk("--> %s\n", __func__);
5185 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005186
Chuck Leverf0920752012-05-21 22:45:41 -04005187 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005188
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005189 args.id_len = scnprintf(args.id, sizeof(args.id),
Trond Myklebust3617e502012-04-30 12:04:58 -04005190 "%s/%s/%u",
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005191 clp->cl_ipaddr,
Trond Myklebust3617e502012-04-30 12:04:58 -04005192 clp->cl_rpcclient->cl_nodename,
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005193 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005194
Chuck Leveracdeb692012-05-21 22:46:16 -04005195 res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005196 GFP_NOFS);
Chuck Leveracdeb692012-05-21 22:46:16 -04005197 if (unlikely(res.server_owner == NULL)) {
5198 status = -ENOMEM;
5199 goto out;
5200 }
5201
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005202 res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005203 GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005204 if (unlikely(res.server_scope == NULL)) {
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005205 status = -ENOMEM;
Chuck Leveracdeb692012-05-21 22:46:16 -04005206 goto out_server_owner;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005207 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005208
Trond Myklebustbbafffd2012-05-24 16:31:39 -04005209 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
Chuck Lever177313f2012-05-21 22:44:58 -04005210 if (unlikely(res.impl_id == NULL)) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005211 status = -ENOMEM;
5212 goto out_server_scope;
5213 }
5214
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005215 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Chuck Lever177313f2012-05-21 22:44:58 -04005216 if (status == 0)
Trond Myklebust32b01312012-05-26 13:41:04 -04005217 status = nfs4_check_cl_exchange_flags(res.flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005218
Chuck Lever177313f2012-05-21 22:44:58 -04005219 if (status == 0) {
Trond Myklebust32b01312012-05-26 13:41:04 -04005220 clp->cl_clientid = res.clientid;
5221 clp->cl_exchange_flags = (res.flags & ~EXCHGID4_FLAG_CONFIRMED_R);
5222 if (!(res.flags & EXCHGID4_FLAG_CONFIRMED_R))
5223 clp->cl_seqid = res.seqid;
5224
Chuck Leveracdeb692012-05-21 22:46:16 -04005225 kfree(clp->cl_serverowner);
5226 clp->cl_serverowner = res.server_owner;
5227 res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04005228
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005229 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04005230 kfree(clp->cl_implid);
5231 clp->cl_implid = res.impl_id;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005232
Chuck Lever177313f2012-05-21 22:44:58 -04005233 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005234 !nfs41_same_server_scope(clp->cl_serverscope,
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005235 res.server_scope)) {
5236 dprintk("%s: server_scope mismatch detected\n",
5237 __func__);
5238 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005239 kfree(clp->cl_serverscope);
5240 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005241 }
5242
Chuck Lever177313f2012-05-21 22:44:58 -04005243 if (clp->cl_serverscope == NULL) {
Chuck Lever79d4e1f2012-05-21 22:44:31 -04005244 clp->cl_serverscope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005245 goto out;
5246 }
Trond Myklebust32b01312012-05-26 13:41:04 -04005247 } else
5248 kfree(res.impl_id);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005249
Chuck Leveracdeb692012-05-21 22:46:16 -04005250out_server_owner:
5251 kfree(res.server_owner);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005252out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005253 kfree(res.server_scope);
5254out:
Chuck Lever177313f2012-05-21 22:44:58 -04005255 if (clp->cl_implid != NULL)
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005256 dprintk("%s: Server Implementation ID: "
5257 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever59155542012-05-21 22:44:41 -04005258 __func__, clp->cl_implid->domain, clp->cl_implid->name,
5259 clp->cl_implid->date.seconds,
5260 clp->cl_implid->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005261 dprintk("<-- %s status= %d\n", __func__, status);
5262 return status;
5263}
5264
Trond Myklebust66245532012-05-25 17:18:09 -04005265static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
5266 struct rpc_cred *cred)
5267{
5268 struct rpc_message msg = {
5269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
5270 .rpc_argp = clp,
5271 .rpc_cred = cred,
5272 };
5273 int status;
5274
5275 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
5276 if (status)
5277 pr_warn("NFS: Got error %d from the server %s on "
5278 "DESTROY_CLIENTID.", status, clp->cl_hostname);
5279 return status;
5280}
5281
5282static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
5283 struct rpc_cred *cred)
5284{
5285 unsigned int loop;
5286 int ret;
5287
5288 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
5289 ret = _nfs4_proc_destroy_clientid(clp, cred);
5290 switch (ret) {
5291 case -NFS4ERR_DELAY:
5292 case -NFS4ERR_CLIENTID_BUSY:
5293 ssleep(1);
5294 break;
5295 default:
5296 return ret;
5297 }
5298 }
5299 return 0;
5300}
5301
5302int nfs4_destroy_clientid(struct nfs_client *clp)
5303{
5304 struct rpc_cred *cred;
5305 int ret = 0;
5306
5307 if (clp->cl_mvops->minor_version < 1)
5308 goto out;
5309 if (clp->cl_exchange_flags == 0)
5310 goto out;
5311 cred = nfs4_get_exchange_id_cred(clp);
5312 ret = nfs4_proc_destroy_clientid(clp, cred);
5313 if (cred)
5314 put_rpccred(cred);
5315 switch (ret) {
5316 case 0:
5317 case -NFS4ERR_STALE_CLIENTID:
5318 clp->cl_exchange_flags = 0;
5319 }
5320out:
5321 return ret;
5322}
5323
Andy Adamson2050f0c2009-04-01 09:22:30 -04005324struct nfs4_get_lease_time_data {
5325 struct nfs4_get_lease_time_args *args;
5326 struct nfs4_get_lease_time_res *res;
5327 struct nfs_client *clp;
5328};
5329
5330static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5331 void *calldata)
5332{
5333 int ret;
5334 struct nfs4_get_lease_time_data *data =
5335 (struct nfs4_get_lease_time_data *)calldata;
5336
5337 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005338 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005339 /* just setup sequence, do not trigger session recovery
5340 since we're invoked within one */
5341 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005342 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005343 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005344
5345 BUG_ON(ret == -EAGAIN);
5346 rpc_call_start(task);
5347 dprintk("<-- %s\n", __func__);
5348}
5349
5350/*
5351 * Called from nfs4_state_manager thread for session setup, so don't recover
5352 * from sequence operation or clientid errors.
5353 */
5354static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5355{
5356 struct nfs4_get_lease_time_data *data =
5357 (struct nfs4_get_lease_time_data *)calldata;
5358
5359 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005360 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5361 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005362 switch (task->tk_status) {
5363 case -NFS4ERR_DELAY:
5364 case -NFS4ERR_GRACE:
5365 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5366 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5367 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005368 /* fall through */
5369 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005370 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005371 return;
5372 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005373 dprintk("<-- %s\n", __func__);
5374}
5375
Trond Myklebust17280172012-03-11 13:11:00 -04005376static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005377 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5378 .rpc_call_done = nfs4_get_lease_time_done,
5379};
5380
5381int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5382{
5383 struct rpc_task *task;
5384 struct nfs4_get_lease_time_args args;
5385 struct nfs4_get_lease_time_res res = {
5386 .lr_fsinfo = fsinfo,
5387 };
5388 struct nfs4_get_lease_time_data data = {
5389 .args = &args,
5390 .res = &res,
5391 .clp = clp,
5392 };
5393 struct rpc_message msg = {
5394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5395 .rpc_argp = &args,
5396 .rpc_resp = &res,
5397 };
5398 struct rpc_task_setup task_setup = {
5399 .rpc_client = clp->cl_rpcclient,
5400 .rpc_message = &msg,
5401 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005402 .callback_data = &data,
5403 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005404 };
5405 int status;
5406
Trond Myklebust9d12b212012-01-17 22:04:25 -05005407 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005408 dprintk("--> %s\n", __func__);
5409 task = rpc_run_task(&task_setup);
5410
5411 if (IS_ERR(task))
5412 status = PTR_ERR(task);
5413 else {
5414 status = task->tk_status;
5415 rpc_put_task(task);
5416 }
5417 dprintk("<-- %s return %d\n", __func__, status);
5418
5419 return status;
5420}
5421
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005422static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5423{
5424 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5425}
5426
5427static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5428 struct nfs4_slot *new,
5429 u32 max_slots,
5430 u32 ivalue)
5431{
5432 struct nfs4_slot *old = NULL;
5433 u32 i;
5434
5435 spin_lock(&tbl->slot_tbl_lock);
5436 if (new) {
5437 old = tbl->slots;
5438 tbl->slots = new;
5439 tbl->max_slots = max_slots;
5440 }
5441 tbl->highest_used_slotid = -1; /* no slot is currently used */
5442 for (i = 0; i < tbl->max_slots; i++)
5443 tbl->slots[i].seq_nr = ivalue;
5444 spin_unlock(&tbl->slot_tbl_lock);
5445 kfree(old);
5446}
5447
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005448/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005449 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005450 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005451static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5452 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005453{
Andy Adamson104aeba2010-01-14 17:45:10 -05005454 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005455 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005456
Andy Adamson104aeba2010-01-14 17:45:10 -05005457 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5458 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005459
Andy Adamson104aeba2010-01-14 17:45:10 -05005460 /* Does the newly negotiated max_reqs match the existing slot table? */
5461 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005462 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005463 if (!new)
5464 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005465 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005466 ret = 0;
5467
5468 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005469 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5470 tbl, tbl->slots, tbl->max_slots);
5471out:
5472 dprintk("<-- %s: return %d\n", __func__, ret);
5473 return ret;
5474}
5475
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005476/* Destroy the slot table */
5477static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5478{
5479 if (session->fc_slot_table.slots != NULL) {
5480 kfree(session->fc_slot_table.slots);
5481 session->fc_slot_table.slots = NULL;
5482 }
5483 if (session->bc_slot_table.slots != NULL) {
5484 kfree(session->bc_slot_table.slots);
5485 session->bc_slot_table.slots = NULL;
5486 }
5487 return;
5488}
5489
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005490/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005491 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005492 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005493static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005494{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005495 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005496 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005497
Andy Adamsonaacd5532011-11-09 13:58:21 -05005498 dprintk("--> %s\n", __func__);
5499 /* Fore channel */
5500 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005501 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5502 if (status) /* -ENOMEM */
5503 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005504 /* Back channel */
5505 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005506 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5507 if (status && tbl->slots == NULL)
5508 /* Fore and back channel share a connection so get
5509 * both slot tables or neither */
5510 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005511 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005512}
5513
5514struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5515{
5516 struct nfs4_session *session;
5517 struct nfs4_slot_table *tbl;
5518
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005519 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005520 if (!session)
5521 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005522
Andy Adamson557134a2009-04-01 09:21:53 -04005523 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005524 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005525 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005526 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005527 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005528
5529 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005530 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005531 spin_lock_init(&tbl->slot_tbl_lock);
5532 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005533 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005534
Trond Myklebust1055d762010-06-16 09:52:27 -04005535 session->session_state = 1<<NFS4_SESSION_INITING;
5536
Andy Adamson557134a2009-04-01 09:21:53 -04005537 session->clp = clp;
5538 return session;
5539}
5540
5541void nfs4_destroy_session(struct nfs4_session *session)
5542{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005543 struct rpc_xprt *xprt;
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005544 struct rpc_cred *cred;
Trond Myklebust2446ab62012-03-01 17:00:56 -05005545
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005546 cred = nfs4_get_exchange_id_cred(session->clp);
5547 nfs4_proc_destroy_session(session, cred);
5548 if (cred)
5549 put_rpccred(cred);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005550
5551 rcu_read_lock();
5552 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5553 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005554 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005555 __func__, xprt);
5556 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005557 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005558 kfree(session);
5559}
5560
Andy Adamsonfc931582009-04-01 09:22:31 -04005561/*
5562 * Initialize the values to be used by the client in CREATE_SESSION
5563 * If nfs4_init_session set the fore channel request and response sizes,
5564 * use them.
5565 *
5566 * Set the back channel max_resp_sz_cached to zero to force the client to
5567 * always set csa_cachethis to FALSE because the current implementation
5568 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5569 */
5570static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5571{
5572 struct nfs4_session *session = args->client->cl_session;
5573 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5574 mxresp_sz = session->fc_attrs.max_resp_sz;
5575
5576 if (mxrqst_sz == 0)
5577 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5578 if (mxresp_sz == 0)
5579 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5580 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005581 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5582 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005583 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005584 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005585
5586 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005587 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005588 __func__,
5589 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005590 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005591
5592 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005593 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5594 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5595 args->bc_attrs.max_resp_sz_cached = 0;
5596 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5597 args->bc_attrs.max_reqs = 1;
5598
5599 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5600 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5601 __func__,
5602 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5603 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5604 args->bc_attrs.max_reqs);
5605}
5606
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005607static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005608{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005609 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5610 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5611
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005612 if (rcvd->max_resp_sz > sent->max_resp_sz)
5613 return -EINVAL;
5614 /*
5615 * Our requested max_ops is the minimum we need; we're not
5616 * prepared to break up compounds into smaller pieces than that.
5617 * So, no point even trying to continue if the server won't
5618 * cooperate:
5619 */
5620 if (rcvd->max_ops < sent->max_ops)
5621 return -EINVAL;
5622 if (rcvd->max_reqs == 0)
5623 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005624 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5625 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005626 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005627}
5628
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005629static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5630{
5631 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5632 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005633
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005634 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5635 return -EINVAL;
5636 if (rcvd->max_resp_sz < sent->max_resp_sz)
5637 return -EINVAL;
5638 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5639 return -EINVAL;
5640 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005641 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005642 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005643 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005644 return -EINVAL;
5645 return 0;
5646}
Andy Adamson8d353012009-04-01 09:22:32 -04005647
Andy Adamson8d353012009-04-01 09:22:32 -04005648static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5649 struct nfs4_session *session)
5650{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005651 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005652
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005653 ret = nfs4_verify_fore_channel_attrs(args, session);
5654 if (ret)
5655 return ret;
5656 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005657}
5658
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005659static int _nfs4_proc_create_session(struct nfs_client *clp,
5660 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005661{
5662 struct nfs4_session *session = clp->cl_session;
5663 struct nfs41_create_session_args args = {
5664 .client = clp,
5665 .cb_program = NFS4_CALLBACK,
5666 };
5667 struct nfs41_create_session_res res = {
5668 .client = clp,
5669 };
5670 struct rpc_message msg = {
5671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5672 .rpc_argp = &args,
5673 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005674 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04005675 };
5676 int status;
5677
5678 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005679 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005680
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005681 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005682
Andy Adamson8d353012009-04-01 09:22:32 -04005683 if (!status)
5684 /* Verify the session's negotiated channel_attrs values */
5685 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005686 if (!status) {
5687 /* Increment the clientid slot sequence id */
5688 clp->cl_seqid++;
5689 }
5690
5691 return status;
5692}
5693
5694/*
5695 * Issues a CREATE_SESSION operation to the server.
5696 * It is the responsibility of the caller to verify the session is
5697 * expired before calling this routine.
5698 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005699int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04005700{
5701 int status;
5702 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005703 struct nfs4_session *session = clp->cl_session;
5704
5705 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5706
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005707 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04005708 if (status)
5709 goto out;
5710
Andy Adamsonaacd5532011-11-09 13:58:21 -05005711 /* Init or reset the session slot tables */
5712 status = nfs4_setup_session_slot_tables(session);
5713 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005714 if (status)
5715 goto out;
5716
5717 ptr = (unsigned *)&session->sess_id.data[0];
5718 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5719 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005720out:
5721 dprintk("<-- %s\n", __func__);
5722 return status;
5723}
5724
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005725/*
5726 * Issue the over-the-wire RPC DESTROY_SESSION.
5727 * The caller must serialize access to this routine.
5728 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005729int nfs4_proc_destroy_session(struct nfs4_session *session,
5730 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005731{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04005732 struct rpc_message msg = {
5733 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
5734 .rpc_argp = session,
5735 .rpc_cred = cred,
5736 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005737 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005738
5739 dprintk("--> nfs4_proc_destroy_session\n");
5740
5741 /* session is still being setup */
5742 if (session->clp->cl_cons_state != NFS_CS_READY)
5743 return status;
5744
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005745 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005746
5747 if (status)
5748 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005749 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005750 "Session has been destroyed regardless...\n", status);
5751
5752 dprintk("<-- nfs4_proc_destroy_session\n");
5753 return status;
5754}
5755
Trond Myklebust7b38c362012-05-23 13:23:31 -04005756/*
5757 * With sessions, the client is not marked ready until after a
5758 * successful EXCHANGE_ID and CREATE_SESSION.
5759 *
5760 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
5761 * other versions of NFS can be tried.
5762 */
5763static int nfs41_check_session_ready(struct nfs_client *clp)
5764{
5765 int ret;
5766
5767 if (clp->cl_cons_state == NFS_CS_SESSION_INITING) {
5768 ret = nfs4_client_recover_expired_lease(clp);
5769 if (ret)
5770 return ret;
5771 }
5772 if (clp->cl_cons_state < NFS_CS_READY)
5773 return -EPROTONOSUPPORT;
Trond Myklebust54ac4712012-05-23 13:26:10 -04005774 smp_rmb();
Trond Myklebust7b38c362012-05-23 13:23:31 -04005775 return 0;
5776}
5777
Trond Myklebustfccba802009-07-21 16:48:07 -04005778int nfs4_init_session(struct nfs_server *server)
5779{
5780 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005781 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005782 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005783
5784 if (!nfs4_has_session(clp))
5785 return 0;
5786
Trond Myklebust1055d762010-06-16 09:52:27 -04005787 session = clp->cl_session;
Trond Myklebust7b38c362012-05-23 13:23:31 -04005788 spin_lock(&clp->cl_lock);
5789 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
Trond Myklebust1055d762010-06-16 09:52:27 -04005790
Trond Myklebust7b38c362012-05-23 13:23:31 -04005791 rsize = server->rsize;
5792 if (rsize == 0)
5793 rsize = NFS_MAX_FILE_IO_SIZE;
5794 wsize = server->wsize;
5795 if (wsize == 0)
5796 wsize = NFS_MAX_FILE_IO_SIZE;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005797
Trond Myklebust7b38c362012-05-23 13:23:31 -04005798 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5799 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
5800 }
5801 spin_unlock(&clp->cl_lock);
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005802
Trond Myklebust7b38c362012-05-23 13:23:31 -04005803 return nfs41_check_session_ready(clp);
Trond Myklebustfccba802009-07-21 16:48:07 -04005804}
5805
Trond Myklebust7b38c362012-05-23 13:23:31 -04005806int nfs4_init_ds_session(struct nfs_client *clp, unsigned long lease_time)
Andy Adamsond83217c2011-03-01 01:34:17 +00005807{
5808 struct nfs4_session *session = clp->cl_session;
5809 int ret;
5810
Trond Myklebust7b38c362012-05-23 13:23:31 -04005811 spin_lock(&clp->cl_lock);
5812 if (test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state)) {
5813 /*
5814 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the
5815 * DS lease to be equal to the MDS lease.
5816 */
5817 clp->cl_lease_time = lease_time;
5818 clp->cl_last_renewal = jiffies;
5819 }
5820 spin_unlock(&clp->cl_lock);
Andy Adamsond83217c2011-03-01 01:34:17 +00005821
Trond Myklebust7b38c362012-05-23 13:23:31 -04005822 ret = nfs41_check_session_ready(clp);
5823 if (ret)
5824 return ret;
5825 /* Test for the DS role */
5826 if (!is_ds_client(clp))
5827 return -ENODEV;
5828 return 0;
Andy Adamsond83217c2011-03-01 01:34:17 +00005829}
5830EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5831
5832
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005833/*
5834 * Renew the cl_session lease.
5835 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005836struct nfs4_sequence_data {
5837 struct nfs_client *clp;
5838 struct nfs4_sequence_args args;
5839 struct nfs4_sequence_res res;
5840};
5841
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005842static void nfs41_sequence_release(void *data)
5843{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005844 struct nfs4_sequence_data *calldata = data;
5845 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005846
Alexandros Batsakis71358402010-02-05 03:45:05 -08005847 if (atomic_read(&clp->cl_count) > 1)
5848 nfs4_schedule_state_renewal(clp);
5849 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005850 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005851}
5852
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005853static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5854{
5855 switch(task->tk_status) {
5856 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005857 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5858 return -EAGAIN;
5859 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005860 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005861 }
5862 return 0;
5863}
5864
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005865static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005866{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005867 struct nfs4_sequence_data *calldata = data;
5868 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005869
Trond Myklebust14516c32010-07-31 14:29:06 -04005870 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5871 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005872
5873 if (task->tk_status < 0) {
5874 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005875 if (atomic_read(&clp->cl_count) == 1)
5876 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005877
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005878 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5879 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005880 return;
5881 }
5882 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005883 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005884out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005885 dprintk("<-- %s\n", __func__);
5886}
5887
5888static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5889{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005890 struct nfs4_sequence_data *calldata = data;
5891 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005892 struct nfs4_sequence_args *args;
5893 struct nfs4_sequence_res *res;
5894
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005895 args = task->tk_msg.rpc_argp;
5896 res = task->tk_msg.rpc_resp;
5897
Trond Myklebust9d12b212012-01-17 22:04:25 -05005898 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005899 return;
5900 rpc_call_start(task);
5901}
5902
5903static const struct rpc_call_ops nfs41_sequence_ops = {
5904 .rpc_call_done = nfs41_sequence_call_done,
5905 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005906 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005907};
5908
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005909static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005910{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005911 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005912 struct rpc_message msg = {
5913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5914 .rpc_cred = cred,
5915 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005916 struct rpc_task_setup task_setup_data = {
5917 .rpc_client = clp->cl_rpcclient,
5918 .rpc_message = &msg,
5919 .callback_ops = &nfs41_sequence_ops,
5920 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5921 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005922
Alexandros Batsakis71358402010-02-05 03:45:05 -08005923 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005924 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005925 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005926 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005927 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005928 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005929 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005930 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005931 msg.rpc_argp = &calldata->args;
5932 msg.rpc_resp = &calldata->res;
5933 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005934 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005935
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005936 return rpc_run_task(&task_setup_data);
5937}
5938
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005939static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005940{
5941 struct rpc_task *task;
5942 int ret = 0;
5943
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005944 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5945 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005946 task = _nfs41_proc_sequence(clp, cred);
5947 if (IS_ERR(task))
5948 ret = PTR_ERR(task);
5949 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005950 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005951 dprintk("<-- %s status=%d\n", __func__, ret);
5952 return ret;
5953}
5954
5955static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5956{
5957 struct rpc_task *task;
5958 int ret;
5959
5960 task = _nfs41_proc_sequence(clp, cred);
5961 if (IS_ERR(task)) {
5962 ret = PTR_ERR(task);
5963 goto out;
5964 }
5965 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005966 if (!ret) {
5967 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5968
5969 if (task->tk_status == 0)
5970 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005971 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005972 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005973 rpc_put_task(task);
5974out:
5975 dprintk("<-- %s status=%d\n", __func__, ret);
5976 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005977}
5978
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005979struct nfs4_reclaim_complete_data {
5980 struct nfs_client *clp;
5981 struct nfs41_reclaim_complete_args arg;
5982 struct nfs41_reclaim_complete_res res;
5983};
5984
5985static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5986{
5987 struct nfs4_reclaim_complete_data *calldata = data;
5988
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005989 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005990 if (nfs41_setup_sequence(calldata->clp->cl_session,
5991 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005992 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005993 return;
5994
5995 rpc_call_start(task);
5996}
5997
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005998static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5999{
6000 switch(task->tk_status) {
6001 case 0:
6002 case -NFS4ERR_COMPLETE_ALREADY:
6003 case -NFS4ERR_WRONG_CRED: /* What to do here? */
6004 break;
6005 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006006 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04006007 /* fall through */
6008 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006009 return -EAGAIN;
6010 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05006011 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006012 }
6013 return 0;
6014}
6015
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006016static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
6017{
6018 struct nfs4_reclaim_complete_data *calldata = data;
6019 struct nfs_client *clp = calldata->clp;
6020 struct nfs4_sequence_res *res = &calldata->res.seq_res;
6021
6022 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04006023 if (!nfs41_sequence_done(task, res))
6024 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006025
Trond Myklebustaa5190d2010-06-16 09:52:25 -04006026 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
6027 rpc_restart_call_prepare(task);
6028 return;
6029 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006030 dprintk("<-- %s\n", __func__);
6031}
6032
6033static void nfs4_free_reclaim_complete_data(void *data)
6034{
6035 struct nfs4_reclaim_complete_data *calldata = data;
6036
6037 kfree(calldata);
6038}
6039
6040static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
6041 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
6042 .rpc_call_done = nfs4_reclaim_complete_done,
6043 .rpc_release = nfs4_free_reclaim_complete_data,
6044};
6045
6046/*
6047 * Issue a global reclaim complete.
6048 */
6049static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
6050{
6051 struct nfs4_reclaim_complete_data *calldata;
6052 struct rpc_task *task;
6053 struct rpc_message msg = {
6054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
6055 };
6056 struct rpc_task_setup task_setup_data = {
6057 .rpc_client = clp->cl_rpcclient,
6058 .rpc_message = &msg,
6059 .callback_ops = &nfs4_reclaim_complete_call_ops,
6060 .flags = RPC_TASK_ASYNC,
6061 };
6062 int status = -ENOMEM;
6063
6064 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006065 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006066 if (calldata == NULL)
6067 goto out;
6068 calldata->clp = clp;
6069 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006070
Trond Myklebust9d12b212012-01-17 22:04:25 -05006071 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006072 msg.rpc_argp = &calldata->arg;
6073 msg.rpc_resp = &calldata->res;
6074 task_setup_data.callback_data = calldata;
6075 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006076 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006077 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006078 goto out;
6079 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05006080 status = nfs4_wait_for_completion_rpc_task(task);
6081 if (status == 0)
6082 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006083 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02006084 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006085out:
6086 dprintk("<-- %s status=%d\n", __func__, status);
6087 return status;
6088}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006089
6090static void
6091nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
6092{
6093 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00006094 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006095
6096 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00006097 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
6098 * right now covering the LAYOUTGET we are about to send.
6099 * However, that is not so catastrophic, and there seems
6100 * to be no way to prevent it completely.
6101 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006102 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006103 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006104 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00006105 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
6106 NFS_I(lgp->args.inode)->layout,
6107 lgp->args.ctx->state)) {
6108 rpc_exit(task, NFS4_OK);
6109 return;
6110 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006111 rpc_call_start(task);
6112}
6113
6114static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
6115{
6116 struct nfs4_layoutget *lgp = calldata;
6117 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6118
6119 dprintk("--> %s\n", __func__);
6120
6121 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
6122 return;
6123
6124 switch (task->tk_status) {
6125 case 0:
6126 break;
6127 case -NFS4ERR_LAYOUTTRYLATER:
6128 case -NFS4ERR_RECALLCONFLICT:
6129 task->tk_status = -NFS4ERR_DELAY;
6130 /* Fall through */
6131 default:
6132 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6133 rpc_restart_call_prepare(task);
6134 return;
6135 }
6136 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006137 dprintk("<-- %s\n", __func__);
6138}
6139
6140static void nfs4_layoutget_release(void *calldata)
6141{
6142 struct nfs4_layoutget *lgp = calldata;
6143
6144 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006145 put_nfs_open_context(lgp->args.ctx);
6146 kfree(calldata);
6147 dprintk("<-- %s\n", __func__);
6148}
6149
6150static const struct rpc_call_ops nfs4_layoutget_call_ops = {
6151 .rpc_call_prepare = nfs4_layoutget_prepare,
6152 .rpc_call_done = nfs4_layoutget_done,
6153 .rpc_release = nfs4_layoutget_release,
6154};
6155
6156int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
6157{
6158 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
6159 struct rpc_task *task;
6160 struct rpc_message msg = {
6161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
6162 .rpc_argp = &lgp->args,
6163 .rpc_resp = &lgp->res,
6164 };
6165 struct rpc_task_setup task_setup_data = {
6166 .rpc_client = server->client,
6167 .rpc_message = &msg,
6168 .callback_ops = &nfs4_layoutget_call_ops,
6169 .callback_data = lgp,
6170 .flags = RPC_TASK_ASYNC,
6171 };
6172 int status = 0;
6173
6174 dprintk("--> %s\n", __func__);
6175
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006176 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006177 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006178 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006179 task = rpc_run_task(&task_setup_data);
6180 if (IS_ERR(task))
6181 return PTR_ERR(task);
6182 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006183 if (status == 0)
6184 status = task->tk_status;
6185 if (status == 0)
6186 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006187 rpc_put_task(task);
6188 dprintk("<-- %s status=%d\n", __func__, status);
6189 return status;
6190}
6191
Benny Halevycbe82602011-05-22 19:52:37 +03006192static void
6193nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6194{
6195 struct nfs4_layoutreturn *lrp = calldata;
6196
6197 dprintk("--> %s\n", __func__);
6198 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006199 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006200 return;
6201 rpc_call_start(task);
6202}
6203
6204static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6205{
6206 struct nfs4_layoutreturn *lrp = calldata;
6207 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006208 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006209
6210 dprintk("--> %s\n", __func__);
6211
6212 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6213 return;
6214
6215 server = NFS_SERVER(lrp->args.inode);
6216 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006217 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006218 return;
6219 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006220 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006221 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006222 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006223 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006224 } else
6225 BUG_ON(!list_empty(&lo->plh_segs));
6226 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006227 lo->plh_block_lgets--;
6228 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006229 dprintk("<-- %s\n", __func__);
6230}
6231
6232static void nfs4_layoutreturn_release(void *calldata)
6233{
6234 struct nfs4_layoutreturn *lrp = calldata;
6235
6236 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006237 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006238 kfree(calldata);
6239 dprintk("<-- %s\n", __func__);
6240}
6241
6242static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6243 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6244 .rpc_call_done = nfs4_layoutreturn_done,
6245 .rpc_release = nfs4_layoutreturn_release,
6246};
6247
6248int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6249{
6250 struct rpc_task *task;
6251 struct rpc_message msg = {
6252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6253 .rpc_argp = &lrp->args,
6254 .rpc_resp = &lrp->res,
6255 };
6256 struct rpc_task_setup task_setup_data = {
6257 .rpc_client = lrp->clp->cl_rpcclient,
6258 .rpc_message = &msg,
6259 .callback_ops = &nfs4_layoutreturn_call_ops,
6260 .callback_data = lrp,
6261 };
6262 int status;
6263
6264 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006265 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006266 task = rpc_run_task(&task_setup_data);
6267 if (IS_ERR(task))
6268 return PTR_ERR(task);
6269 status = task->tk_status;
6270 dprintk("<-- %s status=%d\n", __func__, status);
6271 rpc_put_task(task);
6272 return status;
6273}
6274
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006275/*
6276 * Retrieve the list of Data Server devices from the MDS.
6277 */
6278static int _nfs4_getdevicelist(struct nfs_server *server,
6279 const struct nfs_fh *fh,
6280 struct pnfs_devicelist *devlist)
6281{
6282 struct nfs4_getdevicelist_args args = {
6283 .fh = fh,
6284 .layoutclass = server->pnfs_curr_ld->id,
6285 };
6286 struct nfs4_getdevicelist_res res = {
6287 .devlist = devlist,
6288 };
6289 struct rpc_message msg = {
6290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6291 .rpc_argp = &args,
6292 .rpc_resp = &res,
6293 };
6294 int status;
6295
6296 dprintk("--> %s\n", __func__);
6297 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6298 &res.seq_res, 0);
6299 dprintk("<-- %s status=%d\n", __func__, status);
6300 return status;
6301}
6302
6303int nfs4_proc_getdevicelist(struct nfs_server *server,
6304 const struct nfs_fh *fh,
6305 struct pnfs_devicelist *devlist)
6306{
6307 struct nfs4_exception exception = { };
6308 int err;
6309
6310 do {
6311 err = nfs4_handle_exception(server,
6312 _nfs4_getdevicelist(server, fh, devlist),
6313 &exception);
6314 } while (exception.retry);
6315
6316 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6317 err, devlist->num_devs);
6318
6319 return err;
6320}
6321EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6322
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006323static int
6324_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6325{
6326 struct nfs4_getdeviceinfo_args args = {
6327 .pdev = pdev,
6328 };
6329 struct nfs4_getdeviceinfo_res res = {
6330 .pdev = pdev,
6331 };
6332 struct rpc_message msg = {
6333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6334 .rpc_argp = &args,
6335 .rpc_resp = &res,
6336 };
6337 int status;
6338
6339 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006340 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006341 dprintk("<-- %s status=%d\n", __func__, status);
6342
6343 return status;
6344}
6345
6346int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6347{
6348 struct nfs4_exception exception = { };
6349 int err;
6350
6351 do {
6352 err = nfs4_handle_exception(server,
6353 _nfs4_proc_getdeviceinfo(server, pdev),
6354 &exception);
6355 } while (exception.retry);
6356 return err;
6357}
6358EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6359
Andy Adamson863a3c62011-03-23 13:27:54 +00006360static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6361{
6362 struct nfs4_layoutcommit_data *data = calldata;
6363 struct nfs_server *server = NFS_SERVER(data->args.inode);
6364
6365 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006366 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006367 return;
6368 rpc_call_start(task);
6369}
6370
6371static void
6372nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6373{
6374 struct nfs4_layoutcommit_data *data = calldata;
6375 struct nfs_server *server = NFS_SERVER(data->args.inode);
6376
6377 if (!nfs4_sequence_done(task, &data->res.seq_res))
6378 return;
6379
6380 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006381 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6382 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6383 case -NFS4ERR_BADLAYOUT: /* no layout */
6384 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006385 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006386 break;
6387 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006388 nfs_post_op_update_inode_force_wcc(data->args.inode,
6389 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006390 break;
6391 default:
6392 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6393 rpc_restart_call_prepare(task);
6394 return;
6395 }
6396 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006397}
6398
6399static void nfs4_layoutcommit_release(void *calldata)
6400{
6401 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006402 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006403 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006404
Andy Adamsondb29c082011-07-30 20:52:38 -04006405 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006406 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006407 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6408 list_del_init(&lseg->pls_lc_list);
6409 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6410 &lseg->pls_flags))
6411 put_lseg(lseg);
6412 }
Peng Tao92407e72011-10-23 20:21:17 -07006413
6414 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6415 smp_mb__after_clear_bit();
6416 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6417
Andy Adamson863a3c62011-03-23 13:27:54 +00006418 put_rpccred(data->cred);
6419 kfree(data);
6420}
6421
6422static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6423 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6424 .rpc_call_done = nfs4_layoutcommit_done,
6425 .rpc_release = nfs4_layoutcommit_release,
6426};
6427
6428int
Andy Adamsonef311532011-03-12 02:58:10 -05006429nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006430{
6431 struct rpc_message msg = {
6432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6433 .rpc_argp = &data->args,
6434 .rpc_resp = &data->res,
6435 .rpc_cred = data->cred,
6436 };
6437 struct rpc_task_setup task_setup_data = {
6438 .task = &data->task,
6439 .rpc_client = NFS_CLIENT(data->args.inode),
6440 .rpc_message = &msg,
6441 .callback_ops = &nfs4_layoutcommit_ops,
6442 .callback_data = data,
6443 .flags = RPC_TASK_ASYNC,
6444 };
6445 struct rpc_task *task;
6446 int status = 0;
6447
6448 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6449 "lbw: %llu inode %lu\n",
6450 data->task.tk_pid, sync,
6451 data->args.lastbytewritten,
6452 data->args.inode->i_ino);
6453
Trond Myklebust9d12b212012-01-17 22:04:25 -05006454 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006455 task = rpc_run_task(&task_setup_data);
6456 if (IS_ERR(task))
6457 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006458 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006459 goto out;
6460 status = nfs4_wait_for_completion_rpc_task(task);
6461 if (status != 0)
6462 goto out;
6463 status = task->tk_status;
6464out:
6465 dprintk("%s: status %d\n", __func__, status);
6466 rpc_put_task(task);
6467 return status;
6468}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006469
6470static int
6471_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6472 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6473{
6474 struct nfs41_secinfo_no_name_args args = {
6475 .style = SECINFO_STYLE_CURRENT_FH,
6476 };
6477 struct nfs4_secinfo_res res = {
6478 .flavors = flavors,
6479 };
6480 struct rpc_message msg = {
6481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6482 .rpc_argp = &args,
6483 .rpc_resp = &res,
6484 };
6485 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6486}
6487
6488static int
6489nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6490 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6491{
6492 struct nfs4_exception exception = { };
6493 int err;
6494 do {
6495 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6496 switch (err) {
6497 case 0:
6498 case -NFS4ERR_WRONGSEC:
6499 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006500 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006501 default:
6502 err = nfs4_handle_exception(server, err, &exception);
6503 }
6504 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006505out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006506 return err;
6507}
6508
6509static int
6510nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6511 struct nfs_fsinfo *info)
6512{
6513 int err;
6514 struct page *page;
6515 rpc_authflavor_t flavor;
6516 struct nfs4_secinfo_flavors *flavors;
6517
6518 page = alloc_page(GFP_KERNEL);
6519 if (!page) {
6520 err = -ENOMEM;
6521 goto out;
6522 }
6523
6524 flavors = page_address(page);
6525 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6526
6527 /*
6528 * Fall back on "guess and check" method if
6529 * the server doesn't support SECINFO_NO_NAME
6530 */
6531 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6532 err = nfs4_find_root_sec(server, fhandle, info);
6533 goto out_freepage;
6534 }
6535 if (err)
6536 goto out_freepage;
6537
6538 flavor = nfs_find_best_sec(flavors);
6539 if (err == 0)
6540 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6541
6542out_freepage:
6543 put_page(page);
6544 if (err == -EACCES)
6545 return -EPERM;
6546out:
6547 return err;
6548}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006549
6550static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006551{
6552 int status;
6553 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006554 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006555 };
6556 struct nfs41_test_stateid_res res;
6557 struct rpc_message msg = {
6558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6559 .rpc_argp = &args,
6560 .rpc_resp = &res,
6561 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006562
Trond Myklebust9d12b212012-01-17 22:04:25 -05006563 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006564 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6565
6566 if (status == NFS_OK)
6567 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006568 return status;
6569}
6570
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006571static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006572{
6573 struct nfs4_exception exception = { };
6574 int err;
6575 do {
6576 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006577 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006578 &exception);
6579 } while (exception.retry);
6580 return err;
6581}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006582
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006583static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006584{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006585 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006586 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006587 };
6588 struct nfs41_free_stateid_res res;
6589 struct rpc_message msg = {
6590 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6591 .rpc_argp = &args,
6592 .rpc_resp = &res,
6593 };
6594
Trond Myklebust9d12b212012-01-17 22:04:25 -05006595 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6596 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006597}
6598
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006599static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006600{
6601 struct nfs4_exception exception = { };
6602 int err;
6603 do {
6604 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006605 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006606 &exception);
6607 } while (exception.retry);
6608 return err;
6609}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006610
6611static bool nfs41_match_stateid(const nfs4_stateid *s1,
6612 const nfs4_stateid *s2)
6613{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006614 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006615 return false;
6616
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006617 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006618 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006619 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006620 return true;
6621
6622 return false;
6623}
6624
Andy Adamson557134a2009-04-01 09:21:53 -04006625#endif /* CONFIG_NFS_V4_1 */
6626
Trond Myklebust36281ca2012-03-04 18:13:56 -05006627static bool nfs4_match_stateid(const nfs4_stateid *s1,
6628 const nfs4_stateid *s2)
6629{
Trond Myklebustf597c532012-03-04 18:13:56 -05006630 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006631}
6632
6633
Trond Myklebust17280172012-03-11 13:11:00 -04006634static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006635 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006636 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 .recover_open = nfs4_open_reclaim,
6638 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006639 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006640 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641};
6642
Andy Adamson591d71c2009-04-01 09:22:47 -04006643#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006644static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006645 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6646 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6647 .recover_open = nfs4_open_reclaim,
6648 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006649 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006650 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006651 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006652};
6653#endif /* CONFIG_NFS_V4_1 */
6654
Trond Myklebust17280172012-03-11 13:11:00 -04006655static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006656 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006657 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 .recover_open = nfs4_open_expired,
6659 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006660 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006661 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662};
6663
Andy Adamson591d71c2009-04-01 09:22:47 -04006664#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006665static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006666 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6667 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006668 .recover_open = nfs41_open_expired,
6669 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006670 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006671 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006672};
6673#endif /* CONFIG_NFS_V4_1 */
6674
Trond Myklebust17280172012-03-11 13:11:00 -04006675static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006676 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006677 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006678 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006679};
6680
6681#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006682static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006683 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006684 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006685 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006686};
6687#endif
6688
Trond Myklebust97dc1352010-06-16 09:52:26 -04006689static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6690 .minor_version = 0,
6691 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006692 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006693 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006694 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6695 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6696 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006697};
6698
6699#if defined(CONFIG_NFS_V4_1)
6700static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6701 .minor_version = 1,
6702 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006703 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006704 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006705 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6706 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6707 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006708};
6709#endif
6710
Trond Myklebust97dc1352010-06-16 09:52:26 -04006711const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6712 [0] = &nfs_v4_0_minor_ops,
6713#if defined(CONFIG_NFS_V4_1)
6714 [1] = &nfs_v4_1_minor_ops,
6715#endif
6716};
6717
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006718static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006719 .permission = nfs_permission,
6720 .getattr = nfs_getattr,
6721 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006722 .getxattr = generic_getxattr,
6723 .setxattr = generic_setxattr,
6724 .listxattr = generic_listxattr,
6725 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006726};
6727
David Howells509de812006-08-22 20:06:11 -04006728const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 .version = 4, /* protocol version */
6730 .dentry_ops = &nfs4_dentry_operations,
6731 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006732 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006733 .file_ops = &nfs4_file_operations,
Bryan Schumakerbae36242012-05-10 15:07:31 -04006734 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006735 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 .getattr = nfs4_proc_getattr,
6737 .setattr = nfs4_proc_setattr,
6738 .lookup = nfs4_proc_lookup,
6739 .access = nfs4_proc_access,
6740 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 .create = nfs4_proc_create,
6742 .remove = nfs4_proc_remove,
6743 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006744 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745 .unlink_done = nfs4_proc_unlink_done,
6746 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006747 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006748 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006749 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 .link = nfs4_proc_link,
6751 .symlink = nfs4_proc_symlink,
6752 .mkdir = nfs4_proc_mkdir,
6753 .rmdir = nfs4_proc_remove,
6754 .readdir = nfs4_proc_readdir,
6755 .mknod = nfs4_proc_mknod,
6756 .statfs = nfs4_proc_statfs,
6757 .fsinfo = nfs4_proc_fsinfo,
6758 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006759 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 .decode_dirent = nfs4_decode_dirent,
6761 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006762 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006763 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006765 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006766 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006768 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006769 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006771 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006772 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006773 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006774 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775};
6776
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006777static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6778 .prefix = XATTR_NAME_NFSV4_ACL,
6779 .list = nfs4_xattr_list_nfs4_acl,
6780 .get = nfs4_xattr_get_nfs4_acl,
6781 .set = nfs4_xattr_set_nfs4_acl,
6782};
6783
6784const struct xattr_handler *nfs4_xattr_handlers[] = {
6785 &nfs4_xattr_nfs4_acl_handler,
6786 NULL
6787};
6788
Trond Myklebustef159e92012-02-06 19:50:40 -05006789module_param(max_session_slots, ushort, 0644);
6790MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6791 "requests the client will negotiate");
6792
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793/*
6794 * Local variables:
6795 * c-basic-offset: 8
6796 * End:
6797 */